Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ajenti/jadi
A minimalistic DI container
https://github.com/ajenti/jadi
di ioc python
Last synced: 9 days ago
JSON representation
A minimalistic DI container
- Host: GitHub
- URL: https://github.com/ajenti/jadi
- Owner: ajenti
- License: lgpl-3.0
- Created: 2015-04-22T13:51:31.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-10-28T07:27:29.000Z (about 3 years ago)
- Last Synced: 2024-10-11T11:53:56.397Z (about 1 month ago)
- Topics: di, ioc, python
- Language: Python
- Homepage: http://docs.ajenti.org/en/latest/ref/jadi.html
- Size: 10.7 KB
- Stars: 8
- Watchers: 5
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jadi
[![Build Status](https://travis-ci.org/ajenti/jadi.svg)](https://travis-ci.org/ajenti/jadi)
Minimalistic IoC for Python
```python
import os
import subprocess
from jadi import service, component, interface, Context# @services are singletons within their Context
@service
class FooService(object):
def __init__(self, context):
self.context = contextdef do_foo(self):
BarManager.any(self.context).do_bar()@interface
class BarManager(object):
def __init__(self, context):
pass# @components implement @interfaces
@component(BarManager)
class DebianBarManager(BarManager):
def __init__(self, context):
pass@classmethod
def __verify__(cls):
return os.path.exists('/etc/debian_version')def do_bar(self):
subprocess.call(['cowsay', 'bar'])@component(BarManager)
class RHELBarManager(BarManager):
def __init__(self, context):
pass@classmethod
def __verify__(cls):
return os.path.exists('/etc/redhat-release')def do_bar(self):
subprocess.call(['yum', 'install', 'bar'])# Context tracks instantiated @services and @components
ctx = Context()assert FooService.get(ctx) == FooService.get(ctx)
# .all() returns instance of each implementation
assert len(BarManager.all(ctx)) == 2# .classes() returns class of each implementation
assert len(BarManager.classes(ctx)) == 2# .any() returns first available implementation
assert isinstance(BarManager.any(ctx), BarManager)foo = FooService.get(ctx)
foo.do_foo()```