How to reuse the code in sikuli

Asked by Saket on 2017-11-24

How to reuse the code in sikuli :

Hi Guys, sorry for the repeated question but I am unable to understand / implement the solutions i found.

1. I have created a script to read data from excel row by row from 1st column & i want to paste it somewhere in browser and perform few steps then i want to go back to that excel and read 2nd row data and so on till there is a data in 1st column.
As soon as i find a blank row i want to stop

2. Or i would like to perform read operation say 100 times is it possible ?

My code :
hover("1511501830618.png")
click("1511501830618.png")
hover("1511330922974.png")
click("1511330922974.png")
type(Key.DOWN)
print("Message : Down Button pressed...")
wait(1)
while not exists("1511502908573.png"):
    print("Message : Keyword found...")
    wait(1)
    type("c",KEY_CTRL)
    wait(1)
    hover("1511501976440.png")
    click("1511501976440.png")
    wait(1)
    type("v",KEY_CTRL)
    wait(1)
    click("1511501830618.png")
    type(Key.DOWN)
    wait(1)
    hover("1511502768982.png")
    while exists("1511503440749.png"):
        print("Message : No Keyword found...")
        break;

Please help me with this guys, Thanks in Advance !!

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Saket
Solved:
2017-11-28
Last query:
2017-11-28
Last reply:
2017-11-27
masuo (masuo-ohara) said : #1

define a function with codes that you want to reuse.

def func001:
    Click("image01.png")

Example when you want to use func001 10 times

for no in range(10):
    func001()

Excel supports various export formats. One of the common formats is .csv format.
Jython Sikuli is based on supports csv Python module ( http://www.jython.org/docs/library/csv.html ).
I recommend to try to export Excel data to CSV.
If all the data you need go there, then you already have all the rows exported, so you may go without the part reading the data from Excel, and the second part would be looking like csv examples:

import csv
dataReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
for row in dataReader:
    #pasting code

Saket (saket0123) said : #3

Hi Masuo, I am using functions concept for the 1st time, so i am not sure what i have done in right or wrong. Based on your suggestion I have created a simple login logout function & calling it but I am getting below mentioned error. Can you please help me with this ?

Error :
[error] script [ Test_Function_Script ] stopped with error in line 3 at column 3
[error] SyntaxError ( "mismatched input ' ' expecting EOF", )

For your ref. line 3 in my code is 'def func001:'

My code is :

   def func001:
    hover("1511773804916.png")
    Click("1511773811501.png")
    wait(5)
    hover("1511773854020.png")
    click("1511773872796.png")
    print("Message : Function is called..")

#Example when you want to use func001 10 times

for no in range(10):
    func001()

RaiMan (raimund-hocke) said : #4

Python scripting language uses indentation to mark blocks of code.

So be sure that def and for are at column 0 (unindented).

about indentation: faq 1800

Saket (saket0123) said : #5

don't know but i just commented the code written above & again wrote same line below it and same thing is worked.

masuo (masuo-ohara) said : #6

Sorry I made some typo.

def func001(): # <---- modify func001 ==> func001()
    hover("1511773804916.png")
    click("1511773811501.png") # <---- modify Click ==> click
    wait(5)
    hover("1511773854020.png")
    click("1511773872796.png")
    print("Message : Function is called..")

#Example when you want to use func001 10 times

for no in range(10):
    func001()

Saket (saket0123) said : #7

Yes I searched this & found on net : def func001(): # <---- modify func001 ==> func001()

and this typo was done by my side : click("1511773811501.png") # <---- modify Click ==> click

Yes this worked perfectly
"for no in range(10):
    func001() "

Thanks for your help Masuo, I could understand new concept of functions with this.

Now i will implement this in my actual script where i wanted to reuse / run my particular line of code n times, if i come across any issue. i will mention you in that, if possible help me, Thanks in Advance !!

Saket (saket0123) said : #8

Hi Roman,

Thanks to you as well for your valuable comment, My urgent requirement was to reuse my code for n times. Excel read write operation I did it by code only, but still whenever possible I will try csv read write part mentioned by you.

Thanks for your support but for my current scenario I think function method should work, for now i have tried using function in test script, now i will implement it in my actual script.