{"id":26355719,"url":"https://github.com/ekiro/scopectx","last_synced_at":"2025-07-22T17:04:38.888Z","repository":{"id":57464532,"uuid":"88674461","full_name":"ekiro/scopectx","owner":"ekiro","description":"Simple context library","archived":false,"fork":false,"pushed_at":"2017-07-28T06:35:19.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-12T21:11:23.007Z","etag":null,"topics":["python","python2","python3"],"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/ekiro.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":"2017-04-18T22:03:13.000Z","updated_at":"2017-11-29T14:29:29.000Z","dependencies_parsed_at":"2022-08-31T04:52:03.083Z","dependency_job_id":null,"html_url":"https://github.com/ekiro/scopectx","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ekiro/scopectx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekiro%2Fscopectx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekiro%2Fscopectx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekiro%2Fscopectx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekiro%2Fscopectx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ekiro","download_url":"https://codeload.github.com/ekiro/scopectx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekiro%2Fscopectx/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266535695,"owners_count":23944275,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["python","python2","python3"],"created_at":"2025-03-16T13:17:31.229Z","updated_at":"2025-07-22T17:04:38.870Z","avatar_url":"https://github.com/ekiro.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# scopectx [![PyPI](https://img.shields.io/pypi/pyversions/scopectx.svg)](https://pypi.python.org/pypi/scopectx/) [![Build Status](https://travis-ci.org/ekiro/scopectx.svg?branch=master)](https://travis-ci.org/ekiro/scopectx)\nSimple scoped context library. scopectx allows you to use context object\nattached to some specific execution scope (see usage examples). \nJust like `threading.local`, but for `with` block.\n\n# Installation\n\n    pip install scopectx\n    \n# Usage examples\n\nSimplest possible usage\n\n```python\nfrom scopectx import Context\n\nctx = Context()\n\ndef some_function(s):\n    assert ctx['hello'] == s\n    \ndef nested_context():\n    with ctx:\n        ctx['hello'] = 'kiro'\n        some_function('kiro')\n    \nwith ctx:\n    ctx['hello'] = 'world'\n    some_function('world')\n    nested_context()\n    assert ctx['hello'] == 'world'\n```\n\nRequest context in some web framework:\n\n```python\n# my_framework/app.py\n\nfrom scopectx import Context\n\nrequest_ctx = Context()\n\ndef _handle_request(app, request):\n    f = app.get_handler(request)\n    with request_ctx:\n        request_ctx['db_session'] = DbSession()  # one request = one DbSession\n        respoonse = f(request.content)\n        request_ctx['db_session'].close()\n    return response\n\n\n\n# my_project/my_fancy_resource.py\n\nfrom my_framework.app import request_ctx\n\ndef index_handler(content):\n    request_ctx['some_variable'] = 'some_value'\n    some_fancy_function_also_using_db_session()    \n    request_ctx['db_session'].drop_the_base()\n    return \"hello world\"\n\n```\n\n## Documentation\n\n### scopectx.Context\n\nBase context class, creates new data for every `with` block.\n\n### scopectx.MultiLevelContext\n\nJust like `Context`, but in nested `with` block, it returns value from an\n outer block, instead of rising `KeyError` (in case you will not overwrite it,\n and it's defined somewhere upper):\n\n```python\nfrom scopectx import MultiLevelContext\n\nctx = MultiLevelContext()\n\n\ndef nested():\n    with ctx:\n        try:\n            print(ctx['sth'])  # will raise KeyError\n        except KeyError:\n            pass\n        print(ctx['hello'])  # 'world'\n        ctx['hello'] = 'kiro'\n        print(ctx['hello'])  # 'kiro'\n\n\nwith ctx:\n    ctx['hello'] = 'world'\n    print(ctx['hello'])  # 'world'\n    nested()\n    print(ctx['hello'])  # still 'world'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekiro%2Fscopectx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fekiro%2Fscopectx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekiro%2Fscopectx/lists"}