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 --Example of a figure created by generating and outputting plantuml from a specified object (arrangement of the above figure rotated 90 degrees):
Color description:
--Pink: Object with specified output --Purple: Function --Green: Class --Blue: Instance --Orange: Variable dictionary
objectuml.py
1557592976
--Output plantuml element with generator function --If there is an object link, generate the link destination and print it first
~~ 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.
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.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()
@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
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
You can also call this library from your program and output plantuml.
import objectuml
objectuml.plantuml(Object diagram Output target variable name)
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:
Edit online
button on each diagram on this site, copy and paste the above plantuml code and press the Submit
button to generate the diagram.Recommended Posts