Tool to make drilling into Python objects easy & painless
Project description
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.
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
```python
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[*][0]')
# returns ['x', 1]
```
==============
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.
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
```python
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[*][0]')
# returns ['x', 1]
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
safe_access-1.0.tar.gz
(9.5 kB
view hashes)
Built Distributions
safe_access-1.0-py2.7.egg
(5.8 kB
view hashes)
Close
Hashes for safe_access-1.0.macosx-10.10-intel.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f05ed5e87fc1014d7c2c24418dbc7ea64d2db40a31a8b4ddd025dc374c70208d |
|
MD5 | 39ddc4deb56dd16b147fb2cf5180b3c1 |
|
BLAKE2b-256 | 9cb296843e205f371d6c54c921c51a652cf49dad1e6a86c94c3ec86665fe0219 |