Code Generation UML-> Python (Django)

    Four years ago, getting a new job, I saw a decent size accounting system on python + wxWidgets + MSSql. All source code was automatically generated based on UML diagrams. After a few years, I was so impressed with this development method that I began to use code auto-generation in my own projects on Django.
    Let's take a broad look at what the process of creating a “code-generated” project looks like?


    First we need to find a UML editor with great features. I am using Sybase Power Designer . In it, almost everything is possible.
    Immediately after installation, PowerDesigner can generate C ++, Java, C #, Visual Basic projects. It’s not at all difficult to write your own code generator, so we’ll write a little bit and use your own.

    Everything is ready to draw the first class:


    Let's call it Foo and add the __call__ method to it:


    Let our method print the value of one parameter passed to it:


    Now let's see what we will have at the output of the code generator for the created class: The code generator


    places the source code of each class in a separate module. The modules are arranged according to the hierarchy of the packages.
    So far we have not done anything impressive and the code generator has not justified its existence.
    Let's extend our example with two more CheckHelper classes, PrintHelper. And let’s show that Foo has connections with the composition type:


    Let's look at the generated code:

    # -*- coding: cp1251 -*-

    #***********************************************************************
    #* Module: foo.py
    #* Path: foo
    #* Author: danil
    #* Modified: 11 ноября 2009 г. 10:50:37
    #***********************************************************************
    import checkHelper
    import printHelper

    class Foo(object):
      
      
      class __C_CheckHelper(object):
        def __get__(self, obj, cls):
         if obj is None:
           return checkHelper.CheckHelper
         value = getattr(obj, '#checkHelper', None)
         if value is None:
           value = checkHelper.CheckHelper()
           setattr(obj, '#checkHelper', value)
         return value
        def __set__(self, obj, value):
         raise AttributeError
        def __delete__(self, obj):
         setattr(obj, '#checkHelper', None)
      checkHelper = __C_CheckHelper() #B #B
      
      
      class __C_PrintHelper(object):
        def __get__(self, obj, cls):
         if obj is None:
           return printHelper.PrintHelper
         value = getattr(obj, '#printHelper', None)
         if value is None:
           value = printHelper.PrintHelper()
           setattr(obj, '#printHelper', value)
         return value
        def __set__(self, obj, value):
         raise AttributeError
        def __delete__(self, obj):
         setattr(obj, '#printHelper', None)
      printHelper = __C_PrintHelper() #B #B
      
      def __call__(self, val):
        if self.checkHelper(val):
          self.printHelper(val)

    * This source code was highlighted with Source Code Highlighter.


    It can be seen that class references are wrapped through classes. Objects are created once when they are first accessed. The code generator takes on the generation of such necessary constructions, the programmer only needs to stretch the arrow with the necessary parameters.

    Let's summarize what this approach to project development gives us:
    1. Development speed due to the automatic generation of typical pieces of code.
    2. Ease of reading projects, navigating through UML diagrams is much simpler than reading source codes in files.
    3. Ease of modernization by quickly tracking all communications between objects.

    This is just a review article, if the habrosociety is interested, I’ll write step by step tutorials on generating django projects.

    Also popular now: