views:

5215

answers:

6

A colleague is looking to generate UML class diagrams from heaps of Python source code. He's primarily interested in the inheritance relationships, and mildly interested in compositional relationships, and doesn't care much about class attributes that are just Python primitives.

The source code is pretty straightforward and not tremendously evil--it doesn't do any fancy metaclass magic, for example. (It's mostly from the days of Python 1.5.2, with some sprinklings of "modern" 2.3ish stuff.)

What's the best existing solution to recommend?

+4  A: 

If you use eclipse, maybe PyUML. Haven't used it, though.

DZPM
That's a really good suggestion, but FWIW I notice on the PyUML project site that they don't support Eclipse 3.4 (Ganymede) yet. I'll look forward to trying it when they work that out.
Bill Karwin
+1  A: 

Certain classes of well-behaved programs may be diagrammable, but in the general case, it can't be done. Python objects can be extended at run time, and objects of any type can be assigned to any instance variable. Figuring out what classes an object can contain pointers to (composition) would require a full understanding of the runtime behavior of the program.

Python's metaclass capabilities mean that reasoning about the inheritance structure would also require a full understanding of the runtime behavior of the program.

To prove that these are impossible, you argue that if such a UML diagrammer existed, then you could take an arbitrary program, convert "halt" statements into statements that would impact the UML diagram, and use the UML diagrammer to solve the halting problem, which as we know is impossible.

Glomek
Some good things, but the halt-solving hand-waiving ruins it. Pathological cases are not the matter here. Well-behaved is enough.
ddaa
What do you mean, "hand-waiving"? I didn't write out the full proofs, but I gave enough information that anyone who has seen similar proofs could create them, one for composition and one for inheritance.
Glomek
Here's an analogy: diff/patch can fail in a lot of different ways, some of them trivial. It is still very useful in a lot of real world cases. In reasonable cases, diagramming inheritance is trivial. Delegation is more tricky, but feasible by type inference within the boundaries of a package.
ddaa
+6  A: 

Epydoc is a tool to generate API documentation from Python source code. It also generates UML class diagrams, using Graphviz in fancy ways. Here is an example of diagram generated from the source code of Epydoc itself.

Because Epydoc performs both object introspection and source parsing it can gather more informations respect to static code analysers such as Doxygen: it can inspect a fair amount of dynamically generated classes and functions, but can also use comments or unassigned strings as a documentation source, e.g. for variables and class public attributes.

piro
A: 

The SPE IDE has built-in UML creator. Just open the files in SPE and click on the UML tab.

I don't know how comprhensive it is for your needs, but it doesn't require any additional downloads or configurations to use.

crystalattice
A: 

It is worth mentioning Gaphor. A Python modelling/UML tool.

Ali A
A: 

Sparx's Enterprise Architect performs round-tripping of Python source. They have a free time-limited trial edition.

chimp