Skip to main content

Nightjar

PyPI version PyPI - Downloads PyPI - Python Version GitHub

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:

pip install nightjar

Usage

Example

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

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

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.

# 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 file for details.

Metadata

Release files for nightjar 0.0.2

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

Source distribution (sdist)

Source distribution for nightjar 0.0.2
File Size Uploaded
nightjar-0.0.2.tar.gz 9.9 kB Details

Built distribution (wheel)

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

Total release size: 18.5 kB

Release files / nightjar-0.0.2.tar.gz

Download URL nightjar-0.0.2.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fd43ada1c0627f7365b11162b22775d44c0e594515f5031239cf859a2e8c6a3b
BLAKE2b-256 checksum
How to use checksums
1a449c80e29f77c87d5951e490349faffccdf08f7253f7b1c38d08d51582138b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Release files / nightjar-0.0.2-py3-none-any.whl

Download URL nightjar-0.0.2-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10fa1459ab7bd3e01356171543f57e1d783808bcca0f5306d52b28bb621b20ac
BLAKE2b-256 checksum
How to use checksums
1c707276ff749c005107a2f46b7880ba770fde71c21ed4688040c8aac7750533
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Release history Release notifications | RSS feed

0.1.1

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

This release

0.0.2 This release

2 release files

0.0.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