dict implementation supporting attribute access, recursion, json, hashable, subclass, metaclass
Project description
d1ct - a hybrid object/mapping
FEATURES
- directly inherited from dict
- accessible by attribute. o.x == o['x']
- This works also for all corner cases.
- json.dumps json.loads
- vars() works on it!
- hashable! (should not yet trust on it, though)
- autocomplete works even if the objects comes from a list
- best of all, it works recursively. So all inner dicts will become d1cts too.
CHANGELOG
version 1.0.5
- added the capability to not only accept dict-like mappings, but also lists of objects
which will be returned as a list of d1ct-objects.
So no worries if you send it the json.loads output (which can be either a list or dict)
NO DOWNSIDES?
oh hell yeah it got downsides
the performance which is a magnitude slower (~6x) than builtin dict
to give you an impression of timeit results:
d1ct
0.26289230000111274
0.2543108999961987
0.25743720002355985
0.2560539000260178
0.2556736999831628
0.25478869999642484
0.2541010999993887
0.2552008999919053
0.2651131000020541
0.25846979999914765
dict
0.04928750000544824
0.04902900001616217
0.049049399996874854
0.050644999981159344
0.0496991999971214
0.04940899999928661
0.0497540999785997
0.04959690000396222
0.04939249999006279
0.04959330000565387
So... this implementation might have side effects which I did not notice, because of different use cases or whatever.
Usage / Testing
import d1ct
d = d1ct({"a": 1, "b": {"b1": "c1"}}, kw1=1, kw2=2)
d["z1"] = {}
d.z1.update({"yyyy": 2323232})
assert d.a == d['a'] == 1
assert d.z1 == d['z1'] == {'yyyy': 2323232}
assert d == {'a': 1, 'b': {'b1': 'c1'}, 'kw1': 1, 'kw2': 2, 'z1': {'yyyy': 2323232}}
assert type(d.z1) is d1ct
d.z1.update({"yyyy": 2323232})
assert type(d.z1.yyyy) is int
d.z1.update({"yyyy": {"xbb": [1, 2, 23]}})
assert type(d.z1.yyyy) is d1ct
import json
json.dumps(d)
d |= {'more_keys': {'really': 'yeah!'}}
assert d == {'a': 1,'b': {'b1': 'c1'}, 'kw1': 1,'kw2': 2, 'z1': {'yyyy': {'xbb': [1, 2, 23] } },'more_keys': {'really': 'yeah!'} }
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
File details
Details for the file d1ct-1.0.5.tar.gz
.
File metadata
- Download URL: d1ct-1.0.5.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18d0513d0fb476828c256703bf185ae5fd4e5dfc30d863b88ba99a6d0daf99cb |
|
MD5 | 6999aa533e872cff565b13a1102a050e |
|
BLAKE2b-256 | 1573a169bb4c1112d83b852d9eb57b73a347379dd20ba464bdef8832b70ea0c0 |