Use contexts (`with` statements) to manage scopes.
Project description
scoped-context
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`
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaf529ee820d1e81756e17dad5439127983e8b1e3211c7b5da043324b36bfe31
|
|
| MD5 |
c3e5e44611aa6d06536af34f501aacf4
|
|
| BLAKE2b-256 |
829a6166bb0341381251a7cd385b84d0af7e118cfefdd7430b8485fa9451527a
|
Provenance
The following attestation bundles were made for scoped_context-0.0.1.tar.gz:
Publisher:
release.yaml on shunichironomura/scoped-context
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scoped_context-0.0.1.tar.gz -
Subject digest:
aaf529ee820d1e81756e17dad5439127983e8b1e3211c7b5da043324b36bfe31 - Sigstore transparency entry: 425587215
- Sigstore integration time:
-
Permalink:
shunichironomura/scoped-context@98c0246b9fec285e4ee89332b433b3de4585843d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/shunichironomura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@98c0246b9fec285e4ee89332b433b3de4585843d -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27d597bcd721e978d3b6b59fd78c4135fd6a228f1ecd6ac4f6c188984e21cd51
|
|
| MD5 |
fed8ec5a6efcbf46d730afe2236daad0
|
|
| BLAKE2b-256 |
e9b8e9f25d5633043fc5462a36636a4fd64bb4c1ccff5b7a8cb751384d45ab28
|
Provenance
The following attestation bundles were made for scoped_context-0.0.1-py3-none-any.whl:
Publisher:
release.yaml on shunichironomura/scoped-context
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scoped_context-0.0.1-py3-none-any.whl -
Subject digest:
27d597bcd721e978d3b6b59fd78c4135fd6a228f1ecd6ac4f6c188984e21cd51 - Sigstore transparency entry: 425587227
- Sigstore integration time:
-
Permalink:
shunichironomura/scoped-context@98c0246b9fec285e4ee89332b433b3de4585843d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/shunichironomura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@98c0246b9fec285e4ee89332b433b3de4585843d -
Trigger Event:
workflow_dispatch
-
Statement type: