smalldi


Namesmalldi JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummarySmall, annotation-driven dependency injection for Python.
upload_time2025-08-29 16:53:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT License Copyright (c) 2025 Anna-Sofia Kasierocka 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 annotations dependency injection di typing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Small DI
The smallest dependency injection mechanism possible in python.
SmallDI provides you with intuitive and simple interface for doing dependency
injections in your project.

# Example usage
```python
import random

from smalldi import Injector
from smalldi.annotation import Provide

# Lets create some service
@Injector.singleton
class MeowService:
    _MEOWS = ["Meow", "Meow-meow", "Meowwwwww", "Mrromeowww", "Meeeeoooow"]
    def __init__(self):
        print("Meow-meow! Meowing service is initialized")
    
    def meow(self):
        print(random.choice(self._MEOWS))

# Now lets make purring service.
# But cats do not purr without telling meow!(at least in this test)
# So its time to inject dependency
@Injector.singleton
class PurrService:
    _PURRS = ["Purrrrr", "Purr-purr"]
    
    @Injector.inject
    def __init__(self, meow_service: Provide[MeowService]):
        self.meow_service = meow_service
        print("Purr-purr! Purring service is initialized")

    def purr(self):
        self.meow_service.meow()
        print(random.choice(self._PURRS))

# Now lets put it all together. 
# Ask our services to meow and then purr
@Injector.inject
def main(meow_service: Provide[MeowService], purr_service: Provide[PurrService]):
    meow_service.meow()
    purr_service.purr()

if __name__ == '__main__':
    main()
```

# Library logic
## Injector
Injector is a static class(i.e., one that should never be instantiated) which is the main (and currently the only)
DI container inside the library. Injector provides two decorators:
* `@Injector.singleton` creates an instance of a class which may further be injected in functions
* `@Injector.inject` replaces parameters annotated with type `Provide[Singleton]` with actual instances of Singleton

### Singletons
Singletons are classes having a single instance. In `smalldi` singletons may not take constructor(`__init__`) other
than annotated with `Provide[]` type. Only singletons may be decorated with `@Injector.singleton`. As a consequence, 
only singleton classes may be injected at the current state of library development.

## Provide
`Provide[T]` is an annotation for injector telling it that instead of this argument
instance of `T` should be passed. Caller of function with `Provide[T]` may explicitly
override argument annotated with `Provide[T]` by directly passing `annotated_di_arg=my_value`.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "smalldi",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "annotations, dependency injection, di, typing",
    "author": null,
    "author_email": "Anna-Sofia Kasierocka <f104a@f104a.io>",
    "download_url": "https://files.pythonhosted.org/packages/4c/a4/f2fbca4458b4810e3d79c7ae1d474cb5ffd2174e34718af15271599215b1/smalldi-0.1.1.tar.gz",
    "platform": null,
    "description": "# Small DI\nThe smallest dependency injection mechanism possible in python.\nSmallDI provides you with intuitive and simple interface for doing dependency\ninjections in your project.\n\n# Example usage\n```python\nimport random\n\nfrom smalldi import Injector\nfrom smalldi.annotation import Provide\n\n# Lets create some service\n@Injector.singleton\nclass MeowService:\n    _MEOWS = [\"Meow\", \"Meow-meow\", \"Meowwwwww\", \"Mrromeowww\", \"Meeeeoooow\"]\n    def __init__(self):\n        print(\"Meow-meow! Meowing service is initialized\")\n    \n    def meow(self):\n        print(random.choice(self._MEOWS))\n\n# Now lets make purring service.\n# But cats do not purr without telling meow!(at least in this test)\n# So its time to inject dependency\n@Injector.singleton\nclass PurrService:\n    _PURRS = [\"Purrrrr\", \"Purr-purr\"]\n    \n    @Injector.inject\n    def __init__(self, meow_service: Provide[MeowService]):\n        self.meow_service = meow_service\n        print(\"Purr-purr! Purring service is initialized\")\n\n    def purr(self):\n        self.meow_service.meow()\n        print(random.choice(self._PURRS))\n\n# Now lets put it all together. \n# Ask our services to meow and then purr\n@Injector.inject\ndef main(meow_service: Provide[MeowService], purr_service: Provide[PurrService]):\n    meow_service.meow()\n    purr_service.purr()\n\nif __name__ == '__main__':\n    main()\n```\n\n# Library logic\n## Injector\nInjector is a static class(i.e., one that should never be instantiated) which is the main (and currently the only)\nDI container inside the library. Injector provides two decorators:\n* `@Injector.singleton` creates an instance of a class which may further be injected in functions\n* `@Injector.inject` replaces parameters annotated with type `Provide[Singleton]` with actual instances of Singleton\n\n### Singletons\nSingletons are classes having a single instance. In `smalldi` singletons may not take constructor(`__init__`) other\nthan annotated with `Provide[]` type. Only singletons may be decorated with `@Injector.singleton`. As a consequence, \nonly singleton classes may be injected at the current state of library development.\n\n## Provide\n`Provide[T]` is an annotation for injector telling it that instead of this argument\ninstance of `T` should be passed. Caller of function with `Provide[T]` may explicitly\noverride argument annotated with `Provide[T]` by directly passing `annotated_di_arg=my_value`.\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 Anna-Sofia Kasierocka\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.",
    "summary": "Small, annotation-driven dependency injection for Python.",
    "version": "0.1.1",
    "project_urls": {
        "Documentation": "https://github.com/0xf104a/smalldi#readme",
        "Homepage": "https://github.com/0xf104a/smalldi",
        "Issues": "https://github.com/0xf104a/smalldi/issues",
        "Source": "https://github.com/0xf104a/smalldi"
    },
    "split_keywords": [
        "annotations",
        " dependency injection",
        " di",
        " typing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "03950091c4fef0736222558a85583a9ef851d8c80b0a27a1ca2eae70a830ed0a",
                "md5": "515ae48139e1634e07d273cea4c00c6d",
                "sha256": "c3dd7c88586ebe353aa24907ad5e8a7a315e4fad48139d60c84f34dfbdb0ec27"
            },
            "downloads": -1,
            "filename": "smalldi-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "515ae48139e1634e07d273cea4c00c6d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 5637,
            "upload_time": "2025-08-29T16:53:33",
            "upload_time_iso_8601": "2025-08-29T16:53:33.336032Z",
            "url": "https://files.pythonhosted.org/packages/03/95/0091c4fef0736222558a85583a9ef851d8c80b0a27a1ca2eae70a830ed0a/smalldi-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4ca4f2fbca4458b4810e3d79c7ae1d474cb5ffd2174e34718af15271599215b1",
                "md5": "b544f7609e14df24d52aa8a55433a9df",
                "sha256": "44111865bcf52b813b7ab0a1991a3ba25a2308c0c9abb60c1145ea945c3cc729"
            },
            "downloads": -1,
            "filename": "smalldi-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b544f7609e14df24d52aa8a55433a9df",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 6185,
            "upload_time": "2025-08-29T16:53:34",
            "upload_time_iso_8601": "2025-08-29T16:53:34.747421Z",
            "url": "https://files.pythonhosted.org/packages/4c/a4/f2fbca4458b4810e3d79c7ae1d474cb5ffd2174e34718af15271599215b1/smalldi-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-29 16:53:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "0xf104a",
    "github_project": "smalldi#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "smalldi"
}
        
Elapsed time: 1.89182s