How to pass a two dimensional list/array between groovy scripts in soap ui? - arrays

Problem statement: We need a way to pass a two dimensional list (or array) from one groovy script to other scripts ( to assert values from multiple DB2 tables in other scripts].
Some Background:
Step1: Based on our input xml payload we are capturing the list of nodes (and child elements) in a two dimensional list [][]. [Done]
Step2: Now we want to use the values from each of this list to assert with respect to values in DB2 tables [Also done, however keeping both step1 and step2 in same groovy script].
What we want is to to be able to pass the 2dimensional list from step1 in step2. Specially important since we have multiple tables and we dont want to either add all table steps in one big groovy script Or to duplicate step1 code in each Db2 validataion script.
We read about setting each element value from list at test case level and then reconstructing the array back but we are hesitating to use that method due to (varying &) huge size of list elements (in thousands). Question is: Are there any clean ways to achieve this?
Thanks!

As you are aware of the limitation of the earlier solution, which would only work (sharing of object between the groovy scripts) if the test case is run and does not work if individual steps are run.
Here I want to provide an approach which over comes that by using groovy's meta programming.
In script 1, have the below code:
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
WsdlTestCase.metaClass.myList = [1,2,3,4,5]
In script 2, have the below code:
log.info "From script 2: ${context.testCase.myList}"
assert [1,2,3,4,5] == context.testCase.myList
The above even works if individual steps are run.
Hope this is helpful.
EDIT: come to understand that user required to update the list repeatedly and with metaClass user couldn't update the list. Here is the alternative:
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
WsdlTestCase.metaClass.myObject = new Expando(myList: [1,2,3,4,5])
log.info "list initialized: ${context.testCase.myObject.myList}"
WsdlTestCase.metaClass.myObject = new Expando(myList: [1,2,3,4,5,6,7])
log.info "list updated: ${context.testCase.myObject.myList}"

You can use context
That's real working Groovy Script steps.
step1:
def array = ['Kyiv', 'Boryspil', 'Kharkiv', "L'Viv", "Odesa"]
context.setProperty('cities', array)
log.info( 'script1: '+array )
step2:
def array = context.getProperty('cities')
log.info( 'script2: '+array )
assert array.size()>0
Note:
If you run just one step, then there will be absolutely
independent context.
But if you run the whole testcase then there will be context shared
for the whole testcase.
You can use Run from here context menu in your test case window to run from exact step.

Related

Swift - adding named objects to an array using a variable and a loop

Possibly very stupid question I cannot seem to find an answer for (I am beginning with code)
I want to create a simple loop which appends myArray with three objects, which are members of a custom class MyClass. The objects have the following names: "object1", "object2", "object3".
When I write the following code, there is no issue:
myArray.append(object1)
But I want to write a loop to add all three. Again, very dumb, but I can't figure out how to insert the number in the name of the object as a variable. E.g., here was something I tried
let x = 3
for i in 1...x {
myArray.append(object[i])
}
This gives an error. The reason I want to do it using a loop, and not simply write in the three objects manually, is that I won't always loop three times. Sometimes I'll just want the first two objects, sometimes just the first.
I assume there's some easy way to do this, but when I search it tends to turn up more complex questions

replace page of array with another using paste() R

I have a 3d array with lots of pages (or whatever you call 3rd dimension). I am trying to stick that array into a loop, and loop through the pages to fill in values.
In my real dataset, I'm manipulating values with a handful of custom functions, so the way it works best to minimize the amount of retyping I would need to do when changing variables is to create a new array inside the loop, make changes to that new array, then replace the new page in its spot in the original array. However, I'm having a hard time plugging the new page back into the original array. Example follows.
code="A" # I'll be running this loop with various name codes
assign(paste0("testarray",code),array(dim=c(5,5,20)))
# creates array "testarrayA" . I'm using assign(paste()) because I will
# do this with many different array codes, and I want to only change
# the code value each time for ease
set.seed(5)
testarrayA[,,1] <- runif(25,3,20) #fill first page
Then I want to run this array through a loop to change the values on each page. In the real code, the changes are made with somewhat complex custom functions, so again, for ease, I start by making a smaller array, changing that, then trying to plug back into the array from above. See following.
# this loop doesn't work at the moment, so just run lines
# inside the loop individually to attempt. set "page" object as 1
for(page in 1:dim(get(paste0("testarray",code)))[3] ){
# for pages 1-end of third dimension (20)
temparray <- get(paste0("testarray",code))[,,page:(page+1)]
# create smaller array of only current and (blank) next page of original array
temparray[,,2] <- temparray[,,1]*2
# do functions to change the values on the next page of temporary array.
# (just multiplying values by 2 for this example)
# try to plug in temporary array[2] as original array[page+1].
# both of these codes give me errors at the moment, but these are
# the options I have tried so far.
# trial 1:
get(paste0("testarray",code))[,,page+1] <- temparray[,,2]
# trial 2:
assign(paste0("testarray",code)[,,page+1],temparray[,,2])
}
I believe the problem comes from not being able to use paste() in the receiving end of an assign command. The first trial listed above returns error "target of assignment expands to non-language object," which I'm not quite sure how to interpret, and the second, "incorrect number of dimensions," which shouldn't be the case. When I highlight and run each object individually, they have the same dimensions, so I don't know what I'm doing wrong.
Basically, I need to know how to use paste() on the receiving end of an assign function such as <- or assign(), or if there's a reasonable workaround for the errors I'm getting. I know I could just omit the creation of the smaller temporary array, but in my actual code, that would make a lot more work by requiring me to change names a bunch of times in each command inside the loop when I run on a new dataset. I'd rather just change the "code" if possible. Thanks!

