python sikuli issue

Asked by Prabhas Koya

Once check my small code and tell whats wrong in it? how can i achieve it
click(Region(47,721,39,47))
wait(2)
type("date +%H.%M")
type(Key.ENTER)
a = Region(1,48,57,22).text()
TIME = a
if TIME < 12.45:
  print("SUCCESS")
else:
  print("FAIL")
this means i hava to click terminal and wait 2 seconds and type date format and press enter and copy the output from the region and here my output is time ..but here instead of printing success its giving fail when the statement is True and when the statement is False, it is printing success the reverse!! what should i do??
or

if i tried another code like
Time = run("date +%H%M")
if Time < 12.45:
  print("SUCCESS")
else:
  print("FAIL")
ITS again showing the reverse output!!

what should i do i want to get time synchronized every time ,so i tried this one ..Can anyone give suggestion????

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
masuo (masuo-ohara) said :
#1

Variable "TIME" is string.
Convert "TIME" to floating variable "fTime" for example.
And compare "fTime" and "12.45".

Revision history for this message
Best RaiMan (raimund-hocke) said :
#2

Generally: Python variables should start with a lowercase letter and consist only of letters numbers and underscore.

First trial with Region().text() is not recommended, because the OCR feature is not 100% reliable.

Second case:
timeNow = run("date +%H%M")
this returns a string containing 2 lines (1st return value, 2nd the result of date)
in this case you get 0\n0951

to get something useable:
timeNow = -1
result = run("date +%H%M").split()
if result[0] == "0":
    timeNow = int(result[1]) # timeNow is 951
    print "timeNow =", timeNow
    if timeNow > 1245:
        print "it is 12:46 or later"
    else:
        print "it is earlier than 12:46"

... or as float as you tried:
timeNow = -1
result = run("date +%H.%M").split() # the dot has to be in the format spec
if result[0] == "0":
    timeNow = float(result[1]) # timeNow is 9.51
    print "timeNow =", timeNow
    if timeNow > 12.45:
        print "it is 12:46 or later"
    else:
        print "it is earlier than 12:46"

But why inventing the wheel again - you have Python:
https://docs.python.org/2/library/time.html#time.strftime

timeNow = float(time.strftime("%H.%M"))
print "timeNow =", timeNow
if timeNow > 12.45:
    print "it is 12:46 or later"
else:
    print "it is earlier than 12:46"

Revision history for this message
masuo (masuo-ohara) said :
#3

@RaiMan
You are very kind to beginners. (^^)v

Revision history for this message
RaiMan (raimund-hocke) said :
#4

@masuo
sometimes, yes ;-)
mostly in cases, where I want to check my own knowledge :-))

Revision history for this message
Prabhas Koya (prabhas1) said :
#5

Thanks RaiMan, that solved my question.

Revision history for this message
Prabhas Koya (prabhas1) said :
#6

RAI MAN YOU ARE AWESOME
timeNow = float(time.strftime("%H.%M"))
print "timeNow =", timeNow
THIS SOLVED MY QUESTION

Revision history for this message
Prabhas Koya (prabhas1) said :
#7

How can I run a script which synchronizes time ..That means what should I add to the code such that it synchronises time and runs in background,because I added a variable time and used if else elif statements with the time they run..what should i do???