NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple outputs for python step

Solved!
Go to solution

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"?

0 Kudos
Message 1 of 11
(2,619 Views)

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 }

0 Kudos
Message 2 of 11
(2,602 Views)
Solution
Accepted by topic author stephenb2

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

0 Kudos
Message 3 of 11
(2,509 Views)

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 )

0 Kudos
Message 4 of 11
(2,489 Views)

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

Message 5 of 11
(1,039 Views)

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.

0 Kudos
Message 6 of 11
(921 Views)

I have found a container object to be the most convenient object to receive a tuple output of a python function.

0 Kudos
Message 7 of 11
(909 Views)

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...

0 Kudos
Message 8 of 11
(907 Views)

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

0 Kudos
Message 9 of 11
(889 Views)

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.

 

 

0 Kudos
Message 10 of 11
(867 Views)