Skip to main content

Dependency injection and inversion of control and library for simple service handling

Project description


Python module for IOC container using dependency injection (uses python 3+ type hints)

Easy way to initialize your services

from cynergy import container

class TestClass:

class ParentClass:
    def __init__(self, test_class: TestClass):
        self.test_class = test_class

instance = container.get(ParentClass)  # Returns TestClass initialized as singleton

print(type(instance))  # ParentClass
print(type(instance.test_class))  # TestClass

Access to your configuration from any service

from cynergy import container
from cynergy.config import Config, MemoryConfig
from cynergy.attributes import arguments

class DbConnector:
    def __init__(self, db: str, host: str):
        self.db = db = host

    "db_name": "LocalDbName",
    "hostname": "localhost"

instance = container.get(DbConnector)

print(instance.db)  # LocalDbName
print(  # localhost

** You can implement your own configuration provider (for exmaple you can create DbConfigProvider which provides your settings from the db)

Manually register special types

from cynergy import container

class Original:

class Other:

container.register_class(Original, Other)

instance = container.get(Original)

print(type(instance))  # Other

Register collection of services

from typing import List
from cynergy import container

class HandlerBase:

class SomeHandler1(HandlerBase):

class SomeHandler2(HandlerBase):

class SomeService:
    def __init__(self, handlers: List[HandlerBase]):
        self.handlers = handlers

container.register_many(HandlerBase, [SomeHandler1, SomeHandler2])

instance = container.get(SomeService)

print(type(instance.handlers))  # list
print(type(instance.handlers[0]))  # SomeHandler1
print(type(instance.handlers[1]))  # SomeHandler2

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
cynergy-1.1.13-py3-none-any.whl (6.5 kB) Copy SHA256 hash SHA256 Wheel py3
cynergy-1.1.13.tar.gz (7.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page