Skip to main content

Converter for dataclasses and named tuples

Project description

nt2dc

There should be one—and preferably only one—obvious way to do it. The zen of Python

Python objects

Currently, python offers different ways of modelling a dead-simple objects. The most complex way is simply a plain old python object:

class Complex:
    def __init__(self, real: float = 0.0, imaginary: float = 0.0) -> None:
        self.__real = real
        self.__imaginary = imaginary

    def __set_real(self, value: float) -> None:
        self.__real = value

    def __get_real(self) -> float:
        return self.__real

    real = property(__get_real, __set_real)

    def __set_imaginary(self, value: float) -> None:
        self.__imaginary = value

    def __get_imaginary(self) -> float:
        return self.__imaginary

    imaginary = property(__get_imaginary, __set_imaginary)

    def __repr__(self) -> str:
        return "{} + {}i".format(self.__real, self.__imaginary)

Dataclasses

With newer versions of python 3, the dataclasses have been introduced, that can be created in two ways:

Complex = make_dataclass('Complex', [('real', float), ('imaginary', float)])

or

@dataclass
class Complex:
    real: float = field()
    imaginary: float = field()

Dataclasses are a great way of handling python data in a dead-simple way. Constructor, __repr__ function, getters, setters and introspection is possible and make it more easy to create a serialization and deserialization engine. They hence can easily be combined with multiple frameworks like sqlalchemy, marshmallow and entire web frameworks like bottle, flask etc. to create rest-ful APIs.

Named tuples

On the other hand, there are legacy modelling ways like named tuples, that can be create using two ways:

Complex = namedtuple('Complex', ['real', 'imaginary'])

or

class Complex(NamedTuple):
    real: float
    imaginary: float

Please note, that the latter has type hints while the former does not support type hinting yet.

NamedTuples are like tuples: A read-only combination of multiple values, just like regular tuples, but each value has a unique name.

Conversion

What if, one API provides or requests NamedTuples but you need a dataclass for further processing or vice versa?

This package provides a set of functions for the conversion from one notation (NamedTuple) to the other (Dataclass) and back.

Please note, the type hinted variant should be used.

Licensing

This library is published under BSD-3-Clause license.

Versioning

This library follows semantic versioning 2.0. Any breaking change will produce a new major release. Versions below 1.0 are considered to have a unstable interface.

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

nt2dc-0.9.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

nt2dc-0.9.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file nt2dc-0.9.0.tar.gz.

File metadata

  • Download URL: nt2dc-0.9.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for nt2dc-0.9.0.tar.gz
Algorithm Hash digest
SHA256 8c62309ecdd50193c5063bd0832beb5b4f59bb97e5a9e619d94b216391b18f08
MD5 623a80d26a3a60631a125af1ba27e3dd
BLAKE2b-256 6395d9cd0ee04fd91f62db79b7d82cf33c109625bc10396c916fc95fe8d91140

See more details on using hashes here.

File details

Details for the file nt2dc-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: nt2dc-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for nt2dc-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fc937aadf49d873f7cc2bd6eadca0b25525b16c156c1cb120348175791bdcc8
MD5 c8719f033d67b1dea18ddc374086711b
BLAKE2b-256 40f6d0c1d6201063bca1345af0b33fe92acdb7aa3fdd8c1358ecbf91da408e00

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