Skip to main content

Coverage Status PyTest PyPI version code-style Binder zincware

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
print(Input.get_dict(julian)) # {"name": "Julian", "language": "DE"}

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")

Metadata

Release files for zninit 0.1.11

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

Source distribution (sdist)

Source distribution for zninit 0.1.11
File Size Uploaded Provenance
zninit-0.1.11.tar.gz 13.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 27.4 kB

Release files / zninit-0.1.11.tar.gz

Download URL zninit-0.1.11.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fc14a55bd85a38f8f1411bd319cac541c2e6e16ab402f3f11a94e182b0681965
BLAKE2b-256 checksum
How to use checksums
06345a2b779e62ddb7fc42a25d7056f41bb95a0a2241268f6cb301ff367b58a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.8 Linux/6.2.0-32-generic

Release files / zninit-0.1.11-py3-none-any.whl

Download URL zninit-0.1.11-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78e5c7e1d0d50c131cf8efab58aad4a213446a1c38f38360731bf11b778e29e1
BLAKE2b-256 checksum
How to use checksums
2e1a06bde8267e02d801d4fe04f4fb68540933ef051de4e471ee2d2b350ee65c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.8 Linux/6.2.0-32-generic

Security for zninit 0.1.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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