Easier service location and dependency injection for Pyramid.
Usage
Define services:
# app/services/__init__.py
from .my import MyService
from .another import AnotherService
# app/services/my.py
from pyramid_di import service, RequestScopedBaseService, autowired
@service()
class MyService(RequestScopedBaseService):
def my_method(self):
return 'foobar'
# app/services/another.py
from pyramid_di import service, RequestScopedBaseService, autowired
from .my import MyService
@service()
class AnotherService(RequestScopedBaseService):
dependency = autowired(MyService)
def another_method(self):
return self.dependency.my_method()
Setup when creating the Pyramid app:
# Pyramid setup code:
from pyramid.config import Configurator
with Configurator() as config:
config.include('pyramid_di')
config.scan_services('app.services')
Use in views:
from pyramid_di import autowired
from pyramid.view import view_config
from my.services import AnotherService
class MyViews:
service = autowired(AnotherService)
def __init__(self, request):
# self.request is required for autowired to work
self.request = request
@view_config(route_name='some_view', renderer='json')
def some_view(self):
return self.service.another_method() # 'foobar'
# alternatively, without class-based views:
@view_config(route_name='some_view')
def some_view(request):
service = request.find_service(AnotherService)
service.another_method() # 'foobar'
Mocking services for testing
class MockService:
def another_method(self):
return 'mocked'
def test_views():
request = DummyRequest()
my_views = MyViews(request)
my_views.service = MockService()
assert my_views.some_view() == 'mocked'
Development
Dev setup:
$ python3 -m venv venv
$ pip install -e '.[dev]'
Tests are run with pytest:
$ pytest
Changes
0.4.4
- 2025-01-28 Add Python 3.14 support.
0.4.3
- 2025-01-28 Add support for pyramid_services 2.0+, Python 3.12 and 3.13. Drop support for Python 3.6-3.8.
0.4.2
- 2023-05-28 Change from TravisCI to Github Actions, update testing matrix to cover Python 3.10 and Pyramid 2.0+.
0.4.1
- 2021-03-19 The request-scoped services were not quite correct as they could have been instantiated twice
- once in the traversal-time and the other time after context was set. Now the context is forced to None for the request-scoped services.
0.4.0
- 2020-11-25 Python 3.6+ only; better test coverage, fixes for scoped services, deprecations and so forth.
0.3.dev0
- 2020-11 Unreleased development version
0.2.dev0
- 2020-11-04 Require Python 3 for cleaner code
0.1
- 2018-03-26 Initial release
Metadata
Release files for pyramid-di 0.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyramid_di-0.4.4.tar.gz | 6.6 kB | Without provenance | Details for pyramid_di-0.4.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyramid_di-0.4.4-py3-none-any.whl | Python 3 | none | any | Details for pyramid_di-0.4.4-py3-none-any.whl |
Total release size: 11.8 kB
Release files / pyramid_di-0.4.4.tar.gz
| Download URL | pyramid_di-0.4.4.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c57280fed50a6e0510212e529a6c2e1a35081228e6576bfeb1c5266e1e6ce1b4
|
|
BLAKE2b-256 checksum How to use checksums |
8b23d1673499b683e62cb4f50d860e726127cd0bcaaf9cdd2618d19208909580
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / pyramid_di-0.4.4-py3-none-any.whl
| Download URL | pyramid_di-0.4.4-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cac32727dc6a2c1804bf6065e491235a4d8b370e33f2665b5108d83a4dfa17ad
|
|
BLAKE2b-256 checksum How to use checksums |
6c43cc063437f1ba8b5c05f24559109464b9462b65d8f415859df2d6fcd9e722
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for pyramid-di 0.4.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.