使调试与打印语句更简单,更有效的Python库:PyScribe

g6d7 9年前

一个Python库,使调试与打印语句更简单,更有效。

687474703a2f2f692e696d6775722e636f6d2f4a5a59744964612e706e67.png

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

  1. Includefrom pyscribe import pyscribeat the top of the files you are debugging.
  2. Initialize a variable of your choice topyscribe.Scriber()(E.g.:ps = pyscribe.Scriber())
  3. Make API calls as needed. (E.g.:ps.p(x))
  4. 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.

Example

test.py:
from pyscribe import pyscribe def main():      ps = pyscribe.Scriber()        ps.save_logs(True)        x = 5 ps.p(x)        bar = "foo" for i in xrange(5):          bar += str(i)          ps.iterscribe(bar)        y = "hello" ps.p(y)      ps.watch(y)        y = "world" foo = 1234 ps.d(foo)      ps.d(foo, unit="^")        synonyms = {"clerk": "secretary", "student": "apprentice", "ground": "floor"}      ps.p(synonyms) if __name__ == "__main__":      main()

项目主页:http://www.open-open.com/lib/view/home/1420342141937