Skip to main content

A Python dict subclass which tries to act like JavaScript objects, so you can use the dot-notation ( 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 ( 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 accepted.


$ pip install mydict


Let's give it a try

d = MyDict() = 'bar'

# ==> 'bar'

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

d = MyDict()
if 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() = {'bar': 'baz', 'lst': [{'a': 123}]}

# ==> 'baz'

# ==> 123

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

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

# ==> 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!'}]})

# ==> 'bar'

# ==> 1

# ==> '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)
# ... == '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')
# ... == 'BAR'

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

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

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

d = MyDict({'': 'baz'})
# ...
# '' is not interpreted as a path because the key exists
d[''] = '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'}, '': 'BAZ'})
# ...
d[''] = 'BAZ'  # the "dotted field" ('') has precedence over the path = '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


You have at your disposal a couple of methods to retrieve the MyDict object transformed into something else:


Returns the MyDict object as a JSON string (str):

d = MyDict(foo="bar", arr=[1, 2, {"three": 3}])
# '{"foo": "bar", "arr": [1, 2, {"three": 3}]}'

In some occasions you'll need a plain old Python dict representation of the MyDict object, though is a dict subclass:

d = MyDict(foo="bar", arr=[{"one": 1}, {"two": 2}])
# d_ = {'foo': 'bar', 'arr': [{'one': 1}, {'two': 2}]}

Initialization from JSON

It's also possible to load a JSON from str, bytes, and file-like objects (with a .read() method) using the static method from_json:

d = MyDict.from_json('{"foo": "bar"}')
# == 'bar'

d = MyDict.from_json(b'{"foo": "bar"}')
# == 'bar'

d = MyDict.from_json(open('/path/to/file.json', 'r'))
# d = MyDict.from_json(open('/path/to/file.json', 'rb')) also work
from io import StringIO, BytesIO

s = StringIO()
s.write('{"foo": "bar"}')

d = MyDict.from_json(s)
# == 'bar'

# ...

b = StringIO()
b.write(b'{"foo": "bar"}')

d = MyDict.from_json(b)
# == 'bar'

The tests passed successfully with Python 3.6. With Python 2.7 fail on "bytes stuff" tests, regarding the use of the static method from_json().

$ 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.

Files for mydict, version 1.0.20
Filename, size File type Python version Upload date Hashes
Filename, size mydict-1.0.20-py2.py3-none-any.whl (7.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size mydict-1.0.20.tar.gz (5.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page