How to create Datasets Like MNIST in Pytorch?

I have looked Pytorch source code of MNIST dataset but it seems to read numpy array directly from binaries.
How can I just create train_data and train_labels like it? I have already prepared images and txt with labels.
I have learned how to read image and label and write get_item and len, what really confused me is how to make train_data and train_labels, which is torch.Tensor. I tried to arrange them into python lists and convert to torch.Tensor but failed:
for index in range(0,len(self.files)):
fn, label = self.files[index]
img = self.loader(fn)
if self.transform is not None:
img = self.transform(img)
train_data.append(img)
self.train_data = torch.tensor(train_data)
ValueError: only one element tensors can be converted to Python scalars
There are two ways to go. First, the manual. Torchvision.datasets states the following:
datasets are subclasses of torch.utils.data.Dataset i.e, they have __getitem__ and __len__ methods implemented. Hence, they can all be passed to a torch.utils.data.DataLoader which can load multiple samples parallelly using torch.multiprocessing workers.
So you can just implement your own class which scans for all the images and labels, keeps a list of their paths (so that you don't have to keep them in RAM) and has the __getitem__ method which given index i reads the i-th file, its label and returns them. This minimal interface is enough to work with the parallel dataloader in torch.utils.data.
Secondly, if your data directory can be rearranged into either structure, you can use DatasetFolder and ImageFolder pre-built loaders. This will save you some coding and automatically provide support for data augumentation routines from torchvision.transforms.

How to implement get_tensor_by_name and predict in tensorflow.js

I want to use Faster rcnn inception v2 to do object detection in tensorflow.js. But i can't find some method in tfjs like get_tensor_by_name and session run for prediction.
In tensorflow (python), the code as the following:
Define input and output node:
# Definite input Tensors for detection_graph
self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
# Definite output Tensors for detection_graph
self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
Predict:
(boxes, scores, classes, num) = self.sess.run(
[self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
feed_dict={self.image_tensor: image_np_expanded})
Do anyone know how to implement those two part of code in tfjs?
Please help. Thank you!
You don't have a session.run function in tensorflow.Js as there is in Python. In python, you start defining a graph and in the run function, you execute the graph. Tensors and Variables are assigned values in the graph, but the graph defines only the flow of computation, it does not hold any values. The real computation occurs when you run the session. One can create many session where each session can assign different values to the variable, that is why the graph has a get_by_tensor_name which outputs the tensor whose name is given as parameter.
You don't have the same mechanism in Js. You can use the variable as soon as you define them. It means that whenever you define a new tensor or variable, you can print it in the following line whereas in python, you can only print the tensor or variable only during a session. The get_by_tensor_name does not really have a sense in Js.
As for the predict function, you do have one in Js as well. if you create a model using tf.model or tf.sequential, you can invoke predict to make a prediction.

Jmeter Property with array of values

Requirement: Need to store 50+ values to a Jmeter property and use with idx
In the case of normal variable we can use Country_1 or Country_2.
Do we have any function to set an array of values to jmeter Property and how to get value using index?
Note: In this case,value has to be used in different thread group.
Your ArrayList initialization is not correct, you should be doing something like:
List myList = Arrays.asList('India', 'USA', 'UK')
There is no putObject method in props shorthand (which is basically an instance of java.util.Properties class so you will need to amend your code like:
props.put('Middle', myList)
Once done you will be able to access individual list members using __groovy() function like:
${__groovy(props.get('Middle').get(0),)} - for first member
${__groovy(props.get('Middle').get(1),)} - for second member
${__groovy(props.get('Middle').get(2),)} - for third member
etc.
Demo:
See Apache Groovy - Why and How You Should Use It article for more details on using Groovy scripting in JMeter tests.

Resources