Skip to main content

An opinionated dependency injection library for Python

Project description

PySyringe

An opinionated dependency injection library for Python.

A container that does not rely on adding decorators to your domain classes. It only wraps views in the infrastructure layer to keep your domain and app layer decoupled from the framework and the container.

Usage

# container.py
from myapp.domain import CalendarInterface
from myapp.infra import LoggingEmailSender, SmtpEmailSender, Calendar
from django.core.http import HttpRequest, HttpResponse


class Factory:
    def __init__(self, environment: str) -> None:
        self.environment = environment

    def get_mailer(self) -> EmailSender:
        if self.environment == "production":
            return SmtpEmailSender("mta.example.org", 25)

        return LoggingEmailSender()


factory = Factory(str(settings.ENVIRONMENT))

container = Container(factory)
container.never_provide(HttpRequest)
container.never_provide(HttpResponse)
container.alias(CalendarInterface, Calendar)


# views.py
from container import container

@container.inject
def my_view(request: HttpRequest, calendar: CalendarInterface) -> HttpResponse:
    now = calendar.now()
    return HttpResponse(f"Hello, World! The current time is {now}")

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

pysyringe-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysyringe-1.0.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file pysyringe-1.0.0.tar.gz.

File metadata

  • Download URL: pysyringe-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Linux/6.13.4-arch1-1

File hashes

Hashes for pysyringe-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5ecd212023de39586e9d94a14a6c1fa1faa102c82652296586b5b98621bf61f4
MD5 cd6df89323cb3fbe455e9083f6e308f1
BLAKE2b-256 f3adae2032cf3ee3994e477ebec0375e1136d1dfd836515383f6e49e44086731

See more details on using hashes here.

File details

Details for the file pysyringe-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pysyringe-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Linux/6.13.4-arch1-1

File hashes

Hashes for pysyringe-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 afec2169c216615e7369e929815785eb52da882c382e160f0a8a7e76202c741d
MD5 44a0f86fd035c837786619f53a289125
BLAKE2b-256 f6b5cef067419a7a3ff8acca372884276fe81f3e747aec363a4f322ebd6fcdf5

See more details on using hashes here.

Supported by

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