Skip to main content

Rumple

Simple Dependency Injection Container inspired by Pimple.

https://img.shields.io/travis/Komtet/rumple.svg?style=flat-square

Installation

Using pip:

# pip install rumple

Manually:

$ git clone https://github.com/Komtet/rumple
$ cd rumple
# python setup.py install

Usage

from rumple import Container

c1 = Container()
c2 = Container({
    'param': 'value',
    'service': lambda: object()
})

Defining services

from rumple import Container


class Service(object):
    pass


class AnotherService(object):
    def __init__(self, service):
        self.service = service

container = Container()
container['service'] = lambda: Service()
container['another_service'] = lambda c: AnotherService(c['service'])
another_service = container['another_service']
assert isinstance(another_service, AnotherService)
assert another_service.service is container['service']

Defining parameters

from rumple import Container

container = Container()
container['parameter'] = 'value'
assert container['parameter'] == 'value'

Extending services

from rumple import Container


class Service(object):
    def __init__(self):
        self.optional_dependency = None

    def set_optional_dependency(self, dependency):
        self.optional_dependency = dependency


class OptionalDependency(object):
    pass


def extend_service(service, container):  # Note: you can omit "container" argument
    service.set_optional_dependency(container['optional_dependency'])
    return service

container = Container()
container['service'] = lambda: Service()
container['optional_dependency'] = lambda: OptionalDependency()
container.extend('service', extend_service)

Providers

from rumple import Container, extend, share, value


class Provider(object):
    @share()
    def service(self):
        return object()

    @share('renamed_service')
    def another_service(self):
        return object()

    @share()
    def service_for_extend(self):
        return object()

    @value()
    def first_option(self):
        return 'value_1'

    @value('second_option_renamed')
    def second_option(self):
        return 'value_2'

    @value()
    def _ignored(self):
        return 'value'


class AnotherProvider(object):
    @extend('service_for_extend')
    def extend_service(self, service_for_extend):
        assert isinstance(service_for_extend, object)
        return str(object)

class SomeLibProvider(object):
    __prefix__ = 'some_lib.'

    @share()
    def service(self):
        return object()

    @extend('some_lib.service')
    def extend(self, service):
        return str(service)


container = Container()
container.register(Provider())
container.register(AnotherProvider(), {'additional_option': 'value1'})
container.register(SomeLibProvider(), {'additional_option': 'value2'})
assert isinstance(container['service'], object)
assert isinstance(container['renamed_service'], object)
assert isinstance(container['service_for_extend'], str)
assert container['first_option'] == 'value_1'
assert container['second_option_renamed'] == 'value_2'
assert '_ignored' not in container
assert container['additional_option'] == 'value1'
assert isinstance(container['some_lib.service'], str)
assert container['some_lib.additional_option'] == 'value2'

Iterating through a container

from rumple import Container

container = Container({'k1': 'v1', 'k2': 'v2'})
for item in container:
    print(item)

# Output:
# ('k1', 'v1')
# ('k2', 'v2')

Changelog

0.2.0 (25.01.2019)

  • Ablity to specify vendor prefix in providers

  • Fix of inspect.getargspec() deprecation

0.1.0 (15.07.2015)

  • First release

Contributing

  • Fork and clone it

  • Create your feature branch (git checkout -b awesome-feature)

  • Make your changes

  • Write/update tests, if it’s necessary

  • Update README.md, if it’s necessary

  • Push your branch (git push origin awesome-feature)

  • Send a pull request

LICENSE

The MIT License (MIT)

Metadata

Release files for rumple 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rumple 0.2.0
File Size Uploaded
rumple-0.2.0.tar.gz 4.0 kB Details

Release files / rumple-0.2.0.tar.gz

Download URL rumple-0.2.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de4b61c4dd06d78b1bbe8255da964d1d19535e603532a1f5830a4682c150cccf
BLAKE2b-256 checksum
How to use checksums
2707ce3d637dddc64b9b405f1fb231676112ee9e565995e0e490933554d7aa16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page