Change language

Draw a flow control graph using pycfg | python

We usually draw a control flow graph by hand using pen and paper while analyzing the control flow of a program. CFG helps us find independent paths (

a = 10

while (a " = 0 ):

if a = = 5 :

print (a)

a + = 1

print ( " exited " )

Run the command below in a terminal.

 python path_to / pycfg. py path_to / whiletest.py -d 

Output:

This approach yields graph output having nodes marked as sentences, edges between nodes.

Importing the library into the program

Using linrary import and tkinter , we can do much better than just using one file pycfg.py .

  • CFG view instead of terminal.
  • Find cyclomatic complexity also

Run the command below

 sudo pip install pycfg 

After completion use the same whiletest.py for testing. We can run the following python program in whiletest.py .

 python /path_to/this_file.py /path_to/whiletest.py 

Below is the code —

from pycfg.pycfg import PyCFG, CFGNode, slurp

import argparse

import tkinter as tk

from PIL import ImageTk, Image

 

if __ name__ = = ’__main__’ :

parser = argparse.ArgumentParser ()

  

  parser.add_argument ( ’pythonfile’ , help = ’ The python file to be analyzed’ )

args = parser.parse_args ()

arcs = []

 

cfg = PyCF G ()

cfg.gen_cfg (slurp (args.pythonfile) .strip ())

g = CFGNode.to_graph (arcs)

g.draw ( args.pythonfile + ’.png’ , prog = ’dot’ )

 

# Draw using tkinter.

root = tk.Tk ()

  root.title ( "Control Flow Graph" )

img1 = Image. open ( str (args.pythonfile) + " .png " # PIL solution

  img1 = img1.resize (( 800 , 600 ), Image.ANTIALIAS )

img = ImageTk.PhotoImage (img1)

 

background = " gray "

  

  panel = tk.Label (root, height = 600 , image = img)

  panel.pack (side = " top " , fill = "both" , expand = " yes " )

nodes = g.number_of_nodes ()  # # of nodes.

= g.number_of_edges ()  # of edges.

complexity = edges - nodes + 2   # Cyclomatic complexity

 

  frame = tk.Frame (root, bg = background)

frame.pack (side = "bottom" , fill = " both " , expand = "yes" )

 

tk.Label (frame, text = "Nodes" + str (nodes), bg = background) .pack ()

tk.Label (frame, text = "Edges" + str (edges), bg = background) .pack ()

tk.Label (frame, text = " Cyclo Complexity " +

str (complexity), bg = background) .pack ()

 

  root.mainloop ()

Output:

Link: https://pypi.org/project/pycfg/

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically