Skip to main content

Dependency injection stuff for Pyramid

Project description

pyramid_di

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.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

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

pyramid_di-0.4.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

pyramid_di-0.4.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file pyramid_di-0.4.0.tar.gz.

File metadata

  • Download URL: pyramid_di-0.4.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for pyramid_di-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b64020a35bf256ccd2b059b50812b3ffeff40554c0ab900796904ec127fd7dd1
MD5 ec9febb9191449c3de49fd304ff24f5d
BLAKE2b-256 3c98866b043cd0d4bd5de1a726e3c1b814be636eb8eaf5b13a1e9373344389ea

See more details on using hashes here.

File details

Details for the file pyramid_di-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pyramid_di-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for pyramid_di-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65d1e5e05136692583dbe7bc89f0699afb7dbbb31bf143d50d99ecf8cfc5bc74
MD5 0219162d76790b51f469b1d58c262b0f
BLAKE2b-256 eeb69f6d6a1322a05787ccd27ea6bfe7ca4f80698515881adbe81e9e6b558fef

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