goto or continue execution from other line

Asked by hemanth

i have a line:
if exists(something,3):

but instead of else or continue i want script to skip some lines. Though this is not absolutely necessary i think it would make the code look clean......

What are the best possible ways to implement it?

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
RaiMan (raimund-hocke) said :
#1

not really clear what you are looking for.

You say "skip some lines": what I understand in the sense of programming: some lines should not be executed if a condition is met. this is done in Python/Sikuli by saying:

if not exists(something,3):
    # statement block 1
elsif not exists(something-else,3):
   # statement block 2
else:
   # statement block 3

- block 1 will be skipped, if something exists
- block 2 will be skipped, if something-else exists
- block 3 will be skipped, if either something or something-else does not exist

Thinking in skipping something (do not execute) is rather challenging. I think it is easier and more straightforward, to design a workflow having in mind, what should be executed if a condition is met (which could mean that something is NOT happening)

Revision history for this message
hemanth (hemanthmahesh1947) said :
#2

Yes, it is rather confusing me to think in "if not" rather than "if"!

But my real problem is

if not exists(something,3):
    # statement block 1

else:
   # statement block 2
   !# statement block 3!

- if something does exist,block 1 will be skipped
- if something does not exist,block 1 will be processed
But after processing block 1, i want it to continue to block 3 in the "else" B'cause i need to reuse my code!

Revision history for this message
hemanth (hemanthmahesh1947) said :
#3

so the elseif acts as some kind of intemediatery?
Does that mean (# statement block 2) becomes the "elseif "statement which is processed only if something exists and the rest of the processes is the same?

Revision history for this message
hemanth (hemanthmahesh1947) said :
#4

I think i need to make myself clear.

Example:

openApp("C:\\Program Files (x86)\\Opera 11.00 alpha\\opera.exe")
if exists(something):
       click("button")
else:
       click(something-2)
       click(something-3)

But after processing something, i want it to continue to something-3 skipping something 2 in the "else".

So with the elif would the code be:

openApp("C:\\Program Files (x86)\\Opera 11.00 alpha\\opera.exe")
if exists(something):
       click("button")
elif:
       click(something-2)
else:
       click(something-3)

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

--- 1. if: elif: elif: elif: ... else:
In other languages this would be a case or switch construct: testing some alternatives one after the other and executing the first block whose condition is met. if none mets, the else block is executed. Each elif: needs a condition expression to evaluate to True or False.

--- 2. your example above:

if not exists(something,3):
    # statement block 1
else:
   # statement block 2
   !# statement block 3!

- if something does exist,block 1 will be skipped
- if something does not exist,block 1 will be processed
But after processing block 1, i want it to continue to block 3 in the "else" B'cause i need to reuse my code!

This means block3 has to processed in both cases?

here you have it

if not exists(something,3):
    # statement block 1 executed if something does not exist
else:
   # statement block 2 executed if something exists
# statement block 3 always executed

I do not know your programming skills and especially the knowledge of Python: it is always a good idea to first have some workflow designed and written down representing, what you want to achieve. In Sikuli this often is some translation of your click and key-press behavior to achieve the same manually.

I usually write this down directly in Python using comments and print statements.

-- typical situation with a browser automation:

# open browser and wait for it to be ready
openApp("C:\\Program Files (x86)\\Opera 11.00 alpha\\opera.exe")
# wait for it to be ready or find out, wether we switched to a running instance
wait(3) # TODO to be replaced with more intelligent code later

# open a webpage
# - switch to address field (Firefox ctrl-l)
type("l", KEY_CTRL)
# - fill in url and press enter
url = "this-is-the-url"
paste(url); type(Key.ENTER)

# wait for webpage to be ready
if not exists(something, 10):
    print url, "not there after 10 seconds"; exit(1)
    # TODO later on we will try to take some corrective actions in this case

# we have to do a login
# TODO to be programmed later
popup("Please login and press ok when ready")

# wait for login to be processed
if not exists(something1, 10):
    print url, "login might have failed - waiting 10 seconds"; exit(1)
    # TODO later on we will try to take some corrective actions in this case

# we start clicking now
click(start)

# do we have to wait?
wait(0) # TODO to be adjusted?

# evaluate result
if exists(something2, 0):
   print "we have situation something2"
   # we have to click some options
   click(option1)
   click(option5)
   # TODO: we have to check wether option4 is on and switch it off
elif exists(something3, 0)
   print "we have situation something3"
   # TODO: actions later
   exit() # in this state of development we have to stop here
else:
   print "neither situation something2 nor something 3"
   exit(1)
   # TODO should not happen, we have to evaluate

happy scripting ;-)

BTW: If you have to repeat something, FAQ 1437 might help

Revision history for this message
hemanth (hemanthmahesh1947) said :
#6

Thanks RaiMan, that solved my question.