Scope Injected ContextManager
A decorator/context manager that injects scope vars into a function
- A context manager decorator for Python 3
- Lets you specify variables to extract from the innner-scope (the managed scope)
and will inject them into the
@scope_injected_contextmanagerdecorated function - acts like a defaulted function (think functools.partial)
- allows you to pass in kwargs at runtime as well
Usage
For an extensive collection of examples see tests
Functions decorated with @scope_injected_contextmanager becomes context managers that can be invoked in two different ways:
As an instance
from scope_injected_contextmanager import scope_injected_contextmanager
@scope_injected_contextmanager
def decorated_function(): pass
with decorated_function:
something = 100
Or as a function
from scope_injected_contextmanager import scope_injected_contextmanager
@scope_injected_contextmanager
def decorated_function(): pass
with decorated_function():
something = 100
Simple example
from scope_injected_contextmanager import scope_injected_contextmanager
fetch = lambda request: ("ok", 200)
@scope_injected_contextmanager
def log_request(request, response):
print(f"request: {request} response: {response}")
with log_request:
request = {
"query_args": {
'foo': 10
}
}
response = fetch(request)
# prints
# request: {'query_args': {'foo': 10}} response: ('ok', 200)
Advanced example
from scope_injected_contextmanager import scope_injected_contextmanager
fetch = lambda request: ("ok", 200)
@scope_injected_contextmanager
def log_request(request, response, some_explicit_variable=None):
print(f"request: {request} response: {response} some_explicit_variable: {some_explicit_variable}")
with log_request(some_explicit_variable="foo):
request = {
"query_args": {
'foo': 10
}
}
response = fetch(request)
# prints
# request: {'query_args': {'foo': 10}} response: ('ok', 200) some_explicit_variable: foo
why?
I needed a low-on-syntax context-manager that would log request_args and response. When looking at implementing it i ran into this issue, and asked for help: "Spooky action observed in Python context manager"
I was let down by the fact that you apparently can't give a context manager access to your variables.
Thats why i hacked this together ;-)
Testing
see tests
run make setup-all tox on a (linux or osx) with pyenv installed.
Metadata
Release files for scope-injected-contextmanager 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scope_injected_contextmanager-0.0.2.tar.gz | 4.1 kB | Without provenance | Details for scope_injected_contextmanager-0.0.2.tar.gz |
Release files / scope_injected_contextmanager-0.0.2.tar.gz
| Download URL | scope_injected_contextmanager-0.0.2.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd60c2cee8ba04ed14f23868e22f1f9dbffc669ecdc2107c817e944995f1e00e
|
|
BLAKE2b-256 checksum How to use checksums |
02e3aa7449697d06a9b5f08a7ae4d4ff7aa14b48dd02576e7fc157e1992eb15f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5
|
Security for scope-injected-contextmanager 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.