Skip to main content

Operator to construct nested rollups from lists of records.

Project description

This module is a Python port of the nest operator from Mike Bostock’s d3.js.

Nest allows elements in an array to be grouped into a hierarchical tree structure; think of it like the GROUP BY operator in SQL, except you can have multiple levels of grouping, and the resulting output is a tree rather than a flat table. The levels in the tree are specified by key functions. The leaf nodes of the tree can be sorted by value, while the internal nodes can be sorted by key. An optional rollup function will collapse the elements in each leaf node using a summary function. The nest operator (the object returned by d3.nest) is reusable, and does not retain any references to the data that is nested.

For example, consider the following tabular data structure of Barley yields, from various sites in Minnesota during 1931-2:

>>> yields = [
    {"yield": 27.00, "variety": "Manchuria", "year": 1931, "site": "University Farm"},
    {"yield": 48.87, "variety": "Manchuria", "year": 1931, "site": "Waseca"},
    {"yield": 27.43, "variety": "Manchuria", "year": 1931, "site": "Morris"},
    {"yield": 43.07, "variety": "Glabron",   "year": 1931, "site": "University Farm"},
    {"yield": 55.20, "variety": "Glabron",   "year": 1931, "site": "Waseca"},
    {"yield": 16.18, "variety": "Glabron",   "year": 1932, "site": "University Farm"},
]

To facilitate visualization, it may be useful to nest the elements first by year, and then by variety, as follows:

>>> from nesting import Nest
>>> (Nest()
...     .key( lambda d: d['year'] )
...     .key( lambda d: d['variety'] )
...     .entries(yields))

Or more concisely:

>>> (Nest()
...     .key('year')
...     .key('variety')
...     .entries(yields))

…as both the key and prop functions will interpret non-callables as they key to look up.

This returns a nested array. Each element of the outer array is a key-values pair, listing the values for each distinct key:

[   {"key": 1931, "values": [
        {"key": "Manchuria", "values": [
            {"yield": 27.00, "variety": "Manchuria", "year": 1931, "site": "University Farm"},
            {"yield": 48.87, "variety": "Manchuria", "year": 1931, "site": "Waseca"},
            {"yield": 27.43, "variety": "Manchuria", "year": 1931, "site": "Morris"}, ]},
        {"key": "Glabron", "values": [
            {"yield": 43.07, "variety": "Glabron", "year": 1931, "site": "University Farm"},
            {"yield": 55.20, "variety": "Glabron", "year": 1931, "site": "Waseca"}, ]},
    ]},
    {"key": 1932, "values": [
        {"key": "Glabron", "values": [
            {"yield": 16.18, "variety": "Glabron", "year": 1932, "site": "University Farm"}, ]},
    ]},
]

The nested form allows easy iteration and generation of hierarchical structures in SVG or HTML.

This port is open-source, freely licensed under the MIT License.

Project details


Download files

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

Source Distributions

nesting-0.1.0.zip (10.6 kB view details)

Uploaded Source

nesting-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file nesting-0.1.0.zip.

File metadata

  • Download URL: nesting-0.1.0.zip
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nesting-0.1.0.zip
Algorithm Hash digest
SHA256 f31b7dd8a3ed6050ceffa686777eb1940a18e2348cfda6e93aa583355b5316b7
MD5 c11c293f502a97298aa1b3fac4687ba7
BLAKE2b-256 f057126c1c8b068d96e9cdc74d87295ae72bb56ce15dd8f8602308bb07257f56

See more details on using hashes here.

File details

Details for the file nesting-0.1.0.tar.gz.

File metadata

  • Download URL: nesting-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nesting-0.1.0.tar.gz
Algorithm Hash digest
SHA256 40805454c2b76e65f64e969999615356c48a3ec68169038ca95efd577d3f82f7
MD5 7bbab2843f46eddbd593c8d695c30e50
BLAKE2b-256 1fbf5b5a84dc8b093c7ae7eb313ff2b7d012405f6266742866b00594601e51c2

See more details on using hashes here.

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