Hello,
(VIKTOR connector v5.22.2 / VIKTOR SDK v14.3.0 / Python version 3.11))
I am working on parametrization with several comparable fields. They differ only by a simple pattern (e.g. a counter). Instead of writing them all by hand and having to update them that way, I would like to know how to automate this.
The fields look like this (example):
configurator.Archetypes.complex_typeKeuze_x001 = OptionField("Archetype 001", name="P_ATconfig_Keuze_x001", options=COMPLEX_OPTIONS, default=COMPLEX_OPTIONS[(1)-1])
configurator.Archetypes.complex_typeKeuze_x001_aantal = NumberField("Aantal", name="P_ATconfig_Keuze_x001_aantal", default=1, num_decimals=0, step=1, min=0)
configurator.Archetypes.lb00001 = LineBreak()
I’ve tried building a loop that contains an ‘eval()’ command that runs over a counter ‘nn’ for instance such that all the relevant values (and Fieldname) change accordingly. It is my hope that this loop can substitute all the individual declarations of the fields and is more flexible in terms of total amount of fields etc.
for nn in range(1,16):
eval('configurator.Archetypes.complex_typeKeuze_x' + str(nn).zfill(3) + ' = OptionField("Archetype '+ str(nn).zfill(3) + '"), name="P_ATconfig_Keuze_x'+ str(nn).zfill(3) +'", options=COMPLEX_OPTIONS, default=COMPLEX_OPTIONS[('+ str(nn) +') - 1]')
eval('configurator.Archetypes.complex_typeKeuze_x' + str(nn).zfill(3) + '_aantal = NumberField("Aantal", name="P_ATconfig_Keuze_x' + str(nn).zfill(3) + '"_aantal", default=' + str(default_nn) + ', num_decimals=0, step=1, min=0)')
eval('configurator.Archetypes.lb' + str(nn).zfill(5) + ' = LineBreak()')
However, this does not seem to be allowed: I get the error ‘invalid syntax’ pointing at the ‘=’ declaration, e.g. in the last line:
eval('configurator.Archetypes.lb' + str(nn).zfill(5) + ' = LineBreak()')
Is this a correct approach in which I made a simple mistake, or is this not allowed at all? My main question is: “How can I automate/make flexible the generation of parametrization fields?”
Cheers!
Boudewijn.