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
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file my_dataclass_is_a_dict-0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: my_dataclass_is_a_dict-0.2-py2.py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a62267330c4a4d4828bdf87692deb216a15eb838456cc7a7b0aaa0e23c8d861d |
|
MD5 | abee5ef927f1663aab880c0156e5b854 |
|
BLAKE2b-256 | 5f4dae11912d8381ab666cca46fb84900ef57139c4e97fe930382b05394bb636 |