使调试与打印语句更简单,更有效的Python库:PyScribe
g6d7
10年前
一个Python库,使调试与打印语句更简单,更有效。
Installation
To install pyscribe:
$ pip install pyscribe
It may be necessary to have root privileges, in which case:
$ sudo pip install pyscribe
To uninstall:
$ pip uninstall pyscribe
Usage
- Includefrom pyscribe import pyscribeat the top of the files you are debugging.
- Initialize a variable of your choice topyscribe.Scriber()(E.g.:ps = pyscribe.Scriber())
- Make API calls as needed. (E.g.:ps.p(x))
- Run one of the following commands
$ pyscribe myfile.py
This is the equivalent of running$ python myfile.pywith all calls desugared.
$ pyscribe myfile.py --extraargs "-u asdf"
This is the equivalent of running$ python myfile.py -u asdfwith all calls desugared.
$ pyscribe myfile.py --desugared
This does not run anything, but rather outputs a myfile_desugared.py, which is intended to be run to debug.
Argument Options
- --extraargs-- Arguments intended to be passed to Python file when run. Must be called with --run set
- --clean-- Produce a clean version of the file with all references to PyScribe removed
- --desugared-- Produce a desugared version of the file with all API calls replaced with valid Python.
- --log-- Save logs to a pyscribe_log.txt file along with timestamp.
API Calls
- pyscribe.Scriber(labels=[])-- Initialize PyScribe. If you're scribing values with labels, you can filter by labels by passing in a list of the labels as strings.
- pyscribe.p(object, label=None)-- Print the object value with relevant info dependent on type
- pyscribe.iterscribe(object)-- Log the object value from inside a for or while loop which prints current iteration
- pyscribe.watch(object)-- Log the object whenever its value changes
- pyscribe.d(object, unit="*")-- Distinguish the log with a clear separator defined by the unit
Tests
Test modules are in thetestsdirectory. Specific test cases are in these modules in the form oftestcase.py, and the test runner compares these withtestcase_correct.