What's the Best Way to Generate a Uml Diagram from Python Source Code

How to get Class diagram from Python source code?

Thanks to @Anwarvic and @bruno, I came up with the solution for this.

Firstly, create empty __init__.py file inside Client folder:

(venv) C:\Users\User\Desktop\project\Client> type NUL >  __init__.py

Then go to the parent folder of the Client folder where I want to get the class diagram:

(venv) C:\Users\User\Desktop\project> pyreverse Client -o png

But I got this error:

The output format 'png' is currently not available.
Please install 'Graphviz' to have other output formats than 'dot' or 'vcg'.

After some findings, I found this solution. Then I can run the pyreverse without any error.

This is the class diagram I got using pyreverse:

Sample Image

Automatically Generated Python Code from an UML diagram?

Enterprise Architect is able to generate python code code from UML diagrams.

It is also able to also perform some reverse engineering, and therefore maintain the two versions (UML and python) synchronized together.

However, I have never used it in that way, except for some small tests just to see it in action. I personally find productivity in Python so high, that even if I am really satisfied with EA, I prefer it just to document the design.

class diagram viewer application for python3 source

Yes there is: with pylint comes pyreverse that will generate class diagrams (not as pretty as the one in reference I am afraid, but clear and useful.) There is a dependency with graphviz.

From command line:

To analyse a full or part of a package

$ pyreverse -o png -p <project name>

To analyse one specific file:

$ pyreverse -o png -p myproject \path\to\myproject\myfile.py 

Example output: Class Diagram

Sample Image

Example output: Packages

Sample Image

Can Python pyreverse generate UML diagram for functions?

The reason is the scope of object_a: it is a local variable of the function and not an instance variable. The relationship of C with A is therefore not structural. Hence, it is not an UML association (nor aggregation, nor composition).

At best, we could say there is a usage dependency from C to A. But this usage is specific to the implementation and not necessarily meant by design.

I’m not a Python expert, but if pyreverse is able to spot the right relation with object_b, and if you’d make object_a an instance variable with self.object_a in the assignment, you can hope to get the result that you expected.

Edit: Experimental verification

If the class C is corrected as explained:

class ClassC(object):
def __init__(self):
self.object_b = ClassB()
def perform(self):
self.object_a = ClassA()

pyreverse generates indeed the right result as expected:

Sample Image

For a human reader, it's easy to miss a property. This is why pylint issues a warning on this code:

W0201: Attribute 'object_a' defined outside __init__ (attribute-defined-outside-init)

Note also that if you define a (static) class variable instead of an instance variable, pyreverse does not show it with an underlined name. The reason is probably, because it's not uncommon to hide a class variable with an instance variable of the same name.



Related Topics



Leave a reply



Submit