Skip to main content

Python IOC Container

Project description

pyioc3

Python Inversion of Control (IoC) Container

About

pyioc3 is a lightweight and versatile Inversion of Control (IoC) container for Python. It simplifies the management of dependencies and enables cleaner, more modular code by promoting the use of Dependency Injection (DI) patterns. With pyioc3, you can effortlessly wire up your application's components, specify their lifecycles, and inject dependencies without the hassle of manual instantiation.

Key Features

Multiple APIs

pyioc3 offers multiple configuration interfaces including two manual configuration APIs and a decorator-based autowiring API. These APIs are flexible, yet simple to use.

Fluent Interface

pyioc3 manual APIs support method chaining to minimize boiler-plate code and intermediate variables.

Scoped Lifecycles

pyioc3 supports various scopes, including Singleton, Transient, and Requested. This provides fine-grained control of the lifecycle of your objects.

Predictability

pyioc3 is an immutable ioc container that guarentees predictability of the dependency tree.

Constuctor Based DI

pyioc3 implements constructor based dependency injection making it easy to drop into existing applications.

Performance

pyioc3 pre-computes the dependency tree, resulting in fast instantiations to keep your code fast.

OOP Principles

pyioc3 is designed to improve the maintainability, testability, and flexibility of your Python applications.

Quick Start

Install the package

pip install --user pyioc3

StaticContainerBuilder API

from pyioc3 import StaticContainerBuilder

container = (
    StaticContainerBuilder()
    .bind(Duck)
    .bind(QuackProvider, Squeak)
    .build()
)

duck = container.get(Duck)
duck.quack()

BuilderBase API

from pyioc3.builder import BuilderBase, ProviderBinding

class DuckBuilder(Duck):
    def __init__(self):
        super().__init__(target_t=Duck)

    def with_quack_noise(self, noise: str) -> "DuckBuilder":
        self.using_provider(
            annotation=QuackProvider,
            implementation=DynamicQuackProvider,
            on_activate=lambda x: x.set_quack_noise(noise)
        )
        return self

rubber_duck = (
    DuckBuilder()
    .with_quack_noise("Squeak")
    .build()
)

rubber_duck.quack()

Annotation API

from pyioc3.autowire import bind, AutoWireContainerBuilder


class QuackProvider:
    def quack(self):
        raise NotImplementedError()


@bind()
class Duck:
    def __init__(self, quack: QuackProvider):
        self._quack = quack

    def quack(self):
        self._quack.quack()


@bind(QuackProvider)
class Squeak(QuackProvider):

    def quack(self):
        print("Squeak")


duck = AutoWireContainerBuilder("my_package").build().get(Duck)
duck.quack()

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

pyioc3-1.6.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

pyioc3-1.6.1-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file pyioc3-1.6.1.tar.gz.

File metadata

  • Download URL: pyioc3-1.6.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pyioc3-1.6.1.tar.gz
Algorithm Hash digest
SHA256 8067651f3830caf930303381e3606b96d346632a3ec821b03e21850e34e9e5e1
MD5 7fd8be4dedc5a73911f746055084bbe1
BLAKE2b-256 32d1bf50544f5466e47bda0f2b5018a51138e2837390ffa07d35b8ac85a9874c

See more details on using hashes here.

File details

Details for the file pyioc3-1.6.1-py3-none-any.whl.

File metadata

  • Download URL: pyioc3-1.6.1-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pyioc3-1.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 820fa6f951383ab90372fa32d387f3a471d03eb23caa89501f3b058f65b3bd00
MD5 f3ceba50c8608d0aaeaab0a38052d168
BLAKE2b-256 f129037a8b325c0d01405aa99cfe4d1d400ac98f14fd330fccb4db7a117880fc

See more details on using hashes here.

Supported by

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