A decorator/context manager that injects scope vars into a function
Project description
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_contextmanager
decorated 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.
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
File details
Details for the file scope_injected_contextmanager-0.0.2.tar.gz
.
File metadata
- Download URL: scope_injected_contextmanager-0.0.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd60c2cee8ba04ed14f23868e22f1f9dbffc669ecdc2107c817e944995f1e00e |
|
MD5 | 3962aff261bef1b4dd18ed09c3a722ad |
|
BLAKE2b-256 | 02e3aa7449697d06a9b5f08a7ae4d4ff7aa14b48dd02576e7fc157e1992eb15f |