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 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,
  • 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)

... or less!

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 AIOHTTP, Flask, Pyramid, and first-class async support.

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

Project Links

Release Information

Added

  • A ResourceWarning is now raised when a container or a registry are garbage-collected with pending cleanups.

Changed

  • Cleanups for services are internally context managers now. For your convenience, if you pass an (async) generator function for a factory, the registry will automatically wrap it for you into an (async) context manager. #92

  • Pyramid: svcs.pyramid.get() now takes a Pyramid request as the first argument. svcs.pyramid.get_pings() also doesn't look at thread locals anymore. If you still want to use thread locals, you can use svcs.pyramid.from_svcs(None) to obtain the currently active container.

  • Flask: replace_(value|factory)() is now called overwrite_(value|factory()) to be consistent with the docs lingo. They also completely reset the instantiation cache now (practically speaking: they close the container).

Removed

  • svcs.Container.forget_about(). It doesn't make any sense in a world of recursive dependencies. Just reset the container using svcs.Container.(a)close().

→ 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.15.0.tar.gz (691.1 kB view details)

Uploaded Source

Built Distribution

svcs-23.15.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: svcs-23.15.0.tar.gz
  • Upload date:
  • Size: 691.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.15.0.tar.gz
Algorithm Hash digest
SHA256 94aec2268acaaa3b1da57d63ebbd0a18bd166c341e25af48a9cebc51dd3ff9f4
MD5 dbd7b0374bb32aa532fd0044280d496e
BLAKE2b-256 c49117a8977d793b09976cf3698f68642b4ed462f7df82db111f0a0735a8eba4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: svcs-23.15.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 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.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c11b6812b542bb863dcbb8adae6b728c9de35ffffc18c2cbcda2fecd5fc03a50
MD5 03518027b350e7b23b4b02de3efbd156
BLAKE2b-256 9c351a150b44c0d713ad9b2366741db5b0bbff9e4dcd9c37e5ff3a6f26500b05

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