A lightweight dependency injection library for Python
Project description
Simple Inject
Simple Inject is a lightweight dependency injection library for Python. It provides an easy-to-use interface for managing dependencies across different namespaces and scopes.
Features
- Simple and intuitive API for dependency injection
- Support for multiple namespaces to isolate dependencies
- Scoped dependencies using context managers or decorators
- Nested scopes for fine-grained control
- Easy to integrate with existing projects
- Minimal overhead and dependencies
Installation
You can install Simple Inject using pip:
pip install py-simple-inject
Quick Start
Here's a simple example to get you started:
from simple_inject import provide, inject, create_scope
# Provide a dependency
provide('config', {'debug': True})
# Inject a dependency
config = inject('config')
print(config['debug']) # Output: True
# Use scopes to manage dependencies
with create_scope():
provide('config', {'debug': False})
config = inject('config')
print(config['debug']) # Output: False
# Outside the scope, the original value is preserved
config = inject('config')
print(config['debug']) # Output: True
API Reference
provide(key: str, value: Any, namespace: str = 'default')
Provide a dependency in the current context.
inject(key: str, namespace: str = 'default') -> Any
Inject a dependency from the current context.
create_scope()
Create a new dependency scope. Use with a with
statement.
scoped()
A decorator to create a new dependency scope for a function.
purge(namespace: Optional[str] = None)
Purge dependencies, either from a specific namespace or all namespaces.
Advanced Usage
Using Namespaces
provide('key', 'value1', namespace='ns1')
provide('key', 'value2', namespace='ns2')
print(inject('key', namespace='ns1')) # Output: value1
print(inject('key', namespace='ns2')) # Output: value2
Nested Scopes
provide('key', 'outer')
with create_scope():
provide('key', 'inner')
print(inject('key')) # Output: inner
with create_scope():
provide('key', 'innermost')
print(inject('key')) # Output: innermost
print(inject('key')) # Output: inner
print(inject('key')) # Output: outer
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for py_simple_inject-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4b88de9f84923e803995ba25cfd52702c7fb600a9613abb87a8ce7906a6240c |
|
MD5 | 4a33bcdc049d34c61def830d85b80066 |
|
BLAKE2b-256 | 73fa6ae36470774968ed7070bd1472c983a1d50640895a1cebf0adc6a41c3e68 |