Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

Nest a sequence of records

Project description

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

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

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


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.

Filename, size & hash SHA256 hash help File type Python version Upload date
nestable-0.0.6-py2.py3-none-any.whl (4.1 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Dec 31, 2017
nestable-0.0.6.tar.gz (2.4 kB) Copy SHA256 hash SHA256 Source None Dec 31, 2017

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