Skip to main content

Use contexts (`with` statements) to manage scopes.

Project description

scoped-context

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

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`

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

scoped_context-0.0.1.dev1.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.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: scoped_context-0.0.1.dev1.tar.gz
  • Upload date:
  • Size: 4.0 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.dev1.tar.gz
Algorithm Hash digest
SHA256 aa45adbc6ab22893ecb5d5c76d67900581b35f4c63b49ac25308710aba3e5c17
MD5 cdc35d7ef8e376445914807480f3ade0
BLAKE2b-256 76030d57be2ef9fdaceeabb4ced645268dfe81126d8580de37abaaf74ddeed2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for scoped_context-0.0.1.dev1.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.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for scoped_context-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 7568f613922e8f825fc5e55209d4d1c5c3cc233f3d39effd959d80807b5113cd
MD5 5f95a6f1bef0da587a349c5764464cb9
BLAKE2b-256 7080d883d897e9e364c6d9cd63d8386f005ca0a7d078f1f583c927a525dbf837

See more details on using hashes here.

Provenance

The following attestation bundles were made for scoped_context-0.0.1.dev1-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.

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