| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
Somehow in the 27 years (and counting) of active development of python, no one thought to add a simple and readable way to print stuff during development. (If you know why this is, I'd love to hear).
The wait is over:
{!examples/example.py!}{{ example_html(examples/example.py) }}
debug is like print after a good night's sleep and lots of coffee:
A more complex example of debug shows more of what it can do.
{!examples/complex.py!}{{ example_html(examples/complex.py) }}
debug will return the arguments passed to it meaning you can insert debug(...) into code.
The returned arguments work as follows:
{!examples/return_args.py!}{{ example_html(examples/return_args.py) }}
The debug namespace includes a number of other useful functions:
{!examples/other.py!}{{ example_html(examples/other.py) }}
Python comes with pretty print, problem is quite often it's not that pretty, it also doesn't cope well with non standard python objects (think numpy arrays or django querysets) which have their own pretty print functionality.
To get round this devtools comes with prettier print, my take on pretty printing. You can see it in use above in debug(), but it can also be used directly:
{!examples/prettier.py!}{{ example_html(examples/prettier.py) }}
For more details on prettier printing, see prettier.py.
{!examples/ansi_colours.py!}For more details on ansi colours, see ansi.py.
We all know the annoyance of running code only to discover a missing import, this can be particularly frustrating when the function you're using isn't used except during development.
devtool's debug function can be used without import if you add debug to __builtins__ in sitecustomize.py.
Two ways to do this:
!!! warning This is experimental, please create an issue if you encounter any problems.
To install debug into __builtins__ automatically, run:
python -m devtools installThis command won't write to any files, but it should print a command for you to run to add/edit sitecustomize.py.
To manually add debug to __builtins__, add the following to sitecustomize.py or any code which is always imported.
import sys
# we don't install here for pytest as it breaks pytest, it is
# installed later by a pytest fixture
if not sys.argv[0].endswith('pytest'):
import builtins
try:
from devtools import debug
except ImportError:
pass
else:
setattr(builtins, 'debug', debug)The ImportError exception is important since you'll want python to run fine even if devtools isn't installed.
This approach has another advantage: if you forget to remove debug(...) calls from your code, CI (which won't have devtools installed) should fail both on execution and linting, meaning you don't end up with extraneous debug calls in production code.
| Back | FazBrowse Home | New Git URL |