Skip to main content

Give a dictionary interface to a dataclass

Project description

my-dataclass-is-a-dict

my-dataclass-is-a-dict is a library to force a dataclass to act like a dictionary

Installation

Use the package manager pip to install my-dataclass-is-a-dict.

pip install my-dataclass-is-a-dict

Usage

from dataclasses import dataclass
from my_dataclass_is_a_dict import DictionaryMixin

@dataclass
class MyDataclass(DictionaryMixin):
    attribute1: str
    attribute2: str

instance = MyDataclass("attr1", 123)
assert instance["attribute1"] == "attr1"
assert instance["attribute2"] == 123
repr(instance)  # "MyDataclass(attribute1='attr1', attribute2=123)"

dunder repr

Calling repr on the dataclass will be in the format of a dataclass. If you need to have the dictionary repr output, add repr=False when creating your dataclass

from dataclasses import dataclass
from my_dataclass_is_a_dict import DictionaryMixin

@dataclass(repr=False)
class MyDataclass(DictionaryMixin):
    attribute1: str
    attribute2: str

instance = MyDataclass("attr1", 123)
repr(instance)  # "{attribute1='attr1', attribute2=123}"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

my_dataclass_is_a_dict-0.2-py2.py3-none-any.whl (4.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file my_dataclass_is_a_dict-0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for my_dataclass_is_a_dict-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a62267330c4a4d4828bdf87692deb216a15eb838456cc7a7b0aaa0e23c8d861d
MD5 abee5ef927f1663aab880c0156e5b854
BLAKE2b-256 5f4dae11912d8381ab666cca46fb84900ef57139c4e97fe930382b05394bb636

See more details on using hashes here.

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