{ "info": { "author": "Ian Lin", "author_email": "you@example.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Utilities" ], "description": "flatten-dict\n============\n.. image:: https://img.shields.io/travis/ianlini/flatten-dict/master.svg\n :target: https://travis-ci.org/ianlini/flatten-dict\n.. image:: https://img.shields.io/pypi/v/flatten-dict.svg\n :target: https://pypi.python.org/pypi/flatten-dict\n.. image:: https://img.shields.io/pypi/l/flatten-dict.svg\n :target: https://pypi.python.org/pypi/flatten-dict\n.. image:: https://img.shields.io/github/stars/ianlini/flatten-dict.svg?style=social\n :target: https://github.com/ianlini/flatten-dict\n\nA flexible utility for flattening and unflattening dict-like objects in Python.\n\n\nIntroduction\n------------\nThis Python package provide a function ``flatten()`` for flattening dict-like objects.\nIt also provides some key joining methods (reducer), and you can choose the reducer you want or even implement your own reducer. You can also choose to invert the resulting flat dict.\n\nDocumentation\n-------------\n\nFlatten\n```````\n\n.. code-block:: python\n\n def flatten(d, reducer='tuple', inverse=False, enumerate_types=(), keep_empty_types=()):\n \"\"\"Flatten `Mapping` object.\n\n Parameters\n ----------\n d : dict-like object\n The dict that will be flattened.\n reducer : {'tuple', 'path', 'underscore', Callable}\n The key joining method. If a `Callable` is given, the `Callable` will be\n used to reduce.\n 'tuple': The resulting key will be tuple of the original keys.\n 'path': Use `os.path.join` to join keys.\n 'underscore': Use underscores to join keys.\n inverse : bool\n Whether you want invert the resulting key and value.\n enumerate_types : Sequence[type]\n Flatten these types using `enumerate`.\n For example, if we set `enumerate_types` to ``(list,)``,\n `list` indices become keys: ``{'a': ['b', 'c']}`` -> ``{('a', 0): 'b', ('a', 1): 'c'}``.\n keep_empty_types : Sequence[type]\n By default, ``flatten({1: 2, 3: {}})`` will give you ``{(1,): 2}``, that is, the key ``3``\n will disappear.\n This is also applied for the types in `enumerate_types`, that is,\n ``flatten({1: 2, 3: []}, enumerate_types=(list,))`` will give you ``{(1,): 2}``.\n If you want to keep those empty values, you can specify the types in `keep_empty_types`:\n\n >>> flatten({1: 2, 3: {}}, keep_empty_types=(dict,))\n {(1,): 2, (3,): {}}\n\n Returns\n -------\n flat_dict : dict\n \"\"\"\n\nExamples\n::::::::\n\n.. code-block:: python\n\n In [1]: from flatten_dict import flatten\n\n In [2]: normal_dict = {\n ...: 'a': '0',\n ...: 'b': {\n ...: 'a': '1.0',\n ...: 'b': '1.1',\n ...: },\n ...: 'c': {\n ...: 'a': '2.0',\n ...: 'b': {\n ...: 'a': '2.1.0',\n ...: 'b': '2.1.1',\n ...: },\n ...: },\n ...: }\n\n In [3]: flatten(normal_dict)\n Out[3]:\n {('a',): '0',\n ('b', 'a'): '1.0',\n ('b', 'b'): '1.1',\n ('c', 'a'): '2.0',\n ('c', 'b', 'a'): '2.1.0',\n ('c', 'b', 'b'): '2.1.1'}\n\n In [4]: flatten(normal_dict, reducer='path')\n Out[4]:\n {'a': '0',\n 'b/a': '1.0',\n 'b/b': '1.1',\n 'c/a': '2.0',\n 'c/b/a': '2.1.0',\n 'c/b/b': '2.1.1'}\n\n In [5]: flatten(normal_dict, reducer='path', inverse=True)\n Out[5]:\n {'0': 'a',\n '1.0': 'b/a',\n '1.1': 'b/b',\n '2.0': 'c/a',\n '2.1.0': 'c/b/a',\n '2.1.1': 'c/b/b'}\n\n In [6]: def underscore_reducer(k1, k2):\n ...: if k1 is None:\n ...: return k2\n ...: else:\n ...: return k1 + \"_\" + k2\n ...:\n\n In [7]: flatten(normal_dict, reducer=underscore_reducer)\n Out[7]:\n {'a': '0',\n 'b_a': '1.0',\n 'b_b': '1.1',\n 'c_a': '2.0',\n 'c_b_a': '2.1.0',\n 'c_b_b': '2.1.1'}\n\nIf we have some iterable (e.g., `list`) in the `dict`, we will normally get this:\n\n.. code-block:: python\n\n In [8]: flatten({'a': [1, 2, 3], 'b': 'c'})\n Out[8]:\n {('a',): [1, 2, 3],\n ('b',): 'c'}\n\nIf we want to use its indices as keys, then we can use the parameter `enumerate_types`:\n\n.. code-block:: python\n\n In [9]: flatten({'a': [1, 2, 3], 'b': 'c'}, enumerate_types=(list,))\n Out[9]:\n {('a', 0): 1,\n ('a', 1): 2,\n ('a', 2): 3,\n ('b',): 'c'}\n\nWe can even flatten a `list` directly:\n\n.. code-block:: python\n\n In [10]: flatten([1, 2, 3], enumerate_types=(list,))\n Out[10]:\n {(0,): 1,\n (1,): 2,\n (2,): 3}\n\nIf there is an empty dict in the values, by default, it will disappear after flattened:\n\n.. code-block:: python\n\n In [4]: flatten({1: 2, 3: {}})\n Out[4]: {(1,): 2}\n\nWe can keep the empty dict in the result using ``keep_empty_types=(dict,)``:\n\n.. code-block:: python\n\n In [5]: flatten({1: 2, 3: {}}, keep_empty_types=(dict,))\n Out[5]: {(1,): 2, (3,): {}}\n\nUnflatten\n`````````\n\n.. code-block:: python\n\n def unflatten(d, splitter='tuple', inverse=False):\n \"\"\"Unflatten dict-like object.\n\n Parameters\n ----------\n d : dict-like object\n The dict that will be unflattened.\n splitter : {'tuple', 'path', 'underscore', Callable}\n The key splitting method. If a Callable is given, the Callable will be\n used to split.\n 'tuple': Use each element in the tuple key as the key of the unflattened dict.\n 'path': Use `pathlib.Path.parts` to split keys.\n 'underscore': Use underscores to split keys.\n inverse : bool\n Whether you want to invert the key and value before flattening.\n\n Returns\n -------\n unflattened_dict : dict\n \"\"\"\n\nExamples\n::::::::\n\n.. code-block:: python\n\n In [1]: from flatten_dict import unflatten\n\n In [2]: flat_dict = {\n ...: ('a',): '0',\n ...: ('b', 'a'): '1.0',\n ...: ('b', 'b'): '1.1',\n ...: ('c', 'a'): '2.0',\n ...: ('c', 'b', 'a'): '2.1.0',\n ...: ('c', 'b', 'b'): '2.1.1',\n ...: }\n\n In [3]: unflatten(flat_dict)\n Out[3]:\n {'a': '0',\n 'b': {'a': '1.0', 'b': '1.1'},\n 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}\n\n In [4]: flat_dict = {\n ...: 'a': '0',\n ...: 'b/a': '1.0',\n ...: 'b/b': '1.1',\n ...: 'c/a': '2.0',\n ...: 'c/b/a': '2.1.0',\n ...: 'c/b/b': '2.1.1',\n ...: }\n\n In [5]: unflatten(flat_dict, splitter='path')\n Out[5]:\n {'a': '0',\n 'b': {'a': '1.0', 'b': '1.1'},\n 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}\n\n In [6]: flat_dict = {\n ...: '0': 'a',\n ...: '1.0': 'b/a',\n ...: '1.1': 'b/b',\n ...: '2.0': 'c/a',\n ...: '2.1.0': 'c/b/a',\n ...: '2.1.1': 'c/b/b',\n ...: }\n\n In [7]: unflatten(flat_dict, splitter='path', inverse=True)\n Out[7]:\n {'a': '0',\n 'b': {'a': '1.0', 'b': '1.1'},\n 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}\n\n In [8]: def underscore_splitter(flat_key):\n ...: return flat_key.split(\"_\")\n ...:\n\n In [9]: flat_dict = {\n ...: 'a': '0',\n ...: 'b_a': '1.0',\n ...: 'b_b': '1.1',\n ...: 'c_a': '2.0',\n ...: 'c_b_a': '2.1.0',\n ...: 'c_b_b': '2.1.1',\n ...: }\n\n In [10]: unflatten(flat_dict, splitter=underscore_splitter)\n Out[10]:\n {'a': '0',\n 'b': {'a': '1.0', 'b': '1.1'},\n 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}\n", "description_content_type": "text/x-rst", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/ianlini/flatten-dict", "keywords": "", "license": "MIT", "maintainer": "Ian Lin", "maintainer_email": "you@example.com", "name": "flatten-dict", "package_url": "https://pypi.org/project/flatten-dict/", "platform": "", "project_url": "https://pypi.org/project/flatten-dict/", "project_urls": { "Homepage": "https://github.com/ianlini/flatten-dict", "Repository": "https://github.com/ianlini/flatten-dict" }, "release_url": "https://pypi.org/project/flatten-dict/0.2.0/", "requires_dist": [ "six (>=1.12,<2.0)", "pathlib2 (>=2.3,<3.0)" ], "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "summary": "A flexible utility for flattening and unflattening dict-like objects in Python.", "version": "0.2.0" }, "last_serial": 5902064, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "3317be28adf42bcde486d307e1fc00ac", "sha256": "72201d09e27bfa6b95b6216ab0024ae6f9c3e6c81346920f43eb9f3811a68113" }, "downloads": -1, "filename": "flatten_dict-0.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "3317be28adf42bcde486d307e1fc00ac", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 3985, "upload_time": "2016-12-15T09:34:41", "url": "https://files.pythonhosted.org/packages/96/50/981a4011b34dbe60736859686416e2feabbfe82cfad58b839b9f2ffb8f55/flatten_dict-0.0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "5c91bc50bc263d72ad559feecc61f0a1", "sha256": "a02e3e643169933756c925339e2e93d781bd09fa0347e5b980fc759319fb9364" }, "downloads": -1, "filename": "flatten-dict-0.0.1.tar.gz", "has_sig": false, "md5_digest": "5c91bc50bc263d72ad559feecc61f0a1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2267, "upload_time": "2016-12-15T09:34:43", "url": "https://files.pythonhosted.org/packages/e6/aa/4a5762c82d98cbe7019973ca5c203a521c8533fb364597b9e78b47b031c9/flatten-dict-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "54f5fa5efc4ee02dfcee56111f8fc30a", "sha256": "96cf46d95a74b8075ec7ce2458c2c5cf49278ebf65eb46df9ccf1a86ccb63bcd" }, "downloads": -1, "filename": "flatten_dict-0.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "54f5fa5efc4ee02dfcee56111f8fc30a", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 5819, "upload_time": "2016-12-16T09:28:57", "url": "https://files.pythonhosted.org/packages/4b/08/900dde81affc3aad076232ec2f061c7027ed63f333b96d88a34a531a7a5b/flatten_dict-0.0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ded5fac1c6e29aae06be5a92e785c05c", "sha256": "fe417b12c7003a64b66395ea758271202e6796a4d034f9431cdeb2e6fcfc2896" }, "downloads": -1, "filename": "flatten-dict-0.0.2.tar.gz", "has_sig": false, "md5_digest": "ded5fac1c6e29aae06be5a92e785c05c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3393, "upload_time": "2016-12-16T09:28:59", "url": "https://files.pythonhosted.org/packages/23/fc/31505c42ebb7cb0af467e4e821019f61631e10ae6a882f6e71c65f12879c/flatten-dict-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "83f983412c415e366d6fada37f8bb54b", "sha256": "62e14d3526a2756af5b1845d683d08a6556dec5664dbf5ed6fb20566d697dfe8" }, "downloads": -1, "filename": "flatten_dict-0.0.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "83f983412c415e366d6fada37f8bb54b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 4744, "upload_time": "2018-05-25T16:03:50", "url": "https://files.pythonhosted.org/packages/7a/f3/7311f421e9d9906aa8b2c43d3d3869abf1269ee3b7a98864051d42014da5/flatten_dict-0.0.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b846833ec05bbea2357f0bb39c0b4311", "sha256": "35fa7164b1947cf5d6450dd825e7ba87ccd202251dc2429dce831a81b742e720" }, "downloads": -1, "filename": "flatten-dict-0.0.3.tar.gz", "has_sig": false, "md5_digest": "b846833ec05bbea2357f0bb39c0b4311", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4028, "upload_time": "2018-05-25T16:03:52", "url": "https://files.pythonhosted.org/packages/9a/5a/bc3a3331e31f1fb95f247a1204f0ba2741db1737afeea0e4e01c8f871aff/flatten-dict-0.0.3.tar.gz" } ], "0.0.3.post1": [ { "comment_text": "", "digests": { "md5": "ed42dacca488d7df6a8668c6b426c145", "sha256": "2ee6b3fa263e6404cf2dc3827d9ad11c863268b4f02f80bb6edfc2241cd95ab9" }, "downloads": -1, "filename": "flatten_dict-0.0.3.post1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "ed42dacca488d7df6a8668c6b426c145", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 5174, "upload_time": "2018-06-04T15:54:51", "url": "https://files.pythonhosted.org/packages/7f/c2/a3427d69137de28e3c83bcf0244925542dae7402e26c574d48b304d1f460/flatten_dict-0.0.3.post1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e8a5066e3376b742a4ee3587f251fafa", "sha256": "e3696310519c6d16209288c1c04d8249447b8ea4de83d3f08d6b9bbdc3981f71" }, "downloads": -1, "filename": "flatten-dict-0.0.3.post1.tar.gz", "has_sig": false, "md5_digest": "e8a5066e3376b742a4ee3587f251fafa", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4617, "upload_time": "2018-06-04T15:54:53", "url": "https://files.pythonhosted.org/packages/08/68/eaeb54b9d707701d289698b30753cd1e08aec43ac384beefa91dccdcfe34/flatten-dict-0.0.3.post1.tar.gz" } ], "0.1.0": [ { "comment_text": "", "digests": { "md5": "bff747a48658765415350071c675188b", "sha256": "f4f4901e93c253842afb400ddc5e0089058d69b5d6eb6fc4e65277ee4cd4e67a" }, "downloads": -1, "filename": "flatten_dict-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "bff747a48658765415350071c675188b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6759, "upload_time": "2019-05-30T15:20:05", "url": "https://files.pythonhosted.org/packages/bf/b8/23fbe4b4b85702fd9f4b55f342f3b59dfe26a554a4350fa79f527d0fd7c6/flatten_dict-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "f02556376fe04fb5c74f9166ded6e7c1", "sha256": "82944200b8c257d36778691045c53a3cf6925a069a1e8e0890b5e5f6da0e50de" }, "downloads": -1, "filename": "flatten-dict-0.1.0.tar.gz", "has_sig": false, "md5_digest": "f02556376fe04fb5c74f9166ded6e7c1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5290, "upload_time": "2019-05-30T15:20:07", "url": "https://files.pythonhosted.org/packages/e9/36/1795be8d8133be7a0da14c7cd7adfa6b8a8b8b584fc99e22a142d78fdb6b/flatten-dict-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "2e7763d96fb0a518c5032dcbad225e30", "sha256": "96c1ed23f05128478e30435bb54cc0a0d7d2bc638235d9ef6ca4ca221d924b3f" }, "downloads": -1, "filename": "flatten_dict-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "2e7763d96fb0a518c5032dcbad225e30", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7523, "upload_time": "2019-09-29T08:05:48", "url": "https://files.pythonhosted.org/packages/86/e5/f0e60863e6175ff631261183df6e1ee29509dafe860ae878b52879820dcd/flatten_dict-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "901bc2576e7b43790f5e6b8b9d9ae0f6", "sha256": "077b1540233dd67094881feb58dc353db9f1fedf5faeb51e334fe0cea4fb2b89" }, "downloads": -1, "filename": "flatten-dict-0.2.0.tar.gz", "has_sig": false, "md5_digest": "901bc2576e7b43790f5e6b8b9d9ae0f6", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7990, "upload_time": "2019-09-29T08:05:46", "url": "https://files.pythonhosted.org/packages/ed/e3/ef83c1a44ed70cc0d3b8b7e98b20b7628162999c20e8a928fea473f4e51a/flatten-dict-0.2.0.tar.gz" } ], "0.2.0.dev0": [ { "comment_text": "", "digests": { "md5": "f21b0024ff22d2b81082790f26bec5ba", "sha256": "c9d92e43524f0b19d2086008a7b98ebdf1855eb0524158f413490ce555cacef4" }, "downloads": -1, "filename": "flatten_dict-0.2.0.dev0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f21b0024ff22d2b81082790f26bec5ba", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 6937, "upload_time": "2019-09-29T04:05:20", "url": "https://files.pythonhosted.org/packages/be/4f/c67a8c022a4db64a66eab162593ef7522bf65ca49e0e6416ef17b62d5d8b/flatten_dict-0.2.0.dev0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "7346aeb857837b8cedda6371cf8cf813", "sha256": "0b11b5bac8ba0bc82a890013bbde91e5cc75682a61adba836a08091dfcfdbcac" }, "downloads": -1, "filename": "flatten-dict-0.2.0.dev0.tar.gz", "has_sig": false, "md5_digest": "7346aeb857837b8cedda6371cf8cf813", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7025, "upload_time": "2019-09-29T04:05:17", "url": "https://files.pythonhosted.org/packages/38/a5/341d4231da572c7f94aac793cc74f5bbdae1bae06b7c3aab147f8001b311/flatten-dict-0.2.0.dev0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2e7763d96fb0a518c5032dcbad225e30", "sha256": "96c1ed23f05128478e30435bb54cc0a0d7d2bc638235d9ef6ca4ca221d924b3f" }, "downloads": -1, "filename": "flatten_dict-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "2e7763d96fb0a518c5032dcbad225e30", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7523, "upload_time": "2019-09-29T08:05:48", "url": "https://files.pythonhosted.org/packages/86/e5/f0e60863e6175ff631261183df6e1ee29509dafe860ae878b52879820dcd/flatten_dict-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "901bc2576e7b43790f5e6b8b9d9ae0f6", "sha256": "077b1540233dd67094881feb58dc353db9f1fedf5faeb51e334fe0cea4fb2b89" }, "downloads": -1, "filename": "flatten-dict-0.2.0.tar.gz", "has_sig": false, "md5_digest": "901bc2576e7b43790f5e6b8b9d9ae0f6", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7990, "upload_time": "2019-09-29T08:05:46", "url": "https://files.pythonhosted.org/packages/ed/e3/ef83c1a44ed70cc0d3b8b7e98b20b7628162999c20e8a928fea473f4e51a/flatten-dict-0.2.0.tar.gz" } ] }