Python

Python Code Generator

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.

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.

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.

```python
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.

and other output files…

Lastly, efficiency of python code generator is up to the implementation as in python programming.

Good to know: You can generate Python code using Zontroy.

https://zontroy.com/python-code-generator/

Last updated