python-injection


Namepython-injection JSON
Version 0.8.3 PyPI version JSON
download
home_pagehttps://github.com/100nm/python-injection
SummaryFast and easy dependency injection framework.
upload_time2024-04-21 14:29:04
maintainerNone
docs_urlNone
authorremimd
requires_python<4,>=3.10
licenseMIT
keywords dependencies inject injection
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Basic usage

## Create an injectable

> **Note**: If the class needs dependencies, these will be resolved when the instance is retrieved.

If you wish to inject a singleton, use `singleton` decorator.

```python
from injection import singleton

@singleton
class ServiceA:
    """ class implementation """
```

If you wish to inject a new instance each time, use `injectable` decorator.

```python
from injection import injectable

@injectable
class ServiceB:
    """ class implementation """
```

If you have a constant (such as a global variable) and wish to register it as an injectable, use `set_constant`
function.

```python
from injection import set_constant

class ServiceC:
    """ class implementation """

service_c = set_constant(ServiceC())
```

## Inject an instance

To inject one or several instances, use `inject` decorator.
_Don't forget to annotate type of parameter to inject._

```python
from injection import inject

@inject
def some_function(service_a: ServiceA):
    """ function implementation """
```

If `inject` decorates a class, it will be applied to the `__init__` method.
_Especially useful for dataclasses:_

> **Note**: Doesn't work with Pydantic `BaseModel` because the signature of the `__init__` method doesn't contain the
> dependencies.

```python
from dataclasses import dataclass

from injection import inject

@inject
@dataclass
class SomeDataClass:
    service_a: ServiceA = ...
```

## Get an instance

_Example with `get_instance` function:_

```python
from injection import get_instance

service_a = get_instance(ServiceA)
```

_Example with `get_lazy_instance` function:_

```python
from injection import get_lazy_instance

lazy_service_a = get_lazy_instance(ServiceA)
# ...
service_a = ~lazy_service_a
```

## Inheritance

In the case of inheritance, you can use the decorator parameter `on` to link the injection to one or several other
classes.

