Skip to main content

A simple dependency injection container.

Project description

THIS README IS STILL UNDER DEVELOPMENT!!

python-simple-di is a dependency injection container implementation. With its help you can create instances and its dependencies on runtime.

Install

You can install python-simple-di via pip:

pip install python-simple-di

or via easy_install:

easy_install python-simple-di

Configuration

To configure the di.DIContainer you need to pass a dict with the needed configuration in it. Define the objects name as key to access it at runtime. The value needs to be the configuration to create the instance.

  • type (required): This option defines the type with its complete python dotted path. You can add a python that will dynamicly become added to the sys.path if the instance is requested. Examples:

    'type': 'path.to.my.Type'
    # or
    'type': '/add/to/sys/path:add.to.sys.path.Type'
    
  • args (optional): The args can be a list of values to pass as Arguments or a dict to pass as Keyword Arguments. To mix both define a dictionary with an empty string as key and a list as value. Examples:

    'args': ['first', 3, 'third']
    # or
    'args': {'one': '1', 'two':'two'}
    # or
    'args': {'': [1, 'two'], 'three': 3}
    
  • lazy (optional): This option defines weather the object will be created on runtime or will be created on container initialization. Example:

    'lazy': False # default: True
    
  • singleton (optional, default: True): If this option is set to True, the created instance will become saved inside the container. Next time the same instance will be returned. If this value is set to False a new instance will be created every time.

  • properties (optional): This options works similar to the args option. After an instance was created a buildup is called. This buildup fills the given Properties with the given values.

  • assert_type (optional): Checks weather the created type has the given base_type.

    'type': 'path.to.implementet.Type',
    'assert_type': 'path.to.parent.Type'
    

Argument Resolvers

WILL FOLLOW…

  • ref: …
  • rel: …
  • mod: …
  • factory: …
  • attr: …

Events

You can pass an EventDispatcher into the DiContainer. This Dispatcher will be called if anything interesting happens inside the Container. BaseType is di.DIEventDispatcher.

Usage

container = DIContainer(config)
instance = container.resolve('instance_key')

Project details


Release history Release notifications

History Node

1.7.0

History Node

1.6.0

History Node

1.5.3

History Node

1.5.2

History Node

1.5.1

This version
History Node

1.4.0

History Node

1.3.3

History Node

1.3.2

History Node

1.3.1

History Node

1.3.0

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
python-simple-di-1.4.0.tar.gz (6.0 kB) Copy SHA256 hash SHA256 Source None Aug 7, 2014

Supported by

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