Skip to main content

Add dictionary-like capabilities to dataclasses

Project description

DataDict

Treat dataclasses like dictionaries

v1.0.0 Python versions Tests Code style: black Interrogate

Installing DataDict

DataDict is available at PyPI. You can install it using Pip:

$ python -m pip install datadict

Using DataDict

You can use a DataDict dataclass as a drop-in replacement for dataclasses.dataclass in the standard library:

from datadict import dataclass

@dataclass(order=True)
class Point:
    x: int
    y: int

For instances of the new data class, you can access attributes using square brackets, similar to dictionary access:

>>> point = Point(1, 2)
>>> point
Point(x=1, y=2)

>>> point.x
1
>>> point["x"]
1

>>> point["y"] = 3
>>> point
Point(x=1, y=3)

You can also convert the dataclass instance to a proper dictionary:

>>> point.asdict()
{'x': 1, 'y': 3}

Installing From Source

You can always download the latest version of DataDict from GitHub. DataDict uses Flit as a build tool.

To install DataDict from the downloaded source, run Flit through pip:

$ python -m pip install .

If you want to change and play with the DataDict source code, you should install it in editable mode:

$ python -m pip install -w .[dev,test]

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

datadict-1.0.0.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

datadict-1.0.0-py3-none-any.whl (3.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