nightjar


Namenightjar JSON
Version 0.0.5 PyPI version JSON
download
home_pageNone
SummaryA typing based dispatching library.
upload_time2024-10-13 13:39:00
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords dispatch type-hinting typing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Nightjar

[![PyPI version](https://badge.fury.io/py/nightjar.svg)](https://badge.fury.io/py/nightjar) 
![PyPI - Downloads](https://img.shields.io/pypi/dm/nightjar)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nightjar)
![GitHub](https://img.shields.io/github/license/ysenarath/nightjar)

## Description

This project is a Python package that provides a simple way to create objects of different types based on a configuration object. The package is inspired by how `huggingface/transformers` package creates different types of models based on a configuration object. The package provides a base class `BaseModule` that can be subclassed to create different class types. Each class type is defined by a configuration class that inherits from `BaseConfig`. The `AutoModule` class is used to automatically create instances of the correct object type based on the configuration. The `dispatch` attribute of the configuration class is used to specify the static attribute that determines the object type.

## Installation

To install this package, run the following command:

```bash
pip install nightjar
```

## Usage

### Example

Let's see the usage of this package with an example.

```python
from typing import ClassVar

from nightjar import AutoModule, BaseConifg, BaseModule


class VehicleConfig(BaseConifg, dispatch=["type"]):
    type: ClassVar[str]


class Vehicle(BaseModule):
    config: VehicleConfig


class AutoVehicle(AutoModule):
    def __new__(cls, config: VehicleConfig) -> Vehicle:
        return super().__new__(cls, config)


class CarConfig(VehicleConfig):
    type: ClassVar[str] = "car"


class Car(Vehicle):
    config: CarConfig


class VanConfig(VehicleConfig):
    type: ClassVar[str] = "van"


class Van(Vehicle):
    config: VanConfig
```

### Explanation

```mermaid
classDiagram
    class BaseConfig {
        <<abstract>>
        +dispatch: ClassVar[str | List[str]]
        +from_dict(cls, data: Dict[str, Any]) -> BaseConfig
        +to_dict(self) -> Dict[str, Any]
    }
    class BaseModule {
        <<abstract>>
        +config: BaseConfig
    }
    class AutoModule {
        +__new__(cls, config: BaseConfig) -> BaseModule
    }
    class VehicleConfig {
        +type: ClassVar[str]
    }
    class Vehicle {
        +config: VehicleConfig
    }
    class AutoVehicle {
        +__new__(cls, config: VehicleConfig) -> Vehicle
    }
    class CarConfig {
        +type: ClassVar[str] = "car"
    }
    class Car {
        +config: CarConfig
    }
    class VanConfig {
        +type: ClassVar[str] = "van"
    }
    class Van {
        +config: VanConfig
    }
    BaseConfig <|-- VehicleConfig
    BaseModule <|-- Vehicle
    AutoModule <|-- AutoVehicle
    VehicleConfig <|-- CarConfig
    VehicleConfig <|-- VanConfig
    Vehicle <|-- Car
    Vehicle <|-- Van
```

This package provides a base class `BaseModule` that can be subclassed to create different types of objects. Each object type is defined by a configuration class that inherits from `BaseConfig`. The `AutoModule` class is used to automatically create instances of the correct object type based on the configuration. The `dispatch` attribute of the configuration class is used to specify the static attribute that determines the object type.

`AutoModule` is a generic class that takes the configuration object as init argument and returns an instance of the correct object type. The `__new__` method is used to create the correct object type based on the configuration. It is not necessary to define the `__new__` method in the subclass of `AutoModule` or to subclass `AutoModule` at all. The `AutoModule` class can be used directly to create instances of the correct object type. However, subclassing `AutoModule` can be useful to add additional functionality or to customize the creation of objects and for type hinting.

```python
# use from_dict method to create a configuration object from a dictionary this will automatically create the correct jar config.
config = VehicleConfig.from_dict({"type": "car"})
# Now you can create a car object using the configuration object with Auto* object
car = AutoVehicle(config)
# Now you can access the config object
assert car.config.type == "car", f"expected 'car', got '{car.config.type}'"
```

You could use `AutoJar`, `Jar`, `JarConfig` instead of `AutoModule`, `BaseModule`, `BaseConfig` respectively if you prefer.

## License
Please see the [MIT License](LICENSE) file for details.
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "nightjar",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "dispatch, type-hinting, typing",
    "author": null,
    "author_email": "Yasas Senarath <email@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/76/48/3d7ea4e0e3e4dbe9e2e3f5d42c0adf53e8b8a72c928b356402fe54f7bfc1/nightjar-0.0.5.tar.gz",
    "platform": null,
    "description": "# Nightjar\n\n[![PyPI version](https://badge.fury.io/py/nightjar.svg)](https://badge.fury.io/py/nightjar) \n![PyPI - Downloads](https://img.shields.io/pypi/dm/nightjar)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nightjar)\n![GitHub](https://img.shields.io/github/license/ysenarath/nightjar)\n\n## Description\n\nThis project is a Python package that provides a simple way to create objects of different types based on a configuration object. The package is inspired by how `huggingface/transformers` package creates different types of models based on a configuration object. The package provides a base class `BaseModule` that can be subclassed to create different class types. Each class type is defined by a configuration class that inherits from `BaseConfig`. The `AutoModule` class is used to automatically create instances of the correct object type based on the configuration. The `dispatch` attribute of the configuration class is used to specify the static attribute that determines the object type.\n\n## Installation\n\nTo install this package, run the following command:\n\n```bash\npip install nightjar\n```\n\n## Usage\n\n### Example\n\nLet's see the usage of this package with an example.\n\n```python\nfrom typing import ClassVar\n\nfrom nightjar import AutoModule, BaseConifg, BaseModule\n\n\nclass VehicleConfig(BaseConifg, dispatch=[\"type\"]):\n    type: ClassVar[str]\n\n\nclass Vehicle(BaseModule):\n    config: VehicleConfig\n\n\nclass AutoVehicle(AutoModule):\n    def __new__(cls, config: VehicleConfig) -> Vehicle:\n        return super().__new__(cls, config)\n\n\nclass CarConfig(VehicleConfig):\n    type: ClassVar[str] = \"car\"\n\n\nclass Car(Vehicle):\n    config: CarConfig\n\n\nclass VanConfig(VehicleConfig):\n    type: ClassVar[str] = \"van\"\n\n\nclass Van(Vehicle):\n    config: VanConfig\n```\n\n### Explanation\n\n```mermaid\nclassDiagram\n    class BaseConfig {\n        <<abstract>>\n        +dispatch: ClassVar[str | List[str]]\n        +from_dict(cls, data: Dict[str, Any]) -> BaseConfig\n        +to_dict(self) -> Dict[str, Any]\n    }\n    class BaseModule {\n        <<abstract>>\n        +config: BaseConfig\n    }\n    class AutoModule {\n        +__new__(cls, config: BaseConfig) -> BaseModule\n    }\n    class VehicleConfig {\n        +type: ClassVar[str]\n    }\n    class Vehicle {\n        +config: VehicleConfig\n    }\n    class AutoVehicle {\n        +__new__(cls, config: VehicleConfig) -> Vehicle\n    }\n    class CarConfig {\n        +type: ClassVar[str] = \"car\"\n    }\n    class Car {\n        +config: CarConfig\n    }\n    class VanConfig {\n        +type: ClassVar[str] = \"van\"\n    }\n    class Van {\n        +config: VanConfig\n    }\n    BaseConfig <|-- VehicleConfig\n    BaseModule <|-- Vehicle\n    AutoModule <|-- AutoVehicle\n    VehicleConfig <|-- CarConfig\n    VehicleConfig <|-- VanConfig\n    Vehicle <|-- Car\n    Vehicle <|-- Van\n```\n\nThis package provides a base class `BaseModule` that can be subclassed to create different types of objects. Each object type is defined by a configuration class that inherits from `BaseConfig`. The `AutoModule` class is used to automatically create instances of the correct object type based on the configuration. The `dispatch` attribute of the configuration class is used to specify the static attribute that determines the object type.\n\n`AutoModule` is a generic class that takes the configuration object as init argument and returns an instance of the correct object type. The `__new__` method is used to create the correct object type based on the configuration. It is not necessary to define the `__new__` method in the subclass of `AutoModule` or to subclass `AutoModule` at all. The `AutoModule` class can be used directly to create instances of the correct object type. However, subclassing `AutoModule` can be useful to add additional functionality or to customize the creation of objects and for type hinting.\n\n```python\n# use from_dict method to create a configuration object from a dictionary this will automatically create the correct jar config.\nconfig = VehicleConfig.from_dict({\"type\": \"car\"})\n# Now you can create a car object using the configuration object with Auto* object\ncar = AutoVehicle(config)\n# Now you can access the config object\nassert car.config.type == \"car\", f\"expected 'car', got '{car.config.type}'\"\n```\n\nYou could use `AutoJar`, `Jar`, `JarConfig` instead of `AutoModule`, `BaseModule`, `BaseConfig` respectively if you prefer.\n\n## License\nPlease see the [MIT License](LICENSE) file for details.",
    "bugtrack_url": null,
    "license": null,
    "summary": "A typing based dispatching library.",
    "version": "0.0.5",
    "project_urls": {
        "Documentation": "https://github.com/ysenarath/nightjar",
        "Source": "https://github.com/ysenarath/nightjar"
    },
    "split_keywords": [
        "dispatch",
        " type-hinting",
        " typing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "637efdf781640c61fc75c9a93ba38d1fca4404ee44637a0df35a94cfc4603c1a",
                "md5": "b34650572092bc52944284afb4ecc5b7",
                "sha256": "0d2f4d6a38a7ea82ce4f581c62195ab41b02322e445e8859598c9a0c9e90668a"
            },
            "downloads": -1,
            "filename": "nightjar-0.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b34650572092bc52944284afb4ecc5b7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8594,
            "upload_time": "2024-10-13T13:38:59",
            "upload_time_iso_8601": "2024-10-13T13:38:59.033866Z",
            "url": "https://files.pythonhosted.org/packages/63/7e/fdf781640c61fc75c9a93ba38d1fca4404ee44637a0df35a94cfc4603c1a/nightjar-0.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "76483d7ea4e0e3e4dbe9e2e3f5d42c0adf53e8b8a72c928b356402fe54f7bfc1",
                "md5": "f5f6af0f2247345e635ec8d9289f5471",
                "sha256": "2504acf120992e8707aee1168a9a8bd6ec9b5ddad27c98561ec78c608f0f00c5"
            },
            "downloads": -1,
            "filename": "nightjar-0.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "f5f6af0f2247345e635ec8d9289f5471",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 9948,
            "upload_time": "2024-10-13T13:39:00",
            "upload_time_iso_8601": "2024-10-13T13:39:00.492854Z",
            "url": "https://files.pythonhosted.org/packages/76/48/3d7ea4e0e3e4dbe9e2e3f5d42c0adf53e8b8a72c928b356402fe54f7bfc1/nightjar-0.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-13 13:39:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ysenarath",
    "github_project": "nightjar",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "nightjar"
}
        
Elapsed time: 8.62980s