Recognizing bad returns from Region.text()

Asked by KenK

I am using the region.text() function to get a string from a region that is constantly changing (a floating point number that changes every half second).

Once the text is captured, I convert the text into a floating point number and do analysis on this number:

for example:

str = myRegion.text()
myNum = float(str)

myNum = myNum + 5.0
print myNum

In most cases the capture works fine and I'm able to print out correctly. But in some iterations the text capture fails and the program crashes on the "myNum = float(str)" line because its trying to convert an invalid string capture into a float.

I know the text() function is still in development but, does anyone have an idea of how I can do a check first to make sure the string has a valid float format before I do the conversion to a float value?

I've tried playing around with the str.isdigit(), str.isalpha(), str.isspace() functions on the captured string first but they all crash when they try to check the invalid string.

Thanks, Ken

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

the easiest:

str = myRegion.text()
try:
    myNum = float(str)
except:
    myNum = 0 # or some other not possible value or
if myNum == 0:
    # do some error handling
myNum = myNum + 5.0
print myNum

Revision history for this message
KenK (kenneth-s-kardys) said :
#2

Thanks RaiMan, that solved my question.