Skip to main content

Dependency injection library

Project description

# FunDI

Solution for problem no one had before

Fun stays for function(or for fun if you wish) and DI for Dependency Injection

This library provides fast(to write!) dependency injection and convenient(to use!) Dependency Injection for functional programming on python.

No more words, let's try!

from contextlib import ExitStack
from typing import Generator, Any

from fundi import from_, inject, scan

def require_database_session(database_url: str) -> Generator[str, Any]:
    print(f"Opened database session at {database_url = }")
    yield "database session"
    print("Closed database session")


def require_user(session: str = from_(require_database_session)) -> str:
    return "user"


def application(user: str = from_(require_user), session: str = from_(require_database_session)):
    print(f"Application started with {user = }")


with ExitStack() as stack:
    inject({"session_url": "postgresql://kuyugama:insecurepassword@localhost:5432/database"}, scan(application), stack)

Async? YES!!!

import asyncio
from contextlib import AsyncExitStack
from typing import AsyncGenerator, Any

from fundi import from_, ainject, scan

async def require_database_session(database_url: str) -> AsyncGenerator[str, Any]:
    print(f"Opened database session at {database_url = }")
    yield "database session"
    print("Closed database session")


async def require_user(session: str = from_(require_database_session)) -> str:
    return "user"


async def application(user: str = from_(require_user), session: str = from_(require_database_session)):
    print(f"Application started with {user = }")


async def main():
    with AsyncExitStack() as stack:
        await ainject({"session_url": "postgresql://kuyugama:insecurepassword@localhost:5432/database"}, scan(application), stack)


asyncio.run(main())

Utilities

  • fastdi.order - returns order in which dependencies will be resolved
  • fastdi.tree - returns dependency resolving tree

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

fundi-0.0.3.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

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

fundi-0.0.3-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

Details for the file fundi-0.0.3.tar.gz.

File metadata

  • Download URL: fundi-0.0.3.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for fundi-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ae129c78328f4bab953e06680e2de524e3f5538ed52d934841f5c764db25d3d0
MD5 913f8bf84a36cac2a3abe0704f2d341f
BLAKE2b-256 49ebf9886f2d7c1198a1a42f2bc4dae5f6f6de538794964031b002ff63898f1d

See more details on using hashes here.

File details

Details for the file fundi-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: fundi-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for fundi-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f0b3d52c40aae90d06bc041f69e9d579344717d935195e3e8325e3d08739f77b
MD5 7e42d7d6c8c8e70dc4f507c2219607d5
BLAKE2b-256 c78d5421b561ba1e388145c2c7db717303263007de1da67f7a2b177419c72839

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