Dotsi: Dot-accessible, update-aware Python dicts (& lists).
Project description
Dotsi
Dot-accessible, update-aware Python dicts (& lists). Works recursively, like a charm.
Dotsi defines two classes dotsi.Dict
and dotsi.List
, which work together to bring JavaScript-like dot-notation to Python dicts (and lists therein).
Usage
Let's dive right in:
>>> import dotsi
>>>
>>> d = dotsi.Dict({"foo": {"bar": "baz"}}) # Basic
>>> d.foo.bar
'baz'
>>> d["users"] = [{"id": 0, "name": "Alice"}] # List
>>> d.users[0].name
'Alice'
>>> d.users.append({"id": 1, "name": "Becca"}); # Append
>>> d.users[1].name
'Becca'
>>> d.users += [{"id": 2, "name": "Cathy"}]; # `+=`
>>> d.users[2].name
'Cathy'
>>> d.update({"tasks": [{"id": "a", "text": "Task A"}]});
>>> d.tasks[0].text
'Task A'
>>> d.tasks[0].tags = ["red", "white", "blue"];
>>> d.tasks[0].tags[2];
'blue'
>>> d.tasks[0].pop("tags") # `.pop()`
['red', 'white', 'blue']
>>>
>>> import pprint
>>> pprint.pprint(d)
{'foo': {'bar': 'baz'},
'tasks': [{'id': 'a', 'text': 'Task A'}],
'users': [{'id': 0, 'name': 'Alice'},
{'id': 1, 'name': 'Becca'},
{'id': 2, 'name': 'Cathy'}]}
>>>
>>> type(d.users) # dotsi.Dict (AKA dotsi.DotsiDict)
<class 'dotsi.DotsiList'>
>>> type(d.users[0]) # dotsi.List (AKA dotsi.DotsiList)
<class 'dotsi.DotsiDict'>
>>>
In the above example, while we explicitly initialized d
as an dotsi.Dict
:
d.users
automatically became adotsi.List
.d.users[0]
automatically became adotsi.Dict
.
Dotsi vs Others
Addict:
At Polydojo, we've been using Addict for quite some time. It's a great library! But it doesn't play well with list-nested (inner) dicts.
>>> import addict
>>>
>>> d = addict.Dict({"foo": {"bar": "baz"}})
>>> d.foo
{'bar': 'baz'}
>>> d["users"] = [{"id": 0, "name": "Alice"}]
>>> d.users[0].name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'name'
>>>
EasyDict: EasyDict is another great library. It works recursively, but doesn't fully support list-nested dict updates.
>>> import easydict
>>>
>>> d = easydict.EasyDict({"foo": {"bar": "baz"}})
>>> d.foo
{'bar': 'baz'}
>>> d["users"] = [{"id": 0, "name": "Alice"}]
>>> d.users[0].name
'Alice'
>>> d.users.append({"id": 1, "name": "Becca"});
>>> d.users[1].name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'name'
>>>
Shortcuts
Classes:
dotsi.Dict
is a short alias fordotsi.DotsiDict
.dotsi.List
is a short alias fordotsi.DotsiList
.
Functions:
dotsi.dotsify()
callsdotsi.Dict
/dotsi.List
, as appropriate.dotsi.fy()
is a short alias fordotsi.dotsify()
.
In most cases, all you need is:
dotsi.fy(thing)
, wherething
is adict
orlist
.
License
Copyright (c) 2020 Polydojo, Inc.
The software is released "AS IS" under the MIT License, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. See LICENSE.txt for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dotsi-0.0.1.tar.gz
.
File metadata
- Download URL: dotsi-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.24.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e367426eb3dd3d5455935db437ed9db36ed6d6cdd2e3a7d2804359ba6176718 |
|
MD5 | 40764d9d64f9c6c79dc439c25668cb19 |
|
BLAKE2b-256 | b7c56f5d1a212cb2e08c2987a3b77a308d0bdf878f01ca74969e159b65a54ab3 |
File details
Details for the file dotsi-0.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: dotsi-0.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.24.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 392f101933d53a76ed6abf9d4e7186cc5a31ba21933618e8d2db9be669f9fd85 |
|
MD5 | 9890f0ca88d27507708c597b9122e903 |
|
BLAKE2b-256 | edcf403fb94b96edae46873199ef0a6d28f38b33690ca9da2a495163fee0bf04 |