Skip to main content

Dependency injection for python functions

Project description

python_depends

python_depends is a dependency injection library inspired by the Depends mechanism from FastAPI. It supports both asynchronous and synchronous functions, providing flexibility and ease of use.

Installation

pip install depends_python

Key Features

  • Support for synchronous and asynchronous functions.
  • Simple dependency annotation using the @inject_dependencies decorator.
  • Support for nested dependencies.
  • Ability to mix synchronous and asynchronous dependencies.

Usage Examples

Asynchronous Functions

from python_depends import Depends, inject_dependencies
import asyncio

async def injected():
    return 1

@inject_dependencies
async def inject_value(value: int = Depends(injected)):
    print(value)

if __name__ == '__main__':
    asyncio.run(inject_value())

Basic Example

from python_depends import Depends, inject_dependencies

# Function to inject
def get_value():
    return 1

@inject_dependencies  # inject dependencies
def test_injection(value: int = Depends(get_value)):
    print(value)

if __name__ == '__main__':
    test_injection()

Multiple Dependencies

from python_depends import inject_dependencies, Depends

def injected1():
    return 1

def injected2():
    return 3

@inject_dependencies
def inject_value(value1: int = Depends(injected2), value2: int = Depends(injected1)):
    print(value1 + value2)

if __name__ == "__main__":
    inject_value()

Nested Dependencies

from python_depends import Depends, inject_dependencies

def injected1():
    return 1

@inject_dependencies
def injected2(value: int = Depends(injected1)):
    new_value = 3
    return new_value + value

@inject_dependencies
def inject_value(value: int = Depends(injected2)):
    print(value)

if __name__ == '__main__':
    inject_value()

Mixing Synchronous and Asynchronous Dependencies

from python_depends import Depends, inject_dependencies

async def injected():
    return 1

@inject_dependencies
def inject_value(value: int = Depends(injected)):
    print(value)

if __name__ == '__main__':
    inject_value()

License

MIT License.


The python_depends library is designed to simplify the dependency injection process and make code cleaner and easier to test.

The name of package is depends_python because python_depends is already in use

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

depends_python-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

depends_python-0.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file depends_python-0.0.1.tar.gz.

File metadata

  • Download URL: depends_python-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for depends_python-0.0.1.tar.gz
Algorithm Hash digest
SHA256 670dfed9a28b8aa6da8059d3c207fd4843417559f9965f4909ec2a356015efe9
MD5 8e057cd3a02b5d75abdefb98e5d3eab5
BLAKE2b-256 066b5112c7de1a5285286a8e2df5c8706148f60a7bea573b46b02e71619c7d11

See more details on using hashes here.

File details

Details for the file depends_python-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for depends_python-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 637018d1ff661d647b9b79940e69c20df1b294a30a7ba1d9f7d5e8869d254f20
MD5 23d10ff92cc4f0eb63144d5fcc562086
BLAKE2b-256 ec6158914009a83b717566d0244f40ada1943d6f650ccbcea63d919ad1c4f944

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page