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.1-py2.py3-none-any.whl (3.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for my_dataclass_is_a_dict-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 88d714060453cf399b89777488b9e9cfd513b9e190e8a4708af751c7e13a9950
MD5 1190f647a2225d40ec101ebc32ab729b
BLAKE2b-256 179237d9f20635e1325e8d7710149d66d046a1bacedfdd6ad50618cab10d7b7f

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