Generation process

All the generation code is gathered in pyleecan/Generator. To simplify all the process, we have split the generation of the code in several independent steps. The first one is to read all the CSV files to create a complex structure (dictionary gen_dict) with all the reference data. The second one is to use this structure to generate the classes. As the steps are separated, they can evolve separately. For instance, you can change the way the data is stored in the CSV file without changing the generation, as long as you keep the data structure organization.

The gen_dict structure

The structure gen_dict is a dictionary gathering all the reference data for all the classes. It is the structure that is generated by the first step and which is used to generate the code.

The structure gen_dict is composed of several imbricated lists and dicts. The first level is a dictionary with the group ‘Slot’, ‘Machine’… as key and dictionary as value. This first level matches the group separation of CSV files.

At the second level, we have the content of a CSV file as a dictionary, with CSV name (class name) as key and dictionary as value.

The third level corresponds to the data of a class (or the sheet of the CSV file). These new dictionaries contain the following keys:

  • properties: a list of dictionaries with key matching the CSV sheet column: name, type, min, max, value, desc (one dictionary for each property of the class)

  • constants: a list of dictionaries with the key name, value (one for each constant of the class)

  • methods: a list of all the class method names

  • daughters: a list of all the class daughter names (generated according to the “mother” columns)

  • package: name of the group of the class (same as the first dictionary key, but useful for generation)

  • desc: a string with the description of the class (for help text)

  • mother: a string with the mother class name (if empty, FrozenClass will be used as default)

  • path: path to the corresponding csv file

For instance, the description of the parameter H0 of Slot_Type_1_0 is stored in: gen_dict[‘Slot’][‘Slot_Type_1_0’][‘properties’][0][‘desc’] (if we suppose that H0 is first in the list of properties).

Note that a feature has been added to enable to generate “your own pyleecan” with internal code you don’t intend to share. More information on the corresponding issue.

File organization

The gen_dict structure is then used for the class generation. The corresponding code can be found in /Generator/class_generator.py file. Note that some common functions have been gathered in read_fct.

To gain in readability, the generated files have been split in several parts and some functions have been dedicated to generate specific parts of the code. The generation process is very complex since there are many possible cases. For instance, we need to adapt the code according to the type of property (a PYLEECAN type would need the corresponding import at the start of the file, some classes are inherited, some are not, some use numpy array, some don’t…). If you need to change this code, please consider that it should work for every single class in PYLEECAN: always start by modifying the class generation and then check that all the tests still pass.

The results are saved in pyleecan/Classes/\<Class Name62.py. As these files are completely rewritten by the generator, you must never modify them yourself. If you need to change the code of the generator, you should start by looking at these files (for different cases with/without PYLEECAN classes, with/without inheritance…) to know what you need to generate.