.. note ::
* Source code at https://github.com/bunbun/nested-dict
* Documentation at http://nested-dict.readthedocs.org
##############################################################################
``nested_dict``
##############################################################################
``nested_dict`` extends ``defaultdict`` to support python ``dict`` with multiple levels of nested-ness:
*****************************************************************
Drop in replacement for ``dict``
*****************************************************************
.. <<Python
.. code-block:: Pycon
>>> from nested_dict import nested_dict
>>> nd= nested_dict()
>>> nd["one"] = "1"
>>> nd[1]["two"] = "1 / 2"
>>> nd["uno"][2]["three"] = "1 / 2 / 3"
>>>
... for keys_as_tuple, value in nd.items_flat():
... print ("%-20s == %r" % (keys_as_tuple, value))
...
('one',) == '1'
(1, 'two') == '1 / 2'
('uno', 2, 'three') == '1 / 2 / 3'
..
Python
Each nested level is created magically when accessed, a process known as "auto-vivification" in perl.
******************************************************************************
Specifying the contained type
******************************************************************************
If you want the nested dictionary to hold
* a collection (like the `set <https://docs.python.org/2/library/sets.html>`__ in the first example) or
* a scalar with useful default values such as ``int`` or ``str``.
==============================
*dict* of ``list``\ s
==============================
.. <<Python
.. code-block:: Python
# nested dict of lists
nd = nested_dict(2, list)
nd["mouse"]["2"].append(12)
nd["human"]["1"].append(12)
..
Python
==============================
*dict* of ``set``\ s
==============================
.. <<Python
.. code-block:: Python
# nested dict of sets
nd = nested_dict(2, set)
nd["mouse"]["2"].add("a")
nd["human"]["1"].add("b")
..
Python
==============================
*dict* of ``int``\ s
==============================
.. <<Python
.. code-block:: Python
# nested dict of ints
nd = nested_dict(2, int)
nd["mouse"]["2"] += 4
nd["human"]["1"] += 5
nd["human"]["1"] += 6
nd.to_dict()
#{'human': {'1': 11}, 'mouse': {'2': 4}}
..
Python
==============================
*dict* of ``str``\ s
==============================
.. <<Python
.. code-block:: Python
# nested dict of strings
nd = nested_dict(2, str)
nd["mouse"]["2"] += "a" * 4
nd["human"]["1"] += "b" * 5
nd["human"]["1"] += "c" * 6
nd.to_dict()
#{'human': {'1': 'bbbbbcccccc'}, 'mouse': {'2': 'aaaa'}}
..
Python
##############################################################################
Iterating through ``nested_dict``
##############################################################################
Iterating through deep or unevenly nested dictionaries is a bit of a pain without recursion.
``nested dict`` allows you to **flatten** the nested levels into `tuple <https://docs.python.org/2/library/functions.html#tuple>`__\ s before iteration.
You do not need to know beforehand how many levels of nesting you have:
.. <<Python
.. code-block:: Python
from nested_dict import nested_dict
nd= nested_dict()
nd["one"] = "1"
nd[1]["two"] = "1 / 2"
nd["uno"][2]["three"] = "1 / 2 / 3"
for keys_as_tuple, value in nd.items_flat():
print ("%-20s == %r" % (keys_as_tuple, value))
# (1, 'two') == '1 / 2'
# ('one',) == '1'
# ('uno', 2, 'three') == '1 / 2 / 3'
..
Python
nested_dict provides
* ``items_flat()``
* ``keys_flat()``
* ``values_flat()``
(``iteritems_flat()``, ``iterkeys_flat()``, and ``itervalues_flat()`` are python 2.7-style synonyms. )
##############################################################################
Converting to / from dictionaries
##############################################################################
The magic of ``nested_dict`` sometimes gets in the way (of `pickle <https://docs.python.org/2/library/pickle.html>`__\ ing for example).
We can convert to and from a vanilla python ``dict`` using
* ``nested_dict.to_dict()``
* ``nested_dict constructor``
.. <<Python
.. code-block:: Pycon
>>> from nested_dict import nested_dict
>>> nd= nested_dict()
>>> nd["one"] = 1
>>> nd[1]["two"] = "1 / 2"
#
# convert nested_dict -> dict and pickle
#
>>> nd.to_dict()
{1: {'two': '1 / 2'}, 'one': 1}
>>> import pickle
>>> binary_representation = pickle.dumps(nd.to_dict())
#
# convert dict -> nested_dict
#
>>> normal_dict = pickle.loads(binary_representation)
>>> new_nd = nested_dict(normal_dict)
>>> nd == new_nd
True
..
Python
##############################################################################
``defaultdict``
##############################################################################
``nested_dict`` extends `collections.defaultdict <https://docs.python.org/2/library/collections.html#collections.defaultdict>`__
You can get arbitrarily-nested "auto-vivifying" dictionaries using `defaultdict <https://docs.python.org/2/library/collections.html#collections.defaultdict>`__.
.. <<Python
.. code-block:: Python
from collections import defaultdict
nested_dict = lambda: defaultdict(nested_dict)
nd = nested_dict()
nd[1][2]["three"][4] = 5
nd["one"]["two"]["three"][4] = 5
..
Python
However, only ``nested_dict`` supports a ``dict`` of ``dict`` of ``sets`` etc.
Raw data
{
"_id": null,
"home_page": "http://pypi.python.org/pypi/nested_dict",
"name": "nested_dict",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "nested,dict,defaultdict,dictionary,auto-vivification",
"author": "Leo Goodstadt",
"author_email": "nested_dict@llew.org.uk",
"download_url": "https://files.pythonhosted.org/packages/42/d0/3b27fa65b16a2e44d793af59929fcdb3bb84b4664462ff2830105dfd9b7d/nested_dict-1.61.tar.gz",
"platform": "UNKNOWN",
"description": ".. note ::\n\n * Source code at https://github.com/bunbun/nested-dict\n * Documentation at http://nested-dict.readthedocs.org\n\n##############################################################################\n``nested_dict``\n##############################################################################\n``nested_dict`` extends ``defaultdict`` to support python ``dict`` with multiple levels of nested-ness:\n\n*****************************************************************\nDrop in replacement for ``dict``\n*****************************************************************\n\n\n .. <<Python\n\n .. code-block:: Pycon\n\n >>> from nested_dict import nested_dict\n >>> nd= nested_dict()\n >>> nd[\"one\"] = \"1\"\n >>> nd[1][\"two\"] = \"1 / 2\"\n >>> nd[\"uno\"][2][\"three\"] = \"1 / 2 / 3\"\n >>>\n ... for keys_as_tuple, value in nd.items_flat():\n ... print (\"%-20s == %r\" % (keys_as_tuple, value))\n ...\n ('one',) == '1'\n (1, 'two') == '1 / 2'\n ('uno', 2, 'three') == '1 / 2 / 3'\n\n ..\n Python\n\n Each nested level is created magically when accessed, a process known as \"auto-vivification\" in perl.\n\n\n******************************************************************************\nSpecifying the contained type\n******************************************************************************\n\n If you want the nested dictionary to hold\n * a collection (like the `set <https://docs.python.org/2/library/sets.html>`__ in the first example) or\n * a scalar with useful default values such as ``int`` or ``str``.\n\n==============================\n*dict* of ``list``\\ s\n==============================\n .. <<Python\n\n .. code-block:: Python\n\n # nested dict of lists\n nd = nested_dict(2, list)\n nd[\"mouse\"][\"2\"].append(12)\n nd[\"human\"][\"1\"].append(12)\n\n\n ..\n Python\n\n==============================\n*dict* of ``set``\\ s\n==============================\n .. <<Python\n\n .. code-block:: Python\n\n # nested dict of sets\n nd = nested_dict(2, set)\n nd[\"mouse\"][\"2\"].add(\"a\")\n nd[\"human\"][\"1\"].add(\"b\")\n\n\n ..\n Python\n\n==============================\n*dict* of ``int``\\ s\n==============================\n\n .. <<Python\n\n .. code-block:: Python\n\n # nested dict of ints\n nd = nested_dict(2, int)\n nd[\"mouse\"][\"2\"] += 4\n nd[\"human\"][\"1\"] += 5\n nd[\"human\"][\"1\"] += 6\n\n nd.to_dict()\n #{'human': {'1': 11}, 'mouse': {'2': 4}}\n\n\n ..\n Python\n\n==============================\n*dict* of ``str``\\ s\n==============================\n\n .. <<Python\n\n .. code-block:: Python\n\n # nested dict of strings\n nd = nested_dict(2, str)\n nd[\"mouse\"][\"2\"] += \"a\" * 4\n nd[\"human\"][\"1\"] += \"b\" * 5\n nd[\"human\"][\"1\"] += \"c\" * 6\n\n nd.to_dict()\n #{'human': {'1': 'bbbbbcccccc'}, 'mouse': {'2': 'aaaa'}}\n\n ..\n Python\n\n##############################################################################\nIterating through ``nested_dict``\n##############################################################################\n\nIterating through deep or unevenly nested dictionaries is a bit of a pain without recursion.\n``nested dict`` allows you to **flatten** the nested levels into `tuple <https://docs.python.org/2/library/functions.html#tuple>`__\\ s before iteration.\n\nYou do not need to know beforehand how many levels of nesting you have:\n\n .. <<Python\n\n .. code-block:: Python\n\n from nested_dict import nested_dict\n nd= nested_dict()\n nd[\"one\"] = \"1\"\n nd[1][\"two\"] = \"1 / 2\"\n nd[\"uno\"][2][\"three\"] = \"1 / 2 / 3\"\n\n for keys_as_tuple, value in nd.items_flat():\n print (\"%-20s == %r\" % (keys_as_tuple, value))\n\n # (1, 'two') == '1 / 2'\n # ('one',) == '1'\n # ('uno', 2, 'three') == '1 / 2 / 3'\n\n ..\n Python\n\n\n\nnested_dict provides\n * ``items_flat()``\n * ``keys_flat()``\n * ``values_flat()``\n\n(``iteritems_flat()``, ``iterkeys_flat()``, and ``itervalues_flat()`` are python 2.7-style synonyms. )\n\n##############################################################################\nConverting to / from dictionaries\n##############################################################################\n\nThe magic of ``nested_dict`` sometimes gets in the way (of `pickle <https://docs.python.org/2/library/pickle.html>`__\\ ing for example).\n\nWe can convert to and from a vanilla python ``dict`` using\n * ``nested_dict.to_dict()``\n * ``nested_dict constructor``\n\n .. <<Python\n\n .. code-block:: Pycon\n\n >>> from nested_dict import nested_dict\n >>> nd= nested_dict()\n >>> nd[\"one\"] = 1\n >>> nd[1][\"two\"] = \"1 / 2\"\n\n #\n # convert nested_dict -> dict and pickle\n #\n >>> nd.to_dict()\n {1: {'two': '1 / 2'}, 'one': 1}\n >>> import pickle\n >>> binary_representation = pickle.dumps(nd.to_dict())\n\n #\n # convert dict -> nested_dict\n #\n >>> normal_dict = pickle.loads(binary_representation)\n >>> new_nd = nested_dict(normal_dict)\n >>> nd == new_nd\n True\n\n ..\n Python\n\n\n##############################################################################\n``defaultdict``\n##############################################################################\n``nested_dict`` extends `collections.defaultdict <https://docs.python.org/2/library/collections.html#collections.defaultdict>`__\n\nYou can get arbitrarily-nested \"auto-vivifying\" dictionaries using `defaultdict <https://docs.python.org/2/library/collections.html#collections.defaultdict>`__.\n\n .. <<Python\n\n .. code-block:: Python\n\n from collections import defaultdict\n nested_dict = lambda: defaultdict(nested_dict)\n nd = nested_dict()\n nd[1][2][\"three\"][4] = 5\n nd[\"one\"][\"two\"][\"three\"][4] = 5\n\n ..\n Python\n\nHowever, only ``nested_dict`` supports a ``dict`` of ``dict`` of ``sets`` etc.",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python dictionary with automatic and arbitrary levels of nestedness",
"version": "1.61",
"split_keywords": [
"nested",
"dict",
"defaultdict",
"dictionary",
"auto-vivification"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "e08e2e8de6d002f9ddda4eea5a4528c2",
"sha256": "de0fb5bac82ba7bcc23736f09373f18628ea57f92bbaa13480d23f261c41e771"
},
"downloads": -1,
"filename": "nested_dict-1.61.tar.gz",
"has_sig": false,
"md5_digest": "e08e2e8de6d002f9ddda4eea5a4528c2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12137,
"upload_time": "2015-06-17T18:06:09",
"upload_time_iso_8601": "2015-06-17T18:06:09.570546Z",
"url": "https://files.pythonhosted.org/packages/42/d0/3b27fa65b16a2e44d793af59929fcdb3bb84b4664462ff2830105dfd9b7d/nested_dict-1.61.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2015-06-17 18:06:09",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "nested_dict"
}