Skip to main content

Dependency injection like FastAPI.

Project description

finjet

PyPI version codecov Codacy Badge

Simple dependency injection library like fastapi. It can be used to turn your modules to loosely coupled parts. and configurations to allow you to easily re-use and test your code.

Dependency injection is performed on the arguments given with the Depends function as the default argument. The inserted value will be given values of NamedTuple via Container.configure or the return value of the function.

Installation

Latest PyPI stable release

pip install finjet

Example

from typing import NamedTuple
from finjet import Container, Depends, Singleton, inject


class Config(NamedTuple):
    gear_ratio: int
    tire_r: int = 100


class Engine:
    # gear_ratio will be obtained from `Config`
    def __init__(self, gear_ratio: int = Depends()) -> None:
        self.gear_ratio = gear_ratio


class Tire:
    count = 0

    def __init__(self, tire_r: int = Depends()) -> None:
        Tire.count += 1
        # Actually tire_r is multiplied by instanced number of times.
        self.tire_r = tire_r * Tire.count


def get_rotation_speed(engine: Engine = Depends(Engine)) -> int:
    # Arguments of `Engine` class will inject from dependencies.
    # In this example, the gear_ratio is configured 100 or 50
    return engine.gear_ratio


@inject
def get_tire_speed(
    tire: Tire = Singleton(Tire),
    rpm: int = Depends(get_rotation_speed)
) -> float:
    # Depends is always created such as factory pattern
    # Singleton is only generate at once such as singleton pattern.
    # The singleton object is shared in the Container class.
    return tire.tire_r * rpm


def main():
    container = Container()

    # Configuration of container
    container.configure(Config(100, 100))
    with container:
        print('Speed:', get_tire_speed())  # 10000
        print('#Tire:', Tire.count)  # 1

    # If the configuration value is changed, the displaying value is difference.
    # But `Tire.count` is same so that a second argument, the Tire object is re-used.
    container.configure(Config(20, 100))
    with container:
        print('Speed:', get_tire_speed())  # 2000
        print('#Tire:', Tire.count)  # 1

    # If the configuration value is changed, the displaying value is difference.
    # The `Tire` object is updated.
    container.configure(Config(20, 10))
    with container:
        print('Speed:', get_tire_speed())  # 400
        print('#Tire:', Tire.count)  # 2


if __name__ == '__main__':
    main()

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

finjet-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

finjet-0.1.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file finjet-0.1.2.tar.gz.

File metadata

  • Download URL: finjet-0.1.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10

File hashes

Hashes for finjet-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c8b31922fb40284f2f672abfda97ecdc725b8ce3257f277a895b9f4ef19bd704
MD5 ebb7757826eab2efac1c00d70954e4b6
BLAKE2b-256 095b36e9f8787da6509956e3caba541a7b0c3095c399f63e13d6b3964cabe4b2

See more details on using hashes here.

File details

Details for the file finjet-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: finjet-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10

File hashes

Hashes for finjet-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0dcb6c765d782c0078bc50ac414c52b682bcd8221c95394e5bfd66b1dc4c735f
MD5 1c096205413e9f739f3e98aaf657f2c2
BLAKE2b-256 957c18e207e4acccb4ab39d0851956efa8c4fc51435235e4e33dfce9bb230446

See more details on using hashes here.

Supported by

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