Skip to main content

A Flexible Service Locator

Project description

svcs logo showing a hexagon-shaped radar

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? or watch this short video if you're intrigued:

Watch the video

Project Links

Release Information

Added

  • Python 3.13 support.

  • svcs.Registry now implements a __iter__ method that allows to iterate over its registered services. #106

Removed

  • Python 3.8 support.

Changed

  • Flask: The registry is now stored on app.extensions, not app.config. This is an implementation detail. If you are directly accessing the registry via app.config, this is a breaking change, though you should ideally move to svcs.flask.registry anyway. #71 #72 #73

  • Registry.register_factory() is now more lenient regarding the arguments of the factory. It only looks at the first argument (if present) and ignores the rest. #110

Fixed

  • Container.aget() now also enters and exits synchronous context managers. #93

  • Container.aget() now also enters and exits context managers that are returned by async factories. #105


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-25.1.0.tar.gz (714.1 kB view details)

Uploaded Source

Built Distribution

svcs-25.1.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: svcs-25.1.0.tar.gz
  • Upload date:
  • Size: 714.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for svcs-25.1.0.tar.gz
Algorithm Hash digest
SHA256 64dd74d0c4e8fee79a9ac7550a9d824670b228df390ba1911614e29b59b3f2c2
MD5 1304a881e311f6da70136e8134ebf67c
BLAKE2b-256 08e12d56ec21820cae24a6215dc1d6a224167b0e24368bf53166551064742e0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for svcs-25.1.0.tar.gz:

Publisher: pypi-package.yml on hynek/svcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: svcs-25.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for svcs-25.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df49cb7d1a05dfd2dd60af1a2cb84b9c3bb0a74728833cb8c54a7ceeecce6c97
MD5 0b83424d576eea2e48bb125099bc54d5
BLAKE2b-256 a21ca6b5d90a9ca479805798276728ccbbdff0c7228e2ea93b1f731779d635e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for svcs-25.1.0-py3-none-any.whl:

Publisher: pypi-package.yml on hynek/svcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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