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
Built Distribution
File details
Details for the file autotypedict-1.0.0.tar.gz
.
File metadata
- Download URL: autotypedict-1.0.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48b68230ba73ab8bfaaaf80ed429d2e521984eed8f01bf1d21e74db0becf2f06 |
|
MD5 | 765ea6d6f09ef55237da0da1b7c387d9 |
|
BLAKE2b-256 | 451bf319eac1a1536a561488adc377f686335a6b67d4af209d70ecce65c426a2 |
File details
Details for the file autotypedict-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: autotypedict-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79a9d3c4326129488e4b54f8322def576f784a9703c151ca79c31ff5bba867ba |
|
MD5 | 3c099b2e0c586020b3c8c5ad3dcc83ad |
|
BLAKE2b-256 | 0d8879c0fa3c4d7ea568d780e538c380f5dd9a504993ad270ad6b53929221d20 |