Skip to main content

scoped-context

PyPI PyPI - License PyPI - Python Version uv Ruff PyPI - Downloads

scoped-context helps manage scopes using context managers in Python.

Installation

pip install scoped-context

or

uv add scoped-context

Usage

Get the context stack and current context of a specific class:

from scoped_context import ScopedContext

class A(ScopedContext):
    pass

with A() as a1:
    print(A.context_stack())  # -> `[a1]`
    print(A.current())  # -> `a1`

    with A() as a2:
        print(A.context_stack())  # -> `[a1, a2]`
        print(A.current())  # -> `a2`

    print(A.context_stack())  # -> `[a1]`
    print(A.current())  # -> `a1`

print(A.context_stack())  # -> `[]`
print(A.current())  # -> raises scoped_context.NoContextError

You can mix different subclasses of ScopedContext, and access the class-wide context:

from scoped_context import ScopedContext, get_current_context, get_context_stack

class A(ScopedContext):
    pass

class B(ScopedContext):
    pass

class C(ScopedContext):
    pass

with A() as a1:
    with B() as b1:
        with C() as c1:
            # class-wide context
            print(get_context_stack())  # -> `[a1, b1, c1]`
            print(get_current_context())  # -> `c1`

            # You can specify classes (recommended)
            print(get_context_stack((A, B)))  # -> `[a1, b1]`
            print(get_current_context((A, B)))  # -> `b1`

            # class-specific context
            print(A.context_stack())  # -> `[a1]`
            print(A.current())  # -> `a1`
            print(B.context_stack())  # -> `[b1]`
            print(B.current())  # -> `b1`
            print(C.context_stack())  # -> `[c1]`
            print(C.current())  # -> `c1`

Download files

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

Source Distribution

scoped_context-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

scoped_context-0.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file scoped_context-0.0.1.tar.gz.

File metadata

  • Download URL: scoped_context-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for scoped_context-0.0.1.tar.gz
Algorithm Hash digest
SHA256 aaf529ee820d1e81756e17dad5439127983e8b1e3211c7b5da043324b36bfe31
MD5 c3e5e44611aa6d06536af34f501aacf4
BLAKE2b-256 829a6166bb0341381251a7cd385b84d0af7e118cfefdd7430b8485fa9451527a

See more details on using hashes here.

Provenance

The following attestation bundles were made for scoped_context-0.0.1.tar.gz:

Publisher: release.yaml on shunichironomura/scoped-context

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file scoped_context-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: scoped_context-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for scoped_context-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27d597bcd721e978d3b6b59fd78c4135fd6a228f1ecd6ac4f6c188984e21cd51
MD5 fed8ec5a6efcbf46d730afe2236daad0
BLAKE2b-256 e9b8e9f25d5633043fc5462a36636a4fd64bb4c1ccff5b7a8cb751384d45ab28

See more details on using hashes here.

Provenance

The following attestation bundles were made for scoped_context-0.0.1-py3-none-any.whl:

Publisher: release.yaml on shunichironomura/scoped-context

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 files

Supported by

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