Name | sinj JSON |
Version |
0.6.1
JSON |
| download |
home_page | None |
Summary | IoC container |
upload_time | 2025-02-08 11:46:20 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT License
Copyright (c) 2023 Marius Kavaliauskas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|
keywords |
ioc
inverse-of-control
injection
dependency-injection
dependencies-container
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 💉 sinj
`sinj` (**S**imple **Inj**ect) is yet another IoC framework for python. If you try to avoid global variables and singletons you might end up with a complex graph of dependencies in your application. With `sinj` everything is just flat. If you are coming from Java or C# you might be more familiar with IoC frameworks where dependencies are resolved by interface. In python we do not have interfaces and strict types so here we resolve dependencies by constructor argument names or "inject labels".
# Basic usage and examples
```python
import sinj
c = sinj.Container() # create dependencies container
c.register(SomeClass, "some_class") # add labeled class into container
c.resolve("some_class") # resolve instance by given label
c.inject(some_instance, "some_instance") # add resolved instance to an index
```
The simplest example:
```python
import sinj
class A:
def a(self):
return "a"
class B:
def __init__(self, a):
self._a = a
def b(self):
return self._a() + " b"
ioc_container = sinj.Container()
ioc_container.register(A, "a")
ioc_container.register(B, "b")
b = ioc_container.resolve("b")
print(b.b()) # -> "a b"
```
The same example with annotated classes:
```python
import sinj
class A:
inject = "a" # this label will be used to resolve instance of A
def a(self):
return "a"
class B:
inject = "b" # this label will be used to resolve instance of B
def __init__(self, a): # instance of A is injected here
self._a = a
def b(self):
return self._a() + " b"
ioc_container = sinj.Container()
ioc_container.register(A) # no need to specify label here
ioc_container.register(B) # but you can overwrite it if you want
b = ioc_container.resolve("b")
print(b.b()) # -> "a b"
```
More examples will be available in `./examples`
# Errors
- `sinj.DependencyNotFoundError` - thrown on `resolve` when dependency is not found for a given label (and it is not optional).
- `sinj.CircularDependencyError` - thrown on `resolve` when circular dependency is detected.
- `sinj.DependencyConflictError` - thrown on `register` or `inject` when the container already has something by the label.
- `sinj.DependencyNotMappedError` - thrown on `register` or `inject` when the class is not annotated and the label is not provided in register method.
# Install
### From [pypi.org](https://pypi.org/project/sinj/)
```bash
pip install sinj
```
### From [repository](https://gitlab.com/mrsk/sinj)
```bash
pip install git+https://gitlab.com/mrsk/sinj
```
Raw data
{
"_id": null,
"home_page": null,
"name": "sinj",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "ioc, inverse-of-control, injection, dependency-injection, dependencies-container",
"author": null,
"author_email": "Marius Kavaliauskas <mariuskava+sinj@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/67/50/77966bd225eb99bba81d1246ae73dec9bfc9923ef504507fd758fd7c8267/sinj-0.6.1.tar.gz",
"platform": null,
"description": "\n# \ud83d\udc89 sinj\n\n`sinj` (**S**imple **Inj**ect) is yet another IoC framework for python. If you try to avoid global variables and singletons you might end up with a complex graph of dependencies in your application. With `sinj` everything is just flat. If you are coming from Java or C# you might be more familiar with IoC frameworks where dependencies are resolved by interface. In python we do not have interfaces and strict types so here we resolve dependencies by constructor argument names or \"inject labels\".\n\n# Basic usage and examples\n\n```python\nimport sinj\nc = sinj.Container() # create dependencies container\nc.register(SomeClass, \"some_class\") # add labeled class into container\nc.resolve(\"some_class\") # resolve instance by given label\nc.inject(some_instance, \"some_instance\") # add resolved instance to an index\n```\n\n\nThe simplest example:\n\n```python\nimport sinj\n\nclass A:\n def a(self):\n return \"a\"\n\nclass B:\n def __init__(self, a):\n self._a = a\n \n def b(self):\n return self._a() + \" b\"\n\nioc_container = sinj.Container()\nioc_container.register(A, \"a\")\nioc_container.register(B, \"b\")\n\nb = ioc_container.resolve(\"b\")\nprint(b.b()) # -> \"a b\"\n```\n\nThe same example with annotated classes:\n\n```python\nimport sinj\n\nclass A:\n inject = \"a\" # this label will be used to resolve instance of A\n def a(self):\n return \"a\"\n\nclass B:\n inject = \"b\" # this label will be used to resolve instance of B\n def __init__(self, a): # instance of A is injected here\n self._a = a\n \n def b(self):\n return self._a() + \" b\"\n\nioc_container = sinj.Container()\nioc_container.register(A) # no need to specify label here\nioc_container.register(B) # but you can overwrite it if you want\n\nb = ioc_container.resolve(\"b\")\nprint(b.b()) # -> \"a b\"\n```\n\nMore examples will be available in `./examples`\n\n\n# Errors\n\n\n- `sinj.DependencyNotFoundError` - thrown on `resolve` when dependency is not found for a given label (and it is not optional).\n- `sinj.CircularDependencyError` - thrown on `resolve` when circular dependency is detected.\n- `sinj.DependencyConflictError` - thrown on `register` or `inject` when the container already has something by the label.\n- `sinj.DependencyNotMappedError` - thrown on `register` or `inject` when the class is not annotated and the label is not provided in register method.\n\n\n# Install\n\n\n### From [pypi.org](https://pypi.org/project/sinj/)\n\n```bash\npip install sinj\n```\n\n### From [repository](https://gitlab.com/mrsk/sinj)\n\n```bash\npip install git+https://gitlab.com/mrsk/sinj\n```\n\n",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2023 Marius Kavaliauskas\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n ",
"summary": "IoC container",
"version": "0.6.1",
"project_urls": {
"Homepage": "https://gitlab.com/mrsk/sinj",
"Issue Tracker": "https://gitlab.com/mrsk/sinj/-/issues",
"Source Code": "https://gitlab.com/mrsk/sinj"
},
"split_keywords": [
"ioc",
" inverse-of-control",
" injection",
" dependency-injection",
" dependencies-container"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "ad6301e7e76ab857a9039b25bef8878bb791c7643279812b64fde9882e5b5a88",
"md5": "7ae591e4a4abe47a445c5f6a4535c5f9",
"sha256": "d3f3b601686b897c5c506f02f9fa1f876adbb267137151dc245d3606e059f09b"
},
"downloads": -1,
"filename": "sinj-0.6.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7ae591e4a4abe47a445c5f6a4535c5f9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 5399,
"upload_time": "2025-02-08T11:46:18",
"upload_time_iso_8601": "2025-02-08T11:46:18.996456Z",
"url": "https://files.pythonhosted.org/packages/ad/63/01e7e76ab857a9039b25bef8878bb791c7643279812b64fde9882e5b5a88/sinj-0.6.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "675077966bd225eb99bba81d1246ae73dec9bfc9923ef504507fd758fd7c8267",
"md5": "4f1199ebc50b8431c9b563c5d90e63b3",
"sha256": "24bd7e5958a6fb77169da62b04cf097e7bd28027f04f92b228bc5eb70fca876f"
},
"downloads": -1,
"filename": "sinj-0.6.1.tar.gz",
"has_sig": false,
"md5_digest": "4f1199ebc50b8431c9b563c5d90e63b3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 5299,
"upload_time": "2025-02-08T11:46:20",
"upload_time_iso_8601": "2025-02-08T11:46:20.244223Z",
"url": "https://files.pythonhosted.org/packages/67/50/77966bd225eb99bba81d1246ae73dec9bfc9923ef504507fd758fd7c8267/sinj-0.6.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-08 11:46:20",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "mrsk",
"gitlab_project": "sinj",
"lcname": "sinj"
}