{"id":28428852,"url":"https://github.com/ivannz/plyr","last_synced_at":"2026-04-22T16:34:43.731Z","repository":{"id":41476442,"uuid":"431866294","full_name":"ivannz/plyr","owner":"ivannz","description":"Apply functions over deeply nested dicts, lists and tuples.","archived":false,"fork":false,"pushed_at":"2022-07-22T12:49:47.000Z","size":150,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-16T19:46:44.647Z","etag":null,"topics":["map","nested","nested-structures","python"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivannz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-25T14:03:14.000Z","updated_at":"2022-06-24T06:59:15.000Z","dependencies_parsed_at":"2022-08-20T08:40:54.318Z","dependency_job_id":null,"html_url":"https://github.com/ivannz/plyr","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/ivannz/plyr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivannz%2Fplyr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivannz%2Fplyr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivannz%2Fplyr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivannz%2Fplyr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivannz","download_url":"https://codeload.github.com/ivannz/plyr/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivannz%2Fplyr/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32145866,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-22T15:33:03.595Z","status":"ssl_error","status_checked_at":"2026-04-22T15:30:42.712Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["map","nested","nested-structures","python"],"created_at":"2025-06-05T13:08:00.297Z","updated_at":"2026-04-22T16:34:38.723Z","avatar_url":"https://github.com/ivannz.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Plyr: computing on nested containers\n\n`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.\n\n`plyr` happens to coincide with a similarly named library for [`R` statistical computations language](https://www.r-project.org/), which streamlines dataframe and vector/matrix transformations.\n\n## Setup\n\nThe package can be installed from `pip`:\n\n```bash\npip install python-plyr\n```\n\nThe development environment could be initialized with the following commands:\n\n```bash\n# create a dedicated env\nconda create -n plyr \"python\u003e=3.7\" pip pytest twine \\\n  \u0026\u0026 conda activate plyr \\\n  \u0026\u0026 pip install build\n\n# build and install\npip install -e . -vv\n```\n\n## the Essential Example\n\nBelow we provide an example, which, we hope, illustrates the cases `plyr` might be useful in.\n\n```python\nimport plyr\nfrom collections import namedtuple\n\nnt = namedtuple(\"nt\", \"u,v\")\n\n# add the leaf data in a pair of nested objects\nplyr.apply(\n    lambda u, v: u + v,\n    [{\"a\": (1, 2, 3), \"z\": 3.1415}, nt([1, \"u\"], \"abc\")],\n    [{\"a\": (4, 6, 8), \"z\": 2.7128}, nt([4, \"v\"], \"xyz\")],\n    # _star=True,  # (default) call fn(d1, d2, **kwargs)\n)\n# output: [{'a': (5, 8, 11), 'z': 5.8543}, nt(u=[5, 'uv'], v='abcxyz')]\n\n# join strings in a pair of tuples\nplyr.apply(\n    \" --\u003e\u003e \".join,\n    (\"abc\", \"uvw\", \"xyz\"),\n    (\"123\", \"456\", \"789\"),\n    _star=False,  # call fn((d1, d2,), **kwargs)\n)\n# output: ('abc --\u003e\u003e 123', 'uvw --\u003e\u003e 456', 'xyz --\u003e\u003e 789')\n```\n\nBy default `.apply` performs safety checks to ensure identical 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`.\n\n`plyr.ragged` is a special version of `.apply` which implements leaf broadcasting semantics. When processing multiple nested objects it allows one structure to subsume the other structures: any intermediate leaf data **is broadcasted deeper into the hierarchy** of the other nested structures. Please refer to `./doc/mapping_structures.ipynb` for details.\n\n## Serializing and deserializing\n\nSerialization and deserialization of nested objects can be done by `.flatten` and `.unflatten`. The following snippet shows how to represent a given nested object as a flat list and then undo the process.\n\n```python\nimport plyr\n\no = [1, (2, 3), {\"a\": (4, 5), \"z\": {\"a\": 6}}, 7]\n\nflat, skel = plyr.flatten(o)\nassert o == plyr.unflatten(flat, skel)\n\nflat\n# output: ([1, 2, 3, 4, 5, 6, 7]\n```\n\nThis next example demonstrates how to unpack a stream of data into nested objects.\n\n```python\nimport plyr\n\nstream, _ = iter(range(13)), None\n\nstruct = ({\"foo\": _, \"bar\": [_, _]}, _)\n\nobjects = [plyr.unflatten(stream, struct) for _ in range(3)]\n```\n\nThe following example illustrates how to compute the a functon over a nested object and *invert* the structure (see `.iapply`):\n\n```python\nimport plyr\n\n\ndef func(x, y):\n    return {\"z\": x * y}, -y\n\n\n# `inverted` shall nests the original structure of `o` _under_ func's\n#  return-value structure\nflat, struct = plyr.flatapply(func, (2, {\"a\": 1},), (4, {\"a\": 3},))\ninverted = plyr.apply(plyr.unflatten, *flat, _star=False, struct=struct)\n\ninverted\n# output: ({'z': (8, {'a': 3})}, (-4, {'a': -3}))\n```\n\nHere's how one might broadcast a value across the structure of a specified nested object.\n\n```python\nimport plyr\n\nstruct = {\"foo\": {\"bar\": 1}, \"baz\": (2, 3)}\n\nconst = \"abc\"\nbroadcast = plyr.apply(lambda x: const, struct)\n\nbroadcast\n# output: {'foo': {'bar': 'abc'}, 'baz': ('abc', 'abc')}\n```\n\n## Other Examples\n\nBelow we perform something fancy with `numpy`. Specifically, we stack outputs from some experiments (dicts of arrays), to get the standard deviation between the results.\n\n```python\nimport plyr\nimport numpy as np\n\n\n# some computations\ndef experiment(j):\n    return dict(\n        a=float(np.random.normal()),\n        u=np.random.normal(size=(5, 2)) * 0.1,\n        z=np.random.normal(size=(2, 5)) * 10,\n    )\n\n\n# run 10 replications of an experiment\nresults = [experiment(j) for j in range(10)]\n\n# stack and analyze the results (np.stack needs an iterable argument)\nres = plyr.apply(np.stack, *results, axis=0, _star=False)\n\n# get the shapes\nshapes = plyr.apply(lambda x: x.shape, res)\n\n# compute the std along the replication axis\nplyr.apply(np.std, res, axis=0)\n```\n\nYou may notice that `.apply` is very *unsophisticated*: it applies the specified function to the leaf data regardless of its type, and every dict, list, or tuple is *always* treated as a nested container.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivannz%2Fplyr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivannz%2Fplyr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivannz%2Fplyr/lists"}