Generating UML Diagrams from Python Code

The Unified Modeling Language is a general-purpose, developmental, modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system.

Inspite of designing and thinking through the class structure before coding, developers might have to restructure and refactor the code during implementation. It is also a good idea to generate UML diagrams from code and see if it matches the actual design.

Pyreverse

Pyreverse is a set of utilities to reverse engineering Python code. It uses a representation of a Python project in a class hierarchy which can be used to extract any information (such as generating UML diagrams or unit tests, as pyargo and py2tests).

The package is now a part of pylint so to install it you need pylint installed in your python environement.

pip install pylint

Generating Diagrams

  1. Let's choose a project, feel free to choose any python project of your choice
git clone <python requests>
  1. Create a python virtualenv with pylint installed. Activate the environment.
pip install pylint
  1. Use pyreverse command to create the UML diagram image
pyreverse -o png <path_to_src>

The generated image looks something like this.




I recently moved my blogs from medium. Find more of my writing here.