Calculation
Calculation is a generic object that allow you to call an external function.
Simple calculation
It’s structure is the following :
1from tiramisu import Config, OptionDescription, Leadership, IntOption, Params, ParamOption, ParamValue, ParamContext, ParamIndex
2
3def a_function():
4 pass
5Calculation(a_function)
Positional and keyword arguments
Function’s arguments are also specified in this object. Let’s see with a positional argument and a keyword argument:
1def a_function_with_parameters(value1, value2):
2 return value1 + ' ' + value2
3Calculation(a_function_with_parameters, Params(ParamValue('my value 1'), kwargs={value2: ParamValue('my value 2')}))
The function a_function_with_parameters will be call with the positional argument value1 to my value 1 and the keyword argument value2 to my value 2.
So when this function will be executed, it will return my value 1 my value 2.
Let’s see with two positional arguments:
1def a_function_with_parameters(value1, value2):
2 return value1 + ' ' + value2
3Calculation(a_function_with_parameters, Params((ParamValue('my value 1'), ParamValue('my value 2'))))
As we have several positional arguments, the first Params’ argument is a tuple. This example will return strictly same result has previous example.
Option has an argument
In previous examples, we use ParamValue arguments, which could contain random value. But this value is static and cannot be change.
It could be interesting to use an existant option has an argument:
1def a_function_with_option(option1):
2 return option1
3option1 = IntOption('option1', 'first option', 1)
4Calculation(a_function_with_option, Params(ParamOption(option1)))
As long as option1 is at its default value, the function will return 1. If we set option1 to 12, the function will return 12.
Pay attention to the properties when you use an option. This example will raise a ConfigError:
1def a_function_with_option(option1):
2 return option1
3option1 = IntOption('option1', 'first option', 1, properties=('disabled',))
4Calculation(a_function_with_option, Params(ParamOption(option1)))
It’s up to you to define the desired behavior.
If you want the option to be transitively disabled just set the raisepropertyerror argument to True:
1def a_function_with_option(option1):
2 return option1
3Calculation(a_function_with_option, Params(ParamOption(option1, raisepropertyerror=True)))
If you want to remove option in argument, just set the notraisepropertyerror argument to True:
1def a_function_with_option(option1=None):
2 return option1
3Calculation(a_function_with_option, Params(ParamOption(option1, notraisepropertyerror=True)))
In this case, option1 will not pass to function. You have to set a default value to this argument.
So, function will return None.
In these examples, the function only accesses to the value of the option. But no additional information is given.
It is possible to add the parameter todict to True to have the description of the option in addition to its value.
1def a_function_with_dict_option(option1):
2 return "the option {} has value {}".format(option1['name'], option1['value'])
3Calculation(a_function_with_option, Params(ParamOption(todict=True)))
This function will return the option first option has value 1.
Multi option has an argument
An option could be a multi. Here is an example:
1def a_function_multi(option1):
2 return option1
3option1 = IntOption('option1', 'option1', [1], multi=True)
4Calculation(a_function, Params(ParamOption(option1)))
In this case the function will return the complete list. So [1] in this example.
Leader or follower option has an argument
An option could be a leader:
1def a_function_leader(option):
2 return option
3leader = IntOption('leader', 'leader', [1], multi=True)
4follower1 = IntOption('follower1', 'follower1', default_multi=2, multi=True)
5follower2 = IntOption('follower2', 'follower2', default_multi=3, multi=True)
6leadership = Leadership('leadership', 'leadership', [leader, follower1, follower2])
7Calculation(a_function_leader, Params(ParamOption(leader)))
If the calculation is used in a standard multi, it will return [1].
If the calculation is used in a follower, it will return 1.
An option could be a follower:
1def a_function_follower(follower):
2 return follower
3leader = IntOption('leader', 'leader', [1], multi=True)
4follower1 = IntOption('follower1', 'follower1', default_multi=2, multi=True)
5follower2 = IntOption('follower2', 'follower2', default_multi=3, multi=True)
6leadership = Leadership('leadership', 'leadership', [leader, follower1, follower2])
7Calculation(a_function_follower, Params(ParamOption(follower1)))
If the calculation is used in a standard multi, it will return [2].
If the calculation is used in a follower, it will return 2.
If the calculation is used in a follower we can also retrieve the actual follower index:
1def a_function_index(index):
2 return index
3leader = IntOption('leader', 'leader', [1], multi=True)
4follower1 = IntOption('follower1', 'follower1', default_multi=2, multi=True)
5follower2 = IntOption('follower2', 'follower2', default_multi=3, multi=True)
6leadership = Leadership('leadership', 'leadership', [leader, follower1, follower2])
7Calculation(a_function_index, Params(ParamIndex()))
Context has an argument
It is possible to recover a copy of the context directly in a function. On the other hand, the use of the context in a function is a slow action which will haunt the performances. Use it only in case of necessity:
1def a_function_with_context(context):
2 pass
3Calculation(a_function_with_context, Params(ParamContext()))