Skip to main content

Access dict values as attributes (works recursively).

Project description

EasyDict allows to access dict values as attributes (works recursively). A Javascript-like properties dot notation for python dicts.

USAGE

>>> 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
3

Very 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.9

Can set attributes as easily as getting them :

>>> d = EasyDict()
>>> d.foo = 3
>>> d.foo
3

It 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']
1

LICENSE

  • Lesser GNU Public License

AUTHORS

makinacom

Similar tools

CHANGELOG

1.10 (unreleased)

  • Nothing changed yet.

1.9 (2018-10-18)

  • Fix issue #3 that update and pop now work correctly on EasyDicts.

1.8 (2018-08-17)

  • Update package classifiers.

1.7 (2017-04-27)

  • Prevent copying sub-EasyDicts on assignment to fix unpickling (#7, thanks @Chronos-Sk)

1.6 (2015-01-27)

  • Allow setting attributes via setting items (thanks phivos)

1.5 (2014-08-07)

  • Allow subclassing using self instead of class name (thanks Steve Engledow)

1.4 (2011-03-13)

  • Access class attributes like instance attributes

1.3 (2012-02-08)

  • Better documentation and tests

1.2 (2011-06-08)

  • Fix inclusion of README

1.1 (2012-04-21)

  • Switch to distutils2

1.0 (2011-04-18)

  • Initial working version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_dict_linshangsheng-1.10.dev0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file easy_dict_linshangsheng-1.10.dev0.tar.gz.

File metadata

File hashes

Hashes for easy_dict_linshangsheng-1.10.dev0.tar.gz
Algorithm Hash digest
SHA256 b159dc768da5a78d35821b06a08b9b10e2be0feb49eb3d2bebcf50f57adba411
MD5 9bad7ac0fffbfe596814d0bc5e77db1c
BLAKE2b-256 9a868dd3551f1fe132d96592b5699dfbb7d8cb21ef860043b8da6f1e58bbc2d8

See more details on using hashes here.

File details

Details for the file easy_dict_linshangsheng-1.10.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_dict_linshangsheng-1.10.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 e676ff1d34889cd683aa94bf686cc1e51c15f0418dd334c29b66a3d20c34352d
MD5 defc57e14e75d18be3e442d44cd2f476
BLAKE2b-256 bed4ab7147f4e482d9b16fa613a0a86ac36ce76f5b20441b503f7bbbf97fce23

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page