Skip to content

Instantly share code, notes, and snippets.

@pirosuke
Last active August 30, 2015 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirosuke/00f03ede8921078b0a8c to your computer and use it in GitHub Desktop.
Save pirosuke/00f03ede8921078b0a8c to your computer and use it in GitHub Desktop.
A function to get value from multi level object. Usage: get_obj_attr(item, "ItemAttributes.Title")
def get_obj_attr(root, str_path, default_val="", debug=False):
attr_path = str_path.split(".")
base_obj = root
for attr in attr_path:
if hasattr(base_obj, attr):
base_obj = base_obj.__dict__[attr]
else:
if debug:
print "No attribute: %s" % attr
base_obj = default_val
break
if base_obj != default_val:
base_obj = unicode(base_obj)
return base_obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment