Skip to main content

Dependency injection for pyramid views

Project description

pyramid_autowire is a package that allows your pyramid views to use pyramids registry for dependency injection.

from pyramid_autowire import autowired

@view_defaults(route_name='test_route', mapper=autowired)
class Posts:
    def __init__(
        # Standard pyramid (context, request) and (request,) arguments
        # are available through position-only args
        self, request, /,
        # the database and user objects will be injected from the registry
        database: IDatabase, active_user: IUser
    ):
        self.request = request
        self.database = database
        self.active_user = active_user

    @view_config()
    def view_post(
        self, *,
        # the matchdict is injected as keyword-only args
        post_id
    ):
        return self.database.query(Post, post_id)

def includeme(config):
    registry = config.registry

    # pretend for the example that the database session is global
    registry.registerUtility(MyDatabase(), IDatabase)

    def get_active_user(request):
        # Fetch active user from the request data
        ...

    registry.registerAdapter(get_active_user, (IRequest, ), IUser)

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_autowire-1.0.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

pyramid_autowire-1.0.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pyramid_autowire-1.0.2.tar.gz.

File metadata

  • Download URL: pyramid_autowire-1.0.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.8.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for pyramid_autowire-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d76de8d7ee33cad8ace125324de9186ee4bead7bd52e6618ad9f80dae97f1f94
MD5 21f11ffbc00ad0a5050e0e4e6e544927
BLAKE2b-256 c20438d5c395755335f91260981ffbf478926ee5a239daeba3a34b437654edc3

See more details on using hashes here.

File details

Details for the file pyramid_autowire-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyramid_autowire-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.8.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for pyramid_autowire-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fd0f21616eae0e5e8ad4e8d22fd5562cb3497113a25457298b1fcfc2026b5380
MD5 e5793b1968ac3af4ee3edee5750271e2
BLAKE2b-256 3b1f03f83417d79e8c0d9425cdaf4501e43ef7ded12fe82fa38cc7197c42ad3f

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