zninit


Namezninit JSON
Version 0.1.11 PyPI version JSON
download
home_page
SummaryDescriptor based dataclass implementation
upload_time2023-09-21 15:50:57
maintainer
docs_urlNone
authorzincwarecode
requires_python>=3.8,<4.0
licenseApache-2.0
keywords dataclass descriptor
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Coverage Status](https://coveralls.io/repos/github/zincware/ZnInit/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnInit?branch=main)
![PyTest](https://github.com/zincware/ZnInit/actions/workflows/pytest.yaml/badge.svg)
[![PyPI version](https://badge.fury.io/py/zninit.svg)](https://badge.fury.io/py/zninit)
[![code-style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black/)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/zincware/ZnInit/HEAD)
[![zincware](https://img.shields.io/badge/Powered%20by-zincware-darkcyan)](https://github.com/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](https://docs.python.org/3/howto/descriptor.html). 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](https://github.com/zincware/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.

```python
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.

```python
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.

```python
from zninit import ZnInit, Descriptor

class Animal(ZnInit):
    age: int = Descriptor()

class Cat(Animal):
    name: str = Descriptor()

billy = Cat(age=4, name="Billy")
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "zninit",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "dataclass,descriptor",
    "author": "zincwarecode",
    "author_email": "zincwarecode@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/06/34/5a2b779e62ddb7fc42a25d7056f41bb95a0a2241268f6cb301ff367b58a6/zninit-0.1.11.tar.gz",
    "platform": null,
    "description": "[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnInit/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnInit?branch=main)\n![PyTest](https://github.com/zincware/ZnInit/actions/workflows/pytest.yaml/badge.svg)\n[![PyPI version](https://badge.fury.io/py/zninit.svg)](https://badge.fury.io/py/zninit)\n[![code-style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black/)\n[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/zincware/ZnInit/HEAD)\n[![zincware](https://img.shields.io/badge/Powered%20by-zincware-darkcyan)](https://github.com/zincware)\n\n# ZnInit - Automatic Generation of `__init__` based on Descriptors\n\nThis package provides a base class for `dataclass` like structures with the\naddition of using\n[Descriptors](https://docs.python.org/3/howto/descriptor.html). The main\nfunctionality is the automatic generation of an keyword-only`__init__` based on\nselected descriptors. The descriptors can e.g. overwrite `__set__` or `__get__`\nor have custom metadata associated with them. The `ZnInit` package is used by\n[ZnTrack](https://github.com/zincware/ZnTrack) to enable lazy loading data from\nfiles as well as distinguishing between different types of descriptors such as\n`zn.params` or `zn.outputs`. An example can be found in the `examples`\ndirectory.\n\n# Example\n\nThe most simple use case is a replication of a dataclass like structure.\n\n```python\nfrom zninit import ZnInit, Descriptor\n\n\nclass Human(ZnInit):\n    name: str = Descriptor()\n    language: str = Descriptor(\"EN\")\n\n\n# This will generate the following init:\ndef __init__(self, *, name, language=\"EN\"):\n    self.name = name\n    self.language = language\n\n\nfabian = Human(name=\"Fabian\")\n# or\nfabian = Human(name=\"Fabian\", language=\"DE\")\n```\n\nThe benefit of using `ZnInit` comes with using descriptors. You can subclass the\n`zninit.Descriptor` class and only add certain kwargs to the `__init__` defined\nin `init_descriptors: list`. Furthermore, a `post_init` method is available to\nrun code immediately after initializing the class.\n\n```python\nfrom zninit import ZnInit, Descriptor\n\n\nclass Input(Descriptor):\n    \"\"\"A Parameter\"\"\"\n\n\nclass Metric(Descriptor):\n    \"\"\"An Output\"\"\"\n\n\nclass Human(ZnInit):\n    _init_descriptors_ = [Input] # only add Input descriptors to the __init__\n    name: str = Input()\n    language: str = Input(\"DE\")\n    date: str = Metric()  # will not appear in the __init__\n\n    def _post_init_(self):\n        self.date = \"2022-09-16\"\n\n\njulian = Human(name=\"Julian\")\nprint(julian) # Human(language='DE', name='Julian')\nprint(julian.date)  # 2022-09-16\nprint(Input.get_dict(julian)) # {\"name\": \"Julian\", \"language\": \"DE\"}\n```\n\nOne benefit of `ZnInit` is that it also allows for inheritance.\n\n```python\nfrom zninit import ZnInit, Descriptor\n\nclass Animal(ZnInit):\n    age: int = Descriptor()\n\nclass Cat(Animal):\n    name: str = Descriptor()\n\nbilly = Cat(age=4, name=\"Billy\")\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Descriptor based dataclass implementation",
    "version": "0.1.11",
    "project_urls": {
        "repository": "https://github.com/zincware/ZnInit"
    },
    "split_keywords": [
        "dataclass",
        "descriptor"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2e1a06bde8267e02d801d4fe04f4fb68540933ef051de4e471ee2d2b350ee65c",
                "md5": "39735f00edbf91770f733ee1ab2c8da2",
                "sha256": "78e5c7e1d0d50c131cf8efab58aad4a213446a1c38f38360731bf11b778e29e1"
            },
            "downloads": -1,
            "filename": "zninit-0.1.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "39735f00edbf91770f733ee1ab2c8da2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 13668,
            "upload_time": "2023-09-21T15:50:55",
            "upload_time_iso_8601": "2023-09-21T15:50:55.441714Z",
            "url": "https://files.pythonhosted.org/packages/2e/1a/06bde8267e02d801d4fe04f4fb68540933ef051de4e471ee2d2b350ee65c/zninit-0.1.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06345a2b779e62ddb7fc42a25d7056f41bb95a0a2241268f6cb301ff367b58a6",
                "md5": "901e0bc423b8494768d556f9fc114101",
                "sha256": "fc14a55bd85a38f8f1411bd319cac541c2e6e16ab402f3f11a94e182b0681965"
            },
            "downloads": -1,
            "filename": "zninit-0.1.11.tar.gz",
            "has_sig": false,
            "md5_digest": "901e0bc423b8494768d556f9fc114101",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 13689,
            "upload_time": "2023-09-21T15:50:57",
            "upload_time_iso_8601": "2023-09-21T15:50:57.144828Z",
            "url": "https://files.pythonhosted.org/packages/06/34/5a2b779e62ddb7fc42a25d7056f41bb95a0a2241268f6cb301ff367b58a6/zninit-0.1.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-21 15:50:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zincware",
    "github_project": "ZnInit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "zninit"
}
        
Elapsed time: 1.03624s