Skip to main content

Minimalistic DI library for python.

Project description

Attodi

Deploy Release

Minimalistic DI library for python

Attodi is a small dependency-injection container for Python. You register types (and optionally concrete implementations or pre-built instances) on a ServiceCollection, then resolve fully-constructed instances from a ServiceProvider, which builds constructor arguments automatically from type hints.

Requirements

  • Python >= 3.12

Installation

pip install attodi

Usage

from attodi.core import ServiceCollection, ServiceProvider

class Greeter:
    def __init__(self, name: str):
        self.name = name

    def greet(self) -> str:
        return f"Hello, {self.name}!"

services = ServiceCollection()
services.add_singleton(str, service="World")
services.add_singleton(Greeter)

provider = ServiceProvider(services)
greeter = provider.get_service(Greeter)
print(greeter.greet())  # Hello, World!

ServiceProvider inspects the constructor of the class being resolved, matches its parameter type hints against registered services, resolves them recursively, and instantiates the class with the results.

Service lifetimes

Register services with one of three lifetimes:

Method Lifetime Behavior
add_singleton(cls, concrete=None, service=None) Singleton One shared instance per ServiceProvider (or a pre-built service value).
add_scoped(cls, concrete=None) Scoped One shared instance per scope (see create_scope).
add_transient(cls, concrete=None) Transient A new instance every time it is resolved.
services = ServiceCollection()
services.add_scoped(Database)

provider = ServiceProvider(services)
scope = provider.create_scope()
db = scope.get_service(Database)

Resolving multiple registrations

get_service returns the first matching registration; get_services returns an iterator over every registration that matches a type (including subclasses).

for service in provider.get_services(Person):
    ...

License

GPL-3.0

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

attodi-0.0.10.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

attodi-0.0.10-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file attodi-0.0.10.tar.gz.

File metadata

  • Download URL: attodi-0.0.10.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for attodi-0.0.10.tar.gz
Algorithm Hash digest
SHA256 1e55566b275eb5a11399abe15f3421a92689bd31cb689fd58e6e032fc2b5cc36
MD5 6243899b95478e3269fb329ca228e665
BLAKE2b-256 976e1abd346da2f4bdf039c0dfff74ab270e44464f21618082a85012cd4dc128

See more details on using hashes here.

File details

Details for the file attodi-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: attodi-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for attodi-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 9eaf53c665bca7cdca727260bfeafd164298b525500e5742832d671c5cd392b7
MD5 36225f80a86b27f6ec2e7c8f7395b0f9
BLAKE2b-256 6261233c25788895082d6b240a166d9b6985c27bf08627970d4bf499e6d78239

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