finjet

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:
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
self.tire_r = tire_r * Tire.count
def get_rotation_speed(engine: Engine = Depends(Engine)) -> int:
return engine.gear_ratio
@inject
def get_tire_speed(
tire: Tire = Singleton(Tire),
rpm: int = Depends(get_rotation_speed)
) -> float:
return tire.tire_r * rpm
def main():
container = Container()
container.configure(Config(100, 100))
with container:
print('Speed:', get_tire_speed())
print('#Tire:', Tire.count)
container.configure(Config(20, 100))
with container:
print('Speed:', get_tire_speed())
print('#Tire:', Tire.count)
container.configure(Config(20, 10))
with container:
print('Speed:', get_tire_speed())
print('#Tire:', Tire.count)
if __name__ == '__main__':
main()