Dependency injection like FastAPI.
Project description
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 values will be values of the variable given by Container.configure
and 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
Release history Release notifications | RSS feed
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.1.tar.gz
(5.3 kB
view details)
Built Distribution
File details
Details for the file finjet-0.1.1.tar.gz
.
File metadata
- Download URL: finjet-0.1.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1808192ade2835ad93da1f7f257d76fc86b2b593d18b63e9a95b769c25fb14eb |
|
MD5 | 7e3afe681819fc12cb510134ca06addc |
|
BLAKE2b-256 | b0b51d5a2fd8a9868b08cc3ac9eebba6fad06a636fa9746a610a75bff8a9388d |
File details
Details for the file finjet-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: finjet-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f3d425a9169d88d22c70acf300d9a87869598374d6ebc459a63ee98d16c8b59 |
|
MD5 | 72edb670e27a445ad64087f7b827c190 |
|
BLAKE2b-256 | ceafa94e0e69c4c3a5f81d613f9683649ff5392d1d585c5f078911709d97bf23 |