Typed resolver (dependency container) for Python
Project description
tres
Typed resolver (dependency container) for Python
It provides a dependency container for you to use in typed dependency resolution.
That's all. Very type resolution. Much wow.
Inspired by the dependency container in tsyringe, but more Pythonic.
Usage:
from tres import container, InjectionToken
def a(n: int) -> str:
return str(n)
def b(a: int, b: int) -> int:
return a + b
a_token = InjectionToken[Callable[[int], str]]()
b_token = InjectionToken[Callable[[int, int], int]]()
container.register(a_token, a)
container.register(b_token, b)
container.register(b_token, a) # type error
def c(f: Callable[[int], str]):
print(f(1))
c(container[a_token])
c(container[b_token]) # type error
A longer example registering a Protocol
# application logic
from typing import Protocol, Iterable
class OrdersProtocol(Protocol):
def byId(self, id) -> Order:
...
def getLines(self, id) -> Iterable[OrderLine]:
...
OrdersStoreToken = tres.InjectionToken[OrdersProtocol]()
def calculate_total(orders_store: OrdersProtocol, order_id):
order = orders_store.byId(order_id)
lines = orders_store.getLines(order_id)
return sum(line.price for line in lines) + order.shipping
# implementation
from config import URL
from domain import Order, OrderLine
from application import OrdersProtocol, OrdersStoreToken
class OrdersStore(OrdersProtocol):
def __init__(self, url):
self.url = url
def byId(self, id):
return map(Order, requests.get(f'{self.url}/order/{id}').json())
def getLines(self, id):
return map(OrderLine, requests.get(f'{self.url}/order/{id}/lines').json())
tres.container.register(OrdersStoreToken, OrdersStore(URL))
# consumer
from application import calculate_total, OrdersStoreToken
def order_view(order_id):
orders_store = tres.container[OrdersStoreToken]
order = orders_store.byId(order_id)
total = calculate_total(orders_store, order_id)
return f'{order.id} - {order.date}: {total}'
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
tres-1.3.tar.gz
(2.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tres-1.3.tar.gz.
File metadata
- Download URL: tres-1.3.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.7.5 Darwin/18.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cda155d179bd806e48d00cc0592488d8d0ce8dbce0c6c14ee7d2a6fe6fa1973
|
|
| MD5 |
3859fea93295265a8c8583c1404c513c
|
|
| BLAKE2b-256 |
ea506dc8b2a0ed6f50146d534ce6ec5a4768ae982205489059f6bcc44d1a638b
|
File details
Details for the file tres-1.3-py2.py3-none-any.whl.
File metadata
- Download URL: tres-1.3-py2.py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.7.5 Darwin/18.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da983bf30dcbfb6ffbfbf7d1e0ecb7a56be5252c769aa7050651bd359825d96e
|
|
| MD5 |
1bba20e64c172b0972a89cdb1a5a436b
|
|
| BLAKE2b-256 |
353b2b73eac3ff41972b1fcfaf371777fbc8ce125c5613e7216aabc0bbaba148
|