Hi there,
What is the best and clean solution for this case?
Thanks in advance.
if elementToUpdate != None:
for x in elementToUpdate.Parameters:
if x.Definition.Name in values and x.Definition.Name != IN[1] and x.Definition.Name != "Parameter1" and x.Definition.Name != "Parameter2" and "Parameter3":
Sorry a little mistake in the code.
Take that:
if elementToUpdate != None:
for x in elementToUpdate.Parameters:
if x.Definition.Name in values and x.Definition.Name != IN[1] and x.Definition.Name != "Parameter1" and x.Definition.Name != "Parameter2" and x.Definition.Name != "Parameter3":
First, you are accessing Parameter.Definition.Name multiple times. Start off by accessing it once and storing it in a variable e.g. “name”. You can then get a boolean result for each of the tests you’re performing and finally check if all conditions are satisfied by using the builtin all()
function.
I would also recommend assigning IN[1] to a named variable.
if elementToUpdate is not None:
for x in elementToUpdate.Parameters:
name = x.Definition.Name
test1 = name in values
test2 = name != IN[1]
test3 = name != "Parameter1"
test4 = name != "Parameter2"
test5 = name != "Parameter3"
if all([test1, test2, test3, test4, test5]):
# Do something
Which may result in something like this:
>>> test1 = True
>>> test2 = False
>>> test3 = True
>>> test4 = True
>>> test5 = False
>>> all([test1, test2, test3, test4, test5])
False
>>> test1 = True
>>> test2 = True
>>> test3 = True
>>> test4 = True
>>> test5 = True
>>> all([test1, test2, test3, test4, test5])
True
5 Likes
Thanks @cgartland that works, sorry i replied from my other account.
Solved
1 Like
Alternatively, you could define a list with all of the parameter names (example called parameter_list
) and use name not in parameter_list
to replace the test3-test5 lines and just have it as a single test. Something like this:
paramater_list = ["Parameter1", "Parameter2", "Parameter3"]
if elementToUpdate is not None:
for x in elementToUpdate.Parameters:
name = x.Definition.Name
test1 = name in values
test2 = name != IN[1]
test3 = name not in parameter_list
if all([test1, test2, test3]):
# Do something
It would make it easier to keep track of the parameter names if the list were to get longer. Also helps to cut down on nearly identical lines/functions.
2 Likes
It also becomes much easier to manage this way, especially if you want to group your parameters, e.g.
param_grp1 = ["Param1A", "Param1B", "Param1C"]
param_grp2 = ["Param2A", "Param2B", "Param2C"]
param_grp3 = ["Param3A", "Param3B", "Param3C"]
1 Like
This workflow would be nice if i want to group parameters by Units to Convert. I like it.
Also, if you are converting between units, check out the UnitUtils class.
And there is a way to declare one time the UnitUtils instead of have to declare it depending on the parameter?
If i want to map pressure and air flow i have to declare the unit type for every parameter?