Skip to main content

A typing based dispatching library.

Project description

Nightjar

PyPI version PyPI - Downloads PyPI - Python Version GitHub

Description

This project is a Python package that ...

Installation

To install this package, run the following command:

pip install nightjar

Usage

To use this package, you can import the package and use it as follows:

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

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.

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

nightjar-0.0.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nightjar-0.0.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file nightjar-0.0.1.tar.gz.

File metadata

  • Download URL: nightjar-0.0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for nightjar-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a5bb32787bfc5d722e3df66926e5e5afeb237093ac9445fd8b9cb155b28db3e4
MD5 4eb87f6324a0542f9ae055dd18dc0ddd
BLAKE2b-256 25d252ce185dd7169156fb2396c7a8408c647e0e760cfd20f0ff24d1ad0ce931

See more details on using hashes here.

File details

Details for the file nightjar-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: nightjar-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for nightjar-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77eb95e643063355c7bbc4847efac7df705b86e74b02a84603246ebd29da26eb
MD5 240a759b0f3da38e7efbff6d38391f40
BLAKE2b-256 68ad715bda51fa6b6efb9f4915bdceeb88e1796afce55d72e650e5e84206c418

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page