Skip to main content

Tiny Python Dependency Injection Container.

Project description

# TinyDIC

[![Build Status](https://travis-ci.org/dareenzo/tinydic.svg?branch=master)](https://travis-ci.org/dareenzo/tinydic)
[![Coverage Status](https://coveralls.io/repos/github/dareenzo/tinydic/badge.svg?branch=master)](https://coveralls.io/github/dareenzo/tinydic?branch=master)

Tiny Python Dependency Injection Container.

## Why *TinyDIC*?

I was looking for a Dependency Injection Container in Python, googled a
bit, but only found solutions which were huge and use constructs -- such
as decorators and so on -- which I'm not so fond of, or at least don't
find a good justification for their use, for such a simple and small
thing as an DIC.

So, inspired (or copying) the PHP [Twittee IoC](https://github.com/fabpot-graveyard/twitte)
written by [Fabien Potencier](http://fabien.potencier.org) of [Symfony](http://symfony.com)
fame, I wrote this small class.

Although this is inspired by Twittee, it doesn't fit in 140 chars,
so I could not call it that. But thankfully, Fabien Potencier has another
more full fledged IoC, called Pimple, and that's where I stole the name from.
Just made it *Py* instead of *Pi* because .. err you know... it's Python.

Last, but not least, I've had lots of help on [StackOverflow reviews](http://codereview.stackexchange.com/questions/146964/simple-python-ioc)
... to setup this -- thank you so much!

Last but not least, I've to say that this is a very tiny thing meant for small personal projects so I don't recommend production use.
For something more proper and mature I recommend you check [Pymple](https://github.com/BernhardPosselt/pymple)

## Installation

```sh
pip install tinydic
```

## Usage

```python
from tinydic import Container


class MailService:

def __init__(self, user, password, server):
self.user = user
self.password = password
self.server = server

def send_mail(self, to, subject, contents):
# uses stored settings to send email
pass


def email_validator_service(container):
def functor(email):
return container.MAIL_REGEX.match(email)
return functor


class MailClient:

def __init__(self, mail_service, validate_email):
self.mail_service = mail_service
self.validate_email = validate_email

def send_mail(self, to, message):
if self.validate_email(to):
self.mail_service.send_mail(a, b)

# create container
container = Container()

# register constants / params
container.MAIL_REGEX = 'ugly long regex here'
container.username = 'spam' # container.register('username', 'spam', constant = True)
container.password = 'eggs'
container.server = 'foo.bar.com'

# register objects / services
container.mail_service = lambda c: MailService(c.username, c.password, c.server))
# container.register('mail_service', lambda c: MailService(c.username, c.password, c.server), False)
container.email_validator = email_validator_service
container.client = lambda c: MailClient(c.mail_service, c.email_validator)

container.client.send_mail('eggs@foo.bar.com', 'yo')
```


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

TinyDIC-0.1.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

TinyDIC-0.1.2-py2-none-any.whl (3.3 kB view details)

Uploaded Python 2

File details

Details for the file TinyDIC-0.1.2.tar.gz.

File metadata

  • Download URL: TinyDIC-0.1.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for TinyDIC-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c30ee5f5ff79f25de190028b3ab6c79548012ece4ae00c76ac2b55b91d3e9a9f
MD5 b1249bfdca25058d30113bfa7f4fcf25
BLAKE2b-256 84f996dd50323c13523977808353fa1873bc632dcfc5f4a85ec5837e63584b98

See more details on using hashes here.

File details

Details for the file TinyDIC-0.1.2-py2-none-any.whl.

File metadata

File hashes

Hashes for TinyDIC-0.1.2-py2-none-any.whl
Algorithm Hash digest
SHA256 7f5f7e05093e581d871c46f67ede8e3fdc43c501e78e23b543c1a369852b6279
MD5 8aa8bdf2035bc9e22197a4ed6f220366
BLAKE2b-256 a637aefb0662b53ebd48acf18ce66fcc4dfada6a42c5e42d364c1c67db1c8d30

See more details on using hashes here.

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