# Simple Inject
[中文文档](README_zh.md)
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:
```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
# 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
```python
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
```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
```
## Contributing
Contributions are welcome! Please 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/9f/86/0311b953a05de402d3bd1ebe7c1c6ad372457f950650ea95c3b7baec8650/py_simple_inject-0.3.0.tar.gz",
"platform": null,
"description": "# Simple Inject\n\n[\u4e2d\u6587\u6587\u6863](README_zh.md)\n\nSimple Inject is a lightweight dependency injection library for Python. It provides an easy-to-use interface for managing dependencies across different namespaces and scopes.\n\n## Features\n\n- Simple and intuitive API for dependency injection\n- Support for multiple namespaces to isolate dependencies\n- Scoped dependencies using context managers or decorators\n- Nested scopes for fine-grained control\n- Easy to integrate 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\nHere's a simple example to get you started:\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# 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\n## API Reference\n\n### `provide(key: str, value: Any, namespace: str = 'default')`\n\nProvide a dependency in the current context.\n\n### `inject(key: str, namespace: str = 'default') -> Any`\n\nInject a dependency from the current context.\n\n### `create_scope()`\n\nCreate a new dependency scope. Use with a `with` statement.\n\n### `scoped()`\n\nA decorator to create a new dependency scope for a function.\n\n### `purge(namespace: Optional[str] = None)`\n\nPurge dependencies, either from a specific namespace or all namespaces.\n\n## Advanced Usage\n\n### Using Namespaces\n\n```python\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### Nested 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## Contributing\n\nContributions are welcome! Please 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.3.0",
"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": "73fa6ae36470774968ed7070bd1472c983a1d50640895a1cebf0adc6a41c3e68",
"md5": "4a33bcdc049d34c61def830d85b80066",
"sha256": "a4b88de9f84923e803995ba25cfd52702c7fb600a9613abb87a8ce7906a6240c"
},
"downloads": -1,
"filename": "py_simple_inject-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4a33bcdc049d34c61def830d85b80066",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 4759,
"upload_time": "2024-09-14T06:04:45",
"upload_time_iso_8601": "2024-09-14T06:04:45.158586Z",
"url": "https://files.pythonhosted.org/packages/73/fa/6ae36470774968ed7070bd1472c983a1d50640895a1cebf0adc6a41c3e68/py_simple_inject-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9f860311b953a05de402d3bd1ebe7c1c6ad372457f950650ea95c3b7baec8650",
"md5": "10e5fcab68cab6a0c9a78449f4158bab",
"sha256": "c6cfb6726ce278c253d93816af1b029dd98dcc261a5ab032b632c8189dcb9c72"
},
"downloads": -1,
"filename": "py_simple_inject-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "10e5fcab68cab6a0c9a78449f4158bab",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 5389,
"upload_time": "2024-09-14T06:04:46",
"upload_time_iso_8601": "2024-09-14T06:04:46.359452Z",
"url": "https://files.pythonhosted.org/packages/9f/86/0311b953a05de402d3bd1ebe7c1c6ad372457f950650ea95c3b7baec8650/py_simple_inject-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-14 06:04:46",
"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"
}