Generate and output plantuml object diagram from Python object

Object diagram

In Python, values, variables, functions, classes, methods, modules, etc. are all objects that exist in memory. By understanding what kind of objects exist in memory, you can understand the behavior of Python and understand the execution status. In fact, I sometimes comment on other Python articles by showing object diagrams from the variable dictionary.

Therefore, I made a program to generate and output a plantuml object diagram (text format) from the specified object. In addition, another work is required to convert from plantuml to an image.

--Example of comment: https://qiita.com/risuoku/items/c71e4fdb7ea9d9800e20#comment-7b342ec242f0debc8e41 --Example of an object diagram you wrote yourself image.png --Example of a figure created by generating and outputting plantuml from a specified object (arrangement of the above figure rotated 90 degrees): image.png

Color description:

--Pink: Object with specified output --Purple: Function --Green: Class --Blue: Instance --Orange: Variable dictionary

program

plantuml object diagram generation output script

objectuml.py


1557592976

Ingenuity

--Output plantuml element with generator function --If there is an object link, generate the link destination and print it first

Limitations

~~ You cannot generate a diagram from plantuml if you have cross-references (circular references) of objects. ~~ Where the objects are cross-referenced (circular reference), the display is "cycle link to ~" so that no link line is drawn.

how to use

Command execution

The source code sample.py of the object diagram shown first is shown below. The module name of sample.py is sample with .py removed, so plantuml will be generated and output by executing the command as follows.

plantuml generation output command execution


$ python3 objectuml sample

Sample code

sample.py


class A:
    prop1 = 123
    prop2 = prop1
    def hoge(self):
        return 'superhoge'
    fuga = hoge

class SubA(A):
    prop1 = 777
    def hoge(self):
        return 'hoge'

a = SubA()
Generated output plantuml
@startuml

map 15810144608 #c6ffff {
~__class__ => int
real => 123
}

map 123145300012704 #c6c6ff {
~__class__ => function
~__name__ => 'hoge'
}

map 123145300427112 #ffe2c6 {
~__class__ => mappingproxy
~__len__() => 8
['prop1'] *--> 15810144608
['prop2'] *--> 15810144608
['hoge'] *--> 123145300012704
['fuga'] *--> 123145300012704
}

map 34361518792 #c6ffc6 {
~__class__ => type
~__name__ => 'A'
~__base__ => object
~__dict__ *--> 123145300427112
}

map 123145300207888 #c6ffff {
~__class__ => int
real => 777
}

map 123145300012840 #c6c6ff {
~__class__ => function
~__name__ => 'hoge'
}

map 123145300427784 #ffe2c6 {
~__class__ => mappingproxy
~__len__() => 4
['prop1'] *--> 123145300207888
['hoge'] *--> 123145300012840
}

map 34361519736 #c6ffc6 {
~__class__ => type
~__name__ => 'SubA'
~__base__ *-> 34361518792
~__dict__ *--> 123145300427784
}

map 123145300050208 #ffe2c6 {
~__class__ => dict
~__len__() => 0
}

map 123145299742280 #c6ffff {
~__class__ *-> 34361519736
~__dict__ *--> 123145300050208
}

map 123145300050064 #ffc6ff {
~__class__ => dict
~__len__() => 11
['A'] *--> 34361518792
['SubA'] *--> 34361519736
['a'] *--> 123145299742280
}

@enduml

Standard module object diagram

It is also possible to output the object diagram of the standard library. ~~ Links are not output correctly for libraries with complicated structures, so it seems necessary to add the BUILTIN_TYPES definition. ~~ I think it is now output.

$ python3 objectuml math
Object diagram of math module

image.png

Call from the user program

You can also call this library from your program and output plantuml.

import objectuml

objectuml.plantuml(Object diagram Output target variable name)

Othello game object diagram

The following is an example of adding the process to call this library to the main function of the source code and executing it.

--Source code: https://gist.github.com/shiracamus/492201c280cf937beb114224c871a5b5 --Image converted from the generated and output plantuml: image.png

Reference material

Recommended Posts

Generate and output plantuml object diagram from Python object
How to generate a Python object from JSON
python input and output
Touch a Python object from Elixir
Python, yield, return, and sometimes yield from
Read and use Python files from Python
[Code] Module and Python version output
About Python, from and import, as
Export and output files in Python
Porting and modifying doublet-solver from python2 to python3.
Python> Output numbers from 1 to 100, 501 to 600> For csv
[python] Difference between rand and randn output
Generate C language from S-expressions in Python
Operate Sakura's cloud object storage from Python
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
A python script that draws a band diagram from the VASP output file EIGENVAL
Csv output from Google search with [Python]! 【Easy】
How to generate permutations in Python and C ++
Firebase: Use Cloud Firestore and Cloud Storage from Python
Study from Python Reading and writing Hour9 files
Read JSON with Python and output as CSV
I tried object detection using Python and OpenCV
[Python] How to read data from CIFAR-10 and CIFAR-100
[Python] Find coordinates from two angles and distance
Load and execute command from yml in python
Generate an insert statement from CSV with Python.
Create a datetime object from a string in Python (Python 3.3)
Python built-in object
Perform a Twitter search from Python and try to generate sentences with Markov chains.
python learning output
sql from python
Python built-in object
MeCab from Python
Procedure to load MNIST with python and output to png
Reading CSV data from DSX object storage Python code
Output the time from the time the program was started in python
Preferences to generate animated GIFs from Python on Mac
Receives and outputs standard output of Python 2 and Python 3> C implementations
[Python] Output battles and combinations (nesting for statements and if statements)
Control other programs from Python (communication between Python and exe)
[Python beginner] Extract prefectures and cities from addresses (3 lines).
[Python3] I want to generate harassment names from Japanese!
Get mail from Gmail and label it with Python3
Read json file with Python, format it, and output json
Generate Fibonacci numbers with Python closures, iterators, and generators
Get files from Linux using paramiko and scp [Python]
Recursively search for files and directories in Python and output
[Python] Extract only numbers from lists and character strings
Generate Word Cloud from case law data in python3
[Python] Start a batch file from Python and pass variables.
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit