Using Variables via Settings, refresh?

Asked by David Lafferty

Hi all,
I've been searching through the FAQs and Docs and can't quite see what I'm after.
I've created 3 sikuli script files. Main, CreateAClient, NBWrapUp.
Main basically Imports the other two files.
At the start of Main I've set up a variable myClientName:
Settings.myClientName = "Sikuli " + time.strftime("%Y/%m/%d - %H:%M.%S")

I am then using this variable each time the Main script is invoked in the other two Sikuli Scripts.

I've noticed however that each time I'm running this as I'm testing the scripts the "Time" part of the variable isn't being refreshed, thus making me think that the "Settings.myClientName" is not being refreshed each time. Somehow the first time it has been run it is now set.

Is there a way to refresh this variable? I realise modules get refreshed through the reload() method, but not variables.

Hope someone can help, I realise it's a Friday! :)
Thanks.
Dave

Question information

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

--- I am then using this variable each time the Main script is invoked in the other two Sikuli Scripts.
not really clear, what that means: the Main script is invoked in the other two ???

You say: Main basically Imports the other two files.

Based on this, I guess you have something like this:

# main.sikuli
Settings.myClientName = "Sikuli " + time.strftime("%Y/%m/%d - %H:%M.%S")
import CreateAClient
reload(CreateAClient)
import NBWrapUp
reload(NBWrapUp)
# surely there is more code, but thats the essence

# CreateAClient.sikuli
from sikuli import *
print Settings.myClientName

# NBWrapUp.sikuli
from sikuli import *
print Settings.myClientName

If this is true, then every time, the main script is rerun, Settings.myClientName is set to the actual time value and the subs are run one after the other.

So what is your problem?

Revision history for this message
David Lafferty (david-lafferty) said :
#2

Apologies for the naming convention. Yes there is a Main.sikuli script.
That script as you have correctly guessed contains:
# main.sikuli
Settings.myClientName = "Sikuli " + time.strftime("%Y/%m/%d - %H:%M.%S")
import CreateAClient
reload(CreateAClient)
import NBWrapUp
reload(NBWrapUp)

And within the other two sikuli files, they make use of this variable.

What I was trying to achieve is that everytime I run the Main.sikuli, which in turn runs all the scripts I've wrote altogether I wanted the "global variable" to be the current time & date. It seemed that whilst doing incremental development and testing each new part in turn, the myClientName variable was not being refreshed. It was constantly spitting out the initial time and date.

I've temporarily put the declaration of the variable within it's own sikuli file called initVariables.sikuli which does what it says on the tin. Doing it this way I can use the reload(initVariables) method which seems to be refreshing the myClientName variable.
Is there another way, some syntactical way of doing this without using another file, or would you recommend using other files?

-----------

Also, I've noticed that using import(file1) and then reload(file1) #to capture any changes during development.
Sometimes causing the first file to repeat it's execution.
I tried wrapping the import(file1) in an if statement but this seemed to have no affect, attempting if the import works, don't reload the file. I may be misunderstanding the use of the import/reload function however.

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

--- Sometimes causing the first file to repeat it's execution.
Sorry, I did not think about the fact, that the first time the main script is run, every subscript is run 2 times (first for the import, that is only done once as long as IDE stays open and second with the reload (which in turn is the only processing on rerun))

- this trick helps:
# at the beginning of the main script only once:
try: Settings.isRerun; Settings.isRerun = True
except: Settings.isRerun = False

# every import / reload
import sub
if Settings.isRerun: reload(sub)

on the first run, isRerun will be undefined, so it gets assigned False and only the import will run. The second time and later, isRerun will be True, so the reload will be processed. So in both cases, your sub script will only be run once.

But be aware: after adding a new import/reload, you have to restart the IDE !!

--- It was constantly spitting out the initial time and date.
I cannot reproduce this behavior.

Using this in the IDE:
Settings.myClientName = "Sikuli " + time.strftime("%Y/%m/%d - %H:%M.%S")
print Settings.myClientName

prints the actual time on every rerun.

And this is the preferred solution (you should not need to put it in an extra script).

So you have to check your code and observation.

Revision history for this message
David Lafferty (david-lafferty) said :
#4

Thanks for the time and effort put into that response!
I will give the try / except layout within my Main.sikuli script.

Odd with regard to the time issue. I may continue as I am with the extra setup script as I may require more variables to be used and it would just be handy initialising them all in one location.

I will come back and update this log if successful, thanks again for the advice!
Dave

Can you help with this problem?

Provide an answer of your own, or ask David Lafferty for more information if necessary.

To post a message you must log in.