Where is it better to wrap a function with try-except? - python

Next function will fail with some data. Where is it better to wrap it with a try-except block?
if attr in obj_request:
new_record = GosReestr()
new_record.display_name_department = request.user.profile_ad.department
new_record.set_data(**obj_request[attr][_main_data])
print('before')
new_record.save()
print('after')
Is this better?
if attr in obj_request:
new_record = GosReestr()
new_record.display_name_department = request.user.profile_ad.department
new_record.set_data(**obj_request[attr][_main_data])
print('before')
try:
new_record.save()
except as e
print('after')
Or is it better to wrap everything in one if block?

Related

How to combine multiple try-except blocks?

I have a bunch of statements I want to use try, except on. Yes bad practise.
There must be a more pythonic way to write the following?
try:
E1=clean_html.find("span",{"class":"range-a"}).text
except AttributeError:
E1=None
try:
E2=clean_html.find("span",{"class":"range-b"}).text
except AttributeError:
E2=None
try:
E3=clean_html.find("span",{"class":"range-c"}).text
except AttributeError:
E3=None
try:
E4=clean_html.find("div",{"class":"Description"}).get_text(separator=" ").strip()
except AttributeError:
E4=None
Because this code works fine for me, but it looks unefficient.
You can write a function that handles the try and except. You can pass the post_processing as a lambda if you want the function to handle the problem of None throwing errors when calling methods on it
def get_clean_text(tag, class_name, post_processor):
try:
return post_processor(clean_html.find(tag,{"class": class_name}))
except AttributeError:
return None
E1 = get_clean_text("span", "range-a", lambda o: o.text)
E4 = get_clean_text("div", "Description", lambda o: o.get_text(separator=" ").strip())
You can assign a default to the variables and then do the work. Since you want the default to be the immutable None, you can
E1 = E2 = E3 = E4 = None
try:
E1=clean_html.find("span",{"class":"range-a"}).text
E2=clean_html.find("span",{"class":"range-b"}).text
E3=clean_html.find("span",{"class":"range-c"}).text
E4=clean_html.find("div",{"class":"Description"}).get_text(separator=" ").strip()
except AttributeError:
pass
The best way to handle exceptions depends on more context than we have here, but generally you want to write larger blocks that stay in a sane state on any "exceptional" operation.
Since exceptions take some processing power to setup and may be visually untidy, testing (especially when wrapped in its own function) can be a better way to go.
def get_elem_text(elem, default=None):
if elem:
return elem.text
else:
return default
E1=get_elem_text(clean_html.find("span",{"class":"range-a"}))
E2=get_elem_text(clean_html.find("span",{"class":"range-b"}))
E3=get_elem_text(clean_html.find("span",{"class":"range-c"}))
# I don't know what "get_text" is so can't abstract it
#E4=get_elem_text(clean_html.find("div",{"class":"Description"}), default="")).strip()

One-liner to save value of if statement?

Is there a smart way to write the following code in three or four lines?
a=l["artist"]
if a:
b=a["projects"]
if b:
c=b["project"]
if c:
print c
So I thought for something like pseudocode:
a = l["artist"] if True:
How about:
try:
print l["artist"]["projects"]["project"]
except KeyError:
pass
except TypeError:
pass # None["key"] raises TypeError.
This will try to print the value, but if a KeyError is raised, the except block will be run. pass means to do nothing. This is known and EAFP: it’s Easier to Ask Forgiveness than Permission.
I don't necessarily think that this is better but you could do:
try:
c = l["artist"]["projects"]["project"]
except (KeyError, TypeError) as e:
print e
pass
p = l.get('artist') and l['artist'].get('projects') and l['artist']['projects'].get('project')
if p:
print p
You can also make a more general function for this purpose:
def get_attr(lst, attr):
current = lst
for a in attr:
if current.get(a) is not None:
current = current.get(a)
else:
break
return current
>>> l = {'artist':{'projects':{'project':1625}}}
>>> get_attr(l,['artist','projects','project'])
1625
One-liner (as in the title) without exceptions:
if "artist" in l and l["artist"] and "projects" in l["artist"] and l["artist"]["projects"] and "project" in l["artist"]["projects"]: print l["artist"]["projects"]["project"]
Since you're dealing with nested dictionaries, you might find this generic one-liner useful because it will allow you to access values at any level just by passing it more keys arguments:
nested_dict_get = lambda item, *keys: reduce(lambda d, k: d.get(k), keys, item)
l = {'artist': {'projects': {'project': 'the_value'}}}
print( nested_dict_get(l, 'artist', 'projects', 'project') ) # -> the_value
Note: In Python 3, you'd need to add a from functools import reduce at the top.

Refactor long try-except chain

