Access nested dicts
Project description
Accessor: read nested dictionaries
Build accessor functions using the natural python dot notation.
Installation
python-accessor
is available as a zero-dependency Python package. Install with:
$ pip install python-accessor
Usage
from accessor import accessor as _
name = _.users.name
name(obj) # equvalent of obj['users']['name']
Examples
from accessor import accessor as _
obj = {
'users': [{
'uid': 1234,
'name': {
'first': 'John',
'last': 'Smith',
}
}, {
'uid': 2345,
'name': {
'last': 'Bono'
}
}, {
'uid': 3456
}]
}
_.users[1].name(obj)
# -> {'last': 'Bono'}
_.users.name.last(obj)
# -> ['Smith', 'Bono', None]
_.users.name.first(obj)
# -> ['John', None, None]
_.users.name.first[:1](obj)
# -> ['John']
_.users.uid[:2](obj)
# -> [1234, 2345]
list(map(_.name.last, obj['users']))
# -> ['Smith', 'Bono', None]
list(filter(_.uid > 300, obj['users']))
# -> [{'uid': 3456}]
More Examples! :)
from accessor import accessor as _, select, values
# extract values
list(map(values(_.name.first, _.name.last), obj['users']))
# -> [('John', 'Smith'), (None, 'Bono'), (None, None)]
# extract as dicts
list(map(select(_.name.first, _.name.last),obj['users']))
# -> [{'first': 'John', 'last': 'Smith'}, {'first': None, 'last': 'Bono'}, {'first': None, 'last': None}]
# extract and optionally rename
list(map(select(_.name.uid, x=_.name.last),obj['users']))
# -> [{'uid': '1234', 'x': 'Smith'}, {'uid': 2345, 'x': 'Bono'}, {'uid': 3456, 'x': None}]
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
python-accessor-0.0.1.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for python_accessor-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efb1c1c5d8d9b86679de7970804b236730b56fa9f5bab71e30b83d5b4532ccee |
|
MD5 | e4e038d83fd94d794456c075973be86c |
|
BLAKE2b-256 | 5b57657698e9fa2ce019d78895a72a8edda0c05e3d281b6c9f3fa3dad488e7cc |