Skip to main content

A custom dataclass implementation

Project description

Customdataclass

Custom implementation of the dataclass module from the standard Python library as a base class for other dataclasses. The documentation is available here.

Details

A while back I was working on a project (now lost in a dusty corner of my GitHub profile) that required me to create a lot of data structures (variables used only to hold complex data) and I quickly realised that using dictionaries (or named dictionaries) was slowly becoming messy because:

  • dictionaries are mutable
  • dictionary keys are strings and there's no easy way to set a default value for a key
  • there's no way to ensure that a dictionary is well-formed
  • complex attributes that are needed once or twice cannot be generated on the fly and must be stored in the dictionary

After a quick Google-fu session, I found out the existence of the dataclass decorator, swiftly provided by the dataclasses. This decorator allows you to create a class that is used only to hold data, and it provides a lot of useful features, such as:

  • default values for attributes
  • pre-determined methods (such as __init__, __repr__, etc.)

However, it does not handle easily features like:

  • complex attributes (i.e. attributes that are dataclasses themselves)
  • nested dataclasses (i.e. a dataclass that has another dataclass as an attribute)
  • serialization and deserialization
  • hashing and equality
  • type checking
  • inheritance

All these were important for the project (which once again will be finished, one day) and so I decided to create a custom class that would handle all these features.

I chose to create a class (and not a decorator) because I wanted to be able to inherit from it and to be able to use it as a base class for other dataclasses, thus solving the aforementioned problems.

Provided features

  • default values for attributes
  • type checking (can be deactivated)
  • nested dataclasses (i.e. a dataclass that has another dataclass as an attribute)
  • frozen dataclasses
    • a dataclass cannot be modified after its creation (if the parameter frozen is set to True, as per default)
    • otherwise, a dataclass can be manually frozen using the freeze method
  • equality comparison (via the __eq__ method)
  • hashing (via the __hash__ method)
  • full support inheritance
  • full support for methods overriding and custom properties

Installing

The package is available on PyPI and can be installed using pip:

pip install -u customdataclass

Examples

Examples can be found in the examples folder, both in a text file EXAMPLES.md and in a set of Python scripts.

Tests

Unit tests can be found in the tests folder. Currently, the code coverage for unit tests is close to 100%.

Running the tests

To run the tests, simply run the following command from the root folder of the project:

python3 -m unittest discover -v -s ./tests -p "test*.py

If you're using VScode, you can also use the Run Tests command from the Python Test Explorer for Visual Studio Code extension.

Computing the coverage

To compute the coverage, simply run the following commands from the root folder of the project:

  • coverage run -m unittest discover -s ./tests -p "test*.py"
  • coverage report -m to see the report in the terminal
  • coverage html to see format the report in HTML and see it in the browser

Documentation

Documentation can be found in the docs folder of the repo and on this page.

Building the documentation

To build the documentation, simply run the following command from the src folder of the project:

pdoc --html -o ../docs customdataclass.py --force

Contributing

Pull requests, bug reports and feature requests are more than welcome!

Thank you for your interest in this silly little project of mine!

License

This project is licensed under the MIT License - see the LICENSE.md file for details

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

customdataclass-0.1.2.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

customdataclass-0.1.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file customdataclass-0.1.2.tar.gz.

File metadata

  • Download URL: customdataclass-0.1.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for customdataclass-0.1.2.tar.gz
Algorithm Hash digest
SHA256 88279e7fd033aeb72ae0d453c3e5a0f346231b51dbd402868f0517416fdb553c
MD5 399f41e86dcbf673e9fddd52ec7339cd
BLAKE2b-256 e1dfb64ad6baf9f7034fadf9f9ebf1ee29ceb9403f26bcba16dcb2dfd5158372

See more details on using hashes here.

File details

Details for the file customdataclass-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for customdataclass-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4cb76052e4e697a6f5216a25381f98bdb0afc2f41192125c2c87ebe69fa9eb6e
MD5 6e25b93dafd61da085b8437ee54c026e
BLAKE2b-256 e0a5bf76e41e50c7186bf0892fa7cb30d6f38d3c45e9a29e4788a6b1e1d553c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page