Generating a simple class with a template
In this section, we will be looking at how to generate code for a class without actually defining the class itself. We will be creating a string-based template with the structure of a class we want to develop but not the actual code that can be executed. To explain this further, let us look at an example where we will generate a class named VegCounter
by parsing a series of strings using the ast
module.
The sequence of steps to be followed to generate code for a class is represented in the following flow diagram:
Figure 14.1 – A code generation sequence for a simple class
Let us look at the implementation of this example: