fastinject


Namefastinject JSON
Version 0.0.4 PyPI version JSON
download
home_pageNone
SummaryEasy dependency injection in Python.
upload_time2024-11-14 12:58:35
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords dependency injection injection decouple decoupled inversion
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastInject: easy Python dependency injection

[![coverage](https://img.shields.io/codecov/c/github/mike-huls/fastinject)](https://codecov.io/gh/mike-huls/fastinject)
[![Tests](https://github.com/mike-huls/fastinject/actions/workflows/tests.yml/badge.svg)](https://github.com/mike-huls/fastinject/actions/workflows/tests.yml)
[![version](https://img.shields.io/pypi/v/fastinject?color=%2334D058&label=pypi%20package)](https://pypi.org/project/fastinject)
[![dependencies](https://img.shields.io/librariesio/release/pypi/fastinject)](https://pypi.org/project/fastinject)
[![PyPI Downloads](https://img.shields.io/pypi/dm/fastinject.svg?label=PyPI%20downloads)](https://pypistats.org/packages/fastinject)
[![versions](https://img.shields.io/pypi/pyversions/fastinject.svg?color=%2334D058)](https://pypi.org/project/fastinject)
<br>
[![tweet](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fmike-huls%2Ffastinject)](https://twitter.com/intent/tweet?text=Check%20this%20out:&url=https%3A%2F%2Fgithub.com%2Fmike-huls%2Ffastinject) 
[![xfollow](https://img.shields.io/twitter/follow/mike_huls)](https://twitter.com/intent/follow?screen_name=mike_huls)

[//]: # (|         |                                                                                                                                                                                                                                                                                                                                                               |)
[//]: # (|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|)
[//]: # (| Testing | ![coverage]&#40;https://img.shields.io/codecov/c/github/mike-huls/cachr&#41;                                                                                                                                                                                                                                                                                          |)
[//]: # (| Package | [![PyPI Latest Release]&#40;https://img.shields.io/pypi/v/cachr.svg&#41;]&#40;https://pypi.org/project/cachr/&#41; [![PyPI Downloads]&#40;https://img.shields.io/pypi/dm/cachr.svg?label=PyPI%20downloads&#41;]&#40;https://pypistats.org/packages/cachr&#41; <br/>![status]&#40;https://img.shields.io/pypi/status/cachr&#41; ![dependencies]&#40;https://img.shields.io/librariesio/release/pypi/cachr&#41; |)
[//]: # (| Meta    | ![GitHub License]&#40;https://img.shields.io/github/license/mike-huls/cachr&#41; ![implementation]&#40;https://img.shields.io/pypi/implementation/cachr&#41;  ![versions]&#40;https://img.shields.io/pypi/pyversions/cachr&#41;                                                                                                                                                       |)
[//]: # (| Social  | ![tweet]&#40;https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fmike-huls%2Fcachr&#41; ![xfollow]&#40;https://img.shields.io/twitter/follow/mike_huls?style=social&#41;                                                                                                                                                                           | )

**FastInject** provides easy dependency injection for Python that makes you code decoupled, testable, uncomplicated and more readable.
Decorate your services with the `@injectable` decorator and decorate your function with `@inject`. Done! 
Your function will now be injected with instances of the required service.
```shell
pip install fastinject
```

## Table of Contents
- [Main Features](#main-features)
- [Usage Example](#Usage-example)
- [Installation](#Installation)
- [Dependencies](#Dependencies)
- [License](#license)
- [Documentation](#documentation)
- [Development](#development)
- [Contributing to Cachr](#Development)
<hr>

## Main Features
- 🐍 Pure Python
- 🤸 Flexible
- 🎩 Tailor-made for your app
- 👨‍🎨 Easy to use with decorators

## How to
Injecting services
- [Register and inject a single service](demo/demo1_inject_single_service.py).
- [Register and inject a single service as a singleton](demo/demo2_inject_single_service_singleton.py).

Inject services that depend on one another
- [ServiceConfig: Register multiple dependencies for injection](demo/demo3_inject_service_config.py).
- [ServiceConfig: Register nested dependencies for injection](demo/demo4_inject_service_config_nested_dependencies.py).

Use the service registy imperatively to get and set dependencies on the fly
- [Declare service to be injectable imperatively](demo/demo5_add_and_get_services_from_registry.py).
- [Declare service to be injectable and declare function to inject imperatively](demo/demo6_add_and_get_service_config_imperatively.py).

Use multiple registries?
- [Use multiple registries](demo/demo7_multiple_registries.py)

Register similar services?
- [Register multiple services of the same type?](demo/demo8_register_multiple_instances_of_the_same_type.py)
<hr>

## Usage Example
Below details a 

#### Step 1: Declare service to be injectable
We have a service that we want to inject, so we mark it `injectable` with a decorator:
```python
import time, datetime
from fastinject import injectable

@injectable()           # <-- Just add this decorator to declare the TimeStamp service to be injectable
class TimeStamp:
    ts: float

    def __init__(self) -> None:
        self.ts = time.time()

    @property
    def datetime_str(self) -> str:
        return datetime.datetime.fromtimestamp(self.ts).strftime("%Y-%m-%d %H:%M:%S")
```

Step 2: Use the service in a function that is injected in
```python
from fastinject import inject

@inject()               # <-- This decorator will inject required services in this function
def function_with_injection(ts: TimeStamp):
    print(f"In the injected function, the current time is {ts.datetime_str}.")

if __name__ == "__main__":
    function_with_injection()
```



## Installation
```sh
pip install fastinject
```
The source code is currently hosted on GitHub at:
https://github.com/mike-huls/fastinject

Binary installers for the latest released version are available at the [Python
Package Index (PyPI)](https://pypi.org/project/fastinject).

## Dependencies
FastInject has one major dependency: `injector`. FastInject aims to build on `injector` by making it easier to use.

## License
[MIT](LICENSE.txt)

## Documentation
🔨 Under construction

## Development
Find the changelog and list of upcoming features [here](CHANGELOG.md).
<br>
**Contributions** are always welcome; feel free to submit bug reports, bug fixes, feature requests, documentation improvements or enhancements!

<hr>

[Go to Top](#table-of-contents)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastinject",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "dependency injection, injection, decouple, decoupled, inversion",
    "author": null,
    "author_email": "Mike Huls <mikehuls42@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/1f/da/b4a2c0e1be8694ee9cf418e69780c809f3d8352ccff064725cfb2a74ad53/fastinject-0.0.4.tar.gz",
    "platform": null,
    "description": "# FastInject: easy Python dependency injection\n\n[![coverage](https://img.shields.io/codecov/c/github/mike-huls/fastinject)](https://codecov.io/gh/mike-huls/fastinject)\n[![Tests](https://github.com/mike-huls/fastinject/actions/workflows/tests.yml/badge.svg)](https://github.com/mike-huls/fastinject/actions/workflows/tests.yml)\n[![version](https://img.shields.io/pypi/v/fastinject?color=%2334D058&label=pypi%20package)](https://pypi.org/project/fastinject)\n[![dependencies](https://img.shields.io/librariesio/release/pypi/fastinject)](https://pypi.org/project/fastinject)\n[![PyPI Downloads](https://img.shields.io/pypi/dm/fastinject.svg?label=PyPI%20downloads)](https://pypistats.org/packages/fastinject)\n[![versions](https://img.shields.io/pypi/pyversions/fastinject.svg?color=%2334D058)](https://pypi.org/project/fastinject)\n<br>\n[![tweet](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fmike-huls%2Ffastinject)](https://twitter.com/intent/tweet?text=Check%20this%20out:&url=https%3A%2F%2Fgithub.com%2Fmike-huls%2Ffastinject) \n[![xfollow](https://img.shields.io/twitter/follow/mike_huls)](https://twitter.com/intent/follow?screen_name=mike_huls)\n\n[//]: # (|         |                                                                                                                                                                                                                                                                                                                                                               |)\n[//]: # (|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|)\n[//]: # (| Testing | ![coverage]&#40;https://img.shields.io/codecov/c/github/mike-huls/cachr&#41;                                                                                                                                                                                                                                                                                          |)\n[//]: # (| Package | [![PyPI Latest Release]&#40;https://img.shields.io/pypi/v/cachr.svg&#41;]&#40;https://pypi.org/project/cachr/&#41; [![PyPI Downloads]&#40;https://img.shields.io/pypi/dm/cachr.svg?label=PyPI%20downloads&#41;]&#40;https://pypistats.org/packages/cachr&#41; <br/>![status]&#40;https://img.shields.io/pypi/status/cachr&#41; ![dependencies]&#40;https://img.shields.io/librariesio/release/pypi/cachr&#41; |)\n[//]: # (| Meta    | ![GitHub License]&#40;https://img.shields.io/github/license/mike-huls/cachr&#41; ![implementation]&#40;https://img.shields.io/pypi/implementation/cachr&#41;  ![versions]&#40;https://img.shields.io/pypi/pyversions/cachr&#41;                                                                                                                                                       |)\n[//]: # (| Social  | ![tweet]&#40;https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fmike-huls%2Fcachr&#41; ![xfollow]&#40;https://img.shields.io/twitter/follow/mike_huls?style=social&#41;                                                                                                                                                                           | )\n\n**FastInject** provides easy dependency injection for Python that makes you code decoupled, testable, uncomplicated and more readable.\nDecorate your services with the `@injectable` decorator and decorate your function with `@inject`. Done! \nYour function will now be injected with instances of the required service.\n```shell\npip install fastinject\n```\n\n## Table of Contents\n- [Main Features](#main-features)\n- [Usage Example](#Usage-example)\n- [Installation](#Installation)\n- [Dependencies](#Dependencies)\n- [License](#license)\n- [Documentation](#documentation)\n- [Development](#development)\n- [Contributing to Cachr](#Development)\n<hr>\n\n## Main Features\n- \ud83d\udc0d Pure Python\n- \ud83e\udd38 Flexible\n- \ud83c\udfa9 Tailor-made for your app\n- \ud83d\udc68\u200d\ud83c\udfa8 Easy to use with decorators\n\n## How to\nInjecting services\n- [Register and inject a single service](demo/demo1_inject_single_service.py).\n- [Register and inject a single service as a singleton](demo/demo2_inject_single_service_singleton.py).\n\nInject services that depend on one another\n- [ServiceConfig: Register multiple dependencies for injection](demo/demo3_inject_service_config.py).\n- [ServiceConfig: Register nested dependencies for injection](demo/demo4_inject_service_config_nested_dependencies.py).\n\nUse the service registy imperatively to get and set dependencies on the fly\n- [Declare service to be injectable imperatively](demo/demo5_add_and_get_services_from_registry.py).\n- [Declare service to be injectable and declare function to inject imperatively](demo/demo6_add_and_get_service_config_imperatively.py).\n\nUse multiple registries?\n- [Use multiple registries](demo/demo7_multiple_registries.py)\n\nRegister similar services?\n- [Register multiple services of the same type?](demo/demo8_register_multiple_instances_of_the_same_type.py)\n<hr>\n\n## Usage Example\nBelow details a \n\n#### Step 1: Declare service to be injectable\nWe have a service that we want to inject, so we mark it `injectable` with a decorator:\n```python\nimport time, datetime\nfrom fastinject import injectable\n\n@injectable()           # <-- Just add this decorator to declare the TimeStamp service to be injectable\nclass TimeStamp:\n    ts: float\n\n    def __init__(self) -> None:\n        self.ts = time.time()\n\n    @property\n    def datetime_str(self) -> str:\n        return datetime.datetime.fromtimestamp(self.ts).strftime(\"%Y-%m-%d %H:%M:%S\")\n```\n\nStep 2: Use the service in a function that is injected in\n```python\nfrom fastinject import inject\n\n@inject()               # <-- This decorator will inject required services in this function\ndef function_with_injection(ts: TimeStamp):\n    print(f\"In the injected function, the current time is {ts.datetime_str}.\")\n\nif __name__ == \"__main__\":\n    function_with_injection()\n```\n\n\n\n## Installation\n```sh\npip install fastinject\n```\nThe source code is currently hosted on GitHub at:\nhttps://github.com/mike-huls/fastinject\n\nBinary installers for the latest released version are available at the [Python\nPackage Index (PyPI)](https://pypi.org/project/fastinject).\n\n## Dependencies\nFastInject has one major dependency: `injector`. FastInject aims to build on `injector` by making it easier to use.\n\n## License\n[MIT](LICENSE.txt)\n\n## Documentation\n\ud83d\udd28 Under construction\n\n## Development\nFind the changelog and list of upcoming features [here](CHANGELOG.md).\n<br>\n**Contributions** are always welcome; feel free to submit bug reports, bug fixes, feature requests, documentation improvements or enhancements!\n\n<hr>\n\n[Go to Top](#table-of-contents)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Easy dependency injection in Python.",
    "version": "0.0.4",
    "project_urls": {
        "Bug Tracker": "https://github.com/mike-huls/fastinject/issues",
        "Changelog": "https://github.com/mike-huls/fastinject/blob/master/CHANGELOG.md/",
        "Documentation": "https://github.com/mike-huls/fastinject/blob/master/README.md/",
        "Homepage": "https://github.com/mike-huls/fastinject",
        "Say Thanks!": "https://www.buymeacoffee.com/mikehuls",
        "Source": "https://github.com/mike-huls/fastinject/"
    },
    "split_keywords": [
        "dependency injection",
        " injection",
        " decouple",
        " decoupled",
        " inversion"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3f5d300de350886b6bdcba0f9baee4efe61e65febb48f648c238f1e1762f89ca",
                "md5": "8801578ab02d401a421b623626bec7c6",
                "sha256": "0c54b25acd3407bb1dd548ee275f3ded6cbee2698e5078e0e0a483f61524d936"
            },
            "downloads": -1,
            "filename": "fastinject-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8801578ab02d401a421b623626bec7c6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 8531,
            "upload_time": "2024-11-14T12:58:34",
            "upload_time_iso_8601": "2024-11-14T12:58:34.514474Z",
            "url": "https://files.pythonhosted.org/packages/3f/5d/300de350886b6bdcba0f9baee4efe61e65febb48f648c238f1e1762f89ca/fastinject-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1fdab4a2c0e1be8694ee9cf418e69780c809f3d8352ccff064725cfb2a74ad53",
                "md5": "7822f82359a9b1027fac1ebde0308fbb",
                "sha256": "e041fc9304c1fd10344a7afd351d6af8cc117193c46102b75184d0d7f454ea49"
            },
            "downloads": -1,
            "filename": "fastinject-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "7822f82359a9b1027fac1ebde0308fbb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 14170,
            "upload_time": "2024-11-14T12:58:35",
            "upload_time_iso_8601": "2024-11-14T12:58:35.818291Z",
            "url": "https://files.pythonhosted.org/packages/1f/da/b4a2c0e1be8694ee9cf418e69780c809f3d8352ccff064725cfb2a74ad53/fastinject-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-14 12:58:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mike-huls",
    "github_project": "fastinject",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fastinject"
}
        
Elapsed time: 0.67450s