How to subtract the Number in Robot framework? - robotframework

How to subtract the number in a Robot Framework?
What is the command for it?
For example, if I am getting a count, I want to subtract -1 and map keywords with the resulting value.

If your variable contains an actual number, you can use extended variable syntax. For example, this test will pass:
*** Variables ***
| ${count} | ${99} | # using ${} syntax coerces value to number
*** Test cases ***
| Example
| | Should be equal as numbers | ${count-1} | 98
You can also use the Evaluate keyword to create a python expression. For example:
*** Variables ***
| ${count} | 99
*** Test cases ***
| Example
| | ${count}= | Evaluate | ${count} - 1
| | Should be equal as numbers | ${count} | 98
Note: using Evaluate will work whether ${count} is a number or the string representation of a number.

You could use Evaluate keyword:
*** Test Cases ***
Stackoverflow
${x} = Set Variable 1
${y} = Evaluate ${x} - 1

An expression like this should work:
${token_expire_time} = Evaluate ${token_generate_time}-${expires_in}

If for some reason the conversion with ${} doesn't seem to work, then feel free to use:
Convert to integer keyword
or
Convert to number keyword

Related

When I am trying to increment a variable, unable to compare the variable with values in dictionary

Suppose if ${x} isdictionary and has the value {'a':'4','b':'3'}. Please see the below code. I am getting error after incrementing the value. And the type of this variable ${count} is 'int' before and after increment.
${count}= set variable 3
Dictionary Should Contain Value ${x} ${count} ##--------> True
${Count}= Evaluate ${Count}+1
Dictionary Should Contain Value ${x}} ${count} ##-------> Doesnot contain value '4'
your variable ${count} is at the beginning not an integer. It is string.
Integer has to be set like this in Robot Framework
${count}= set variable ${3}
After your Evaluate you have an integer because 3+1 evaluates to 4
You dictionary has strings as values. {'a':'4','b':'3'}
So the first call works, because you expect the string "3" in the dictionary.
The second call failed, because the integer 4 ins not equal to the string "4"
This code would work:
*** Settings ***
Library Collections
*** Variables ***
&{x} a=4 b=3
*** Test Cases ***
test
${count}= set variable 3
Dictionary Should Contain Value ${x} ${count}
${Count}= Evaluate str(${Count}+1)
Dictionary Should Contain Value ${x} ${count}

SumoLogic:Can I have graph of min/max difference?

I want to show a graph of minimum value, maximum value and difference between maximum and minimum for each timeslice.
It works ok for min and max
| parse "FromPosition *)" as FromPosition
| timeslice 2h
| max(FromPosition) ,min(FromPosition) group by _timeslice
but I couldn't find the correct way to specify the difference.
e.g.
| (max(FromPosition)- min(FromPosition)) as diffFromPosition by _timeslice
returns error -Unexpected token 'b' found.
I've tried a few different combinations to declare them on different lines as suggested on https://help.sumologic.com/05Search/Search-Query-Language/aaGroup. e.g.
| int(FromPosition) as intFromPosition
| max(intFromPosition) as maxFromPosition , min(intFromPosition) as minFromPosition
| (maxFromPosition - minFromPosition) as diffFromPosition
| diffFromPosition by _timeslice
without success.
Can anyone suggest the correct syntax?
Try this:
| parse "FromPosition *)" as FromPosition
| timeslice 2h
| max(FromPosition), min(FromPosition) by _timeslice
| _max - _min as diffFromPosition
| fields _timeslice, diffFromPosition
the group by is for the min and max functions to know what range to work with, not the group by for the overall search query. That's why you were getting the syntax errors and one reason I prefer to just use by as above.
For these kinds of queries I usually prefer a box plot where you would just do:
| min(FromPosition), pct(FromPosition, 25), pct(FromPosition, 50), pct(FromPosition, 75), max(FromPosition) by _timeslice
Then selecting box plot as the graph type. Looks great on a dashboard and provides a lot of detailed information about deviation and such at a glance.

groupby an element with jq

I have the following json:
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d2"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d3"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d4"},"org":"AB KIO","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d5"},"org":"GH SVS","rc":{"$event":"17"}}
How could i achieve the following output result? (tsv)
13 TΙ UIH 2
13 AB KIO 1
17 GH SVS 1
so far from what i have searched,
jq -sr 'group_by(.org)|.[]|[.[0].org, length]|#tsv'
how could i add one more group_by to achieve the desired result?
I was able to obtain the expected result from your sample JSON using the following :
group_by(.org, .rc."$event")[] | [.[0].rc."$event", .[0].org, length] | #tsv
You can try it on jqplay.org.
The modification of the group_by clause ensures we will have one entry by pair of .org/.rc.$event (without it we would only have one entry by .org, which might hide some .rc.$event).
Then we add the .rc.$event to the array you create just as you did with the .org, accessing the value of the first item of the array since we know they're all the same anyway.
To sort the result, you can put it in an array and use sort_by(.[0]) which will sort by the first element of the rows :
[group_by(.org, .rc."$event")[] | [.[0].rc."$event", .[0].org, length]] | sort_by(.[0])[] | #tsv

How to get average of last N numbers in a stream with static memory

I have a stream of numbers and in every cycle I need to count the average of last N of them. This can be, of course, solved using an array where I store the last N numbers and in every cycle I shift it, add the new one and count the average.
N = 3
+---+-----+
| a | avg |
+---+-----+
| 1 | |
| 2 | |
| 3 | 2.0 |
| 4 | 3.0 |
| 3 | 3.3 |
| 3 | 3.3 |
| 5 | 3.7 |
| 4 | 4.0 |
| 5 | 4.7 |
+---+-----+
First N numbers (where there "isn't enough data for counting the average") doesn't interest me much, so the results there may be anything/undefined.
My question is, can this be done without using an array, that is, with static amount of memory? If so, then how?
I'll do the coding myself - I just need to know the theory.
Thanks
Think of this as a black box containing some state. If you control the input stream, you can draw conclusions on the state. In your sliding window array-based approach, it is kind of obvious that if you feed a bunch of zeros into the algorithm after the original input, you get a bunch of averages with a decreasing number of non-zero values taken into account. The last one has just one original non-zero value, so if you multiply that my N you get the last input back. Using that and the second-to-last output which accounts for two non-zero inputs, you can reconstruct the second-to-last input, and so on.
So essentially your algorithm needs to maintain sufficient state to reconstruct the last N elements of input, at least if you formulate it as an on-line algorithm. I don't think an off-line algorithm can do any better, except if you consider it reading the input multiple times, but I don't have as strong an agument for that.
Of course, in some theoretical models you can avoid the array and e.g. encode all the state into a single arbitrary length integer, but that's just cheating the theory, and doesn't make any difference in practice.

How to configure begin and end date of SPELL in TraMineR

In the SPELL format of TraMineR, for a given individual i, should end date at t and start date at t+1 be the same or incremented by 1?
My dataset is built this way:
id | start | end | state
1 | 2/1/12 | 3/6/12 | "a"
1 | 3/6/12 | 1/14/13 | "b"
1 | 1/14/13| 2/2/13 | "c"
Should I add 1 day to each start beginning at row 2?
The seqformat function of TraMineR expects integer values as begin and end arguments. If you provide dates, their corresponding integer values will be considered.
Now, seqformat has an overwrite argument that permits to control the handling of overlaps. By setting overwrite = FALSE, you get the results that you would obtain by adding 1 day to the start values of your second and third rows. With the default overwrite = TRUE, the most recent episode overwrites the older one when they overlap.

Resources