Skip to main content

Descriptor based dataclass implementation

Project description

Coverage Status PyTest PyPI version code-style Binder

ZnInit - Automatic Generation of __init__ based on Descriptors

This package provides a base class for dataclass like structures with the addition of using Descriptors. The main functionality is the automatic generation of an keyword-only__init__ based on selected descriptors. The descriptors can e.g. overwrite __set__ or __get__ or have custom metadata associated with them. The ZnInit package is used by ZnTrack to enable lazy loading data from files as well as distinguishing between different types of descriptors such as zn.params or zn.outputs. An example can be found in the examples directory.

Example

The most simple use case is a replication of a dataclass like structure.

from zninit import ZnInit, Descriptor


class Human(ZnInit):
    name: str = Descriptor()
    language: str = Descriptor("EN")


# This will generate the following init:
def __init__(self, *, name, language="EN"):
    self.name = name
    self.language = language


fabian = Human(name="Fabian")
# or
fabian = Human(name="Fabian", language="DE")

The benefit of using ZnInit comes with using descriptors. You can subclass the zninit.Descriptor class and only add certain kwargs to the __init__ defined in init_descriptors: list. Furthermore, a post_init method is available to run code immediately after initializing the class.

from zninit import ZnInit, Descriptor


class Input(Descriptor):
    """A Parameter"""


class Metric(Descriptor):
    """An Output"""


class Human(ZnInit):
    _init_descriptors_ = [Input] # only add Input descriptors to the __init__
    name: str = Input()
    language: str = Input("DE")
    date: str = Metric()  # will not appear in the __init__

    def _post_init_(self):
        self.date = "2022-09-16"


julian = Human(name="Julian")
print(julian) # Human(language='DE', name='Julian')
print(julian.date)  # 2022-09-16

One benefit of ZnInit is that it also allows for inheritance.

from zninit import ZnInit, Descriptor

class Animal(ZnInit):
    age: int = Descriptor()
    
class Cat(Animal):
    name: str = Descriptor()
    
billy = Cat(age=4, name="Billy")

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

zninit-0.1.7.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

zninit-0.1.7-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file zninit-0.1.7.tar.gz.

File metadata

  • Download URL: zninit-0.1.7.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.13 Linux/5.4.0-135-generic

File hashes

Hashes for zninit-0.1.7.tar.gz
Algorithm Hash digest
SHA256 0362899d2519d9183cf4b5e264d4876c48649f5101020ab5dfa104935d707bde
MD5 7d16fea44c24e426ef171fc52b389123
BLAKE2b-256 547f7340d4a762d5a3f8aac957f68f7f2e148bd42c8f3602015866cd0f40dc4c

See more details on using hashes here.

File details

Details for the file zninit-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: zninit-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.13 Linux/5.4.0-135-generic

File hashes

Hashes for zninit-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b9cc8466e6975af03ed1fd0abcbebc7f57c01daebeed1ae1012c01fdd1212b88
MD5 592275c0ca075a63270b70d6c3c58cf8
BLAKE2b-256 a24a817c1f4d2bb6fb849abeaa50e3313a9f0c6488065db46cfe7c606df200a8

See more details on using hashes here.

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