Math functions?

10,342 views
Skip to first unread message

Claes

unread,
Feb 16, 2009, 10:08:25 AM2/16/09
to robotframework-users
How can i perform math functions in Robot Framework?

Say for example I want to calculate ${nbrRows}/${50} and put it into $
{nbrPages}. How can I do that?

/Claes

Pekka Klärck

unread,
Feb 16, 2009, 10:38:28 AM2/16/09
to claes...@gmail.com, robotframework-users
2009/2/16 Claes <claes...@gmail.com>:

>
> How can i perform math functions in Robot Framework?
>
> Say for example I want to calculate ${nbrRows}/${50} and put it into $
> {nbrPages}. How can I do that?

A general solution is having a suitable library for that. In your case
you needed to have Divide keyword. Having Math library as a standard
library for Robot Framework would be pretty nice actually.

If you only need to do simple calculations and are OK with your test
data getting a bit more complicated, you may consider either of the
workarounds below.

1) Use built-in Evaluate keyword which takes a Python expression and
returns its result. It ought to be possible to do just:

| ${nbrPages} = | Evaluate | ${nmrRows} / 50 |

2) Use extended variable syntax. If ${nbrRows} is an integer, you
should be able to use ${nbrRows / 50} without even having ${nbrPages}.
See user guide for more information about the extended variable syntax
in general.

Cheers,
.peke

Claes

unread,
Feb 18, 2009, 9:22:55 AM2/18/09
to robotframework-users
> > Say for example I want to calculate ${nbrRows}/${50} and put it into $
> > {nbrPages}. How can I do that?
>
> 1) Use built-in Evaluate keyword which takes a Python expression and
> returns its result. It ought to be possible to do just:
>
> | ${nbrPages} = | Evaluate | ${nmrRows} / 50 |

Evalute works. ${nbrPages} = 3.125. How can I round this up without
creating my own keyword "Round Up"?

/Claes

Ps. The documentation on Evaluate could be updated with a link to a
page where I could read more about how Python evaluates things. The
text "Evaluates the given expression in Python and returns the
results." is to thin for me as I don't know how to program Python. I.e
I don't know what operators work. +, -, /, * is straight forward by
does % work and is there more of them?

Claes

unread,
Feb 19, 2009, 3:47:37 AM2/19/09
to robotframework-users
> > 1) Use built-in Evaluate keyword which takes a Python expression and
> > returns its result. It ought to be possible to do just:
>
> > | ${nbrPages} = | Evaluate | ${nmrRows} / 50 |
>
> Evalute works. ${nbrPages} = 3.125. How can I round this up without
> creating my own keyword "Round Up"

With the hope that I could use Pythons math lib i tried:

| ${nbrPages} = | Evaluate | math.ceil( 50/16 ) |

But got the answer Evaluating expression 'math.ceil( 50/16 )' failed.
Error: name 'math' is not defined

Would it be possible to extend Evaluate to be able to handle Python
math-commands?

Regards
Claes

Pekka Klärck

unread,
Feb 19, 2009, 6:34:50 AM2/19/09
to robotframework-users
accidentally replied only to sender originally...


---------- Forwarded message ----------
From: Pekka Klärck <pe...@iki.fi>
Date: 2009/2/18
Subject: Re: Math functions?
To: claes...@gmail.com


2009/2/18 Claes <claes...@gmail.com>:


>
>> > Say for example I want to calculate ${nbrRows}/${50} and put it into $
>> > {nbrPages}. How can I do that?
>>
>> 1) Use built-in Evaluate keyword which takes a Python expression and
>> returns its result. It ought to be possible to do just:
>>
>> | ${nbrPages} = | Evaluate | ${nmrRows} / 50 |
>
> Evalute works. ${nbrPages} = 3.125. How can I round this up without
> creating my own keyword "Round Up"?

Python has built-in method 'round' but it rounds to nearest number,
and 'int' always rounds down. I don't know any simple way to round up
using Evaluate but this ought to work:

int(__import__('math').ceil(${nmrRows} / 50))

You should consider creating a higher level keyword for this one or
perhaps creating you own library. In a library you could just have:

import math

def round_up(number):
return int(math.ceil(float(number)))


Note that 'float' is only needed to handle possible strings containing numbers.

> Ps. The documentation on Evaluate could be updated with a link to a
> page where I could read more about how Python evaluates things. The
> text "Evaluates the given expression in Python and returns the
> results." is to thin for me as I don't know how to program Python. I.e
> I don't know what operators work. +, -, /, * is straight forward by
> does % work and is there more of them?

Yeah, we could add http://python.org there but I expect people can
actually found that or other Python resources easily if they want. If
you want to learn some Python I recommend the official tutorial and
http://diveintopython.org.

Cheers,
.peke

Pekka Klärck

unread,
Feb 19, 2009, 6:41:11 AM2/19/09
to claes...@gmail.com, robotframework-users
2009/2/19 Claes <claes...@gmail.com>:

It would be possible, but then we probably needed to support all other
Python modules too. Instead you can use either __import__ function
with Evaluate keyword or have a simple library with this
functionality. There are examples of both in the mail I just forwarded
to the list.

Cheers,
.peke

Claes

unread,
Feb 20, 2009, 8:04:00 AM2/20/09
to robotframework-users
Solution summary:

Pekka Klärck wrote:
Python has built-in method 'round' but it rounds to nearest number,
and 'int' always rounds down. I don't know any simple way to round up
using Evaluate but this ought to work:

int(__import__('math').ceil(${nmrRows} / 50))

--

Janne Härkönen wrote:
Using 'Evaluate' makes the test data much more cryptic than it needs
to be.
A much easier solution is to implement your own test library with the
needed functionality.

| ${nbrPages} = | Divide And Round Up | ${nbrRows}/${50} |

for that, you'd need to have a test library, for example Math.py,
containing:
----------------------------------------------------------------
import math

def divide_and_round_up(dividend, divisor):
return math.ceil( float(dividend) / float(divisor) )
-----------------------------------------------------------------

and presto, you're set.
(Note that float() is used to convert the incoming strings to numbers.
Using int() would result in division without reminder)

--

Myself I tried

| ${nbrPages}= | Evaluate | int(__import__('math').ceil( 50.0/16.0 ))
|

And it worked! Haven't tried creating a library but that should work
as well.

Thanks for solving my problem
/Claes
Reply all
Reply to author
Forward
0 new messages