https://github.com/ekiro/scopectx
Simple context library
https://github.com/ekiro/scopectx
python python2 python3
Last synced: 8 months ago
JSON representation
Simple context library
- Host: GitHub
- URL: https://github.com/ekiro/scopectx
- Owner: ekiro
- License: mit
- Created: 2017-04-18T22:03:13.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-28T06:35:19.000Z (over 8 years ago)
- Last Synced: 2025-06-12T21:11:23.007Z (10 months ago)
- Topics: python, python2, python3
- Language: Python
- Size: 6.84 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scopectx [](https://pypi.python.org/pypi/scopectx/) [](https://travis-ci.org/ekiro/scopectx)
Simple scoped context library. scopectx allows you to use context object
attached to some specific execution scope (see usage examples).
Just like `threading.local`, but for `with` block.
# Installation
pip install scopectx
# Usage examples
Simplest possible usage
```python
from scopectx import Context
ctx = Context()
def some_function(s):
assert ctx['hello'] == s
def nested_context():
with ctx:
ctx['hello'] = 'kiro'
some_function('kiro')
with ctx:
ctx['hello'] = 'world'
some_function('world')
nested_context()
assert ctx['hello'] == 'world'
```
Request context in some web framework:
```python
# my_framework/app.py
from scopectx import Context
request_ctx = Context()
def _handle_request(app, request):
f = app.get_handler(request)
with request_ctx:
request_ctx['db_session'] = DbSession() # one request = one DbSession
respoonse = f(request.content)
request_ctx['db_session'].close()
return response
# my_project/my_fancy_resource.py
from my_framework.app import request_ctx
def index_handler(content):
request_ctx['some_variable'] = 'some_value'
some_fancy_function_also_using_db_session()
request_ctx['db_session'].drop_the_base()
return "hello world"
```
## Documentation
### scopectx.Context
Base context class, creates new data for every `with` block.
### scopectx.MultiLevelContext
Just like `Context`, but in nested `with` block, it returns value from an
outer block, instead of rising `KeyError` (in case you will not overwrite it,
and it's defined somewhere upper):
```python
from scopectx import MultiLevelContext
ctx = MultiLevelContext()
def nested():
with ctx:
try:
print(ctx['sth']) # will raise KeyError
except KeyError:
pass
print(ctx['hello']) # 'world'
ctx['hello'] = 'kiro'
print(ctx['hello']) # 'kiro'
with ctx:
ctx['hello'] = 'world'
print(ctx['hello']) # 'world'
nested()
print(ctx['hello']) # still 'world'
```