{"id":13595956,"url":"https://github.com/sloev/scope_injected_contextmanager","last_synced_at":"2025-04-23T04:43:36.729Z","repository":{"id":57464535,"uuid":"241390791","full_name":"sloev/scope_injected_contextmanager","owner":"sloev","description":"A decorator/context manager that injects scope vars into a function","archived":false,"fork":false,"pushed_at":"2020-02-18T15:08:53.000Z","size":6,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T22:11:44.207Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sloev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-18T15:03:51.000Z","updated_at":"2024-06-18T14:05:38.000Z","dependencies_parsed_at":"2022-09-19T05:31:26.072Z","dependency_job_id":null,"html_url":"https://github.com/sloev/scope_injected_contextmanager","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sloev%2Fscope_injected_contextmanager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sloev%2Fscope_injected_contextmanager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sloev%2Fscope_injected_contextmanager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sloev%2Fscope_injected_contextmanager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sloev","download_url":"https://codeload.github.com/sloev/scope_injected_contextmanager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250372919,"owners_count":21419722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T16:02:02.246Z","updated_at":"2025-04-23T04:43:36.705Z","avatar_url":"https://github.com/sloev.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Scope Injected ContextManager\n\n[![Build Status](https://travis-ci.org/sloev/scope_injected_contextmanager.svg?branch=master)](https://travis-ci.org/sloev/scope_injected_contextmanager) [![Latest Version](https://img.shields.io/pypi/v/scope_injected_contextmanager.svg)](https://pypi.python.org/pypi/scope_injected_contextmanager) [![Python Support](https://img.shields.io/pypi/pyversions/scope_injected_contextmanager.svg)](https://pypi.python.org/pypi/scope_injected_contextmanager) [![Examples tested with pytest-readme](http://img.shields.io/badge/readme-tested-brightgreen.svg)](https://github.com/boxed/pytest-readme)\n\nA decorator/context manager that injects scope vars into a function\n\n* A context manager decorator for **Python 3**\n* Lets you specify variables to extract from the innner-scope (the managed scope)\nand will inject them into the `@scope_injected_contextmanager` decorated function\n* acts like a defaulted function *(think functools.partial)*\n* allows you to pass in kwargs at runtime as well\n\n## Usage\n\n*For an extensive collection of examples see [tests](./tests/test_all.py)*\n\nFunctions decorated with `@scope_injected_contextmanager` becomes context managers that can be invoked in two different ways:\n\n**As an instance** \n```python\nfrom scope_injected_contextmanager import scope_injected_contextmanager\n@scope_injected_contextmanager\ndef decorated_function(): pass\n\nwith decorated_function:\n    something = 100\n```\n\n**Or as a function**\n\n```python\nfrom scope_injected_contextmanager import scope_injected_contextmanager\n@scope_injected_contextmanager\ndef decorated_function(): pass\n\nwith decorated_function():\n    something = 100\n```\n\n### Simple example\n\n```python\n\nfrom scope_injected_contextmanager import scope_injected_contextmanager\n\nfetch = lambda request: (\"ok\", 200)\n\n@scope_injected_contextmanager\ndef log_request(request, response):\n    print(f\"request: {request} response: {response}\")\n\nwith log_request:\n    request = {\n        \"query_args\": {\n            'foo': 10\n        }\n    }\n    response = fetch(request)\n\n# prints\n# request: {'query_args': {'foo': 10}} response: ('ok', 200)\n```\n\n### Advanced example\n\n```python\n\nfrom scope_injected_contextmanager import scope_injected_contextmanager\n\nfetch = lambda request: (\"ok\", 200)\n\n@scope_injected_contextmanager\ndef log_request(request, response, some_explicit_variable=None):\n    print(f\"request: {request} response: {response} some_explicit_variable: {some_explicit_variable}\")\n\nwith log_request(some_explicit_variable=\"foo):\n    request = {\n        \"query_args\": {\n            'foo': 10\n        }\n    }\n    response = fetch(request)\n\n# prints\n# request: {'query_args': {'foo': 10}} response: ('ok', 200) some_explicit_variable: foo\n```\n\n## why?\n\nI needed a low-on-syntax context-manager that would log request_args and response.\nWhen looking at implementing it i ran into this issue, and asked for help: \n[\"Spooky action observed in Python context manager\"](https://stackoverflow.com/questions/60270909/spooky-action-observed-in-python-context-manager)\n\nI was let down by the fact that you apparently can't give a context manager access to your variables.\n\nThats why i hacked this together ;-)\n\n## Testing\n\nsee [tests](./tests/test_all.py)\n\nrun `make setup-all tox` on a (linux or osx) with [pyenv](https://github.com/pyenv/pyenv#installation) installed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsloev%2Fscope_injected_contextmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsloev%2Fscope_injected_contextmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsloev%2Fscope_injected_contextmanager/lists"}