Skip to content

A small tool to generate and illustrate (via dot) the method resolution order graph of Python objects

Notifications You must be signed in to change notification settings

ri0t/python-mro-graph

Repository files navigation

python-mro-graph generator

A small tool to generate and illustrate (via dot) the method resolution order graph of Python objects

Based off of Michele Simionato's original work from 2003, which you can find here: http://www.phyast.pitt.edu/~micheles/python/drawMRO.html

I added packaging foam and a click based command line interface and some minor updates. Nothing that changes the basic functionality much, except perhaps an additional dot-source export.

Requirements

Generating a graph needs the dot tool installed. Usually this tool resides in the graphviz package.

Additionally - to display the resulting graphs - either imagemagick or ghostview or some other postscript or png viewer is required.

Usage

Command line tool

You will need to be able to import the object you want to investigate.

Simple usage: builtins

A simple example demonstrating usage for builtin objects:

mro-graph "" "dict"

..which displays the MRO of a simple dictionary. Notice that IMPORT_EXEC is left empty, that is because we do not need to import anything to inspect builtins.

Complex usage: packaged objects

Say, you have a package called test which contains a module called test_module which in turn contains the object you want to inspect, called test_object.

To inspect it, call mro-graph and instruct it to generate a graph:

mro-graph "from test.test_module import test_object" "test_object"

..which should result in it displaying the MRO graph to you.

See the command line help for more details:

mro-graph --help

As importable component

You can also do it this way:

from mro_graph import MROGraph

MROGraph(dict)

This will generate and display the MRO graph of Python's builtin dictionary. By using it this way, you can inspect live objects right inside your program.

License

Copyright (C) 2003 Michele Simionato

Copyright (C) 2019-2020 Heiko 'riot' Weinen

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

About

A small tool to generate and illustrate (via dot) the method resolution order graph of Python objects

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages