orderedattrdict
===============
.. image:: https://img.shields.io/travis/sanand0/orderedattrdict.svg
:target: https://travis-ci.org/sanand0/orderedattrdict
.. image:: https://img.shields.io/pypi/v/orderedattrdict.svg
:target: https://pypi.python.org/pypi/orderedattrdict
An ordered dictionary with attribute-style access.
Usage
-----
``AttrDict`` behaves exactly like ``collections.OrderedDict``, but also allows
keys to be accessed as attributes::
>>> from orderedattrdict import AttrDict
>>> conf = AttrDict()
>>> conf['z'] = 1
>>> assert conf.z == 1
>>> conf.y = 2
>>> assert conf['y'] == 2
>>> conf.x = 3
>>> assert conf.keys() == ['z', 'y', 'x']
**NOTE**: If the key clashes with an ``OrderedDict`` attribute or starts with
``__`` (two underscores), you can't access it as an attribute. For example::
>>> a = AttrDict(keys=1)
>>> a.keys
<bound method AttrDict.keys of AttrDict([('keys', 1)])>
>>> a['keys']
1
Load JSON preserving the order of keys::
>>> import json
>>> data = json.load(open('test.json'), object_pairs_hook=AttrDict)
Load YAML preserving the order of keys::
>>> import yaml
>>> from orderedattrdict.yamlutils import AttrDictYAMLLoader
>>> data = yaml.load(open('test.yaml'), Loader=AttrDictYAMLLoader)
Make PyYAML *always* load all dictionaries as ``AttrDict``::
>>> from orderedattrdict.yamlutils import from_yaml
>>> yaml.add_constructor(u'tag:yaml.org,2002:map', from_yaml)
>>> yaml.add_constructor(u'tag:yaml.org,2002:omap', from_yaml)
``json.dump``, ``yaml.dump`` and ``yaml.safe_dump`` convert ``AttrDict`` into
dictionaries, retaining the order::
>>> json.dumps(data)
>>> yaml.dump(data)
CounterAttrDict
---------------
``CounterAttrDict`` provides a Counter with ordered keys and attribute-style
access::
>>> from orderedattrdict import CounterAttrDict
>>> c = CounterAttrDict()
>>> c.x
0
>>> c.elements
<bound method CounterAttrDict.elements of CounterAttrDict()>
>>> c.x += 1
>>> c.y += 2
>>> c.most_common()
[('y', 2), ('x', 1)]
>>> list(c.elements())
['x', 'y', 'y']
>>> c.subtract(y=1)
>>> c
CounterAttrDict([('x', 1), ('y', 1)])
DefaultAttrDict
---------------
``DefaultAttrDict`` provides a defaultdict with ordered keys and attribute-style
access. This can be used with a list factory to collect items::
>>> from orderedattrdict import DefaultDict
>>> d = DefaultAttrDict(list)
>>> d.x.append(10) # Append item without needing to initialise list
>>> d.x.append(20)
>>> sum(d.x)
30
or with a set to collect unique items::
>>> d = DefaultAttrDict(set)
>>> d.x.add(5)
>>> d.x.add(2)
>>> d.x.add(5) # Duplicate item is ignored
>>> sum(d.x)
7
Tree
----
``Tree`` lets you can set attributes in any level of the hierarchy::
>>> node = Tree()
>>> node
Tree()
>>> node.x.y = 1
>>> node
Tree([('x', Tree([('y', 1)]))])
>>> node.x.z = 2
>>> node
Tree([('x', Tree([('y', 1), ('z', 2)]))])
>>> node.y.a.b = 3
>>> node
Tree([('x', Tree([('y', 1), ('z', 2)])), ('y', Tree([('a', Tree([('b', 3)]))]))])
Installation
------------
This is a pure-Python package built for Python 2.7+ and Python 3.0+. To set up::
pip install orderedattrdict
Updating
--------
Test locally::
rm -rf build dist
flake8 .
python setup.py test
Update version in ``setup.py`` and ``Changelog`` below. Then commit. Then::
git tag -a v1.x.x # Annotate with a one-line summary of features
git push --follow-tags
# Ensure that travis builds pass
python setup.py sdist bdist_wheel --universal
twine upload dist/*
Changelog
---------
- ``1.0``: Basic implementation
- ``1.1``: Add utilities to load and save as YAML
- ``1.2``: Allow specific keys to be excluded from attribute access
- ``1.3``: Restore ``<<`` merge tags for YAML
- ``1.4.1``: Add ``CounterAttrDict`` and ``DefaultAttrDict``
- ``1.4.2``: Add Python 3.5 support
- ``1.4.3``: Fix bdist installation issues for Python 2.7
- ``1.5``: Add ``Tree`` data structure
- ``1.6``: ``str()`` prints human-friendly dict
Raw data
{
"_id": null,
"home_page": "https://github.com/sanand0/orderedattrdict",
"name": "orderedattrdict",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "ordereddict ordered map attrdict tree conf config configuration yaml json",
"author": "S Anand",
"author_email": "root.node@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/1f/52/e2d7c5030160f9fa9086300ca4990b69a84f97fc1b70cf5e03b59c31ae2b/orderedattrdict-1.6.0.tar.gz",
"platform": "",
"description": "orderedattrdict\n===============\n\n.. image:: https://img.shields.io/travis/sanand0/orderedattrdict.svg\n :target: https://travis-ci.org/sanand0/orderedattrdict\n\n.. image:: https://img.shields.io/pypi/v/orderedattrdict.svg\n :target: https://pypi.python.org/pypi/orderedattrdict\n\n\nAn ordered dictionary with attribute-style access.\n\nUsage\n-----\n\n``AttrDict`` behaves exactly like ``collections.OrderedDict``, but also allows\nkeys to be accessed as attributes::\n\n >>> from orderedattrdict import AttrDict\n >>> conf = AttrDict()\n >>> conf['z'] = 1\n >>> assert conf.z == 1\n >>> conf.y = 2\n >>> assert conf['y'] == 2\n >>> conf.x = 3\n >>> assert conf.keys() == ['z', 'y', 'x']\n\n**NOTE**: If the key clashes with an ``OrderedDict`` attribute or starts with\n``__`` (two underscores), you can't access it as an attribute. For example::\n\n >>> a = AttrDict(keys=1)\n >>> a.keys\n <bound method AttrDict.keys of AttrDict([('keys', 1)])>\n >>> a['keys']\n 1\n\nLoad JSON preserving the order of keys::\n\n >>> import json\n >>> data = json.load(open('test.json'), object_pairs_hook=AttrDict)\n\nLoad YAML preserving the order of keys::\n\n >>> import yaml\n >>> from orderedattrdict.yamlutils import AttrDictYAMLLoader\n >>> data = yaml.load(open('test.yaml'), Loader=AttrDictYAMLLoader)\n\nMake PyYAML *always* load all dictionaries as ``AttrDict``::\n\n >>> from orderedattrdict.yamlutils import from_yaml\n >>> yaml.add_constructor(u'tag:yaml.org,2002:map', from_yaml)\n >>> yaml.add_constructor(u'tag:yaml.org,2002:omap', from_yaml)\n\n``json.dump``, ``yaml.dump`` and ``yaml.safe_dump`` convert ``AttrDict`` into\ndictionaries, retaining the order::\n\n >>> json.dumps(data)\n >>> yaml.dump(data)\n\nCounterAttrDict\n---------------\n\n``CounterAttrDict`` provides a Counter with ordered keys and attribute-style\naccess::\n\n >>> from orderedattrdict import CounterAttrDict\n >>> c = CounterAttrDict()\n >>> c.x\n 0\n >>> c.elements\n <bound method CounterAttrDict.elements of CounterAttrDict()>\n >>> c.x += 1\n >>> c.y += 2\n >>> c.most_common()\n [('y', 2), ('x', 1)]\n >>> list(c.elements())\n ['x', 'y', 'y']\n >>> c.subtract(y=1)\n >>> c\n CounterAttrDict([('x', 1), ('y', 1)])\n\nDefaultAttrDict\n---------------\n\n``DefaultAttrDict`` provides a defaultdict with ordered keys and attribute-style\naccess. This can be used with a list factory to collect items::\n\n >>> from orderedattrdict import DefaultDict\n >>> d = DefaultAttrDict(list)\n >>> d.x.append(10) # Append item without needing to initialise list\n >>> d.x.append(20)\n >>> sum(d.x)\n 30\n\nor with a set to collect unique items::\n\n >>> d = DefaultAttrDict(set)\n >>> d.x.add(5)\n >>> d.x.add(2)\n >>> d.x.add(5) # Duplicate item is ignored\n >>> sum(d.x)\n 7\n\nTree\n----\n\n``Tree`` lets you can set attributes in any level of the hierarchy::\n\n >>> node = Tree()\n >>> node\n Tree()\n >>> node.x.y = 1\n >>> node\n Tree([('x', Tree([('y', 1)]))])\n >>> node.x.z = 2\n >>> node\n Tree([('x', Tree([('y', 1), ('z', 2)]))])\n >>> node.y.a.b = 3\n >>> node\n Tree([('x', Tree([('y', 1), ('z', 2)])), ('y', Tree([('a', Tree([('b', 3)]))]))])\n\nInstallation\n------------\n\nThis is a pure-Python package built for Python 2.7+ and Python 3.0+. To set up::\n\n pip install orderedattrdict\n\nUpdating\n--------\n\nTest locally::\n\n rm -rf build dist\n flake8 .\n python setup.py test\n\nUpdate version in ``setup.py`` and ``Changelog`` below. Then commit. Then::\n\n git tag -a v1.x.x # Annotate with a one-line summary of features\n git push --follow-tags\n # Ensure that travis builds pass\n python setup.py sdist bdist_wheel --universal\n twine upload dist/*\n\nChangelog\n---------\n\n- ``1.0``: Basic implementation\n- ``1.1``: Add utilities to load and save as YAML\n- ``1.2``: Allow specific keys to be excluded from attribute access\n- ``1.3``: Restore ``<<`` merge tags for YAML\n- ``1.4.1``: Add ``CounterAttrDict`` and ``DefaultAttrDict``\n- ``1.4.2``: Add Python 3.5 support\n- ``1.4.3``: Fix bdist installation issues for Python 2.7\n- ``1.5``: Add ``Tree`` data structure\n- ``1.6``: ``str()`` prints human-friendly dict\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "OrderedDict with attribute-style access",
"version": "1.6.0",
"project_urls": {
"Homepage": "https://github.com/sanand0/orderedattrdict"
},
"split_keywords": [
"ordereddict",
"ordered",
"map",
"attrdict",
"tree",
"conf",
"config",
"configuration",
"yaml",
"json"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f329b9fc8515d60bf1bb13e206c2bce356abd8e98a01128f9200387747c4996b",
"md5": "860b717c727de76a0b96166bb243bf2a",
"sha256": "3651524e352ceff85f45bc16eeb917123c06e16f65ca501f79de5bdd7f269bf3"
},
"downloads": -1,
"filename": "orderedattrdict-1.6.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "860b717c727de76a0b96166bb243bf2a",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 8683,
"upload_time": "2020-06-23T11:35:07",
"upload_time_iso_8601": "2020-06-23T11:35:07.189617Z",
"url": "https://files.pythonhosted.org/packages/f3/29/b9fc8515d60bf1bb13e206c2bce356abd8e98a01128f9200387747c4996b/orderedattrdict-1.6.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1f52e2d7c5030160f9fa9086300ca4990b69a84f97fc1b70cf5e03b59c31ae2b",
"md5": "e1de2420b9b9821355f82c4999baf7c4",
"sha256": "c7c4df0f1dadeec11b0e15e132afb91206b2d1047c9b61bcfa720a5ce2565cd9"
},
"downloads": -1,
"filename": "orderedattrdict-1.6.0.tar.gz",
"has_sig": false,
"md5_digest": "e1de2420b9b9821355f82c4999baf7c4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7187,
"upload_time": "2020-06-23T11:35:09",
"upload_time_iso_8601": "2020-06-23T11:35:09.658784Z",
"url": "https://files.pythonhosted.org/packages/1f/52/e2d7c5030160f9fa9086300ca4990b69a84f97fc1b70cf5e03b59c31ae2b/orderedattrdict-1.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-06-23 11:35:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sanand0",
"github_project": "orderedattrdict",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "orderedattrdict"
}