Skip to main content

Nest a sequence of records

Project description

https://img.shields.io/pypi/v/nestable.svg https://travis-ci.org/iaindillingham/nestable.svg?branch=master

Nestable nests a sequence of records: that is, Nestable groups a sequence of records into a nested dict.

Nestable is a partial implementation of D3’s Nest operator.

>>> from nestable import nest
>>> records = [
...     {'country': 'Algeria', 'region': 'Africa'},
...     {'country': 'Benin', 'region': 'Africa'},
...     {'country': 'Albania', 'region': 'EEG'},
... ]
>>> accessor = lambda x: (x.get('region'), x.get('country'))
>>> obs = nest(records, accessor)
>>> exp = {'EEG': {'Albania': [{'country': 'Albania', 'region': 'EEG'}]},
...        'Africa': {'Algeria': [{'country': 'Algeria', 'region': 'Africa'}],
...                   'Benin': [{'country': 'Benin', 'region': 'Africa'}]}}
>>> obs == exp
True

Like D3’s Nest operator, Nestable allows you to supply a rollup function that is applied to each sequence of leaf elements. The return value of this function will replace each sequence of leaf elements in the nested dict.

>>> obs = nest(records, accessor, rollup=len)
>>> exp = {'EEG': {'Albania': 1}, 'Africa': {'Algeria': 1, 'Benin': 1}}
>>> obs == exp
True

Unlike D3’s Nest operator, however, Nestable doesn’t support sorted keys or sorted values.

Development

Activate a new virtual environment and install nestable in development mode with its development dependencies:

pip install -e . -r requirements.txt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for nestable, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size nestable-0.0.6-py2.py3-none-any.whl (4.1 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size nestable-0.0.6.tar.gz (2.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page