A Flexible Service Locator
Project description
A Flexible Service Locator for Python.
svcs (pronounced services) is a dependency container for Python. It gives you a central place to register factories for types/interfaces and then imperatively acquire instances of those types with automatic cleanup and health checks.
It's suitable for implementing Inversion of Control using either dependency injection or service location while not requiring global state, decorators, or mangling of function signatures.
Benefits:
- Eliminates tons of repetitive boilerplate code,
- unifies acquisition and cleanups of services,
- provides full static type safety for them,
- simplifies testing through loose coupling,
- improves live introspection and monitoring with health checks.
The goal is to minimize the code for acquiring pluggable services to:
from svcs.your_framework import svcs_from
def view(request):
db, api, cache = svcs_from(request).get(Database, WebAPIClient, Cache)
... or less!
To a type checker like Mypy, db
has the type Database
, api
has the type WebAPIClient
, and cache
has the type Cache
.
db
, api
, and cache
will be automatically cleaned up when the request ends -- it's context managers all the way down.
svcs comes with seamless integration for AIOHTTP, FastAPI, Flask, Pyramid, and Starlette.
While svcs also has first-class support for static typing, it is strictly optional and will always remain so. svcs also doesn't check your types at runtime. It only forwards the type you have asked for to the type checker. If you don't use a type checker, that information is ignored without any runtime overhead.
Read on in Why? if you're intrigued!
Project Links
Release Information
Changed
-
Backwards-Incompatible: Since multiple people have been bit by the
enter=True
default forRegistry.register_value()
, and it's very early in svcs life, we're changing the default toenter=False
for all versions ofregister_value()
.This means that you have to explicitly opt-in to context manager behavior which makes a lot more sense for singletons like connection pools which are the most common candidates for registered values.
(The irony of shipping a backwards-incompatible change in the release directly following the adoption of a backwards-compatibility policy not lost on me.) #50 #51
Added
-
Container-local registries! Sometimes it's useful to bind a value or factory only to a container. For example, request metadata or authentication information.
You can now achieve that with
svcs.Container.register_local_factory()
andsvcs.Container.register_local_value()
. Once something local is registered, a registry is transparently created and it takes precedence over the global one when a service is requested. The local registry is closed together with the container. #56 -
Flask:
svcs.flask.registry
which is awerkzeug.local.LocalProxy
for the currently active registry onflask.current_app
.
Fixed
-
We've stopped rewriting the public names of our objects and
typing.get_type_hints()
now works on them as expected for Python 3.10 and later. #52 #53 -
The detection of container arguments in
svcs.Registry()
when using string-based type annotations is more robust now. #55
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 fabulous GitHub Sponsors.
The Bestagon radar logo is made by Lynn Root, based on an Font Awesome icon. svcs has started out as a wrapper around wired by Michael Merickel and has been heavily influenced by it.
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.21.0.tar.gz
.
File metadata
- Download URL: svcs-23.21.0.tar.gz
- Upload date:
- Size: 710.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b56db0b55c1b08b7834fba91ac7a30c0d4a238453ab660bf39de9abcb2f21b31 |
|
MD5 | aa0ec37d37371fccac8b2b0d23295bf9 |
|
BLAKE2b-256 | ebdaa079c42b943eee05081da0210e14bf585dd79633fc36794a7c72fdaa0d2e |
File details
Details for the file svcs-23.21.0-py3-none-any.whl
.
File metadata
- Download URL: svcs-23.21.0-py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f7c0cab2d022db627abad1f89d5c8a82401cd27bd9b55a701099970192b1e8b |
|
MD5 | ca2bb402de03c8073b339b5223040efa |
|
BLAKE2b-256 | a1d1c06d79c72038f4b5a9c7e4a004178786c572f984c45b358bf3dab9cabb93 |