Skip to main content

py2cfg

Python3 control flow graph generator

py2cfg is a package that can be used to produce control flow graphs (CFGs) for Python 3 programs. The CFGs it generates can be easily visualised with graphviz. That graphical analysis is the main purpose of the module.

Examples

Below is an example of a piece of code that generates the Fibonacci sequence and the CFG produced for it with py2cfg:

# fib.py

def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

fib_gen = fib()
for _ in range(10):
    next(fib_gen)

See the ast: examples/fib.ast

class Sort:
    def merge(self, l, r):
        i = 0
        j = 0
        arr = []
        size = len(l) + len(r)
        for k in range(0, size):
            lSentinel = i == len(l)
            rSentinel = j == len(r)
            if i == len(l):
                arr.append(r[j])
                j += 1
            elif j == len(r):
                arr.append(l[i])
                i += 1
            elif l[i] <= r[j]:
                arr.append(l[i])
                i += 1
            else:
                arr.append(r[j])
                j += 1

        return arr

    def merge_sort(self, src):
        n = len(src) / 2
        l = src[0:n]
        r = src[n:]
        if len(l) > 1:
            l = self.merge_sort(l)
        if len(r) > 1:
            r = self.merge_sort(r)

        src = self.merge(l, r)
        return src

    def insertion_sort(self, src):
        j = 1
        for j in range(1, len(src)):
            i = j - 1
            key = src[j]
        while i >= 0 and src[i] >= key:
            src[i + 1] = src[i]
            i = i - 1
            src[i + 1] = key
        return src

See the ast: examples/speed_sort.ast

It can also be used interactively with python's pudb debugger, which opens a browser to view the CFG, which then changes as you step through the code.

More examples

After cloning, see ./examples/ for some code snippets to run this on. To generate .svg or .png or .pdf images for each example, clone this repo and run the following command in the repo root directory:

git clone repourl
cd intorepodirectory
./generate_examples.sh

Installation via pip3

Note: installation is not required, but is handy.

To install simply run

pip3 install py2cfg --user

or clone this repo and pip install locally

git clone <url>
cd intoprojectdirectory
pip3 install . --user
# or to hack on this project:
pip3 install --editable . --user

Usage

#!/bin/bash

# Install
pip3 install py2cfg --user

# Generage flow-chart
py2cfg <yourcode.py> --short --format svg --outfile <filename>

# To also generate a diffable ast file (can help in grading, for example):
py2cfg <yourcode.py> --short --diffable --format svg --outfile <filename>

# To trace code in pudb while stepping through flow-char:
py2cfg <yourcode.py> --debug

It can be used three ways:

Run via shell command

If you have installed, then the default command is py2cfg:

py2cfg <file.py> --outfile <filename>

This will create a <file>.svg file, which contains the colored cfg of the file. If you don't want to install via pip, the innards of the py2cfg command can be run right from the repo:

Run with pudb3

py2cfg <file.py> --debug

Via wrapper

If you have not installed, then you can run a script present in the repo, py2cfg/_runner.py, to directly generate a CFG of a Python program and visualise it:

cd intoreporootdir
python3 py2cfg/_runner.py <path_to_my_code.py> --outfile <filename>

Via import

Whether or not you have installed (easier if you have), to use py2cfg in your own python code, simply import the module in your Python interpreter or program. Then use the py2cfg.CFGBuilder class to build CFGs. For example, to build the CFG of a program defined in a file with the path ./example.py, the following code can be used:

from py2cfg import CFGBuilder

cfg = CFGBuilder().build_from_file('example', './example.py')

This returns the CFG for the code in ./example.py in the cfg variable. The first parameter of build_from_file is the desired name for the CFG, and the second one is the path to the file containing the source code. The produced CFG can then be visualised with:

cfg.build_visual('exampleCFG', 'pdf')

The first paramter of build_visual is the desired name for the DOT file produced by the method, and the second one is the format to use for the visualisation.

Contributing

Issues

Modifications and improvements to this project are driven via Gitlab-Issues. Check them out to create one, or fix one.

Unit tests

Our minimal tests are run via Gitlab-CI. Make sure you don't break them!

Type hinting

Note: any new additions to the project should adhere to type-hinting standards enforced by:

mypy --strict --disallow-any-explicit *.py
  • This is a current issue -- yes we need to fix some things...

Style

To maximize the ability of version control to pin down changes, and keep the style consistent, before you make any commit to the project, run this strict code auto-formatter:

black py2cfg/*.py

Do so automatically with pre-commit: https://black.readthedocs.io/en/stable/version_control_integration.html

Project history

Note: py2cfg took it's original inspiration from an older cfg project, though has long since diverged:

Metadata

Release files for py2cfg 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py2cfg 1.0.5
File Size Uploaded
py2cfg-1.0.5.tar.gz 52.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py2cfg 1.0.5
File Interpreter ABI Platform
py2cfg-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 118.5 kB

Release files / py2cfg-1.0.5.tar.gz

Download URL py2cfg-1.0.5.tar.gz
Size 52.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bcc5b3de6d4a5bce1729dc80b103056bb581c4f0c9096ea67ff6188be2c8c62c
BLAKE2b-256 checksum
How to use checksums
36ceb316ec315a2ddce05c375d174928cba9a68b98232071b9f617dd8457dfb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.7

Release files / py2cfg-1.0.5-py3-none-any.whl

Download URL py2cfg-1.0.5-py3-none-any.whl
Size 66.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc056c10a8ea410e01c65b3e70625787f333580ab28912bbb47cdec773a7864f
BLAKE2b-256 checksum
How to use checksums
294d5861c8e94ceadba2178475dec847745a6209b584cf2e157b66f7dc63f5a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.6.0

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.0

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page