Python Code Generator - Zontroy

Python Code Generator

Generate Python Code and Get More Productive

Currently there is an ongoing discussion among developers about code generators and if they are beneficial or not. Similar discussions were held in the industrial revolution hundreds year ago. These two situations are very similar and code generation seems an inevitable step for software development. Currently, we are already using code generation by using intellisense features of an IDE or plugins. Code generators are similar, but more specialized tools that are one step ahead in this regard. When we set out with this thought, a Python Code Generator is a required tool for a python developer to be efficient and productive.

Zontroy Generator is a Python Code Generator as well as for other programming languages, and it provides an environment facilitating software development. You will speed up by getting rid of the tedious tasks with Zontroy.
Let’s use Zontroy to generate some Python code. First of all, we need a data model. For this example, we are going to use the well-known DVDRENTAL database.

You can download the database file from postgresqltutorial.com or you can use your own database.

Also, if you haven’t installed Zontroy yet, download free version here.

Creating a Zontroy Project

Our database is ready and now we are going to create our Zontroy Project. After running Zontroy, go to File -> New Project.

Click Create button.

Enter the required database and project information according to your local configuration.

After clicking Create, our empty project is going to be similar to the screenshot provided below. The zproject file is the main project file of Zontroy. There is no need to write code inside this file unless there is a special need for that.

Creating Template File

A template file functions as the input for Zontroy for it to generate code. The output will be generated code. How the output code is organized varies according to the type of the template file type we use. There are three types of Zontroy template files to meet all generation needs.

1- Zontroy Repeating File (zref) : This template file generates one code file for each entity in data model.

2 – Zontroy Single File (zsif) : This template file generates one single code file for all entities in data model.

3 – Zontroy Inner Repeating File (ziref) : This template file generates a directory and a code file for each entity in data model.

We are going to use a Zontroy Repeating File for our example. It means that Zontroy will generate a python class for each table in our database. Let’s right click python folder and click Add File to add a template file.

Select Zontroy Repeating File (.zref) and enter file name as below.

By writing such a code in the name area, we are telling Zontroy to produce files with the name of each entity. Even generating file names is possible with Zontroy.

After this, click Add File button and double click the newly created file. In this file, we are going to implement methods for Create, Retrieve, Update and Delete functionality. Let’s write the code sample below inside the code editor.

from models.models import [[[zg-entity...zg-name]]]

def get_[[[zg-entity...zg-name]]]s(request):
    limit = request.args.get('limit', None)
    order_by = request.args.get('order_by', None)
    filter_obj = {}

    for key in request.args:
        if key not in ['limit', 'order_by']:
            filter_obj[key] = request.args[key]

    [[[zg-entity...zg-name]]]s = [[[zg-entity...zg-name]]].query \
                .filter_by(**filter_obj) \
                .order_by(order_by) \
                .limit(limit) \
                .all()

    return [[[zg-entity...zg-name]]]s


def create_[[[zg-entity...zg-name]]](db, body):
    [[[zg-entity...zg-name]]] = [[[zg-entity...zg-name]]].from_json(body)
    db.session.add([[[zg-entity...zg-name]]])
    db.session.commit()

    return [[[zg-entity...zg-name]]]


def update_[[[zg-entity...zg-name]]](db, [[[zg-entity...zg-name]]], body):
    zg-for(((zg-item:::[[[zg-entity...zg-fields]]]))){{{
    [[[zg-entity...zg-name]]].[[[zg-item...zg-name]]] = body.get('[[[zg-item...zg-name]]]', [[[zg-entity...zg-name]]].[[[zg-item...zg-name]]])}}}
    db.session.commit()


def delete_[[[zg-entity...zg-name]]](db, [[[zg-entity...zg-name]]]):
    db.session.delete([[[zg-entity...zg-name]]])
    db.session.commit()

Here, entity name will take place of [[[zg-entity…zg-name]]]. Additionally, zg-for iterates fields of an entity and assigns the field to zg-item. You can use any variable name instead of “item”, but “zg-” prefix is necessary. In for loop, we can take the name, data type or target type of each field. In for loop, we can also access the entity.

After implementing our template code, save the file. Right click it, and click Generate This File(s).

When code generation is completed, you will see the generated files.

All done. We created 15 code files and implemented classes with CRUD methods in a few minutes. Let’s have a look at the automatically generated files.

actor.py code file:

from models.models import actor

def get_actors(request):
    limit = request.args.get('limit', None)
    order_by = request.args.get('order_by', None)
    filter_obj = {}

    for key in request.args:
        if key not in ['limit', 'order_by']:
            filter_obj[key] = request.args[key]

    actors = actor.query \
                .filter_by(**filter_obj) \
                .order_by(order_by) \
                .limit(limit) \
                .all()

    return actors


def create_actor(db, body):
    actor = actor.from_json(body)
    db.session.add(actor)
    db.session.commit()

    return actor


def update_actor(db, actor, body):
    
    actor.actor_id = body.get('actor_id', actor.actor_id)
    actor.first_name = body.get('first_name', actor.first_name)
    actor.last_name = body.get('last_name', actor.last_name)
    actor.last_update = body.get('last_update', actor.last_update)
    db.session.commit()


def delete_actor(db, actor):
    db.session.delete(actor)
    db.session.commit()

address.py code file:

from models.models import address

def get_addresss(request):
    limit = request.args.get('limit', None)
    order_by = request.args.get('order_by', None)
    filter_obj = {}

    for key in request.args:
        if key not in ['limit', 'order_by']:
            filter_obj[key] = request.args[key]

    addresss = address.query \
                .filter_by(**filter_obj) \
                .order_by(order_by) \
                .limit(limit) \
                .all()

    return addresss


def create_address(db, body):
    address = address.from_json(body)
    db.session.add(address)
    db.session.commit()

    return address


def update_address(db, address, body):
    
    address.address_id = body.get('address_id', address.address_id)
    address.address = body.get('address', address.address)
    address.address2 = body.get('address2', address.address2)
    address.district = body.get('district', address.district)
    address.city_id = body.get('city_id', address.city_id)
    address.postal_code = body.get('postal_code', address.postal_code)
    address.phone = body.get('phone', address.phone)
    address.last_update = body.get('last_update', address.last_update)
    db.session.commit()


def delete_address(db, address):
    db.session.delete(address)
    db.session.commit()

other files…

As you see, even in this simple example there is more than copy and paste. You can also generate other files in python like html. Lastly, efficiency of python code generator is up to the implementation as in python programming.

Comments

  • Derek Candido

    5 August 2022

    Pretty! This was an extremely wonderful post. Thanks for providing this information.

    reply
  • OTS List

    7 July 2022

    Interesting article, thanks!

    reply

Post a Comment