=================================
(Yet Another) Auto-Attribute Dict
=================================
An ``aadict`` is a python dict sub-class that allows attribute-style
access to dict items, e.g. ``d.foo`` is equivalent to ``d['foo']``.
``aadict`` also provides a few other helpful methods, such as ``pick``
and ``omit`` methods. Also, an ``aadict`` is more call chaining
friendly (e.g. methods such as `update` return ``self``) and is
pickle'able.
Project
=======
* Homepage: https://github.com/metagriffin/aadict
* Bugs: https://github.com/metagriffin/aadict/issues
TL;DR
=====
Install:
.. code-block:: bash
$ pip install aadict
Use:
.. code-block:: python
from aadict import aadict
# attribute access
d = aadict(foo='bar', zig=87)
assert d.foo == d['foo'] == 'bar'
# helper methods
assert d.pick('foo') == {'foo': 'bar'}
assert d.omit('foo') == {'zig': 87}
# method chaining
d2 = aadict(x='y').update(d).omit('zig')
assert d2.x == 'y' and d2.foo == 'bar' and d2.zig is None
# converting a dict to an aadict recursively
d3 = aadict.d2ar(dict(foo=dict(bar='zig')))
assert d3.foo.bar == 'zig'
Details
=======
The aadict module provides the following functionality:
aadict
------
An `aadict` object is basically identical to a `dict` object, with the
exception that attributes, if not reserved for other purposes, map to
the dict's items. For example, if a dict ``d`` has an item ``'foo'``,
then a request for ``d.foo`` will return that item lookup. aadicts
also have several helper methods, for example ``aadict.pick``. To
fetch the value of an item that has the same name as one of the helper
methods you need to reference it by item lookup,
i.e. ``d['pick']``. The helper methods are:
* **aadict.pick** instance method:
Returns a new aadict, reduced to only include the specified
keys. Example:
.. code-block:: python
d = aadict(foo='bar', zig=87, zag=['a', 'b'])
assert d.pick('foo', 'zag') == {'foo': 'bar', 'zag': ['a', 'b']}
* **aadict.omit** instance method:
Identical to the ``aadict.pick`` method, but returns the complement,
i.e. all of those keys that are *not* specified. Example:
.. code-block:: python
d = aadict(foo='bar', zig=87, zag=['a', 'b'])
assert d.omit('foo', 'zag') == {'zig': 87}
* **aadict.d2ar** class method:
Recursively converts the supplied `dict` to an `aadict`, including
all sub-list and sub-dict types. Due to being recursive, but only
copying dict-types, this is effectively a hybrid of a shallow and
a deep clone. Example:
.. code-block:: python
d = aadict.d2ar(dict(foo=dict(bar='zig')))
assert d.foo.bar == 'zig'
Without the recursive walking, the ``.bar`` attribute syntax
would yield an AttributeError exception because d.foo would
reference a `dict` type, not an `aadict`.
* **aadict.d2a** class method:
Converts the supplied `dict` to an `aadict`. Example:
.. code-block:: python
d = aadict.d2a(dict(foo='bar'))
assert d.foo == d['foo'] == 'bar'
Note that this is identical to just using the constructor,
but is provided as a symmetry to the ``aadict.d2ar`` class
method, e.g.:
.. code-block:: python
d = aadict(dict(foo='bar'))
assert d.foo == d['foo'] == 'bar'
Raw data
{
"_id": null,
"home_page": "http://github.com/metagriffin/aadict",
"name": "aadict",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "auto attribute access dict helpers pick omit",
"author": "metagriffin",
"author_email": "mg.pypi@uberdev.org",
"download_url": "https://files.pythonhosted.org/packages/50/30/2d6c516ff308e1a2e25b8cf4274bd0ed763861b3d2b5cd1fb54c6d59bfcb/aadict-0.2.3.tar.gz",
"platform": "any",
"description": "=================================\n(Yet Another) Auto-Attribute Dict\n=================================\n\nAn ``aadict`` is a python dict sub-class that allows attribute-style\naccess to dict items, e.g. ``d.foo`` is equivalent to ``d['foo']``.\n``aadict`` also provides a few other helpful methods, such as ``pick``\nand ``omit`` methods. Also, an ``aadict`` is more call chaining\nfriendly (e.g. methods such as `update` return ``self``) and is\npickle'able.\n\n\nProject\n=======\n\n* Homepage: https://github.com/metagriffin/aadict\n* Bugs: https://github.com/metagriffin/aadict/issues\n\n\nTL;DR\n=====\n\nInstall:\n\n.. code-block:: bash\n\n $ pip install aadict\n\nUse:\n\n.. code-block:: python\n\n from aadict import aadict\n\n # attribute access\n d = aadict(foo='bar', zig=87)\n assert d.foo == d['foo'] == 'bar'\n\n # helper methods\n assert d.pick('foo') == {'foo': 'bar'}\n assert d.omit('foo') == {'zig': 87}\n\n # method chaining\n d2 = aadict(x='y').update(d).omit('zig')\n assert d2.x == 'y' and d2.foo == 'bar' and d2.zig is None\n\n # converting a dict to an aadict recursively\n d3 = aadict.d2ar(dict(foo=dict(bar='zig')))\n assert d3.foo.bar == 'zig'\n\n\nDetails\n=======\n\nThe aadict module provides the following functionality:\n\n\naadict\n------\n\nAn `aadict` object is basically identical to a `dict` object, with the\nexception that attributes, if not reserved for other purposes, map to\nthe dict's items. For example, if a dict ``d`` has an item ``'foo'``,\nthen a request for ``d.foo`` will return that item lookup. aadicts\nalso have several helper methods, for example ``aadict.pick``. To\nfetch the value of an item that has the same name as one of the helper\nmethods you need to reference it by item lookup,\ni.e. ``d['pick']``. The helper methods are:\n\n* **aadict.pick** instance method:\n\n Returns a new aadict, reduced to only include the specified\n keys. Example:\n\n .. code-block:: python\n\n d = aadict(foo='bar', zig=87, zag=['a', 'b'])\n assert d.pick('foo', 'zag') == {'foo': 'bar', 'zag': ['a', 'b']}\n\n* **aadict.omit** instance method:\n\n Identical to the ``aadict.pick`` method, but returns the complement,\n i.e. all of those keys that are *not* specified. Example:\n\n .. code-block:: python\n\n d = aadict(foo='bar', zig=87, zag=['a', 'b'])\n assert d.omit('foo', 'zag') == {'zig': 87}\n\n* **aadict.d2ar** class method:\n\n Recursively converts the supplied `dict` to an `aadict`, including\n all sub-list and sub-dict types. Due to being recursive, but only\n copying dict-types, this is effectively a hybrid of a shallow and\n a deep clone. Example:\n\n .. code-block:: python\n\n d = aadict.d2ar(dict(foo=dict(bar='zig')))\n assert d.foo.bar == 'zig'\n\n Without the recursive walking, the ``.bar`` attribute syntax\n would yield an AttributeError exception because d.foo would\n reference a `dict` type, not an `aadict`.\n\n* **aadict.d2a** class method:\n\n Converts the supplied `dict` to an `aadict`. Example:\n\n .. code-block:: python\n\n d = aadict.d2a(dict(foo='bar'))\n assert d.foo == d['foo'] == 'bar'\n\n Note that this is identical to just using the constructor,\n but is provided as a symmetry to the ``aadict.d2ar`` class\n method, e.g.:\n\n .. code-block:: python\n\n d = aadict(dict(foo='bar'))\n assert d.foo == d['foo'] == 'bar'\n",
"bugtrack_url": null,
"license": "GPLv3+",
"summary": "An auto-attribute dict (and a couple of other useful dict functions)",
"version": "0.2.3",
"split_keywords": [
"auto",
"attribute",
"access",
"dict",
"helpers",
"pick",
"omit"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "c2832f665d91026d635e7b9262bb8479",
"sha256": "a77328ac55dbb5735da99441870251befe135f687ab707a7a178561363b27704"
},
"downloads": -1,
"filename": "aadict-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "c2832f665d91026d635e7b9262bb8479",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 16511,
"upload_time": "2016-11-12T17:07:33",
"upload_time_iso_8601": "2016-11-12T17:07:33.012554Z",
"url": "https://files.pythonhosted.org/packages/50/30/2d6c516ff308e1a2e25b8cf4274bd0ed763861b3d2b5cd1fb54c6d59bfcb/aadict-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2016-11-12 17:07:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "metagriffin",
"github_project": "aadict",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "aadict"
}