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)  # 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}]

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-1.0.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_accessor-1.0.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file python_accessor-1.0.2.tar.gz.

File metadata

  • Download URL: python_accessor-1.0.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_accessor-1.0.2.tar.gz
Algorithm Hash digest
SHA256 08ceeeebf9b33379fc5814fc3b9b05e57d55f2fe30cbdb8c46a542dd9d4734d0
MD5 e9aa57b6f39bf593bfd6dd171812f60f
BLAKE2b-256 ee30be7e1fe59210134804cb1adb91fae5451bc7664154cdd65aabcdb4636ee5

See more details on using hashes here.

File details

Details for the file python_accessor-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for python_accessor-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0445dccbef777db2665a1da126f7fa10accdba28cf32f2ecaf5e0d5905f2c25d
MD5 0cc52ce5f2f60dbf12015529ed65f6b4
BLAKE2b-256 5026f38c2125395d05c9067f94d67aa87873f077f727b7446a1a060f2d044bdf

See more details on using hashes here.

Supported by

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