# Simple Inject
[中文 README](README_zh.md)
Simple Inject is a lightweight Python dependency injection library. It provides an easy-to-use interface for managing dependencies across different namespaces and scopes.
## Features
- Simple and intuitive dependency injection API
- Supports multiple namespaces to isolate dependencies
- Implements scoped dependencies using context managers or decorators
- Supports nested scopes for fine-grained control
- Supports automatic dependency injection through parameters
- Easy integration with existing projects
- Minimal overhead and dependencies
## Installation
You can install Simple Inject using pip:
```
pip install py-simple-inject
```
## Quick Start
### Basic Usage
Here is a simple example demonstrating basic dependency injection and scope management:
```python
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
```
### Using Namespaces
```py
from simple_inject import provide, inject, create_scope
provide('key', 'value1', namespace='ns1')
provide('key', 'value2', namespace='ns2')
print(inject('key', namespace='ns1')) # Output: value1
print(inject('key', namespace='ns2')) # Output: value2
```
### Using Scopes
```py
provide('config', {'debug': 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
```
Scopes can also be used with the `scoped` decorator:
```py
@scoped()
def scoped_function():
provide('key', 'scoped_value')
return inject('key')
provide('key', 'outer_value')
print(inject('key')) # Output: outer_value
print(scoped_function()) # Output: scoped_value
print(inject('key')) # Output: outer_value
```
### Nested Scopes
Scoped scopes can be nested, and dependencies in inner scopes will override those in outer scopes.
```python
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
```
### Automatic Injection via Function Parameters
Simple Inject also supports automatic injection via function parameters. The following example demonstrates how to use this advanced feature:
```python
from simple_inject import provide, inject, create_scope, auto_inject, Inject
class Engine:
def start(self):
print("Engine started")
# Provide a dependency
provide('engine', Engine())
# Manually inject a dependency
engine = inject('engine')
engine.start() # Output: Engine started
# Use automatic injection
@auto_inject()
def drive(car: str, engine: Engine = Inject('engine')):
print(f"Driving {car}")
engine.start()
drive("Tesla") # Output: Driving Tesla and Engine started
# Use scopes to manage dependencies
with create_scope():
provide('engine', Engine()) # Provide a new Engine instance
drive("BMW") # Output: Driving BMW and Engine started
# Outside the scope, the original value is preserved
drive("Toyota") # Output: Driving Toyota and Engine started
```
## API Reference
### `provide(key: str, value: Any, namespace: str = 'default')`
Provides a dependency in the current context.
### `inject(key: str, namespace: str = 'default') -> Any`
Injects a dependency from the current context.
### `create_scope()`
Creates a new dependency scope. Used with the `with` statement.
### `scoped()`
Decorator to create a new dependency scope for a function.
### `auto_inject()`
Decorator to automatically inject parameters marked with `Inject`.
### `Inject(key: str, namespace: str = 'default')`
Class to mark a parameter for automatic injection.
### `purge(namespace: Optional[str] = None)`
Clears dependencies, either for a specific namespace or for all namespaces.
## Contributing
Contributions are welcome! Feel free to submit a Pull Request.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "py-simple-inject",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "dependency injection, DI, IoC, inversion of control",
"author": null,
"author_email": "frostime <frostime@foxmail.com>",
"download_url": "https://files.pythonhosted.org/packages/a8/10/71bca10562f26b2d1c9a436761ed450edb85bf016f3d3b2e22b48b513523/py_simple_inject-0.5.1.tar.gz",
"platform": null,
"description": "# Simple Inject\n\n[\u4e2d\u6587 README](README_zh.md)\n\nSimple Inject is a lightweight Python dependency injection library. It provides an easy-to-use interface for managing dependencies across different namespaces and scopes.\n\n## Features\n\n- Simple and intuitive dependency injection API\n- Supports multiple namespaces to isolate dependencies\n- Implements scoped dependencies using context managers or decorators\n- Supports nested scopes for fine-grained control\n- Supports automatic dependency injection through parameters\n- Easy integration with existing projects\n- Minimal overhead and dependencies\n\n## Installation\n\nYou can install Simple Inject using pip:\n\n```\npip install py-simple-inject\n```\n\n## Quick Start\n\n### Basic Usage\n\nHere is a simple example demonstrating basic dependency injection and scope management:\n\n```python\nfrom simple_inject import provide, inject, create_scope\n\n# Provide a dependency\nprovide('config', {'debug': True})\n\n# Inject a dependency\nconfig = inject('config')\nprint(config['debug']) # Output: True\n```\n\n### Using Namespaces\n\n```py\nfrom simple_inject import provide, inject, create_scope\n\nprovide('key', 'value1', namespace='ns1')\nprovide('key', 'value2', namespace='ns2')\n\nprint(inject('key', namespace='ns1')) # Output: value1\nprint(inject('key', namespace='ns2')) # Output: value2\n```\n\n### Using Scopes\n\n```py\nprovide('config', {'debug': True})\n\n# Use scopes to manage dependencies\nwith create_scope():\n provide('config', {'debug': False})\n config = inject('config')\n print(config['debug']) # Output: False\n\n# Outside the scope, the original value is preserved\nconfig = inject('config')\nprint(config['debug']) # Output: True\n```\n\nScopes can also be used with the `scoped` decorator:\n\n```py\n@scoped()\ndef scoped_function():\n provide('key', 'scoped_value')\n return inject('key')\n\nprovide('key', 'outer_value')\nprint(inject('key')) # Output: outer_value\nprint(scoped_function()) # Output: scoped_value\nprint(inject('key')) # Output: outer_value\n```\n\n### Nested Scopes\n\nScoped scopes can be nested, and dependencies in inner scopes will override those in outer scopes.\n\n```python\nprovide('key', 'outer')\n\nwith create_scope():\n provide('key', 'inner')\n print(inject('key')) # Output: inner\n\n with create_scope():\n provide('key', 'innermost')\n print(inject('key')) # Output: innermost\n\n print(inject('key')) # Output: inner\n\nprint(inject('key')) # Output: outer\n```\n\n### Automatic Injection via Function Parameters\n\nSimple Inject also supports automatic injection via function parameters. The following example demonstrates how to use this advanced feature:\n\n```python\nfrom simple_inject import provide, inject, create_scope, auto_inject, Inject\n\nclass Engine:\n def start(self):\n print(\"Engine started\")\n\n# Provide a dependency\nprovide('engine', Engine())\n\n# Manually inject a dependency\nengine = inject('engine')\nengine.start() # Output: Engine started\n\n# Use automatic injection\n@auto_inject()\ndef drive(car: str, engine: Engine = Inject('engine')):\n print(f\"Driving {car}\")\n engine.start()\n\ndrive(\"Tesla\") # Output: Driving Tesla and Engine started\n\n# Use scopes to manage dependencies\nwith create_scope():\n provide('engine', Engine()) # Provide a new Engine instance\n drive(\"BMW\") # Output: Driving BMW and Engine started\n\n# Outside the scope, the original value is preserved\ndrive(\"Toyota\") # Output: Driving Toyota and Engine started\n```\n\n## API Reference\n\n### `provide(key: str, value: Any, namespace: str = 'default')`\n\nProvides a dependency in the current context.\n\n### `inject(key: str, namespace: str = 'default') -> Any`\n\nInjects a dependency from the current context.\n\n### `create_scope()`\n\nCreates a new dependency scope. Used with the `with` statement.\n\n### `scoped()`\n\nDecorator to create a new dependency scope for a function.\n\n### `auto_inject()`\n\nDecorator to automatically inject parameters marked with `Inject`.\n\n### `Inject(key: str, namespace: str = 'default')`\n\nClass to mark a parameter for automatic injection.\n\n### `purge(namespace: Optional[str] = None)`\n\nClears dependencies, either for a specific namespace or for all namespaces.\n\n## Contributing\n\nContributions are welcome! Feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.",
"bugtrack_url": null,
"license": "MIT",
"summary": "A lightweight dependency injection library for Python",
"version": "0.5.1",
"project_urls": {
"Bug tracker": "https://github.com/frostime/simple-inject-py/issues",
"Homepage": "https://github.com/frostime/simple-inject-py"
},
"split_keywords": [
"dependency injection",
" di",
" ioc",
" inversion of control"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d02f28ff263d84d4335bf58007312c4390ad10450b2bfa6cd34ca05d5c53f162",
"md5": "ec7de7ec8fa7f6d574905e18fad0754c",
"sha256": "53d017b181bce81bea62ed6bd8a19cb83f42827ccc04eed96de1b03c3f1003e8"
},
"downloads": -1,
"filename": "py_simple_inject-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ec7de7ec8fa7f6d574905e18fad0754c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6072,
"upload_time": "2024-10-14T14:35:03",
"upload_time_iso_8601": "2024-10-14T14:35:03.999548Z",
"url": "https://files.pythonhosted.org/packages/d0/2f/28ff263d84d4335bf58007312c4390ad10450b2bfa6cd34ca05d5c53f162/py_simple_inject-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a81071bca10562f26b2d1c9a436761ed450edb85bf016f3d3b2e22b48b513523",
"md5": "234dd91be3d272f46a13de7e2c26b194",
"sha256": "b52cf0fb63fcb991c4a79ed4a8f54dd5cc2cbaf89868eb966631f456dc2806f6"
},
"downloads": -1,
"filename": "py_simple_inject-0.5.1.tar.gz",
"has_sig": false,
"md5_digest": "234dd91be3d272f46a13de7e2c26b194",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 7871,
"upload_time": "2024-10-14T14:35:05",
"upload_time_iso_8601": "2024-10-14T14:35:05.179727Z",
"url": "https://files.pythonhosted.org/packages/a8/10/71bca10562f26b2d1c9a436761ed450edb85bf016f3d3b2e22b48b513523/py_simple_inject-0.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-14 14:35:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "frostime",
"github_project": "simple-inject-py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "py-simple-inject"
}