02-08-2023 11:02 AM
If I were writing a LabVIEW action step, I might have a VI with 3 string outputs: id1, id2, and id3. These outputs would show up in the module tab.
How do I get the outputs from a python step, where the python file ends in "return id1, id2, id3"?
Solved! Go to Solution.
02-08-2023 11:33 AM
I think one solution would be to return a list and assign to an array of TestStand variables. Is this the preferred method?
So, from python: "return id1, id2, id3"
And assign to in TS: { Locals.id1, Locals.id2, Locals.id3 }
02-20-2023 08:00 PM
stephenb2,
Unlike LabVIEW, Python has a constraint that it can only return one value from a function. To return multiple values, typically, Python developers return a tuple with those multiple values. Apart from tuple, as you mentioned, you can also return a list containing multiple values.
In TestStand, contents of the tuple can be stored in a container and similarly contents of list can be stored in an array. Depending on what data type you return in your function, in the Python step, provide the location of either container or array to the return value.
-Shashidhar
02-22-2023 03:28 PM
Thanks for pointing that out. I don't know if I will use containers often, but it's good to have the option, especially when there are return values of mixed types.
In the latter case, I can use a Python passfail step and something like this for the post-expression step:
AllOf(Locals.returned_tuple.responseNumber == 1, Locals.returned_tuple.response_string == "abc") ?
( Step.Result.PassFail = True ) :
( Step.Result.PassFail = False )
08-13-2024 03:03 PM
Python can and does have the ability to return multiple values and assign them to independent variables or to even skip assigning the returned value to a variable.
As an example:
speed, load, ramp = ExtractTestParameters("c:\somefile") # returns 3 distinct values
speed, _, ramp = ExtractTestParameters("c:\somefile") # returns 3distinct values but only sets speed and ramp
08-28-2024 07:04 AM
Like @RGSchmidt pointed out, Python is able to return multiple values from a Function it seems to be more a limitation of the python implementation in TestStand itself.
08-28-2024 09:45 AM
I have found a container object to be the most convenient object to receive a tuple output of a python function.
08-28-2024 09:52 AM
I tried multiple approaches today, but container was the only working one.
As my TestStand sequence is automatically generated with a python script, I wanna avoid implementing using containers or creating containers at all.
I tried passing the values natural so as return True, False and in TestStand accepting them as {Locals.one, Locals.two} didn't worked.
Then I tried passing a python tuple, but this didn't work either so return (True, False)
A list in python doesn't bring up an error message, but therefore not accepting the new values at all.
At that point I straight gave up...
08-28-2024 12:06 PM
RGSchmidt and maxhuebn,
RGSchmidt in the example is using concept of 'Unpacking of Tuples' in Python.
In the example ExtractTestParameters is returning a tuple and the code is using unpacking of tuple to extract individual values of the tuple.
You can modify the code like following:
return_value = ExtractTestParameters("c:\somefile")
speed, load, ramp = return_value
You can verify that type of return_value is a tuple.
There are lot of documents that explains unpacking of tuple and returning multiple values using tuples in Python. Some of the links are:
https://www.w3schools.com/python/python_tuples_unpack.asp
https://flexiple.com/python/python-return-multiple-values
Python can support returning only one object. If anyone wants to return multiple values, returning tuple with all return values is a common solution.
Tuple in Python maps to a container in TestStand. If your function returns a tuple, it needs to be stored in a container.
-Shashidhar
08-29-2024 03:42 AM
Hi Shashidbar,
Ok your correct with the single value statement.
But to point that out python is using an implicit declaration of a tuple or more precise if there had been no explicit approach to package the values python will make a tuple out of it.
So those two are exactly the same, in their handling.
Implicit
return one, two
explicit
return (one, two)
But there are still other ways like returning a list or a dict, at least that are the python names for those types.
Is there any thing in Teststand that can use a list?
Or are only tuples supported and therefore we need a container to unpack.