plugable dependency injection and result processing
Project description
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) -> 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:
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
in-n-out-0.1.5.tar.gz
(33.9 kB
view details)
Built Distribution
in_n_out-0.1.5-py3-none-any.whl
(19.1 kB
view details)
File details
Details for the file in-n-out-0.1.5.tar.gz
.
File metadata
- Download URL: in-n-out-0.1.5.tar.gz
- Upload date:
- Size: 33.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4477842a6eca98394e74ab5de45e38461dbe9198045243130573805131a4fdb0 |
|
MD5 | 1f3479310759d1ab2010abf426939269 |
|
BLAKE2b-256 | 21fd2603b1190154370bb73018ea4abb182c44dc1bf8cb46823b3588bf64103e |
File details
Details for the file in_n_out-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: in_n_out-0.1.5-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ed85c723059a5fbaef62126b0e24a7427bcaefe6b62bceae93e2d04a85ab33d |
|
MD5 | ef27b90afe3e8926d0140037a89cf657 |
|
BLAKE2b-256 | f3f51987ee25d6213039ba327b420c694286c0a30f4561871ce5cd0978f0d655 |