I am trying to pass a value to a python function but I need to have it as a float

1,258 views
Skip to first unread message

steve....@gmail.com

unread,
Dec 18, 2014, 10:27:44 PM12/18/14
to robotframe...@googlegroups.com
I tried to do a convert to number in robot framework and send that value, but it gets sent as a string

Perhaps I can change the python function parameters but I have not specified them as float, but convert them later with float ()  in the function to do the arithmetic and return the value 

I get an error that I cannot convert to float.

Can you help?


Tatu Aalto

unread,
Dec 19, 2014, 1:54:27 AM12/19/14
to steve....@gmail.com, robotframework-users

I am assuming that we are taking about python keywords.

> I tried to do a convert to number in robot framework and send that value, but it gets sent as a string

This is expected functionality, in most of the cases. More details are in the user guide [1].

> Perhaps I can change the python function parameters but I have not specified them as float, but convert them later with float ()  in the function to do the arithmetic and return the value 

Python is strongly and dynamically typed language. So if your code will require other type than string, you must explicitly convert the arguments to some other type.

> I get an error that I cannot convert to float.

With out seeing the exact error message, it is hard to say for sure, but I would assume that you are converting something to a number which cannot be converted to a number. Example float('foobar') would throw an exception.

-Tatu

[1] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#argument-types

steve....@gmail.com

unread,
Dec 19, 2014, 6:56:02 AM12/19/14
to robotframe...@googlegroups.com, steve....@gmail.com
no, it is not a series of letters, but a value, like 9.92

Tatu Aalto

unread,
Dec 19, 2014, 10:18:55 AM12/19/14
to steve....@gmail.com, robotframework-users

Ugh

Without seeing the whole stack trace, it is impossible for me to say anything concrete. Run the test with loglevel set as trace [1] and one is able to see the whole stack trace in the log. Then could you send the whole stack trace and if possible the relevant lines of code to us and perhaps then we are able to help you.

-Tatu
Send from my mobile
[1] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#setting-log-level

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Guy Kisel

unread,
Dec 19, 2014, 12:53:35 PM12/19/14
to robotframe...@googlegroups.com, steve....@gmail.com
I might be misunderstanding the problem, but I think the solution is to use ${9.92} instead of 9.92 in your .robot files.

Another possible solution is to use a decorator for automatic type conversion, such as https://github.com/guykisel/python-autocast-decorator or https://github.com/pekkaklarck/argtypes


On Friday, December 19, 2014 7:18:55 AM UTC-8, Tatu Aalto wrote:

Ugh

Without seeing the whole stack trace, it is impossible for me to say anything concrete. Run the test with loglevel set as trace [1] and one is able to see the whole stack trace in the log. Then could you send the whole stack trace and if possible the relevant lines of code to us and perhaps then we are able to help you.

-Tatu
Send from my mobile
[1] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#setting-log-level

On 19 Dec 2014 13:56, <steve....@gmail.com> wrote:
no, it is not a series of letters, but a value, like 9.92



Le vendredi 19 décembre 2014 01:54:27 UTC-5, Tatu Aalto a écrit :

I am assuming that we are taking about python keywords.

> I tried to do a convert to number in robot framework and send that value, but it gets sent as a string

This is expected functionality, in most of the cases. More details are in the user guide [1].

> Perhaps I can change the python function parameters but I have not specified them as float, but convert them later with float ()  in the function to do the arithmetic and return the value 

Python is strongly and dynamically typed language. So if your code will require other type than string, you must explicitly convert the arguments to some other type.

> I get an error that I cannot convert to float.

With out seeing the exact error message, it is hard to say for sure, but I would assume that you are converting something to a number which cannot be converted to a number. Example float('foobar') would throw an exception.

-Tatu

[1] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#argument-types

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Tatu Aalto

unread,
Dec 19, 2014, 3:57:01 PM12/19/14
to guy....@gmail.com, robotframework-users, steve....@gmail.com
Ugh

There are also automatic conversions in Robot Framework, but the python float()[1] function does pretty good job in converting number like objects to float. Perhaps a simple example explains the issue better:
====== Test Suite =====
*** Settings ***
Library           PythonFloat.py

*** Test Cases ***
example
    ${arg}=    Convert To Float    1
    Should Be Equal As Numbers
    ...    ${arg}
    ...    1.0
    ${arg}=    Convert To Float    ${1}
    Should Be Equal As Numbers
    ...    ${arg}
    ...    1.0
    ${arg}=    Convert To Float    NotNumber

====== PythonFloat.py ======
def convert_to_float(arg):
    return float(arg)

========================

So if you pass a number like object to the float() function, it will convert the number like object to float. But if the object is not number like, then an error will be thrown, like in the last keyword call. Therefore I am reluctant to believe that the python float() would fail to convert number like object to float. Running the test case with trace level, shows the full stack trace and pinpoints the problem quite clearly. After that it is fairly easy to add print statements to relevant places or debug and get to the bottom of the problem (where the later part may not be easy, because the code in the keyword/function might be complex).

When I have difficult problem, which I am unable to solve by adding print statements, I use winpdb[2] to debug the problem. Regardless of the name winpdb is a platform independent debugger and in my opinion is more easy to use than the python default debugger. Also winpdb works well when the keyword/function[3] is executed from Robot Framework.

In any case, if you are not able to get to the bottom of the problem, could you send the whole stack trace and if possible the relevant lines of code to us and perhaps then we are able to help you. I understand the later part may not possible because of the contractual agreements.

-Tatu
[1] https://docs.python.org/2/library/functions.html#float
[2] http://winpdb.org/
[3] http://winpdb.org/docs/embedded-debugging/

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

Pekka Klärck

unread,
Dec 22, 2014, 6:37:21 PM12/22/14
to Guy Kisel, robotframework-users, steve....@gmail.com
2014-12-19 19:53 GMT+02:00 Guy Kisel <guy....@gmail.com>:
> I might be misunderstanding the problem, but I think the solution is to use
> ${9.92} instead of 9.92 in your .robot files.

I would try to avoid using automatic variables like ${9.92} rather
convert values in keywords themselves. As Tatu already commented,
using `float()` in Python ought to work just fine in this particular
case.

> Another possible solution is to use a decorator for automatic type
> conversion, such as https://github.com/guykisel/python-autocast-decorator or
> https://github.com/pekkaklarck/argtypes

Yeah, automatic type conversion is a good idea, especially if
conversion is needed by multiple keywords. This is basically same as
using `float()` (or some other converter) in keywords.

Notice that that the latter project above is sill work-in-progress and
may get API changes. My plan is to finalize it, document it, and
release 1.0 version once we get RF 2.8.7 out of the door.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org
Reply all
Reply to author
Forward
0 new messages