Skip to main content

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()

Metadata

Release files for finjet 0.1.3

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

Source distribution (sdist)

Source distribution for finjet 0.1.3
File Size Uploaded
finjet-0.1.3.tar.gz 5.5 kB Details

Built distribution (wheel)

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

Total release size: 11.4 kB

Release files / finjet-0.1.3.tar.gz

Download URL finjet-0.1.3.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
83c76e31e0ee06b88f442ed5e043123df5ed746006a43e699b57b460a8d16567
BLAKE2b-256 checksum
How to use checksums
55fe549f4b4b1ad195e810aea37a213c6dad3c213adc100db0d3fc0cd7aaff4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.9 Windows/10

Release files / finjet-0.1.3-py3-none-any.whl

Download URL finjet-0.1.3-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
feca07725d00895158beef9fc232c804ed3f6acbe652377e12d61fcdaa72aecf
BLAKE2b-256 checksum
How to use checksums
8672cc547bcf93eb16c1a0ff067f7f72ea57b4e196dc0d6cca983a0ae10c48cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.9 Windows/10

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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