程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] artifact to easily visualize Python program calling process

編輯:Python

Today we share a Python The divine third-party library in the field -- pycallgraph, Through this library and in combination with graphviz Tools , Can be very convenient to complete Python Visualization of the application invocation process

Let's take a look at the renderings

What about? , It's amazing ~

Let's finish the visualization process together

install graphviz Tools

Process of generating pictures , It depends on tools graphviz Of , Let's download and install first

Download address

http://www.graphviz.org/download/

Detailed for graphviz Tools , Everyone should be familiar with , We have used this tool to visualize the decision tree , You can see it here

Introduction to data analysis series - Decision tree practice

The above link contains the detailed installation and configuration process , I won't go into that here

actual combat

Next we need to install two more Python Dependency Library

pip install pycallgraph

Let's write a basic code first

from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
class Banana:
    def eat(self):
        pass
class Person:
    def __init__(self):
        self.no_bananas()
    def no_bananas(self):
        self.bananas = []
    def add_banana(self, banana):
        self.bananas.append(banana)
    def eat_bananas(self):
        [banana.eat() for banana in self.bananas]
        self.no_bananas()
def main():
    graphviz = GraphvizOutput()
    graphviz.output_file = 'basic.png'
    with PyCallGraph(output=graphviz):
        person = Person()
        for a in range(10):
            person.add_banana(Banana())
        person.eat_bananas()
if __name__ == '__main__':
    main()

Simple code , Two simple classes are defined , The main pycallgraph The core code is main Function , stay with Under code block , Just execute the code we defined once

Run the above code , Will generate... In the current directory basic.png Picture file

From the generated image, you can clearly see the running process of the whole code , from main Code block to class initialization , It can be said that it is clear at a glance

Let's take a more complicated example

import re
from pycallgraph import PyCallGraph
from pycallgraph import Config
from pycallgraph.output import GraphvizOutput
def main():
    graphviz = GraphvizOutput()
    graphviz.output_file = 'regexp.png'
    config = Config(include_stdlib=True)
    with PyCallGraph(output=graphviz, config=config):
        reo = compile()
        match(reo)
def compile():
    return re.compile('^[abetors]*$')
def match(reo):
    [reo.match(a) for a in words()]
def words():
    return [
        'abbreviation',
        'abbreviations',
        'abettor',
        'abettors',
        'abilities',
        'ability',
        'abrasion',
        'abrasions',
        'abrasive',
        'abrasives',
    ]
if __name__ == '__main__':
    main()

The code is also not responsible , However, the compiler calls re Regular , Let's take a look at the resulting image

You can see that the whole code process is a lot more complicated , Because many regular internal functions are called internally , But the whole is still very clear

It can be said that this divine third-party library , Definitely many Python lovers , Especially just getting started Python The gospel of friends in the field , When we come across some unfamiliar and more complex code blocks , Use this library to try visualization , See if you can get inspiration from it ~

Okay , That's all I share today , Just order one if you like Fabulous Well ~

 Past highlights
It is suitable for beginners to download the route and materials of artificial intelligence ( Image & Text + video ) Introduction to machine learning series download Chinese University Courses 《 machine learning 》( Huang haiguang keynote speaker ) Print materials such as machine learning and in-depth learning notes 《 Statistical learning method 》 Code reproduction album machine learning communication qq Group 955171419, Please scan the code to join wechat group 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved