Skip to main content
# 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')
```


Metadata

Release files for TinyDIC 0.1.2

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

Source distribution (sdist)

Source distribution for TinyDIC 0.1.2
File Size Uploaded
TinyDIC-0.1.2.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for TinyDIC 0.1.2
File Interpreter ABI Platform
TinyDIC-0.1.2-py2-none-any.whl Python 2 none any Details

Total release size: 6.7 kB

Release files / TinyDIC-0.1.2.tar.gz

Download URL TinyDIC-0.1.2.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c30ee5f5ff79f25de190028b3ab6c79548012ece4ae00c76ac2b55b91d3e9a9f
BLAKE2b-256 checksum
How to use checksums
84f996dd50323c13523977808353fa1873bc632dcfc5f4a85ec5837e63584b98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / TinyDIC-0.1.2-py2-none-any.whl

Download URL TinyDIC-0.1.2-py2-none-any.whl
Size 3.3 kB
Tags Python 2
SHA-256 checksum
How to use checksums
7f5f7e05093e581d871c46f67ede8e3fdc43c501e78e23b543c1a369852b6279
BLAKE2b-256 checksum
How to use checksums
a637aefb0662b53ebd48acf18ce66fcc4dfada6a42c5e42d364c1c67db1c8d30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.0

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