Skip to main content

A Flexible Service Locator

Project description

svcs logo showing a hexagon-shaped radar

A Flexible 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.

The code has 100% test and type coverage, and the shipped Flask and Pyramid integrations have been in production for years, but the API details can still change.

svcs (pronounced services) is a dependency container for Python. It gives you a central place to register factories for types/interfaces and then imperatively request instances of those types with automatic cleanup and health checks.

It's a flexible Inversion of Control container suitable for 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,
  • and allows for easy health checks across all services.

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)

To a type checker like Mypy, db has the type Database, api has the type WebAPIClient, and cache has the type Cache.

svcs comes with seamless integration for the Flask and Pyramid web frameworks and has first-class async support.

While svcs 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 find that intriguing!

Project Links

Release Information

Added

  • svcs now logs registrations at debug level along with a stacktrace. So if you ever get confused where your factories are coming from, set the log level to debug and trace your registrations!

Changed

  • Ooof. It's obvious in hindsight, but accessing anything directly on a request object like in the request.svcs.get() examples erases type information and everything becomes a big soup of Any.

    Therefore, we've added a new "best practice" for integrations to have a svcs_from() function that extracts containers from request objects (or from thread locals in the case of Flask).


→ Full Changelog

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svcs-23.12.0.tar.gz (125.1 kB view details)

Uploaded Source

Built Distribution

svcs-23.12.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file svcs-23.12.0.tar.gz.

File metadata

  • Download URL: svcs-23.12.0.tar.gz
  • Upload date:
  • Size: 125.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for svcs-23.12.0.tar.gz
Algorithm Hash digest
SHA256 67c71b6ac895519b343222f2a31f0334ee491ba40859bf8b318dca9840134153
MD5 0c0f3aa9e849ef630e43dd397b11caad
BLAKE2b-256 9a21e0c240b51c3ca8c177614dc24c3628b1786712a0f5b418a73478aa8ae7dc

See more details on using hashes here.

File details

Details for the file svcs-23.12.0-py3-none-any.whl.

File metadata

  • Download URL: svcs-23.12.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for svcs-23.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 430a35b7223a2c8315d113d8b9b786028a11dad8c55e91afe243c96e77fb0eaa
MD5 4465ffca91dc4a3ca0694cf2c0a50f99
BLAKE2b-256 e5bc7599ed82e321ba6d1ac713ea39f10f8467c52f1d7ed3a7cab872e920ebf0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page