pythondi
pythondi is a lightweight dependency injection library for python
Support both sync and async functions
Installation
pip3 install pythondi
Usage
First, you have to binding classes to provider.
from pythondi import Provider
provider = Provider()
provider.bind(Repo, SQLRepo)
provider.bind(Usecase, CreateUsecase)
After binding, you need to configure it to container
from pythondi import configure, configure_after_clear
# Inject with configure
configure(provider=provider)
# Or if you want to fresh inject, use `configure_after_clear`
configure_after_clear(provider=provider)
Import inject
from pythondi import inject
Add type annotations that you want to inject dependencies
class Usecase:
def __init__(self, repo: Repo):
self.repo = repo
Add decorator
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
Initialize class with no arguments
usecase = Usecase()
Or, you can also inject manually through decorator arguments
class Usecase:
@inject(repo=SQLRepo)
def __init__(self, repo):
self.repo = repo
In this case, do not have to configure providers and type annotation.
Lazy initializing
from pythondi import Provider
provider = Provider()
provider.bind(Repo, SQLRepo, lazy=True)
You can use lazy initializing through lazy option. (default False)
For singleton, use lazy=False.
class Usecase:
@inject(repo=SQLRepo)
def __init__(self, repo):
self.repo = repo
By default, manual injection is lazy. If you want a singleton, instantiate it like repo=SQLRepo().
For test
In case of test codes, you probably want to use mock objects.
In that case, you must use keyword arguments.
class MockRepo:
pass
@inject()
def test(repo: Repo):
return repo
Yes:
test(repo=MockRepo())
No:
test(MockRepo())
test(MockRepo)
Note
At the moment of inject, class is automatically initialized.
So you don't have to initialize your class inside of code.
Yes:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
No:
@inject()
def __init__(self, repo: Repo):
self.repo = repo()
General example
import abc
from pythondi import Provider, configure, configure_after_clear, inject
class Repo:
"""Interface class"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get(self):
pass
class SQLRepo(Repo):
"""Impl class"""
def get(self):
print('SQLRepo')
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
if __name__ == '__main__':
# Init provider
provider = Provider()
# Bind `Impl` class to `Interface` class
provider.bind(Repo, SQLRepo)
# Inject with configure
configure(provider=provider)
# Or if you want to fresh injection, use `configure_after_clear`
configure_after_clear(provider=provider)
# Init class without arguments
u = Usecase()
FastAPI example
from fastapi import FastAPI, APIRouter
from pythondi import Provider, configure, inject
import abc
router = APIRouter()
class Repo:
"""Interface class"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get(self):
pass
class SQLRepo(Repo):
"""Impl class"""
def __init__(self):
pass
def get(self):
print('SQLRepo')
@router.route('/')
def home():
usecase = Usecase()
usecase.repo.get()
return {'hello': 'world'}
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
def create_app():
provider = Provider()
provider.bind(Repo, SQLRepo)
configure(provider=provider)
app = FastAPI()
app.include_router(router)
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)
Flask example
from flask import Flask, Blueprint, jsonify
from pythondi import Provider, configure, inject
import abc
bp = Blueprint('home', __name__)
class Repo:
"""Interface class"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get(self):
pass
class SQLRepo(Repo):
"""Impl class"""
def __init__(self):
pass
def get(self):
print('SQLRepo')
@bp.route('/')
def home():
usecase = Usecase()
usecase.repo.get()
return jsonify({'hello': 'world'})
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
def create_app():
provider = Provider()
provider.bind(Repo, SQLRepo)
configure(provider=provider)
app = Flask(__name__)
app.register_blueprint(bp)
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)
Sanic example
import abc
from sanic import Sanic, Blueprint
from sanic.response import json
from pythondi import Provider, configure, inject
class Repo:
"""Interface class"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get(self):
pass
class SQLRepo(Repo):
"""Impl class"""
def __init__(self):
pass
def get(self):
print('SQLRepo')
bp = Blueprint('home', url_prefix='/')
@bp.route('/')
async def home(request):
usecase = Usecase()
usecase.repo.get()
return json({'hello': 'world'})
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
def create_app():
provider = Provider()
provider.bind(Repo, SQLRepo)
configure(provider=provider)
app = Sanic(__name__)
app.blueprint(bp)
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)
Django example
"""
In case of django, just put the initializing code inside of django startup
You can use project folder's __init__.py or urls.py
"""
Metadata
Release files for pythondi 1.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pythondi-1.2.4.tar.gz | 7.5 kB | Without provenance | Details for pythondi-1.2.4.tar.gz |
Release files / pythondi-1.2.4.tar.gz
| Download URL | pythondi-1.2.4.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d5b0c13b172b655a170ee4f5fce63e05e93e24138623b11f3b38f3bec6b2c56
|
|
BLAKE2b-256 checksum How to use checksums |
ff529f64db9f6cc59726b031c86768f2c47dc4c38fe69895b3e2143bd8e0b3d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Security for pythondi 1.2.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.