Dictionary-like class that automatically typecasts string-like values to standard types
Project description
autotypedict
Dictionary-like class that automatically typecasts string-like values to standard types.
Installation
Using pip
python -m pip install -U autotypedict
Usage
Importing the class
from autotypedict import AutoTypeDict
Examples
>>> d = {"foo": {"baz": "42"}, "bar": {"baz": "n/a", "qux": "true"}}
>>> AutoTypeDict(d) # typecast every value
{'foo': {'baz': 42}, 'bar': {'baz': None, 'qux': True}}
>>> AutoTypeDict(d, ignored_keys=("baz",)) # ignore "baz"
{'foo': {'baz': '42'}, 'bar': {'baz': 'n/a', 'qux': True}}
>>> AutoTypeDict(d, ignored_keys=("bar.baz",)) # ignore d["bar"]["baz"]
{'foo': {'baz': 42}, 'bar': {'baz': 'n/a', 'qux': True}}
>>> td = AutoTypeDict()
>>> td.data = d # updating data attribute to ignore typecasting
>>> td
{'foo': {'baz': '42'}, 'bar': {'baz': 'n/a', 'qux': 'true'}}
Caveats
Values are only typecasted when the AutoTypeDict
instance is updated directly, so avoid using type-specific operations on mutable objects within the dictionary (e.g. list.append
, dict.update
).
If using these methods is unavoidable, call the .refresh()
instance method to re-process the dictionary.
Alternatives to common type operations:
Operation | Non-typecasted | Typecasted |
---|---|---|
Append to list | td["list"].append(v) |
td["list"] += [v] |
Update dict | td["dict"].update({k: v}) |
td["dict"][k] = v |
Merge dict | td["dict"].update(other_dict) |
td["dict"] |= merge_dict [1] |
[1]: Python 3.9 or later
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
autotypedict-1.0.0.tar.gz
(4.2 kB
view hashes)
Built Distribution
Close
Hashes for autotypedict-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79a9d3c4326129488e4b54f8322def576f784a9703c151ca79c31ff5bba867ba |
|
MD5 | 3c099b2e0c586020b3c8c5ad3dcc83ad |
|
BLAKE2b-256 | 0d8879c0fa3c4d7ea568d780e538c380f5dd9a504993ad270ad6b53929221d20 |