Python
Last updated
Last updated
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.
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.