A Service Locator for Python
Project description
svcs: A Service Locator for Python
Warning ☠️ Not ready yet! ☠️
This project is only public to gather feedback, and everything can and will change until the project is proclaimed stable.
Currently only Flask support is production-ready, but API details can still change.
At this point, it's unclear whether this project will become a "proper Hynek project". I will keep using it for my work projects, but whether this will grow beyond my personal needs depends on community interest.
svcs (pronounced services) is a service locator for Python. It provides you with a central place to register factories for types/interfaces and then imperatively request instances of those types with automatic cleanup and health checks.
This allows you to configure and manage resources in one central place and access them all in a consistent way.
In practice that means that at runtime, you say "Give me a database connection!", and svcs will give you whatever you've configured it to return when asked for a database connection. This can be an actual database connection or it can be a mock object for testing.
If you like the Dependency Inversion Principle (aka "program against interfaces, not implementations"), you would register concrete factories for abstract interfaces; in Python usually a Protocol
or an Abstract Base Class.
That:
- unifies acquisition and cleanups of resources,
- simplifies testing,
- and allows for easy health checks across all resources.
No global mutable state is necessary – but possible for extra comfort.
The goal is to minimize your business code to:
def view(request):
db = request.services.get(Database)
api = request.services.get(WebAPIClient)
or even:
def view():
db = services.get(Database)
api = services.get(WebAPIClient)
The latter already works with Flask.
You set it up like this:
from sqlalchemy import Connection, create_engine
...
engine = create_engine("postgresql://localhost")
def engine_factory():
with engine.connect() as conn:
yield conn
registry = svcs.Registry()
registry.register_factory(Connection, engine_factory)
The generator-based setup and cleanup may remind you of Pytest fixtures.
Unlike typical dependency injection that passes your dependencies as arguments, the active obtainment of resources by calling get()
when you know you're going to need it avoids the conundrum of either having to pass a factory (e.g., a connection pool -- which also puts the onus of cleanup on you), or eagerly creating resources that are never used.
svcs comes with full async support via a-prefixed methods (i.e. aget()
instead of get()
, et cetera).
For now, please refer to the GitHub README for latest documentation.
Release Information
Changed
- Renamed from reg-svc to svcs. Sadly the more obvious names are all taken.
Credits
svcs is written by Hynek Schlawack and distributed under the terms of the MIT license.
The development is kindly supported by my employer Variomedia AG and all my amazing GitHub Sponsors.
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 svcs-23.4.0.tar.gz
.
File metadata
- Download URL: svcs-23.4.0.tar.gz
- Upload date:
- Size: 22.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06313fba3d66b22849cfcae797a8fe875221ef0c452ef3f300296f03b0f6b2c6 |
|
MD5 | 570aab5c8e9b3e0a48a2c2dc79136033 |
|
BLAKE2b-256 | 3b0e661ecd159a9c14bd7b0d88c97e77b810478c47d6afb701a0d82b02eb8438 |
File details
Details for the file svcs-23.4.0-py3-none-any.whl
.
File metadata
- Download URL: svcs-23.4.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27b188cd9ff63b556d0a63861a8950c343f6bda9658bc0a5236faca5948580d1 |
|
MD5 | d185f5ded97f4064aa796e133113d8cd |
|
BLAKE2b-256 | 384687d0e33b0b05b3b2cc825cd05a60c09c039fcc984506b543fa2e1fa3521b |