Context package to use data between function calls, use timers and log it for Django.
Project description
PyCTX for Django
django-pyctx is a context package to use data between function calls, use timers and log it.
For detailed documentation please visit Wiki.
Quick Start
-
Add django_pyctx to your
INSTALLED_APPS
setting like this:INSTALLED_APPS = [ #..., "django_pyctx", ]
-
Add django_pyctx.middlewares.RequestCTXMiddleware to your
MIDDLEWARE
setting like this:MIDDLEWARE = [ "django_pyctx.middlewares.RequestCTXMiddleware", # ..., ]
Please add "django_pyctx.middlewares.RequestCTXMiddleware" to at the beginning of the MIDDLEWARE list.
- Start the development server and enjoy :)
Sample Usage
-
You can reach
RequestContext
instance in views fromrequest
:request.ctx
-
Example django function-based
view
:from django.http import JsonResponse def run(request): y = 5 with request.ctx.log.timeit('index_timer'): request.ctx.log.set_data('isEven', y % 2) request.ctx.log.set_data('y', y) request.ctx.log.start_timer('timer1') import time time.sleep(1) request.ctx.log.stop_timer('timer1') time.sleep(5) return JsonResponse({})
You can see the stdout. You are probably seeing something like this:
{
"type": "REQ",
"ctxId": "28d30c66-6c00-405e-b3ac-e931097b6e50",
"startTime": "2019-08-23 10:48:18.555994",
"endTime": "2019-08-23 10:48:24.566458",
"data": {
"isEven": 1,
"y": 5
},
"timers": {
"ALL": 6.010477,
"request": 6.010464,
"index_timer": 6.008194,
"timer1": 1.004116
},
"http": {}
}
NOTE: this output formatted
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
Built Distribution
Hashes for django_pyctx-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc39bf827e8127292455a25f85f42fa9880841b2288cbb8821008e83f565f30b |
|
MD5 | 97dde247b5fcaa9bf0bbb25396173e12 |
|
BLAKE2b-256 | 5d309c15a383e050fffc2d4028b834254a81e34286c8721a829fa53f22c5cf38 |