Skip to main content

IoC container

Project description

💉 sinj

sinj (Simple Inject) is yet another IoC framework for python. If you try to avoid global variables and singletons you might end up with a complex graph of dependencies in your application. With sinj everything is just flat. If you are coming from Java or C# you might be more familiar with IoC frameworks where dependencies are resolved by interface. In python we do not have interfaces and strict types so here we resolve dependencies by constructor argument names or "inject labels".

Basic usage and examples

import sinj
c = sinj.Container() # create dependencies container
c.register(SomeClass, "some_class") # add labeled class into container
c.resolve("some_class") # resolve instance by given label
c.inject(some_instance, "some_instance") # add resolved instance to an index

The simplest example:

import sinj

class A:
    def a(self):
        return "a"

class B:
    def __init__(self, a):
        self._a = a
    
    def b(self):
        return self._a() + " b"

ioc_container = sinj.Container()
ioc_container.register(A, "a")
ioc_container.register(B, "b")

b = ioc_container.resolve("b")
print(b.b()) # -> "a b"

The same example with annotated classes:

import sinj

class A:
    inject = "a" # this label will be used to resolve instance of A
    def a(self):
        return "a"

class B:
    inject = "b" # this label will be used to resolve instance of B
    def __init__(self, a): # instance of A is injected here
        self._a = a
    
    def b(self):
        return self._a() + " b"

ioc_container = sinj.Container()
ioc_container.register(A) # no need to specify label here
ioc_container.register(B) # but you can overwrite it if you want

b = ioc_container.resolve("b")
print(b.b()) # -> "a b"

More examples will be available in ./examples

Errors

  • sinj.DependencyNotFoundError - thrown on resolve when dependency is not found for a given label (and it is not optional).
  • sinj.CircularDependencyError - thrown on resolve when circular dependency is detected.
  • sinj.DependencyConflictError - thrown on register or inject when the container already has something by the label.
  • sinj.DependencyNotMappedError - thrown on register or inject when the class is not annotated and the label is not provided in register method.

Install

From pypi.org

pip install sinj

From repository

pip install git+https://gitlab.com/mrsk/sinj

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

sinj-0.6.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

sinj-0.6.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file sinj-0.6.1.tar.gz.

File metadata

  • Download URL: sinj-0.6.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sinj-0.6.1.tar.gz
Algorithm Hash digest
SHA256 24bd7e5958a6fb77169da62b04cf097e7bd28027f04f92b228bc5eb70fca876f
MD5 4f1199ebc50b8431c9b563c5d90e63b3
BLAKE2b-256 675077966bd225eb99bba81d1246ae73dec9bfc9923ef504507fd758fd7c8267

See more details on using hashes here.

File details

Details for the file sinj-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: sinj-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sinj-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3f3b601686b897c5c506f02f9fa1f876adbb267137151dc245d3606e059f09b
MD5 7ae591e4a4abe47a445c5f6a4535c5f9
BLAKE2b-256 ad6301e7e76ab857a9039b25bef8878bb791c7643279812b64fde9882e5b5a88

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