Skip to main content

A Service Locator for Python

Project description

svcs

svcs: A 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.

Currently only Flask support is production-ready, but API details can still change.

At this point, it's unclear whether this project will become a "proper Hynek project". I will keep using it for my work projects, but whether this will grow beyond my personal needs depends on community interest.

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

This allows you to configure and manage resources in one central place and access them all in a consistent way.


In practice that means that at runtime, you say "Give me a database connection!", and svcs will give you whatever you've configured it to return when asked for a database connection. This can be an actual database connection or it can be a mock object for testing.

If you like the Dependency Inversion Principle (aka "program against interfaces, not implementations"), you would register concrete factories for abstract interfaces; in Python usually a Protocol or an Abstract Base Class.

That:

  • unifies acquisition and cleanups of resources,
  • simplifies testing,
  • and allows for easy health checks across all resources.

No global mutable state is necessary – but possible for extra comfort.

The goal is to minimize your business code to:

def view(request):
    db = request.services.get(Database)
    api = request.services.get(WebAPIClient)

or even:

def view():
    db = services.get(Database)
    api = services.get(WebAPIClient)

The latter already works with Flask.

You set it up like this:

from sqlalchemy import Connection, create_engine

...

engine = create_engine("postgresql://localhost")

def engine_factory():
    with engine.connect() as conn:
        yield conn

registry = svcs.Registry()
registry.register_factory(Connection, engine_factory)

The generator-based setup and cleanup may remind you of Pytest fixtures.

Unlike typical dependency injection that passes your dependencies as arguments, the active obtainment of resources by calling get() when you know you're going to need it avoids the conundrum of either having to pass a factory (e.g., a connection pool -- which also puts the onus of cleanup on you), or eagerly creating resources that are never used.

svcs comes with full async support via a-prefixed methods (i.e. aget() instead of get(), et cetera).


For now, please refer to the GitHub README for latest documentation.

Release Information

Changed

  • Renamed from reg-svc to svcs. Sadly the more obvious names are all taken.

→ 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 amazing GitHub Sponsors.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

svcs-23.3.1.dev24-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file svcs-23.3.1.dev24-py3-none-any.whl.

File metadata

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

File hashes

Hashes for svcs-23.3.1.dev24-py3-none-any.whl
Algorithm Hash digest
SHA256 f20560a5ecbadb7e4652c1c4c4278b100b5dccc97882e5fa17d988b7c662e288
MD5 45f0d0051b136cb6756c23ab37e215c0
BLAKE2b-256 ffcf029c9d5545a5bdf820af242422d01389483f0f38e4d15a66e83649d8e889

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