9

I am maintaining a Python program, and am struggling to understand the relationships between the various classes. I think it would be helpful to see a diagram of how the classes interact.

What options are there available that might allow me to do this?

David Sykes
  • 48,469
  • 17
  • 71
  • 80
  • There is another solution for this on a different SO post: https://stackoverflow.com/questions/260165/whats-the-best-way-to-generate-a-uml-diagram-from-python-source-code – Carlos A. Wong Mar 01 '21 at 16:55
  • 1
    Does this answer your question? [What's the best way to generate a UML diagram from Python source code?](https://stackoverflow.com/questions/260165/whats-the-best-way-to-generate-a-uml-diagram-from-python-source-code) – bad_coder Apr 15 '22 at 14:18

6 Answers6

3

Just my 2 cents.

Case tools like Enterprise Architect can generate class diagrams from python code, however for the purpose of understanding I prefer to coarsely model the classes and relationships by hand.

I too use UML when I want to understand new code, to get a coarse overview of the collaborations between classes, and to get a view of inheritance heirachies.

Most IDEs have means to explore the code, but I find small cohesive UML diagrams easier to digest and memorize.

I also find domain models easier to understand when on a class diagram.

chickeninabiscuit
  • 9,061
  • 12
  • 50
  • 56
1

If you are seeking IDEs that have that feature, then:

  1. Komodo
  2. Pydev for eclipse
Amirshk
  • 8,170
  • 2
  • 35
  • 64
1

gaphor has a feature to import python module and generate uml class diagrams, anyway it's not so good.

Anyway, code analysis tools on python don't work very well since no one can "predict" which arguments will be passed (or returned) by functions and so on. Most of them "guess" the type passed.

Hoping that python 3 with the "function annotation" can solve this sort of "problem"

pygabriel
  • 9,840
  • 4
  • 41
  • 54
1

Great question! Depending on how hands-on you are, you can consider using the trace module on a run of your code.

python -m trace -T yourprogram.py

Will give you who-called-who information. You can either parse this, or write some code that uses trace programatically to extract your call graph.

Once that's done, a bit of dot hacking, and you've got a diagram. Once you've done this, it'd make a cool blog post about what you did and how it worked out.

1

Check out epydoc. It's generally thought of as a documentation generator, but check out the (automatically generated) graph in this example:

http://epydoc.sourceforge.net/api/epydoc.apidoc.RoutineDoc-class.html

EnigmaCurry
  • 5,597
  • 2
  • 23
  • 15
0

If it's Django you can also do

./manage.py graph_models 

if you are using Django Command Extenstions

Adam Nelson
  • 7,932
  • 11
  • 44
  • 64