Skip to main content

Converts python source code to colorful Control Flow Graphs (CFGs).

Project description

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)

The image only works in the Gitlab README.md

After cloning, see ./examples/ for some code snippets to run this on. To generate *_cfg.png 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

Usage

It can be used three ways:

Run via shell command

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

py2cfg <file.py>

This will create a _cfg.png 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:

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

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! [ ] This is a current task: the tests work locally, but not all work on Gitlab-CI...

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 an commit to the project, run:

black py2cfg/*.py

Project history

Note: py2cfg is a significantly re-worked and improved fork of the older staticfg project:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2cfg-0.3.4.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py2cfg-0.3.4-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file py2cfg-0.3.4.tar.gz.

File metadata

  • Download URL: py2cfg-0.3.4.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.7 Linux/4.19.107-1.pvops.qubes.x86_64

File hashes

Hashes for py2cfg-0.3.4.tar.gz
Algorithm Hash digest
SHA256 fb6cc1b9c2f5e77286ea91308bac07d6ed40a3a6a2d4f2d4c129c2d8a64b4f4c
MD5 02d828066fefdab26cb11dea548255e3
BLAKE2b-256 6b7adc48023b6d1528c2a1bf391ff60d76eb0389051a174ce9d3b540150e3fb6

See more details on using hashes here.

File details

Details for the file py2cfg-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: py2cfg-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.7 Linux/4.19.107-1.pvops.qubes.x86_64

File hashes

Hashes for py2cfg-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 408e398fbcb4bbd33cdd2250bad85c46f6ef5c4e832225dd2886e027e654535d
MD5 148328428ff2007a03bca4a3bf6dd6cf
BLAKE2b-256 ebd68f007f47577329e61f274df13b0ea0c18f4cb8f5a748e43c1942e7d8923c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page