I have a feeling that this sequence might be written shorter:
dim = Dimension.objects.get(pk=rows['pk'])
try:
dim.name = rows['name']
except KeyError:
pass
try:
dim.external_flg = rows['external_flg']
except:
pass
try:
dim.ext_owner = rows['ext_owner']
except KeyError:
pass
try:
dim.ext_table_name = rows['ext_table_name']
except KeyError:
pass
try:
dim.ext_start_date_column_name = rows['ext_start_date_column_name']
except KeyError:
pass
try:
dim.ext_end_date_column_name = rows['ext_end_date_column_name']
except KeyError:
pass
I've never had any experience in Python code optimization and working with exceptions but I'd be glad to have an alternative examples how it could be shortened.
Thank you!
Use the dict.get() method:
dim.name = rows.get('name', dim.name)
Or, for more DRY solution, put all assignments into the loop:
for field_name in ('name', 'external_flg', 'ext_owner', 'ext_table_name',
'ext_start_date_column_name', 'ext_end_date_column_name'):
if field_name in rows:
setattr(dim, field_name, rows[field_name])
To handle the list data you can make the similar loop:
for i, field_name in enumerate(('name', 'external_flg', 'ext_owner',
'ext_table_name', 'ext_start_date_column_name',
'ext_end_date_column_name')):
if i < len[rows]:
setattr(dim, field_name, rows[i])
Something like this should work — this answer includes try/catch block to catch KeyError :-)
attributes = ['name', 'external_flg', 'ext_owner', 'ext_table_name',
'ext_start_date_column_name', 'ext_end_date_column_name']
dim = Dimension.objects.get(pk=rows['pk'])
for attr in attributes:
try:
setattr(dim, attr, rows[attr])
except KeyError:
pass

Check if object path exists in tree of objects

I have a tree of objects and I need to check that particular object contains specific branch of objects. For example:
def specificNodeHasTitle(specificNode):
# something like this
return specificNode.parent.parent.parent.header.title != None
Is there an elegant way to do this without throwing exception if needed attribute is missing?
This works as long as you don't need indexes of arrays in your path to the item.
def getIn(d, arraypath, default=None):
if not d:
return d
if not arraypath:
return d
else:
return getIn(d.get(arraypath[0]), arraypath[1:], default) \
if d.get(arraypath[0]) else default
getIn(specificNode,["parent", "parent", "parent", "header", "title"]) is not None
Use try..except:
def specificNodeHasTitle(specificNode):
try:
return specificNode.parent.parent.parent.header.title is not None
except AttributeError:
# handle exception, for example
return False
There is nothing wrong with raising exceptions, by the way. It is a normal part of Python programming. Using try..except is the way to handle them.
For your specific case, the solution provided by unutbu is the best and the most pythonic, but I can't help trying to show the great capabilities of python and its getattr method:
#!/usr/bin/env python
# https://stackoverflow.com/questions/22864932/python-check-if-object-path-exists-in-tree-of-objects
class A(object):
pass
class Header(object):
def __init__(self):
self.title = "Hello"
specificNode=A()
specificNode.parent = A()
specificNode.parent.parent = A()
specificNode.parent.parent.parent = A()
specificNode.parent.parent.parent.header = Header()
hierarchy1="parent.parent.parent.header.title"
hierarchy2="parent.parent.parent.parent.header.title"
tmp = specificNode
for attr in hierarchy1.split('.'):
try:
tmp = getattr(tmp, attr)
except AttributeError:
print "Ouch... nopes"
break
else:
print "Yeeeps. %s" % tmp
tmp = specificNode
for attr in hierarchy2.split('.'):
try:
tmp = getattr(tmp, attr)
except AttributeError:
print "Ouch... nopes"
break
else:
print "Yeeeps. %s" % tmp
That outputs:
Yeeeps. Hello
Ouch... nopes
Python's great :)

Concise replacement for long list of Try-Except statement in Python?

I have this long list of try except statement:
try:
uri = entry_obj['media$group']['media$content'][0]['url']
except (KeyError, IndexError):
uri = None
try:
position = entry_obj['yt$position']['$t']
except KeyError:
position = None
try:
description = entry_obj['content']['$t']
except KeyError:
description = None
try:
seconds = entry_obj['media$group']['yt$duration']['seconds']
except KeyError:
seconds = None
try:
thumbnails = entry_obj['media$group']['media$thumbnail']
except KeyError:
thumbnails = None
Is there a more concise way to write this?
If you tire of figuring out what to use for default values in get() calls, just write a helper function:
def resolve(root, *keys):
for key in keys:
try:
root = root[key]
except (KeyError, IndexError):
return None
return root
Then you just write, e.g.:
uri = resolve(entry_obj, 'media$group', 'media$content', 0, 'url')
To simplify the calls a little, you might beef up the helper function to take a single string for the keys and split on spaces; that way you don't have to type so many quotes, and we can also add a default value argument:
def resolve(root, keys, default=None):
for key in keys.split():
try:
root = root[key]
except (TypeError, KeyError):
try:
root = root[int(key)]
except (IndexError, ValueError, KeyError):
return default
uri = resolve(entry_obj, 'media$group media$content 0 url', '')
I thought of another good way to do this, not sure how it compares to kindall's method. We first define a method property:
def res(self, property):
try:
return property()
except (KeyError, IndexError):
return None
Then replace the try-except statements with:
url = res(lambda: entry_obj['media$group']['media$content'][0]['url'])
position = res(lambda: entry_obj['yt$position']['$t'])
description = res(lambda: entry_obj['content']['$t'])
duration = res(lambda: entry_obj['media$group']['yt$duration']['seconds'])
thumbnails = res(lambda: entry_obj['media$group']['media$thumbnail'])
Use the get method of dictionaries instead:
position = entry_object.get('yt$position').get('$t')
get will handle the case of a key not existing for you, and give you a (changable) fallback value instead in that case. You'll still need to handle the first IndexError manually, but all the ones that are just except KeyError: will disappear.

Categories