Skip to main content

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


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)

Uploaded Source

Built Distribution

python_accessor-0.0.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page