**Warning: if the child class is in another file, make sure that file is imported before injection.**
[_See `load_package` function._](utils.md#load_package)

_Example with one class:_

```python
class AbstractService(ABC):
    ...

@injectable(on=AbstractService)
class ConcreteService(AbstractService):
    ...
```

_Example with several classes:_

```python
class AbstractService(ABC):
    ...

class ConcreteService(AbstractService):
    ...

@injectable(on=(AbstractService, ConcreteService))
class ConcreteServiceOverload(ConcreteService):
    ...
```

If a class is registered in a package, and you want to override it, there is the `mode` parameter:

```python
@injectable
class InaccessibleService:
    ...

# ...

@injectable(on=InaccessibleService, mode="override")
class ServiceOverload(InaccessibleService):
    ...
```

## Recipes

A recipe is a function that tells the injector how to construct the instance to be injected. It is important to specify 
the return type annotation when defining the recipe.

```python
from injection import injectable

@injectable
def service_d_recipe() -> ServiceD:
    """ recipe implementation """
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/100nm/python-injection",
    "name": "python-injection",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.10",
    "maintainer_email": null,
    "keywords": "dependencies, inject, injection",
    "author": "remimd",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/f8/53/bb704315519e53c9a6faa4dea64c9d343ab6c6df613a554a7e3dd211afda/python_injection-0.8.3.tar.gz",
    "platform": null,
    "description": "# Basic usage\n\n## Create an injectable\n\n> **Note**: If the class needs dependencies, these will be resolved when the instance is retrieved.\n\nIf you wish to inject a singleton, use `singleton` decorator.\n\n```python\nfrom injection import singleton\n\n@singleton\nclass ServiceA:\n    \"\"\" class implementation \"\"\"\n```\n\nIf you wish to inject a new instance each time, use `injectable` decorator.\n\n```python\nfrom injection import injectable\n\n@injectable\nclass ServiceB:\n    \"\"\" class implementation \"\"\"\n```\n\nIf you have a constant (such as a global variable) and wish to register it as an injectable, use `set_constant`\nfunction.\n\n```python\nfrom injection import set_constant\n\nclass ServiceC:\n    \"\"\" class implementation \"\"\"\n\nservice_c = set_constant(ServiceC())\n```\n\n## Inject an instance\n\nTo inject one or several instances, use `inject` decorator.\n_Don't forget to annotate type of parameter to inject._\n\n```python\nfrom injection import inject\n\n@inject\ndef some_function(service_a: ServiceA):\n    \"\"\" function implementation \"\"\"\n```\n\nIf `inject` decorates a class, it will be applied to the `__init__` method.\n_Especially useful for dataclasses:_\n\n> **Note**: Doesn't work with Pydantic `BaseModel` because the signature of the `__init__` method doesn't contain the\n> dependencies.\n\n```python\nfrom dataclasses import dataclass\n\nfrom injection import inject\n\n@inject\n@dataclass\nclass SomeDataClass:\n    service_a: ServiceA = ...\n```\n\n## Get an instance\n\n_Example with `get_instance` function:_\n\n```python\nfrom injection import get_instance\n\nservice_a = get_instance(ServiceA)\n```\n\n_Example with `get_lazy_instance` function:_\n\n```python\nfrom injection import get_lazy_instance\n\nlazy_service_a = get_lazy_instance(ServiceA)\n# ...\nservice_a = ~lazy_service_a\n```\n\n## Inheritance\n\nIn the case of inheritance, you can use the decorator parameter `on` to link the injection to one or several other\nclasses.\n\n**Warning: if the child class is in another file, make sure that file is imported before injection.**\n[_See `load_package` function._](utils.md#load_package)\n\n_Example with one class:_\n\n```python\nclass AbstractService(ABC):\n    ...\n\n@injectable(on=AbstractService)\nclass ConcreteService(AbstractService):\n    ...\n```\n\n_Example with several classes:_\n\n```python\nclass AbstractService(ABC):\n    ...\n\nclass ConcreteService(AbstractService):\n    ...\n\n@injectable(on=(AbstractService, ConcreteService))\nclass ConcreteServiceOverload(ConcreteService):\n    ...\n```\n\nIf a class is registered in a package, and you want to override it, there is the `mode` parameter:\n\n```python\n@injectable\nclass InaccessibleService:\n    ...\n\n# ...\n\n@injectable(on=InaccessibleService, mode=\"override\")\nclass ServiceOverload(InaccessibleService):\n    ...\n```\n\n## Recipes\n\nA recipe is a function that tells the injector how to construct the instance to be injected. It is important to specify \nthe return type annotation when defining the recipe.\n\n```python\nfrom injection import injectable\n\n@injectable\ndef service_d_recipe() -> ServiceD:\n    \"\"\" recipe implementation \"\"\"\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Fast and easy dependency injection framework.",
    "version": "0.8.3",
    "project_urls": {
        "Homepage": "https://github.com/100nm/python-injection",
        "Repository": "https://github.com/100nm/python-injection"
    },
    "split_keywords": [
        "dependencies",
        " inject",
        " injection"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9e494e12f866272465154d9209e2aa6ee1152d8a01b160e58544137abb5bf69a",
                "md5": "bdda7ba184727e727e11fed3058889a0",
                "sha256": "1e87efe3a1ff6ee4845e455f774a68435ecc56d0d0ee58ac292e171e8deabfbb"
            },
            "downloads": -1,
            "filename": "python_injection-0.8.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bdda7ba184727e727e11fed3058889a0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.10",
            "size": 14696,
            "upload_time": "2024-04-21T14:29:02",
            "upload_time_iso_8601": "2024-04-21T14:29:02.784506Z",
            "url": "https://files.pythonhosted.org/packages/9e/49/4e12f866272465154d9209e2aa6ee1152d8a01b160e58544137abb5bf69a/python_injection-0.8.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f853bb704315519e53c9a6faa4dea64c9d343ab6c6df613a554a7e3dd211afda",
                "md5": "19d0d39caba52d42196d823aa59f25f0",
                "sha256": "0e6e40bfa04943875248e51012db58f43d8be32e6501561dfd71e1112151a6ad"
            },
            "downloads": -1,
            "filename": "python_injection-0.8.3.tar.gz",
            "has_sig": false,
            "md5_digest": "19d0d39caba52d42196d823aa59f25f0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.10",
            "size": 11340,
            "upload_time": "2024-04-21T14:29:04",
            "upload_time_iso_8601": "2024-04-21T14:29:04.458042Z",
            "url": "https://files.pythonhosted.org/packages/f8/53/bb704315519e53c9a6faa4dea64c9d343ab6c6df613a554a7e3dd211afda/python_injection-0.8.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-21 14:29:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "100nm",
    "github_project": "python-injection",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "python-injection"
}
        
Elapsed time: 0.24673s