define import and call it randomly?

Asked by Ben Dix on 2020-12-03

Hey Folks,

trying to call scripts from a main sikuli file in a random order...

I red this older article (https://answers.launchpad.net/sikuli/+faq/2446) and also the new documentation, and it looks like, there are still problems importing other sikuli files by import function using the IDE, while working on the imported sikuli file.

By using the we_are_in_rerun function, it is working 😄

But do I have to use the rerun function only while editing the file? or could this stay part of the script without causing trouble?

The next thing I try to do is to have for example 8 sikuli files and I want them to be executed in a random order, but every file only once, until all scripts are done 😄

maybe too tough for a python newbie like me, but you only learn by trying to reach huge goals 😄
greetings

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2020-12-03
Last reply:
2020-12-04
Ben Dix (bendix80) said : #1

I found a way, I think, but still need help defining my list to be importable items 😄
My search goes on, but here is what I wrote so far 😄

import random

scriptlist = ['Test1', 'Test2', 'Test3']
print("These are the scripts I want to execute " , scriptlist)
random.shuffle(scriptlist)
print("So I shuffled them to a new order " , scriptlist)
print("then I'll pick always the first of the list to execute it and then remove the first of that list")
print("The first script to take would be " + scriptlist[0])
scriptlist.pop(0)
print("The second script to take would be " + scriptlist[0])
scriptlist.pop(0)
print("The third script to take would be " + scriptlist[0])

Ben Dix (bendix80) said : #2

ok, sorry for talking to myself 😄
but proudly presenting a solution, working in theory and also in a different IDE environment (importing py files) so far:

import random

scriptlist = ['Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9']
print("These are the scripts I want to execute " , scriptlist)
Test1 = scriptlist[0]
Test2 = scriptlist[1]
Test3 = scriptlist[2]
Test4 = scriptlist[3]
Test5 = scriptlist[4]
Test6 = scriptlist[5]
Test7 = scriptlist[6]
Test8 = scriptlist[7]
Test9 = scriptlist[8]

def selectScript():
 if scriptlist[0] is Test1:
  Test1import()
 else:
  ()
 if scriptlist[0] is Test2:
  Test2import()
 else:
  ()
 if scriptlist[0] is Test3:
  Test3import()
 else:
  ()
 if scriptlist[0] is Test4:
  Test4import()
 else:
  ()
 if scriptlist[0] is Test5:
  Test5import()
 else:
  ()
 if scriptlist[0] is Test6:
  Test6import()
 else:
  ()
 if scriptlist[0] is Test7:
  Test7import()
 else:
  ()
 if scriptlist[0] is Test8:
  Test8import()
 else:
  ()
 if scriptlist[0] is Test9:
  Test9import()
 else:
  ()

def Test1import():
 import Test1

def Test2import():
 import Test2

def Test3import():
 import Test3

def Test4import():
 import Test4

def Test5import():
 import Test5

def Test6import():
 import Test6

def Test7import():
 import Test7

def Test8import():
 import Test8

def Test9import():
 import Test9

def Test10import():
 import Test10

random.shuffle(scriptlist)
print("So I shuffled them to a new order " , scriptlist)
selectScript()
print("then I'll pick always the first of the list to execute it and then remove the first of that list")
print("The first script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The second script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The third script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The 4. script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The 5. script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The 6. script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The 7. script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The 8. script to take would be " + scriptlist[0])
scriptlist.pop(0)
selectScript()
print("The 9. script to take would be " + scriptlist[0])
scriptlist.pop(0)

resulting in:

These are the scripts I want to execute ['Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9']
So I shuffled them to a new order ['Test3', 'Test6', 'Test4', 'Test7', 'Test9', 'Test2', 'Test8', 'Test5', 'Test1']
say hello to my friends - this is the Test3 script-------------------------------------------------------3-3-3--3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3
then I'll pick always the first of the list to execute it and then remove the first of that list
The first script to take would be Test3
say hello to my friends - this is the Test6 script-------------------------------------------------------6-6-6--6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6
The second script to take would be Test6
say hello to my friends - this is the Test4 script-------------------------------------------------------4-4-4--4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4
The third script to take would be Test4
say hello to my friends - this is the Test7 script-------------------------------------------------------7-7-7--7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7
The 4. script to take would be Test7
say hello to my friends - this is the Test9 script-------------------------------------------------------9-9-9--9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9
The 5. script to take would be Test9
say hello to my friends - this is the Test2 script-------------------------------------------------------2-2-2--2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2
The 6. script to take would be Test2
say hello to my friends - this is the Test8 script-------------------------------------------------------8-8-8--8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8
The 7. script to take would be Test8
say hello to my friends - this is the Test5 script-------------------------------------------------------5-5-5--5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5
The 8. script to take would be Test5
say hello to my friends - this is the Test1 script-------------------------------------------------------1-1-1--1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1
The 9. script to take would be Test1

😄

now trying to implement this to sikuli 😄

RaiMan (raimund-hocke) said : #4

********************* corrections to comment #3

To get to the next level of insight ;-)
first read this:
https://sikulix-2014.readthedocs.io/en/latest/scripting.html#runScript

As mentioned in the cited answer, import should not be used to run code, that is at indent level 0, because it is only executed at the first import. There are a few exceptions, but an imported script should only contain name definitions (variables, functions and classes).

so runScript() is your feature (see above).

a comment on your solution:
Your script is a sequence of lines produced by copy&paste.
That usually does not make sense, since scripting (as any programming language) has enough features to follow the DRY principle (Don't repeat yourself).

On top scripting usually is run by an Interpreter, which can be given some piece of code, that is created on the fly and handed down to be run.

The relevant Python features are eval() (evaluate an expression) and exec() (run some lines of code). In both cases, the parameter is a string, that must evaluate to a valid expression or valid code.

So your code as an example:

import random
scripts = ["Test" + str(x) for x in range(1, 9)] # a Python-typical construct: list-comprehension
random.shuffle(scripts)
for script in scripts:
    exec("import " + script)

... and with runScript()
import random
scripts = ["Test" + str(x) for x in range(1, 9)]
random.shuffle(scripts)
for script in scripts:
    exec("runScript(%s)" % script)

Have fun :-))

RaiMan (raimund-hocke) said : #9

user is happy ;-)