anydi


Nameanydi JSON
Version 0.38.0 PyPI version JSON
download
home_pageNone
SummaryDependency Injection library
upload_time2025-01-15 08:05:51
maintainerNone
docs_urlNone
authorNone
requires_python~=3.9
licenseNone
keywords application async asyncio dependencies dependency injection di
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AnyDI

<p align="center">
    <i>Modern, lightweight Dependency Injection library using type annotations.</i>
</p>

<p align="center">
    <a href="https://github.com/antonrh/anydi/actions/workflows/ci.yml" target="_blank">
        <img src="https://github.com/antonrh/anydi/actions/workflows/ci.yml/badge.svg" alt="CI">
    </a>
    <a href="https://codecov.io/gh/antonrh/anydi" target="_blank">
        <img src="https://codecov.io/gh/antonrh/anydi/branch/main/graph/badge.svg?token=67CLD19I0C" alt="Coverage">
    </a>
    <a href="https://anydi.readthedocs.io/en/latest/?badge=latest" target="_blank">
        <img src="https://readthedocs.org/projects/anydi/badge/?version=latest" alt="Documentation">
    </a>
</p>

---
Documentation

http://anydi.readthedocs.io/

---

`AnyDI` is a modern, lightweight Dependency Injection library suitable for any synchronous or asynchronous applications with Python 3.9+, based on type annotations ([PEP 484](https://peps.python.org/pep-0484/)).

The key features are:

* **Type-safe**: Resolves dependencies using type annotations.
* **Async Support**: Compatible with both synchronous and asynchronous providers and injections.
* **Scoping**: Supports singleton, transient, and request scopes.
* **Easy to Use**: Designed for simplicity and minimal boilerplate.
* **Named Dependencies**: Supports named dependencies using `Annotated` type.
* **Resource Management**: Manages resources using context managers.
* **Modular: Facilitates** a modular design with support for multiple modules.
* **Scanning**: Automatically scans for injectable functions and classes.
* **Integrations**: Provides easy integration with popular frameworks and libraries.
* **Testing**: Simplifies testing by allowing provider overrides.

## Installation

```shell
pip install anydi
```

## Quick Example

*app.py*

```python
from anydi import auto, Container

container = Container()


@container.provider(scope="singleton")
def message() -> str:
    return "Hello, world!"


@container.inject
def say_hello(message: str = auto) -> None:
    print(message)


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

## FastAPI Example

*app.py*

```python
from fastapi import FastAPI

import anydi.ext.fastapi
from anydi import Container
from anydi.ext.fastapi import Inject

container = Container()


@container.provider(scope="singleton")
def message() -> str:
    return "Hello, World!"


app = FastAPI()


@app.get("/hello")
def say_hello(message: str = Inject()) -> dict[str, str]:
    return {"message": message}


# Install the container into the FastAPI app
anydi.ext.fastapi.install(app, container)
```



## Django Ninja Example

*container.py*

```python
from anydi import Container


def get_container() -> Container:
    container = Container()

    @container.provider(scope="singleton")
    def message() -> str:
        return "Hello, World!"

    return container
```

*settings.py*

```python
INSTALLED_APPS = [
    ...
    "anydi.ext.django",
]

ANYDI = {
    "CONTAINER_FACTORY": "myapp.container.get_container",
    "PATCH_NINJA": True,
}
```

*urls.py*

```python
from django.http import HttpRequest
from django.urls import path
from ninja import NinjaAPI

from anydi import auto

api = NinjaAPI()


@api.get("/hello")
def say_hello(request: HttpRequest, message: str = auto) -> dict[str, str]:
    return {"message": message}


urlpatterns = [
    path("api/", api.urls),
]
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "anydi",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "~=3.9",
    "maintainer_email": null,
    "keywords": "application, async, asyncio, dependencies, dependency injection, di",
    "author": null,
    "author_email": "Anton Ruhlov <antonruhlov@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/50/3b/38cfc93c7616aa7d5e9fc73d9540d8c6f48c7457eb831e7053b5113549ad/anydi-0.38.0.tar.gz",
    "platform": null,
    "description": "# AnyDI\n\n<p align=\"center\">\n    <i>Modern, lightweight Dependency Injection library using type annotations.</i>\n</p>\n\n<p align=\"center\">\n    <a href=\"https://github.com/antonrh/anydi/actions/workflows/ci.yml\" target=\"_blank\">\n        <img src=\"https://github.com/antonrh/anydi/actions/workflows/ci.yml/badge.svg\" alt=\"CI\">\n    </a>\n    <a href=\"https://codecov.io/gh/antonrh/anydi\" target=\"_blank\">\n        <img src=\"https://codecov.io/gh/antonrh/anydi/branch/main/graph/badge.svg?token=67CLD19I0C\" alt=\"Coverage\">\n    </a>\n    <a href=\"https://anydi.readthedocs.io/en/latest/?badge=latest\" target=\"_blank\">\n        <img src=\"https://readthedocs.org/projects/anydi/badge/?version=latest\" alt=\"Documentation\">\n    </a>\n</p>\n\n---\nDocumentation\n\nhttp://anydi.readthedocs.io/\n\n---\n\n`AnyDI` is a modern, lightweight Dependency Injection library suitable for any synchronous or asynchronous applications with Python 3.9+, based on type annotations ([PEP 484](https://peps.python.org/pep-0484/)).\n\nThe key features are:\n\n* **Type-safe**: Resolves dependencies using type annotations.\n* **Async Support**: Compatible with both synchronous and asynchronous providers and injections.\n* **Scoping**: Supports singleton, transient, and request scopes.\n* **Easy to Use**: Designed for simplicity and minimal boilerplate.\n* **Named Dependencies**: Supports named dependencies using `Annotated` type.\n* **Resource Management**: Manages resources using context managers.\n* **Modular: Facilitates** a modular design with support for multiple modules.\n* **Scanning**: Automatically scans for injectable functions and classes.\n* **Integrations**: Provides easy integration with popular frameworks and libraries.\n* **Testing**: Simplifies testing by allowing provider overrides.\n\n## Installation\n\n```shell\npip install anydi\n```\n\n## Quick Example\n\n*app.py*\n\n```python\nfrom anydi import auto, Container\n\ncontainer = Container()\n\n\n@container.provider(scope=\"singleton\")\ndef message() -> str:\n    return \"Hello, world!\"\n\n\n@container.inject\ndef say_hello(message: str = auto) -> None:\n    print(message)\n\n\nif __name__ == \"__main__\":\n    say_hello()\n```\n\n## FastAPI Example\n\n*app.py*\n\n```python\nfrom fastapi import FastAPI\n\nimport anydi.ext.fastapi\nfrom anydi import Container\nfrom anydi.ext.fastapi import Inject\n\ncontainer = Container()\n\n\n@container.provider(scope=\"singleton\")\ndef message() -> str:\n    return \"Hello, World!\"\n\n\napp = FastAPI()\n\n\n@app.get(\"/hello\")\ndef say_hello(message: str = Inject()) -> dict[str, str]:\n    return {\"message\": message}\n\n\n# Install the container into the FastAPI app\nanydi.ext.fastapi.install(app, container)\n```\n\n\n\n## Django Ninja Example\n\n*container.py*\n\n```python\nfrom anydi import Container\n\n\ndef get_container() -> Container:\n    container = Container()\n\n    @container.provider(scope=\"singleton\")\n    def message() -> str:\n        return \"Hello, World!\"\n\n    return container\n```\n\n*settings.py*\n\n```python\nINSTALLED_APPS = [\n    ...\n    \"anydi.ext.django\",\n]\n\nANYDI = {\n    \"CONTAINER_FACTORY\": \"myapp.container.get_container\",\n    \"PATCH_NINJA\": True,\n}\n```\n\n*urls.py*\n\n```python\nfrom django.http import HttpRequest\nfrom django.urls import path\nfrom ninja import NinjaAPI\n\nfrom anydi import auto\n\napi = NinjaAPI()\n\n\n@api.get(\"/hello\")\ndef say_hello(request: HttpRequest, message: str = auto) -> dict[str, str]:\n    return {\"message\": message}\n\n\nurlpatterns = [\n    path(\"api/\", api.urls),\n]\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Dependency Injection library",
    "version": "0.38.0",
    "project_urls": {
        "Repository": "https://github.com/antonrh/anydi"
    },
    "split_keywords": [
        "application",
        " async",
        " asyncio",
        " dependencies",
        " dependency injection",
        " di"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0951c4a040ad39a318868ef1fbb486026ea0c2df0f6d83f1928b3a85bc81d8e5",
                "md5": "81edfc8a5e17b4a390e64a531051b04d",
                "sha256": "87c62494f2940adf91c1592517af260dac62073016b8eaeb6cc7910fe05717a7"
            },
            "downloads": -1,
            "filename": "anydi-0.38.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "81edfc8a5e17b4a390e64a531051b04d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "~=3.9",
            "size": 29219,
            "upload_time": "2025-01-15T08:05:48",
            "upload_time_iso_8601": "2025-01-15T08:05:48.254767Z",
            "url": "https://files.pythonhosted.org/packages/09/51/c4a040ad39a318868ef1fbb486026ea0c2df0f6d83f1928b3a85bc81d8e5/anydi-0.38.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "503b38cfc93c7616aa7d5e9fc73d9540d8c6f48c7457eb831e7053b5113549ad",
                "md5": "dd488b2ac9830e3ec9816420f34648fc",
                "sha256": "11c86e4669e5002f4a50dbd4a2b91b28c2c8d6367e251a21f1d97361f96e0a48"
            },
            "downloads": -1,
            "filename": "anydi-0.38.0.tar.gz",
            "has_sig": false,
            "md5_digest": "dd488b2ac9830e3ec9816420f34648fc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.9",
            "size": 120878,
            "upload_time": "2025-01-15T08:05:51",
            "upload_time_iso_8601": "2025-01-15T08:05:51.141481Z",
            "url": "https://files.pythonhosted.org/packages/50/3b/38cfc93c7616aa7d5e9fc73d9540d8c6f48c7457eb831e7053b5113549ad/anydi-0.38.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-15 08:05:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "antonrh",
    "github_project": "anydi",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "anydi"
}
        
Elapsed time: 0.40675s