One of the challenges I'm having is getting a stack trace or examining variables which should be in scope when an error occurs. However I am not finding this is the case. For example if a piece of code fields, I'd like to be able to see where in the loop in fails. However despite using %debug, I can never get any values out.
For example my code:
if a[field].to_list()[0] == b[field].to_list()[0]:
result = True
fails, and I'd like to know what the value of field is. But I can never find a way to make this work using %debug.
Maybe this try and except block helps you
for field in fields:
try:
if a[field].to_list()[0] == b[field].to_list()[0]:
result = True
except Exception as e:
print(field)
print(e)
Related
I would like to use the following code from Pyomo Accessing solver status and termination conditions
results = opt.solve(instance) # Solving a model instance
instance.load(results) # Loading solution into results object
if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
# Do something when the solution in optimal and feasible
elif (results.solver.termination_condition == TerminationCondition.infeasible):
# Do something when model in infeasible
else:
# Something else is wrong
print “Solver Status: ”, result.solver.status
Hoever, I get an error saying Expected an indented block at the elif. When inserting an indented block, I get the error Invalid syntax. I posted a screenshot of both cases. I do not understand why I get this error? I just copied and pasted the code from the official pyomo website. Do you have any idea why I am getting this error and how I can get rid of it?
You likely need to have at least 1 line of executable code within each if or elif block. Right now, you just have a comment line.
While you are "shelling out" the program, just put the command pass in each block and see if that helps. So:
if (something >= something_else):
# do something
pass
else:
# do the other thing
pass
....
When code is laid out using whitespace like python, you need something actually in the block to show that it's there. A comment isn't enough as these are ignored.
Your code currently looks like:
if ... :
# comment where block should be
elif ... :
print "something"
The comment doesn't count as an indented block.
If you really have no code to put in there yet, you can use the no-op statement pass:
if ... :
# todo
pass
elif ... :
print "something"
I'm trying to use try/except to query BigQuery tables, sometimes the query may not be correct in which case pandas raises a GenericGBQException error.
My problem is I get name 'GenericGBQException' is not defined when trying to handle this error, example code below:
try:
df = pd.read_gbq(query, projID)
query_fail = 0
except GenericGBQException:
query_fail = 1
if query_fail == 1:
do some stuff
I can get by with catching all exceptions though obviously it's not ideal.
I suspect you want to catch pd.GenericGBQException. (Or perhaps gbq.GenericGBQException -- it depends on your imports. Are you importing the module that defines the exception you're trying to catch?)
Also, consider catching PandasError, the base class of all exceptions from the package: https://github.com/pydata/pandas/blob/master/pandas/io/gbq.py#L85
So I am writing some python scripts using ESRI's arcpy. I am inserting data into a db using . When all is well, i.e the object is created. If not, say the object already exists, nothing. No message, error, nothing. What are "best practices" for cases like this?
try:
arcpy.AddField_management(out_feature_path, 'URN', 'text', 0, 0, 100,
field_alias = 'URN',
field_is_nullable = "nullable", field_domain = '')
except Exception as error:
print(error)
Code supplied above, I dont think I need it, but I always see people getting upset for not posting code :) .
Basically I'm doing some web scraping with selenium and need to define a variable as one thing if no error occurs, or another thing if an error does occur.
Snippet:
try:
raw_cc_timeframe = driver.find_element_by_xpath("//*[#id='nearbyStore']/div/div/div/div/div/div/ul/li[1]/div[1]/p")
cc_timeframe = raw_cc_timeframe.text
except NoSuchElementException:
cc_timeframe = ""
I want the variable named cc_timeframe to be called the name of the element if the element exists, however if it does not, I want the variable to be blank.
I keep getting an unboundlocalerror and really can't figure out why despite reading numerous posts.
I've tried setting the variable to global, however when I run this function hundreds of times, the variables don't seem to reset each time, leading to wrong values.
I'm pretty new to all this so any help would be much appreciated.
Try setting cc_timeframe="" before doing the search and using pass as the response to the not found exception
try:
cc_timeframe = ""
raw_cc_timeframe = driver.find_element_by_xpath("//*[#id='nearbyStore']/div/div/div/div/div/div/ul/li[1]/div[1]/p")
cc_timeframe = raw_cc_timeframe.text
except NoSuchElementException:
pass
I'm trying to use try/except to query BigQuery tables, sometimes the query may not be correct in which case pandas raises a GenericGBQException error.
My problem is I get name 'GenericGBQException' is not defined when trying to handle this error, example code below:
try:
df = pd.read_gbq(query, projID)
query_fail = 0
except GenericGBQException:
query_fail = 1
if query_fail == 1:
do some stuff
I can get by with catching all exceptions though obviously it's not ideal.
I suspect you want to catch pd.GenericGBQException. (Or perhaps gbq.GenericGBQException -- it depends on your imports. Are you importing the module that defines the exception you're trying to catch?)
Also, consider catching PandasError, the base class of all exceptions from the package: https://github.com/pydata/pandas/blob/master/pandas/io/gbq.py#L85