Skip to main content

A simple Inversion of Control container

Project description

https://travis-ci.org/owncloud/ocdev.svg

Installation

This library is a Python 3.2+ library.

Install it via pip for Python 3:

sudo pip3 install pymple

Usage

Pymple nows three types of parameters:

  • Values: A value is simply value that is saved and reused for all other factories/singletons

  • Singletons: A singleton is a callable that is executed once and the result is saved so future calls to the build method will return the same instance

  • Factories: A factory is callable that is executed again everytime it is accessed

from pymple import Container

# register simple values
container.register('value', 2)
container.build('value') == 2 # True

# register singletons
class MyClass:

    def __init__(self, value):
        self.value = value


container.register_singleton('MyClass', lambda x: MyClass(x.build('value')))
container.build('MyClass') == container.build('MyClass') # True
container.build('MyClass').value == 2 # True

# register factories (no instance will be saved)
container.register_factory('MyClass', lambda x: MyClass(x.build('value')))
container.build('MyClass') == container.build('MyClass') # False
container.build('MyClass').value == 2 # True

Using the @inject decorator

Instead of registering all values in the container, you can try to let the container assemble the class automatically

# in module some.module
class A:
    pass

# somewhere else
from pymple import Container

container = Container()
a = container.build('some.module.A')

from some.module.A import A
isinstance(a, A) # True

This works if the constructor is empty. If the constructor is not empty, the container needs a map from parameter value to container value as a static _inject attribute on the class. This attribute can be set with the @inject decorator:

from pymple import inject, Container

@inject(value='some.module.A', value2='param')
class C:

    def __init__(self, value, value2):
      self.value = value
      self.value2 = value2

container = Container()
container.register('param', 3)
c = container.build('module.file.C')

from some.module import A
isinstance(c.value, A) # True
c.value2 == 3 # True

Extending the container

You can also extend the container to make it reusable:

from pymple.container import Container

class MyContainer(Container):

    def __init(self):
        super().__init__()
        self.register('value', 3)


container = MyContainer()
container.build('value') == 3 # True

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

pymple-0.0.2.tar.gz (15.1 kB view details)

Uploaded Source

File details

Details for the file pymple-0.0.2.tar.gz.

File metadata

  • Download URL: pymple-0.0.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pymple-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2ff64f8d0c479adb491ddc508a8d1a7287a8030b16c625d17a68203fe6174650
MD5 9db7be924fa816741f1eda26d75d041d
BLAKE2b-256 8c4d02f72c23b7bd14ce745e1d2b1d7af896f432d1c3e75e924cffaccf3d9135

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