Skip to main content

simplistic dependency injection container for python

Project description

# Simple Inversion-Of-Control-Container for python #

This package contains a simple inversion-of-control container. Every injectable element is registered
to the ServiceRegistry. By convention, the class' name with the first letter uncapitalized is used
(UserService -> userService).

## Features ##

* lazy initialization of services (if not registered as object)
* dependency cycle detection

An example:


class Service(object):

# this will make the service registry inject a service named "someOtherService" which
# comes from class SomeOtherService
_someOtherService = None

def __init__(self):

# will be called after everything is injected
def postInit(self):

# will be called right before the object is destroyed (the registry's destroy
# method is called)
def preDestroy(self):

class SomeOtherService(object):

# Use it like

reg = ServiceRegistry()


Once the system has all required services registered, a service can be injected by doing


class WiredUser(object):


def __init__(self, *args):

wiredUser = reg.createWired(WiredUser, 'arg1', 'arg2')


Wired objects are not automatically part of the service registry, only if added by calling `reg.registerServiceInstance`.
They can also wire new objects on the fly:


class WiredUser(object):


class UserCreator(object):

def createUser(self):
return self._serviceRegistry.createWired(WiredUser)

userCreator = reg.createWired(UserCreator)

# create some wired users
userA = userCreator.createUser()
userB = userCreator.createUser()

Project details

Download files

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

Files for pythonioc, version 0.2.5
Filename, size File type Python version Upload date Hashes
Filename, size pythonioc-0.2.5.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page