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!) 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({"database_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():
    async with AsyncExitStack() as stack:
        await ainject({"database_url": "postgresql://kuyugama:insecurepassword@localhost:5432/database"}, scan(application), stack)


asyncio.run(main())

Resolve dependencies by type

It's simple! Use from_ on type annotation

from contextlib import ExitStack

from fundi import from_, inject, scan

class Session:
    """Database session"""

def require_user(_: from_(Session)) -> str:
    return "user"


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


with ExitStack() as stack:
    inject({"db": Session()}, scan(application), stack)

Note: while resolving dependencies by type parameter names doesn't really matter.

Utilities

  • fundi.order - returns order in which dependencies will be resolved
  • fundi.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.5.tar.gz (29.3 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.5-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fundi-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f12065198a4419b2699d36a3e65b88ad61ae149cb4ba4bf891ea76410d35dc58
MD5 d4e4c6ad8869b2962f9bcdba90140e64
BLAKE2b-256 1185f68753716caf1c97346bd6a7a23dde9a0637e1a96ac6ce314214300ea1a0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for fundi-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3741feeda402a5f87b7c7c262a6ed02cfdf984e0e671cde064aeaa9ad7db7afa
MD5 562f5c6352c683e9bf4ce10c1c7c57b6
BLAKE2b-256 24a379ef4a6b842eef0d6ff9a9572a8d02d6ea15275cdab68f14988ec45e6c2a

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