Skip to content

Simple library to allow you to drill down multiple levels into any python object without having to worry about AttributeErrors, KeyErrors, or IndexErrors

License

Notifications You must be signed in to change notification settings

jwegan/safe_access_py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Safe Access

Tired of always checking hasattr, len, or if a key is in a dictionary? safe_access allows you to safely drill down multiple levels into any python object without having to worry about AttributeErrors, KeyErrors, or IndexErrors. Oh, and it supports wildcards.

Available on pypi: https://pypi.python.org/pypi/safe_access

from safe_access import safe_access
class A(object):
  pass

a = A()
a.b = {"abc": ['x', 'y', 'z'], "def": [1, 2, 3]}

# Access valid path
print safe_access(a, 'a.b["abc"][1]', default_value=7)
# returns 'y'

# Access valid path with variable substitution
myvar = 1
print safe_access(a, 'a.b["abc"][myvar]', default_value=7, myvar=myvar)
# returns 'y'

# Access that causes index out of range, but returns default value of 7
print safe_access(a, 'a.b["abc"][404]', default_value=7)
# returns 7

# Access non-existant attribute
print safe_access(a, 'a.bad_attribute')
# returns None

# Access wildcard
print safe_access(a, 'a.b[*][0]')
# returns ['x', 1]

Limitations:

  1. Does not support function calls at this time

  2. Does not support escaped quotations

  3. Does not support variable references withing tuple literals

About

Simple library to allow you to drill down multiple levels into any python object without having to worry about AttributeErrors, KeyErrors, or IndexErrors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages