anydi


Nameanydi JSON
Version 0.39.3 PyPI version JSON
download
home_pageNone
SummaryDependency Injection library
upload_time2025-02-17 18:26:29
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 typing import Annotated

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: Annotated[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/5e/68/1ff9c238f2f0cd93d2069556d688ab4927edfa3f5982820549e7c23b9c87/anydi-0.39.3.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 typing import Annotated\n\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: Annotated[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.39.3",
    "project_urls": {
        "Repository": "https://github.com/antonrh/anydi"
    },
    "split_keywords": [
        "application",
        " async",
        " asyncio",
        " dependencies",
        " dependency injection",
        " di"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "af50ee0380fcafa9264c83646973bb19285eb88cd303ea92073265a1d4303251",
                "md5": "8cc3d73e1640e1d51e96c6ed12c1d090",
                "sha256": "dcb3db2cfed7ac998c643d7dfe187c705decc355c87810a6792d332adf0a2210"
            },
            "downloads": -1,
            "filename": "anydi-0.39.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8cc3d73e1640e1d51e96c6ed12c1d090",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "~=3.9",
            "size": 28665,
            "upload_time": "2025-02-17T18:26:28",
            "upload_time_iso_8601": "2025-02-17T18:26:28.271214Z",
            "url": "https://files.pythonhosted.org/packages/af/50/ee0380fcafa9264c83646973bb19285eb88cd303ea92073265a1d4303251/anydi-0.39.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5e681ff9c238f2f0cd93d2069556d688ab4927edfa3f5982820549e7c23b9c87",
                "md5": "b913ae72a2a6932de49521c395a2cb43",
                "sha256": "245ad19b2c4947dde990d0168b233868ee801c3b0dde40cf11f03fbbf1029b74"
            },
            "downloads": -1,
            "filename": "anydi-0.39.3.tar.gz",
            "has_sig": false,
            "md5_digest": "b913ae72a2a6932de49521c395a2cb43",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.9",
            "size": 123802,
            "upload_time": "2025-02-17T18:26:29",
            "upload_time_iso_8601": "2025-02-17T18:26:29.688514Z",
            "url": "https://files.pythonhosted.org/packages/5e/68/1ff9c238f2f0cd93d2069556d688ab4927edfa3f5982820549e7c23b9c87/anydi-0.39.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-17 18:26:29",
    "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.39101s