Skip to main content

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.

Metadata

Release files for nt2dc 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nt2dc 0.9.0
File Size Uploaded
nt2dc-0.9.0.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nt2dc 0.9.0
File Interpreter ABI Platform
nt2dc-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / nt2dc-0.9.0.tar.gz

Download URL nt2dc-0.9.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8c62309ecdd50193c5063bd0832beb5b4f59bb97e5a9e619d94b216391b18f08
BLAKE2b-256 checksum
How to use checksums
6395d9cd0ee04fd91f62db79b7d82cf33c109625bc10396c916fc95fe8d91140
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / nt2dc-0.9.0-py3-none-any.whl

Download URL nt2dc-0.9.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fc937aadf49d873f7cc2bd6eadca0b25525b16c156c1cb120348175791bdcc8
BLAKE2b-256 checksum
How to use checksums
40f6d0c1d6201063bca1345af0b33fe92acdb7aa3fdd8c1358ecbf91da408e00
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.0

2 release files

0.7.5

2 release files

0.7.2

2 release files

0.7.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page