Online Python code generator

Python generator

We recently introduced the online code template editor in GenMyModel for creating  your own templates which generate code from your diagrams.

The editor is a full-featured MTL tool runing in the browser. Below are a few snapshots and details about the Python template of our library. In our sample library, the Python template produces the classes, attributes, inheritance relations, etc from the class diagrams. The template takes a UML model, browses its elements and translates the elements into code. The template is written in the standard MTL syntax (like Acceleo) and can easily be customized.

Provide input UML Class Diagram

The Python generator takes a UML class diagram as an input.

code generator python diagram

Preview the generated code

Open the simpleUML2Python template from the generator panel on the left and click ‘Preview’. The generated files are automatically opened on the right:

code generator python template

When generating from our your class diagram above, you end up with a single model.py file as there’s only one package.

python code

Customize the template

The simpleUML2Python template is composed of 6 template rules. Each rule deals with one UML element type and each time this element type is found in the input model it outputs the corresponding code.

code generator python view

[template public generate(m : Package)]

For each package found in the input model, it produces a Python file having the package name.

[template public generate(m : Class)]

For each package found in the input model, it produces a Python file having the package name.

[template public generate(m : Property)]

Set a default value for each attribute.

[template public generate(m : Interface)]
[template public generate(m : Operation)] 
[template public generate(m : Enumeration)] 
[template public generate(m : Enumeration)]

We’ll add more stuff here soon. As always, we’d love to hear what you think! Please reach out here or on Twitter, Facebook and UserVoice.

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

About the author

Tom Team

GenMyModel co-founder - @tomlegrd