Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/makinacorpus/easydict
Access dict values as attributes (works recursively)
https://github.com/makinacorpus/easydict
Last synced: 1 day ago
JSON representation
Access dict values as attributes (works recursively)
- Host: GitHub
- URL: https://github.com/makinacorpus/easydict
- Owner: makinacorpus
- License: lgpl-3.0
- Created: 2011-04-18T12:52:49.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2024-03-04T12:03:18.000Z (8 months ago)
- Last Synced: 2024-05-12T02:43:44.411Z (6 months ago)
- Language: Python
- Homepage:
- Size: 44.9 KB
- Stars: 286
- Watchers: 35
- Forks: 47
- Open Issues: 23
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES
- License: LICENSE
Awesome Lists containing this project
README
.. image:: https://img.shields.io/pypi/v/easydict.svg
:target: https://pypi.python.org/pypi/easydict.. image:: https://img.shields.io/pypi/dm/easydict.svg
:target: https://pypi.python.org/pypi/easydict========
Easydict
========*EasyDict* allows to access dict values as attributes (works recursively).
A Javascript-like properties dot notation for python dicts.INSTALL
=======::
pip install easydictUSAGE
=====::
>>> from easydict import EasyDict as edict
>>> d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
>>> d.foo
3
>>> d.bar.x
1
>>> d = edict(foo=3)
>>> d.foo
3Very useful when exploiting parsed JSON content !
::
>>> from easydict import EasyDict as edict
>>> from simplejson import loads
>>> j = """{
"Buffer": 12,
"List1": [
{"type" : "point", "coordinates" : [100.1,54.9] },
{"type" : "point", "coordinates" : [109.4,65.1] },
{"type" : "point", "coordinates" : [115.2,80.2] },
{"type" : "point", "coordinates" : [150.9,97.8] }
]
}"""
>>> d = edict(loads(j))
>>> d.Buffer
12
>>> d.List1[0].coordinates[1]
54.9Can set attributes as easily as getting them :
::
>>> d = EasyDict()
>>> d.foo = 3
>>> d.foo
3It is still a ``dict`` !
::
>>> d = EasyDict(log=False)
>>> d.debug = True
>>> d.items()
[('debug', True), ('log', False)]Instance and class attributes are accessed like usual objects...
::
>>> class Flower(EasyDict):
... power = 1
...
>>> f = Flower({'height': 12})
>>> f.power
1
>>> f['power']
1LICENSE
=======* Lesser GNU Public License
AUTHORS
=======* Mathieu Leplatre
|makinacom|_
.. |makinacom| image:: http://depot.makina-corpus.org/public/logo.gif
.. _makinacom: http://www.makina-corpus.comSimilar tools
=============* `TreeDict `_, a fast and full-featured dict-like tree container.
* `addict `_