Usually, we draw manual Control Flow Graph using pen and paper by analyzing the control flow of the program. CFG helps us finding independent paths (Cyclomatic Complexity), which leads to the number of test cases required to test the program. We can automate the CFG task using a Python library called pycfg. This library takes a Python script as input and gives graph as output.
We can use pycfg in 2 following ways
By Directly using the file
- Download pycfg tar file
- UnZip it
Note: pycfg.py file location after unzipping is
Let’s take whiletest.py file to get CFG.
Run below command on terminal.
python path_to/pycfg.py path_to/whiletest.py -d
This approach gives output in the form of Graph having Nodes, labeled Sentences, edges between nodes.
By importing library in the program
With help of importing linrary and tkinter, we can do way batter than just using pycfg.py file alone.
- Representing CFG instead of terminal.
- Find cyclomatic complexity also
Run the below command
sudo pip install pycfg
Once Done, using same whiletest.py for testing. We can run the following python program on whiletest.py.
python /path_to/this_file.py /path_to/whiletest.py
Below is the code –
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Draw Graph Grid Using Turtle in Python
- Python OpenCV: Optical Flow with Lucas-Kanade method
- Python OpenCV - Dense optical flow
- Draw Circle in Python using Turtle
- Find and Draw Contours using OpenCV | Python
- Draw a unstructured triangular grid as lines or markers in Python using Matplotlib
- Draw Cube and Cuboid in Python using Turtle
- Draw Shape inside Shape in Python Using Turtle
- Draw contours on an unstructured triangular grid in Python using Matplotlib
- Draw Colored Solid Cube using Turtle in Python
- Draw Panda Using Turtle Graphics in Python
- Draw Heart Using Turtle Graphics in Python
- Draw Ellipse Using Turtle in Python
- Draw Dot Patterns Using Turtle in Python
- Draw Chess Board Using Turtle in Python
- Draw Spiraling Star using Turtle in Python
- Draw Spiraling Polygon using Turtle in Python
- Draw Spiraling Square using Turtle in Python
- Draw Spiraling Triangle using Turtle in Python
- Draw an Olympic Symbol in Python using Turtle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.