Why is there an error: "File "stdin1", line 1, invaild syntax" - python-3.x

Code:
def profile(name, age, lang1,lang2,lang3, lang4, lang5):
print("이름 : {0}\t나이 : {1}\t".format(name,age), end="")
print(lang1,lang2,lang3,lang4,lang5)
SyntaxError: invalid syntax
C:/Users/man/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/man/Desktop/python work space/edu.py"
File "", line 1
C:/Users/man/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/man/Desktop/python work space/edu.py"
I don't know how to fix it please help me.

Related

Why do I get a syntax error in Python despite using exitStack() as in the doc?

I'm trying to execute some python's code and can't figure out what's the issue :
def elboguide(design, dim=10):
# pyro.set_rng_seed(10)
theta_mu = pyro.param("theta_mu", torch.zeros(dim, 1, 2, 2))
theta_sig = pyro.param("theta_sig", torch.ones(dim, 1, 2, 2),
constraint=torch.distributions.constraints.positive)
batch_shape = design.shape[:-2]
with ExitStack() as stack:
for plate in iter_plates_to_shape(batch_shape):
stack.enter_context(plate)
theta_shape = batch_shape + theta_mu.shape[-2:]
pyro.sample("theta", dist.Normal(
theta_mu.expand(theta_shape),
theta_sig.expand(theta_shape)).to_event(2)
)
The issue is in this function, more specifically on the line
with ExitStack() as stack:
When running the code I only get the error :
File "C:\Users\natha\Documents\prog\python_dev\RL-BOED\RL-BOED\source.py", line 52
with ExitStack() as stack:
^
SyntaxError: invalid syntax
the documentation of contextlib shows an use case where the syntax is exactly the same :
https://docs.python.org/3/library/contextlib.html
What is wrong with this code ? I'm using Python 3.11.0 by the way.

OSError: [Errno 22] Invalid argument Getting invalid argument while parsing xml in python

I have below xml where i am trying extract value which is required
xml = '<s:Envelope xmlns:s="schemas.xmlsoap.org/soap/envelope/"><s:Body><GetService xmlns="abc.com/Service">
<GetService xmlns:a="abc.com" xmlns:i="www.w3.org/2001/XMLSchema-instance">
<a:EnvironmentName>test</a:EnvironmentName><a:HasUpdates>true</a:HasNewUpdates><a:active>false</a:active><a:Time>13:37:22</a:Time><a:ServiceUrisString><s><u t="1" n="net://abc.com/" i="net.tcp://abc.com/" /><u t="2"
" /></s></a:ServiceUrisString></GetService></GetServiceRegistryResponse></s:Body></s:Envelope>
'
What iam looking for is to get the value for 'active' and 'time' from above xml
active=false
time=13:37:22
Below is the code
import xml.etree.ElementTree as ET
tree=ET.parse(xml)
print(tree)
But the parsing not working as expected
Below I am getting following error
OSError: [Errno 22] Invalid argument:
SO how to resolve this issue?
ET.parse() is a function for reading xml from disk
try ET.fromstring(xml)

Updating python 2 code to python 3 code for googleads api

I'm trying to use the google ads api with python 3 and I'm facing an issue with their generate_refresh_token.py file. The file has been updated for python 3 but I need to debug it as it still has some python 2 code in it. For example, print statements didn't have () in them and there was an instance of using raw_input() instead of input().
Anyway, I'm getting an error message that I can't figure out. Can someone please help me out here?
I've tried googling the solution but I'm a bit lost here.
The code starts from line 110 and ends at line 122:
print ('Access token: %s') % flow.credentials.token
print ('Refresh token: %s') % flow.credentials.refresh_token
if __name__ == '__main__':
args = parser.parse_args()
configured_scopes = [SCOPE]
if not (any([args.client_id, DEFAULT_CLIENT_ID]) and
any([args.client_secret, DEFAULT_CLIENT_SECRET])):
raise AttributeError('No client_id or client_secret specified.')
if args.additional_scopes:
configured_scopes.extend(args.additional_scopes.replace(' ', '').split(','))
main(args.client_id, args.client_secret, configured_scopes)
The code is supposed to throw out an access token that I can use but it's giving me this error:
Access token: %s
Traceback (most recent call last):
File "generate_refresh_token.py", line 122, in <module>
main(args.client_id, args.client_secret, configured_scopes)
File "generate_refresh_token.py", line 110, in main
print ('Access token: %s') % flow.credentials.token
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
I believe that this is also a python 2 vs python 3 problem and i would appreciate it if someone could help me out with this!
Update the print statements.
print ('Access token: {}'.format(flow.credentials.token))
print ('Refresh token: {}'.format(flow.credentials.refresh_token))
You have a typo with your parentheses. Print should be:
print('Access token: %s'% flow.credentials.token)
print('Refresh token: %s' % flow.credentials.refresh_token)
To be safe use format:
print('Access token: {}'.format(flow.credentials.token))
print('Refresh token: {}'.format(flow.credentials.refresh_token))

