The Botox is a lightweight dependency injection pattern implementation for Python.
Project description
Botox
Botox is a lightweight dependency injection implementation based on Python typing. It helps deliver the configured functional objects, decreasing coupling between a class and its dependencies.
Delivery
Global variables? Proxy objects? Application context?
There should be one (and preferably only one) obvious way to do it.
Botox helps you isolate a class from the impact of different design changes and defects. Meaning that, instead of thinking about interdependence between software modules you will now find yourself spending much of your time having to focus a class on the task it is designed for.
Configuration
Monkey-patching? Decorators?
Explicit is better than implicit.
Botox allows a class the flexibility of being configurable. The class may act on anything that supports the intrinsic interface the class expects. Explicit configurations can be written separately for different situations that require different implementations of dependencies.
Usage
Now is better than never.
Botox doesn’t require any change in code behavior it can be applied to legacy code as a refactoring.
Installation
Install and update using pip:
pip install -U botox-di
Examples
Class Injection
Can be used to reduce boilerplate code in the application classes since all work to initialize or set up dependencies is handled separately.
class PaymentService:
...
class BillingService:
...
class SalesService:
def __init__(self, payment_service: PaymentService, billing_service: BillingService):
self.payment_service = payment_service
self.billing_service = billing_service
...
injector = Injector()
injector.prepare(PaymentService)
injector.prepare(BillingService)
injector.prepare(SalesService)
sales = injector.deliver(SalesService)
sales.do_something()
The result is class that is easier to unit test in isolation using stubs or mock objects that simulate other objects.
# test_sales.py
injector.prepare(PaymentService, PaymentServiceStub)
Value Injection
Can be used when exactly one object is needed to coordinate actions across the system.
class AppSettings:
...
settings = AppSettings()
injector = Injector()
injector.prepare(AppSettings, settings)
assert injector.deliver(AppSettings) is settings
Lambda Injection
Can be used to wrap Proxy objects in legacy code as refactoring.
from flask import g, current_app
injector = Injector()
injector.prepare(Flask, lambda: current_app)
injector.prepare(Something, lambda: g.something)
Function Injection
Can be used to make factory functions with dependencies.
def create_api_client(settings: Settings):
with open(settings.api_configuration_file) as file:
key, url = file.read(...)
return ApiClient(key, url)
injector = Injector()
injector.provide(Settings)
injector.provide(ApiClient, create_api_client)
Advanced
from aiohttp import web
from botox import Injector
class TemplateRepository:
def get_template(self):
return "Hello, {}!"
class HelloService:
def __init__(self, template_repository: TemplateRepository):
self.template_repository = template_repository
def get_hello_message(self, name):
template = self.template_repository.get_template()
return template.format(name)
async def handle(request):
service = request.app.injector.deliver(HelloService)
name = request.match_info.get('name', "Anonymous")
text = service.get_hello_message(name)
return web.Response(text=text)
app = web.Application()
app.injector = Injector()
app.injector.prepare(HelloService)
app.injector.prepare(TemplateRepository)
app.add_routes([
web.get('/', handle),
web.get('/{name}', handle)
])
web.run_app(app)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file botox-di-1.4.2.tar.gz
.
File metadata
- Download URL: botox-di-1.4.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00662a07e73d61c726c9f4f88baba68c249bca9f2daf3b839edefb79c81eb513 |
|
MD5 | 19aa6d82389acb2c2c6a443f399bf5b2 |
|
BLAKE2b-256 | 8d48a6225e84f65db8be640d376ea02b334fb9bec0d59b5f7b606c858ad0acf9 |
File details
Details for the file botox_di-1.4.2-py3-none-any.whl
.
File metadata
- Download URL: botox_di-1.4.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d00933fc24794a7c3f6c346081cfb4224ddee6ab953f9c70662b4d256353f61 |
|
MD5 | 5a5c90fc10580a8badb5b2367c616d39 |
|
BLAKE2b-256 | 58af54e87f6f255b8c4c859ee532ceccfa195f8856164364058203563f6242a8 |