Skip to main content

Components in Python

Project description

https://img.shields.io/pypi/v/compynent.svg https://img.shields.io/travis/caioaao/compynent.svg Documentation Status

An easy way to define a dependency graph between parts of your application and manage their life-cycle using the built-in context managers.

Compynent is a micro library for dependency injection and managing state in an application.

Install

Recommended install is using pip:

$ pip install compynent

Quick start

“Components” in your application are just context managers. All of them receive their dependencies in their init, and they will be initialized by the time the context is “entered”.

For example, let’s define two components A, and B, and A depends on B.

from contextlib import contextmanager
@contextmanager
def component_a(b):
    print('entered A')
    yield b
    print('exiting A')
@contextmanager
def component_b():
    print('entered B')
    yield 35
    print('exiting B')

Now, let’s define our system config and use it inside a context:

from compynent import System
system = System({'a': (component_a, ['b']),
                 'b': (component_b, [])})
with system.start() as ctx:
    print('A: %d' % ctx['a'])
    print('B: %d' % ctx['b'])

When we run the code block above, we get:

entered B
entered A
A: 35
B: 35
exiting A
exiting B

But why?

Dependency Injection is a good idea for many reasons. I suggest reading about it and learning for yourself (trust me, it’s worth it).

Now, why not use one of the many dependency injection libraries out there? My second suggestion is to start a Python REPL and run import this (or read about the zen of python). This library is focused on the second and third lines:

  • Explicit is better than implicit

  • Simple is better than complex

All the libraries fall short in one or two of these rules. Some will depend on incantations to automatically find where to inject code, while others provide a complex API for defining all sorts of dependencies, bindings, providers, containers, etc. This library doesn’t do that. It provides a way to define that component A depends on component B and that’s it. Everything else is up to the user. No singletons, factories, etc., for the sole reason that it’s not needed in Python.

Another reason is state management. One important feature of dependency injection is being able to share state between different parts of your code while maintaining the parts decoupled. This means the dependency graph is important to define the order in which the application parts must be initialized. But, if you want an application that is actually easily testable, you also want the cleanup to be done properly in the reverse order of initialization. Most libraries out there assume your program will run forever or that you will handle releasing resources manually after it’s done. Well, Python solves that pretty well already with context managers, so why not take advantage of that?

Dependency Injection Libraries

Python community came up with other great libraries. If this one is not for you, make sure to check the others out:

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

It was inspired by the Component library for Clojure, written by Stuart Sierra. I hope this project carries some of that simplicity with it.

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

compynent-1.0.1.tar.gz (11.0 kB view hashes)

Uploaded Source

Built Distribution

compynent-1.0.1-py2.py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page