Debugging tools for django
Project description
dj.debug
Simple debug utils for Django apps to aid with test-driven development.
Install
$ pip install dj.debug
For development install
$ pip install -e git+git@github.com:translate/dj.debug#egg=dj.debug
$ pip install dj.debug[test]
Initializing the builtin object
The utils can be imported directly, but the better way is to use _trace builtin functionality.
To activate this call the following, anywhere in your code - eg. in your test settings
>>> from dj.debug import trace_debug
>>> trace_debug()
Once this is done, you will have a _trace object available anywhere in your environment.
>>> _trace
<dj.debug.builtin.Trace object at ...>
You can specify the name of the builtin
>>> trace_debug("_t")
>>> _t
<dj.debug.builtin.Trace object at ...>
Using the builtin object has the advantage that it won’t be recognized by linters if you accidentally leave it in your code.
Usage - pdb
The _t object provides quick access to pdb
>>> _t.pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb)
Usage - trace debugging
If a line of code that you want to debug is hit many times prior to the point at which you want to debug it you can use the _t object’s debug flag
>>> def commonly_hit_code(*args, **kwargs):
... if _t.debug: _t.pdb.set_trace()
>>> def some_other_code(*args, **kwargs):
... # we only want to debug after this point
... _t.debug = True
... something_which_triggers_commonly_hit_code()
Usage - sql debugging
This tools is useful for finding non-performant code in Django. By tracing the sql that is being run in blocks of code, you can find and fix querysets that trigger too many queries, are too complex, have overly large results, etc. You can also use the output in Django’s dbshell to analyze, improve and add indeces where appropriate.
>>> with _t.debug_sql():
... trigger_some_orm_action()
dj.debug usage
dj.debug.debug_sql
…todo…
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
File details
Details for the file dj.debug-0.0.2.tar.gz
.
File metadata
- Download URL: dj.debug-0.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b7629bde97f1cce6077ac430537089f5a2552973004951cb3590e30fec9054a |
|
MD5 | 63597c56b1554da0f88d77aff2be079d |
|
BLAKE2b-256 | da9b60b1d15995e79fff65e888937cc42daf0e52537eddd5a64e9c44236576f5 |