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 hashes)

Uploaded Python 2 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