How to catch errors (Smartsheet API Python SDK)

I am missing fundamental knowledge.
How to 'properly' catch errors returned by the API.
I'm using Python 3.+
If I pass in the wrong sheet ID
try:
update_sht = SmSh.Sheets.get_sheet(dd_id)
print("OK?:", update_sht, flush=True)
except:
print("Error Print:\n", update_sht)
I get this response (in the IPython console)
Response: {
status: 404 Not Found
content: {
{
"errorCode": 1006,
"message": "Not Found",
"refId": "jod4cgoou0sw"
}
}
OK?: {"result": {"code": 1006, "errorCode": 1006, "message": "Not Found",
"name": "ApiError", "recommendation": "Do not retry without fixing the
problem. ", "refId": "jod4cgoou0sw", "shouldRetry": false,
"statusCode": 404}}
and while it returns an error response, it isn't an exception according to try/except.
At this point, I would like to exit out of the loop I am in, instead of continuing on until I get to other lines of code like this
for col in update_sht.columns:
that DO give errors that cause the program to fail.
Traceback (most recent call last):
File "<ipython-input-195-85dde6ec7071>", line 1, in <module>
xxx(debug=False)
File "<ipython-input-194-0b889c817b08>", line 75, in xxx
for col in update_sht.columns:
AttributeError: 'Error' object has no attribute 'columns'
I'm doing more than one thing on the sheet, if I find it, and would prefer not to have a try/except around error line of code (I exaggerate) unless I need them for other errors.
I know/hope this is easier than I have been trying to make it, but as I opened with, I am missing something fundamental.
-Craig
---- UPDATE ----
I am going around in circles.
If errors_as_exceptions is true, then this
update_sht = SmSh.Sheets.get_sheet(dd_id)
raises and exception, but
print(update_sht)
or anything similar shows the previous good value in the object.
How do I determine the status code and error codes so I can take appropriate action?
Nothing I have tried has worked.
If errors_as_exceptions is false, then this
update_sht = SmSh.Sheets.get_sheet(dd_id)
print(update_sht.result.code)
gives me the error code, but only when there is an error ... so I need to catch the error that occurs when there is no
error.
If I raise the error (errors_as_exceptions=True), how do I determine the status code and error code and if I don't raise the error,
how do I do the same?
I want to prevent my code from failing and give the user useful information on what needs to be fixed.
If you set ss.errors_as_exceptions() and your code looks something like this
try:
my_sheet = ss.Sheets.get_sheet(sheet_ID)
print(my_sheet)
except Exception as e:
print(e.message)
The result will look something like this 1006: Not Found. So, the exception message appears to be errorCode:message.
If you want the Python SDK to raise exceptions for API errors call the errors_as_exceptions method on the client object, e.g.
ss = smartsheet.Smartsheet()
ss.errors_as_exceptions()

Syntax error in python when using {} to reference a variable earlier in code

Here is my code:
name = input("What's your name? ")
age = input("how old are you? ")
age_in_dog_years = age * 7
print(f"{name}you are {age_in_dog_years}in dog years.")
The error I get is:
File "input.py", line 7
print(f"{name} you are {age_in_dog_years} in dog years.")
^
SyntaxError: invalid syntax
can't seem to understand the rror

Resources