Skip to main content

Scope Injected ContextManager

Build Status Latest Version Python Support Examples tested with pytest-readme

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.

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)

Source distribution for scope-injected-contextmanager 0.0.2
File Size Uploaded
scope_injected_contextmanager-0.0.2.tar.gz 4.1 kB Details

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

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page