Skip to main content

An IOC Container for Python 3.10+

Project description

Clean IoC

A simple dependency injection library for python that requires nothing of your application code (except that you use typing).

Read the docs to find out more.

Basic Registering and resolving

There are 4 basic modes of registering a new set of classes

Implementation

class UserRepository(abc.ABC):
    @abc.abstractmethod
    def add(self, user):
        pass

class InMemoryUserRepository(UserRepository):

    def __init__(self):
        self.users = []

    def add(self, user):
        # This is obviously terrible, but it's for demo purposes
        self.users.append(user)

class SqlAlchemyUserRepository(UserRepository):

    def __init__(self):
        # Do some db stuff here
        pass

    def add(self, user):
        # Do some db stuff here
        pass

container = Container()
container.register(UserRepository, InMemoryUserRepository)


repository = container.resolve(UserRepository) # This will return an InMemoryUserRepository

Concrete Class

class ClientDependency:
    def get_int(self):
        return 10

class Client:
    def __init__(self, dep: ClientDependency):
        self.dep = dep

    def get_number(self):
        return self.dep.get_int()


container = Container()
container.register(ClientDependency)
container.register(Client)

client = container.resolve(Client)

client.get_number() # returns 10

Factory

class ClientDependency:
    def get_int(self):
        return 10

class Client:
    def __init__(self, dep: ClientDependency):
        self.dep = dep

    def get_number(self):
        return self.dep.get_int()

def client_factory(dep: ClientDependency):
    return Client(dep=dep)


container = Container()
container.register(ClientDependency)
container.register(Client, factory=client_factory)

client = container.resolve(Client)

client.get_number() # returns 10

Instance

class ClientDependency:
    def __init__(self, num):
        self.num = num

    def get_int(self):
        return self.num

class Client:
    def __init__(self, dep: ClientDependency):
        self.dep = dep

    def get_number(self):
        return self.dep.get_int()

client_dependency = ClientDependency(num=10)

container = Container()
container.register(ClientDependency, instance=client_dependency)
container.register(Client)

client = container.resolve(Client)

client.get_number() # returns 10

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

clean_ioc-1.9.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

clean_ioc-1.9.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file clean_ioc-1.9.0.tar.gz.

File metadata

  • Download URL: clean_ioc-1.9.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.15 Linux/6.8.0-1014-azure

File hashes

Hashes for clean_ioc-1.9.0.tar.gz
Algorithm Hash digest
SHA256 5831e54e356153857049489142b6592132f978c9eb471c142933e2723a9de350
MD5 7379fbb65f6bdd0324ebbc52b8697966
BLAKE2b-256 446bc365463752c326f10e1e9550dd9c50d90cd28c8d35ef6bcd366e2972e3ef

See more details on using hashes here.

File details

Details for the file clean_ioc-1.9.0-py3-none-any.whl.

File metadata

  • Download URL: clean_ioc-1.9.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.15 Linux/6.8.0-1014-azure

File hashes

Hashes for clean_ioc-1.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c122d741d8bc9fbcffc312a4311f279489b7aefb1687440ab397839e0d42f95
MD5 7f702a0d25c61464228dad84457dd542
BLAKE2b-256 ce0116d28ae77f00ef7e902b870a95917d254cce0eabf05131a532d4c472b126

See more details on using hashes here.

Supported by

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