Skip to main content

Mapping tools for nested containers.

Project description

Plyr: computing on nested containers

plyr [/plaɪ'ə/], derived from applier, is a python C-extension, that implements a map-like logic, which computes a specified function on the lower-most non-container data of arbitrarily nested built-in python containers, i.e. dicts, lists, tuples. It automatically unpacks nested containers in order to call the same function on their underlying non-container objects and then reassembles the structures. See the docstring of plyr.apply for details.

plyr [/plaɪ'ə/] is derived from applier, but also happens to coincide with a similarly named library for R statistical computations language, which streamlines dataframe and vector/matrix transformations.

Example

Below we provide hopefully an illustrative example of the cases plyr might be useful in.

import plyr

# add the leaf data in a pair of nested objects
plyr.apply(
    lambda u, v: u + v,
    [{'a': [1, 2, 3]}, {'b': 1, 'z': 'abc'}, ],
    [{'a': [4, 6, 8]}, {'b': 4, 'z': 'xyz'}, ],
    # _star=True,  # (default) call fn(d1, d2, **kwargs)
)
# output: [{'a': [5, 8, 11]}, {'b': 5, 'z': 'abcxyz'}]

# join strings in a pair of tuples
plyr.apply(
    ' -->> '.join,
    ('a-b-c', 'u-v-w', 'x-y-z',),
    ('123', '456', '789',),
    _star=False,  # call fn((d1, d2,), **kwargs)
)
# output: ('abc -->> 123', 'uvw -->> 456', 'xyz -->> 789')

By default .apply performs safety checks to ensure indentical structure if multiple nested objects are given. If the arguments have identical structure by design, then these integrity checks may be turned off by specifying _safe=False. Please refer to the docs of plyr.apply.

Other examples

Below we perform something fancy with numpy. Specifically we stack experimental results (dicts of arrays), then get the standard deviation between the results

import plyr
import numpy as np


# some computations
def experiment(j):
    return dict(
        a=float(np.random.normal()),
        u=np.random.normal(size=(5, 2)) * 0.1,
        z=np.random.normal(size=(2, 5)) * 10,
    )


# run 10 replications of an experiment
results = [experiment(j) for j in range(10)]

# stack and analyze the results (np.stack needs an iterable argument)
res = plyr.apply(np.stack, *results, axis=0, _star=False)

# get the shapes
shapes = plyr.apply(lambda x: x.shape, res)

# compute the std along the replication axis
plyr.apply(np.std, res, axis=0)

You may notice that .apply is very straightforward: it applies the specified function regardless of the leaf data type.

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-plyr-0.5.3.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

python_plyr-0.5.3-cp38-cp38-macosx_10_9_x86_64.whl (15.3 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

File details

Details for the file python-plyr-0.5.3.tar.gz.

File metadata

  • Download URL: python-plyr-0.5.3.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for python-plyr-0.5.3.tar.gz
Algorithm Hash digest
SHA256 842e81e4d950cedd21642e136c08dde6d4549f11b11bd933ea15fa4df9221d0a
MD5 0fd7e0ff0cf1268f02cd030c34040df9
BLAKE2b-256 33dc3f1241863bfdd8f83f92ff2a36de51db5d33bc3c8ca3dcdcd96b99812dfe

See more details on using hashes here.

File details

Details for the file python_plyr-0.5.3-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: python_plyr-0.5.3-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for python_plyr-0.5.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e2b1fff0a734aa070b29f5aab152a4c112588f44e1ddef4d37e3763c7bcab351
MD5 0160c78149da043b22b5e35bf3badd04
BLAKE2b-256 2c1946025c895bc15a02af051adfeb5d554a7bd5d29471f8d46a2e12628ac938

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