4. Define a macro command in Python#
It is necessary to define:
the actual catalog of the keywords composing the macro,
the method of typing the data structures produced,
the Python method defining the body of the macro: the « basic » commands produced by the macro and their sequence.
The first two points are common with writing an ordinary command (except for a minor difference in the typing method).
It is possible to restore all this in the code command catalog and thus make the macro-command visible to all. You can also keep your development private with the advantage of not having to modify the execution parameters or the executable by placing these three elements directly at the top of the command file, or by importing them (Python import) from an agreed location.
4.1. Write the macro command catalog#
The catalog of a macro is similar to that of a simple command. The three differences are:
we declare an object MACRO (and not PROCou OPER),
the reserved keyword open does not contain an integer (designating the top-level routine number FORTRAN for OPERet PROC) but a python method name,
the product concept is not necessarily unique, declared to the left of the « = » sign.
Product concepts can be specified as simple keyword arguments. A simple keyword that defines a concept to be produced is declared of type CO:
NUME_DDL = SIMP (status='f', typ=CO)
The type of the returned object will be defined in the sd_prod function (cf. next paragraph).
The same keyword can have a double role: taking a concept as an argument or producing a new concept. For example, one would write:
NUME_DDL = SIMP (status='f', typ =( nume_ddl_sdaster, CO))
This use is strongly discouraged for future developments. It is recommended to define a keyword for each function.
4.2. Define the type of product concepts#
The definition of the type of product concepts of a macro-command is carried out in a manner similar to that of a simple command of type OPER.
If the order only produces a concept that can be found to the left of the = sign as in:
a = COMMANDE ()
we will proceed in the same way as for a simple command of type OPER.
In the case where the macro command can produce several concepts, some of which are keyword arguments, some additional information must be added. First of all, it is essential to provide a Python function, named sd_prod, in the macro definition. Then, the simple keywords containing the user name of the concept to be produced must be of the CO type (reserved name).
For example:
def ma_macro_prod (self, NUME_DDL,, MATRICE, **args):
if args.get (“__all__”): return ([evol_noli], [nume_dll_sdaster], [None, matr_asse_depl_r])
self.type_sdprod (NUME_DDL, nume_ddl_sdaster) If MATRICE: self.type_sdprod (MATRICE, matr_asse_depl_r) Return evol_noli
…
MA_MACRO = MACRO (sd_prod=ma_macro_prod,…
MATRICE = SIMP (status=”f”, typ=CO), … NUME_DDL = SIMP (status=”o”, typ=nume_ddl_sdaster),)
In this case, three concepts can be produced:
classically, an evol_noli concept that will have been given by the user to the left of the « = » sign.
a concept of the type matr_asse_depl_rif the user enters the simple keyword MATRICE.
a nume_ddl concept whose name is given to the NUME_DDL keyword.
When a keyword can have a concept to produce as an argument, the keyword must appear in the list of arguments of the sd_prod function and the concept must be typed using the type_sdprod method of the self argument, which is the macro command object.
NB: The self argument is not present for a OPERou a PROC (it is the object MACRO).
As for commands (see [D5.01.01]), the sd_product function must return all possible types if __all__=True. As the macro command can return several objects (3 in the example), if __all__=True, the function returns a list of 3 lists of possible types, one list for each result.
If a result is not always produced (here for MATRICE), the possible types are then None (nothing is produced) and the true types.
If the macro command does not return a result to the left of the « = » sign, the first list is then set to [None] (instead of [evol_noli] here).
In some cases, a macro command returns an indefinite number of results (one per occurrence of a keyword factor). In this case, the corresponding list is not repeated.