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? if you're intrigued!

Project Links

Release Information

Added

  • Backwards-compatibility! svcs is pronounced stable now -- no more rug-pulls planned! #36

  • Flask: svcs.flask.container which is a werkzeug.local.LocalProxy (like, for example, flask.current_app) and is the currently active container when accessed within a request context.


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

Uploaded Source

Built Distribution

svcs-23.20.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for svcs-23.20.0.tar.gz
Algorithm Hash digest
SHA256 603ef10f4254c99722ef94b619e29dfe0c886c9f920ee8ce88b11de0987e085b
MD5 c4777d7457429ce1939100a0eb58cf6c
BLAKE2b-256 0a44680c9d7105dd61654e39fbd429845b1781c6312d8805a5a7bad83074c4f8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for svcs-23.20.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed6af9db0b07b83e660223b6272b04103d9a58ec71fbbd96b307ae1dcf24730f
MD5 c25b5f4c7089dc22629b8d9ae9a1634f
BLAKE2b-256 be8e2fcb09ca592d1b9b41b77a8af8f31ceedafee3d1605167b1e587bff649f5

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