Skip to main content

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


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)

Uploaded Source

Built Distribution

autotypedict-1.0.0-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page