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) # equivalent 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}]
Release files for python-accessor 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_accessor-1.0.2.tar.gz | 7.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_accessor-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:11.9 kB
Release files / python_accessor-1.0.2.tar.gz
| Download URL | python_accessor-1.0.2.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08ceeeebf9b33379fc5814fc3b9b05e57d55f2fe30cbdb8c46a542dd9d4734d0
|
|
BLAKE2b-256 checksum How to use checksums |
ee30be7e1fe59210134804cb1adb91fae5451bc7664154cdd65aabcdb4636ee5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / python_accessor-1.0.2-py3-none-any.whl
| Download URL | python_accessor-1.0.2-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0445dccbef777db2665a1da126f7fa10accdba28cf32f2ecaf5e0d5905f2c25d
|
|
BLAKE2b-256 checksum How to use checksums |
5026f38c2125395d05c9067f94d67aa87873f077f727b7446a1a060f2d044bdf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|