Skip to main content

A Python dict subclass which tries to act like JavaScript objects, so you can use the dot-notation (d.foo) to access members of the object.

Project description

A Python dict subclass which tries to act like JavaScript objects, so you can use the dot notation (d.foo) to access members of the object. If the member doesn’t exist yet then it’s created when you assign a value to it. Brackets notation (d[‘foo’]) is also possible.

Installation

$ pip install mydict

Examples

Let’s give it a try

d = MyDict()
d.foo = 'bar'

print(d.foo)
# ==> 'bar'

If you try to get the value of a non-existing member then a None value is returned

d = MyDict()
if d.foo is None:
    print('"foo" does not exist yet!')

If that value is “complex” (a dict or another MyDict instance), then it’s also recursively transformed into a MyDict object, so you can access it in the same way

d = MyDict()
d.foo = {'bar': 'baz', 'lst': [{'a': 123}]}

print(d.foo.bar)
# ==> 'baz'

print(d.foo.bar)
# ==> 'baz'

print(d.foo.lst[0].a)
# ==> 123

Values in lists are accessed, as you expect, with the brackets notation (d[0]):

d = MyDict()
d.foo = [1, 2, 3]

print(d.foo[2])
# ==> 3

We can instantiate it from a dict of any level of complexity

d = MyDict({'foo': 'bar', 'baz': [1, 2, {'foo': 'bar', 'baz': 'Hello, world!'}}])

print(d.foo)
# ==> 'bar'

print(d.baz[0])
# ==> 1

print(d.baz[2].foo)
# ==> 'bar'

with keywords in the constructor

d = MyDict(a=1, b=2.2, c=[1, 2, 3], d=[{'x': 1, 'y': [100, 200, 300]}])
...
d.a == 1
d.b == 2.2
d.c[0] == 1
d.d[0].x == 1
d.d[0].y[1] == 200

or both

d = MyDict({'foo': 'bar'}, baz=123)
...
d.foo == 'bar'
d.baz == 123

Please, take into account that keyword initialization has precedence over the dict (first parameter of the constructor)

d = MyDict({'foo': 'bar'}, foo='BAR')
...
d.foo == 'BAR'

It’s also possible to access members using a path with get or brackets notation (d[’…’]):

d = MyDict(foo={'bar': 'baz'})
...
d['foo.bar'] == 'baz'
d.get('foo.bar') == 'baz'

But when those keys with dots exists in the tree they are accessed using the corresponding key

d = MyDict({'foo.bar': 'baz'})
...
# 'foo.bar' is not interpreted as a path because the key exists
d['foo.bar'] = 'baz'

But there’s a particular case, if a dotted key exists and match an existing path, then this ain’t work properly, or work in a different way depending on the method of access used, to be correct

d = MyDict({'foo': {'bar': 'baz'}, 'foo.bar': 'BAZ'})
...
d['foo.bar'] = 'BAZ'  # the "dotted field" ('foo.bar') has precedence over the path
d.foo.bar = 'baz'  # it's not possible to detect a "dotted key" using "dot notation"

Personally, I don’t see this as a great issue because I generally avoid using dots in keys, like in the previous case

The tests passed successfully with Python 3.6 and Python 2.7 versions.

$ pytest mydict -v

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

mydict-1.0.8.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mydict-1.0.8-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file mydict-1.0.8.tar.gz.

File metadata

  • Download URL: mydict-1.0.8.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mydict-1.0.8.tar.gz
Algorithm Hash digest
SHA256 84c3ad4642fb292aea2999f69dd0f64d11c083cf0ec4d9ac7e99ad0e87812466
MD5 7d1dedc60559603eca558854a44083be
BLAKE2b-256 f64ea1bd95a904105a31ca6cbc17cab02f07e5cae1516caef09ebc0bf0351146

See more details on using hashes here.

File details

Details for the file mydict-1.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for mydict-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 c244e7f9a4bd3ebb01f97d0a685db84da280c552367e46eb7cde79b7492b8636
MD5 f2de53ecea8b8f8af6b39b3b3a2ee161
BLAKE2b-256 92dee1000045d3767b3b20d422409d036a9e8534aeed7ec0cec98ae04adf3702

See more details on using hashes here.

Supported by

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