{ "info": { "author": "Aetius Flavius", "author_email": "aetius.flavius.390@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3" ], "description": "# Nifty Nesting\n\n### Python utilities for manipulating arbitrarily nested data structures.\n\nIncludes: `flatten`, `map`, `pack_into`, `filter`, `reduce`, `assert_same_structure`\n\nHeavily inspired by the [internal nesting utilities in TensorFlow.](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/util/nest.py)\n\nSupports `collections.Sequence` (`list`, `tuple`, etc.), `collections.Mapping` (`dict`, etc.), `set`, `namedtuple`, and `attr` data classes as part of the nesting structure.\n\nAllows users to specify what elements should be considered part of the nesting structure and which elements should be considered \"atomic\" data elements via an `is_atomic` argument to all functions.\n\nExamples:\n\n### flatten\n\nReturns a list containing every atomic element of a nested structure. Elements are returned in a determinstic order.\n\n```python\nimport nifty_nesting as nest\n\nstructure = [1, (2, {'a': 3}, 4]\nflat = nest.flatten(structure)\nassert flat == [1, 2, 3, 4]\n\nstructure = ([1, 2], {'a': [3, 4], 'b': [5, 6]})\nflat = nest.flatten(structure, is_atomic=lambda x: isinstance(x, list))\nassert flat == [[1, 2], [3, 4], [5, 6]] \n```\n\n### map\n\nMaps every atomic element of a nested structure.\n\n```python\nimport nifty_nesting as nest\n\nstructure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}\nmapped = nest.map(lambda x: 2*x, structure)\nassert mapped == {'a': [2, 4], 'b': (6, 8, {'c': 10})}\n\nstructure = ([1, 2], {'a': [3, 4], 'b': [5, 6]})\nmapped = nest.map(lambda x: max(x), structure, is_atomic=lambda x: isinstance(x, list))\nassert mapped == (2, {'a': 4, 'b': 6})\n```\n\n### pack_list_into\n\nPacks a flat list into any arbitrary structure with the same number of atomic elements. Elements are packed in a deterministic order that is compatible with flat lists created by `flatten`.\n\n```python\nimport nifty_nesting as nest\n\nstructure = (1, {'key': [2, {3, 4}, 5]}, [6, 7])\nflat_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']\npacked = nest.pack_list_into(structure, flat_list)\nassert packed == ('a', {'key': ['b', {'c', 'd'}, 'e']}, ['f', 'g'])\n```\n\n## Documentation\n\n### Main functions\n\n#### flatten(structure, is_atomic=is_scalar)\n\n Returns a flattened list containing the atomic elements of `structure`.\n\n The elements of `structure` are flattened in a deterministic order.\n\n ```\n import nifty_nesting as nest\n flat = nest.flatten([1, (2, {'a': 3}, 4])\n assert flat == [1, 2, 3, 4]\n ```\n\n Arguments:\n structure: An arbitrarily nested structure of elements.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Returns:\n A list containing every atomic element of `structure`.\n\n#### map(func, structure, is_atomic=is_scalar)\n Maps the atomic elements of `structure`.\n\n ```\n import nifty_nesting as nest\n structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}\n mapped = nest.map(lambda x: 2*x, structure)\n assert mapped == {'a': [2, 4], 'b': (6, 8, {'c': 10})}\n ```\n\n Arguments:\n func: The function to use to map atomic elements of `structure`.\n structure: An arbitrarily nested structure of elements.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Returns:\n A structure with the same structure as `structure`, with the atomic elements\n mapped according to `func`.\n\n#### reduce(func, structure, is_atomic=is_scalar):\n Reduces the atomic elements of `structure`.\n\n ```\n import nifty_nesting as nest\n structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}\n reduced = nest.reduce(lambda x, y: x+y, structure)\n assert reduced == 15\n ```\n\n Arguments:\n func: The function to use to reduce atomic elements of `structure`.\n structure: An arbitrarily nested structure of elements.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Returns:\n The reduced value.\n\n #### filter(func, structure, keep_structure=True, is_atomic=is_scalar)\n Filters the atomic elements of `structure`.\n\n ```\n import nifty_nesting as nest\n structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}\n filtered = nest.filter(lambda x: x > 2, structure)\n assert filtered == {'a': [], 'b': (3, 4, {'c': 5})}\n\n filtered = nest.filter(lambda x: x > 2, structure, keep_structure=False)\n assert filtered == {'b': (3, 4, {'c': 5})}\n ```\n\n Arguments:\n func: The function to use to filter atomic elements of `structure`.\n structure: An arbitrarily nested structure of elements.\n keep_structure: Whether or not to preserve empty substructures. If\n `True`, these structures will be kept. If `False`, they will be\n entirely filtered out.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Returns:\n The filtered elements of `structure` in the same structure as `structure`.\n\n #### assert_same_structure(structure1, structure2, is_atomic=is_scalar)\n Asserts that `structure1` and `structure2` have the same nested structure.\n\n ```\n import nifty_nesting as nest\n structure1 = {'a': [1, 2], 'b': (3, 4, {'c': 5})}\n structure1 = {'a': ['a', 'b'], 'b': ('c', 'd', {'c': 'e'})}\n nest.assert_same_structure(structure1, structure2)\n ```\n\n Arguments:\n structure1: An arbitrarily nested structure of elements.\n structure2: An arbitrarily nested structure of elements.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Raises:\n `AssertionError` if the structures are not the same.\n\n#### pack_list_into(structure, flat_list, is_atomic=is_scalar)\n Packs the atomic elements of `flat_list` into the same structure as `structure`.\n\n ``\n import nifty_nesting as nest\n structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}\n flat_list = [2, 4, 6, 7, 10]\n packed = nest.pack_list_into(structure, flat_list)\n assert packed == {'a': [2, 4], 'b': (6, 8, {'c': 10})}\n ```\n\n Arguments:\n structure: An arbitrarily nested structure of elements.\n flat_list: A flat list with the same number of atomic elements as\n `structure`.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Returns:\n A structure with the atomic elements of `flat_list` packed into the same\n structure as `structure`.\n\n### Helper functions for `is_atomic` \n\n #### is_scalar(element)\n An `is_atomic` criterion. Returns `True` for scalar elements.\n\n Scalar elements are : strings and any object that is not one of:\n collections.Sequence, collections.Mapping, set, or attrs object.\n\n ```\n import nifty_nesting as nest\n flat = nest.flatten([1, [2, 3]], is_atomic=is_scalar)\n assert flat == [1, 2, 3]\n ```\n\n Arguments:\n element: The element to check.\n\n Returns:\n `True` if the element is a scalar, else `False`.\n\n#### has_max_depth(depth, is_atomic=is_scalar)\n Returns an `is_atomic` criterion that checks the depth of a structure.\n\n This function returns a function that can be passed to `is_atomic` to\n preserve all structures up to a given depth.\n\n ```\n import nifty_nesting as nest\n flat = nest.flatten([[1, 2], [3, [4, 5]]], is_atomic=has_max_depth(1))\n assert flat == [[1, 2], [3], [4, 5]]\n ```\n\n Arguments:\n depth: The maximum depth a structure can have in order to be considered\n as an atomic element. For instance, `[1, 2, {'a': 3}]` has a depth of 2.\n is_atomic: A function that returns `True` if a certain element\n of `structure` ought to be treated as an atomic element, i.e.\n not as part of the nesting structure.\n\n Returns:\n A function that can be passed to `is_atomic` to check for elements\n with a depth of `depth` or less.\n\n#### is_sequence(element)\n\nReturns `True` for instances of `collections.Sequence`.\n\n#### is_mapping(element)\n\nReturns `True` for instances of `collections.Mapping`.\n\n#### is_set(element)\n\n Returns `True` for instances of `set`.\n\n #### is_namedtuple(element)\n\n Returns `True` for instances of `namedtuple`.\n\n #### is_attrs_object(element)\n\n Returns `True` for instances of `attr`-decorated classes.\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/aetiusflavius/nifty-nesting/", "keywords": "nested,data,structure,arbitrary,utilities,manipulation", "license": "", "maintainer": "", "maintainer_email": "", "name": "nifty-nesting", "package_url": "https://pypi.org/project/nifty-nesting/", "platform": "", "project_url": "https://pypi.org/project/nifty-nesting/", "project_urls": { "Homepage": "https://github.com/aetiusflavius/nifty-nesting/" }, "release_url": "https://pypi.org/project/nifty-nesting/0.2.3/", "requires_dist": [ "attrs", "six" ], "requires_python": "", "summary": "Python utilities for arbitrarily nested data structures.", "version": "0.2.3" }, "last_serial": 4611139, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "7d228e86a66f0181f246bd98c8f1bdd8", "sha256": "5dee9595c3fba8d5f74e111f95f4ec0e6689d5027a484e7160e243ba58c78f2d" }, "downloads": -1, "filename": "nifty_nesting-0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "7d228e86a66f0181f246bd98c8f1bdd8", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8928, "upload_time": "2018-12-18T07:27:49", "url": "https://files.pythonhosted.org/packages/22/d2/1d76ea631c963b81ee02bc8b5c04da723ac1c6c0f1795378c3821292f7bc/nifty_nesting-0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b990bd7deafb93fe13eeabd55fe67249", "sha256": "7069f2e120a56db24d0e63c33f774536e847e727c9588d1ccf8202c149d7b8ea" }, "downloads": -1, "filename": "nifty-nesting-0.1.tar.gz", "has_sig": false, "md5_digest": "b990bd7deafb93fe13eeabd55fe67249", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7644, "upload_time": "2018-12-18T07:27:53", "url": "https://files.pythonhosted.org/packages/e4/fb/a9d9dbaeb819f4c96eab821e950f5f8df68364b7a5d4bb4b10ccdd907149/nifty-nesting-0.1.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "542f443fb362be4d7b47b20c7fae863d", "sha256": "5113ad9bc0d738dfa24cd5b84874ee6c1afade87580bed6c93354dac9143a95e" }, "downloads": -1, "filename": "nifty_nesting-0.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "542f443fb362be4d7b47b20c7fae863d", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9715, "upload_time": "2018-12-18T07:30:30", "url": "https://files.pythonhosted.org/packages/db/8f/7cf9a7c6a5daa9f7df065957254a9d5347ce8f80082920dafe67eea62b00/nifty_nesting-0.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3e0cae48bd3ef74bac5f18052af286ff", "sha256": "b3a8694c2d5c56b6f81149da0bda030502120d9e1a70fe027f8b0869af6da3b9" }, "downloads": -1, "filename": "nifty-nesting-0.1.1.tar.gz", "has_sig": false, "md5_digest": "3e0cae48bd3ef74bac5f18052af286ff", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7828, "upload_time": "2018-12-18T07:30:32", "url": "https://files.pythonhosted.org/packages/e8/66/df2c649b94cc6e52484be83ef5f8a20644ed1093ed327a5ef2ae5c291214/nifty-nesting-0.1.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "6f2d66f92161d02bf30d458f6a773436", "sha256": "55cc8b5c89c01a7a1e558f347945873eb99fcee2e7d4bf91b0abc0c504dca17f" }, "downloads": -1, "filename": "nifty_nesting-0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "6f2d66f92161d02bf30d458f6a773436", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9759, "upload_time": "2018-12-18T07:40:07", "url": "https://files.pythonhosted.org/packages/f3/a1/7693fb34f77a94bb01fa5e682f764f26a6916654f2fc5e1f2fc2bf15fd72/nifty_nesting-0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ef8c4b45a16155228b6602c0b894e4c2", "sha256": "234e3463f82d0f589cae02086d8c0295e5d43f118b6a71cacfd68b9e03fe1831" }, "downloads": -1, "filename": "nifty-nesting-0.2.tar.gz", "has_sig": false, "md5_digest": "ef8c4b45a16155228b6602c0b894e4c2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7962, "upload_time": "2018-12-18T07:40:09", "url": "https://files.pythonhosted.org/packages/9a/bd/84601115760def70b587fa4f052258cd190805db72c628391c00440b0fff/nifty-nesting-0.2.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "aa153d9bbb42a59c9e7f2761231e447d", "sha256": "dfa6c6adf46b677808cd61de697b9f066c949754748ddf0221790d396a56386b" }, "downloads": -1, "filename": "nifty_nesting-0.2.1-py3-none-any.whl", "has_sig": false, "md5_digest": "aa153d9bbb42a59c9e7f2761231e447d", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9790, "upload_time": "2018-12-18T07:48:36", "url": "https://files.pythonhosted.org/packages/0b/d2/b97787ce84d177e49fdb7d0478beabf818e0a40ab7bd4e8ff1dc01bca671/nifty_nesting-0.2.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "383f3268674eb0db46935e08d4554d05", "sha256": "cc4abbd61af72313889017c74ff53705f3e52f69fda5b6b2df87633b8bfa8b64" }, "downloads": -1, "filename": "nifty-nesting-0.2.1.tar.gz", "has_sig": false, "md5_digest": "383f3268674eb0db46935e08d4554d05", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7979, "upload_time": "2018-12-18T07:47:27", "url": "https://files.pythonhosted.org/packages/fa/8c/f3c2f3093ece6693131ccbc531b600f56cafcd09fc31e9af936c77022144/nifty-nesting-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "a7adfde1f055ff5b16325af8a62f9f39", "sha256": "f4d5b5b6539c900802bd4fa305500f6ed0fe26f98461a293a8562fe129a778e6" }, "downloads": -1, "filename": "nifty_nesting-0.2.2-py3-none-any.whl", "has_sig": false, "md5_digest": "a7adfde1f055ff5b16325af8a62f9f39", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9792, "upload_time": "2018-12-18T07:49:13", "url": "https://files.pythonhosted.org/packages/70/f3/c96647d3b4218758bd656ff8b73db8027b520a55693a0fd3e30c1493b102/nifty_nesting-0.2.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "933668c5e535e592bb2d947ec422cab9", "sha256": "48977c0310793fa2e46d6409524ab38df7fc4933e49ab165da4f8726dbecad89" }, "downloads": -1, "filename": "nifty-nesting-0.2.2.tar.gz", "has_sig": false, "md5_digest": "933668c5e535e592bb2d947ec422cab9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7976, "upload_time": "2018-12-18T07:49:14", "url": "https://files.pythonhosted.org/packages/69/74/5ebccf81945a083ec497878694b80538fdae59888f371b5e116724e26362/nifty-nesting-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "a8ef448bc2591c68c67ab3b52ac74c51", "sha256": "bed69f433dd1bc6529d525c1e3a7aecd48bee9335fd5080fe46e45830399b4c8" }, "downloads": -1, "filename": "nifty_nesting-0.2.3-py3-none-any.whl", "has_sig": false, "md5_digest": "a8ef448bc2591c68c67ab3b52ac74c51", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9810, "upload_time": "2018-12-18T07:52:22", "url": "https://files.pythonhosted.org/packages/50/c8/faff9ae2eac4c3242768e2c2f14bb82b470ee606e0c865f672ef97c87440/nifty_nesting-0.2.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "87604a084ae05155a8c9198920659b29", "sha256": "225b8f1dd23b7f2fb82af45f5686be71095a514d99e6c1986ab43bb152a35b28" }, "downloads": -1, "filename": "nifty-nesting-0.2.3.tar.gz", "has_sig": false, "md5_digest": "87604a084ae05155a8c9198920659b29", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8079, "upload_time": "2018-12-18T07:52:27", "url": "https://files.pythonhosted.org/packages/9f/34/ee25d7949835ee61651892d745bd7db68423e95942b03627451965c3e647/nifty-nesting-0.2.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "a8ef448bc2591c68c67ab3b52ac74c51", "sha256": "bed69f433dd1bc6529d525c1e3a7aecd48bee9335fd5080fe46e45830399b4c8" }, "downloads": -1, "filename": "nifty_nesting-0.2.3-py3-none-any.whl", "has_sig": false, "md5_digest": "a8ef448bc2591c68c67ab3b52ac74c51", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9810, "upload_time": "2018-12-18T07:52:22", "url": "https://files.pythonhosted.org/packages/50/c8/faff9ae2eac4c3242768e2c2f14bb82b470ee606e0c865f672ef97c87440/nifty_nesting-0.2.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "87604a084ae05155a8c9198920659b29", "sha256": "225b8f1dd23b7f2fb82af45f5686be71095a514d99e6c1986ab43bb152a35b28" }, "downloads": -1, "filename": "nifty-nesting-0.2.3.tar.gz", "has_sig": false, "md5_digest": "87604a084ae05155a8c9198920659b29", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8079, "upload_time": "2018-12-18T07:52:27", "url": "https://files.pythonhosted.org/packages/9f/34/ee25d7949835ee61651892d745bd7db68423e95942b03627451965c3e647/nifty-nesting-0.2.3.tar.gz" } ] }