plugable dependency injection and result processing
Project Links
Meta
Author: Talley Lambert
Requires Python: >=3.8
Classifiers
Development Status
- 3 - Alpha
License
- OSI Approved :: BSD License
Programming Language
- Python :: 3
- Python :: 3.8
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
Typing
- Typed
in-n-out
Python dependency injection you can taste.
A lightweight dependency injection and result processing framework for Python using type hints. Emphasis is on simplicity, ease of use, and minimal impact on source code.
import in_n_out as ino
class Thing:
def __init__(self, name: str):
self.name = name
# use ino.inject to create a version of the function
# that will retrieve the required dependencies at call time
@ino.inject
def func(thing: Thing):
return thing.name
def give_me_a_thing() -> Thing:
return Thing("Thing")
# register a provider of Thing
ino.register_provider(give_me_a_thing)
print(func()) # prints "Thing"
def give_me_another_thing() -> Thing:
return Thing("Another Thing")
with ino.register_provider(give_me_another_thing, weight=10):
print(func()) # prints "Another Thing"
This also supports processing return values as well (injection of intentional side effects):
@ino.inject_processors
def func2(thing: Thing) -> str:
return thing.name
def greet_name(name: str):
print(f"Hello, {name}!")
ino.register_processor(greet_name)
func2(Thing('Bob')) # prints "Hello, Bob!"
Alternatives
Lots of other python DI frameworks exist, here are a few alternatives to consider:
0.2.1
Apr 22, 2024
0.2.0
Mar 18, 2024
0.1.9
Oct 10, 2023
0.1.8
Jul 06, 2023
0.1.7
Feb 27, 2023
0.1.6
Nov 09, 2022
0.1.5
Aug 14, 2022
0.1.4
Aug 12, 2022
0.1.3
Jul 16, 2022
0.1.2
Jul 14, 2022
0.1.1
Jul 06, 2022
0.0.1rc3
Jul 05, 2022
0.0.1rc2
Jul 05, 2022
0.0.1rc1
Jun 30, 2022