VizTracer Tools can visualize and track Python Code , Let you have a deeper understanding of how it works .
With Python Projects are getting bigger and bigger 、 More complicated , It's challenging to understand it . Even if you wrote the whole project yourself , It's impossible to know exactly how a project works . In order to understand your code better , Debugging and analyzing code becomes critical .
VizTracer It's a tool like this , It's through tracking and Visualization Python Code execution process , To help you understand the code . There is no need to make any changes to the source code ,VizTracer You can record the entry of the function / exit , Function parameter / Return value and any variable , And then through Trace-Viewer Use an intuitive Google front-end interface to display data .
Here's a run Monte carlo tree search Example :
The time and time of each function are recorded on the stack , So you can see what happens when you run the program . You can zoom in to see the details of any particular point :
VizTracer You can also automatically record function parameters and return values . You can click on the function entry and see the details :
Or you can create a whole new signal , And use it to record variables . for example , This shows the cost value when performing gradient descent :
Compared with other tools with complex settings ,VizTracer It's very simple to use , And there's no dependency . You can start your pip Install it :
pip install viztracer
You can also track your program by typing (<your_script.py>
It's the name of your script ):
viztracer <your_script.py>
VizTracer A... Will be generated in your working directory HTML The report , You can Chrome Open it in the browser .
VizTracer There are other advanced features , For example, the filter function , You can use it to filter out functions you don't want to track , Get a clearer report . for example , To include only the functions in the file , You need :
viztracer include_files ./ --run <your_script.py>
Record function parameters and return values :
viztracer --log_function_args --log_return_value <your_script.py>
Record any variable that matches a regular expression :
# log variables starts with a
viztracer --log_var a.* --run <your_script.py>
You can get other functions by making minor changes to the source code , For example, custom events are used to record values and objects .
VizTracer It also includes a virtual debugger (vdb), It can debug VizTracer Log file . It can be used vdb Debug your running code ( And pdb Very similar ) So that you can understand the code flow . What's useful is , It also supports time backtracking , Because it knows what happened .
It's different from some prototypes ,VizTracer Use pure C Language realizes its core , This will greatly reduce the cost , Make it similar to cProfile The level of .
VizTracer It's open source. , stay Apache 2.0 Release under license , Supports all common operating system platforms (Linux、macOS and Windows). You can GitHub Learn more about its features and access the source code .
via: https://opensource.com/article/20/11/python-code-viztracer
author :Tian Gao Topic selection :lujun9972 translator :xiao-song-123 proofreading :wxy
This paper is written by LCTT Original compilation ,Linux China Honor roll out