How to use readline.read_history_file with Sikuli?

Asked by Tet Yoon Lee

Am I right that readline.read_history_file/readline.write_history_file are unavailable with sikuli? I'm fairly inexperienced with jython or coding in general but from what I've tried and the error messages I receive I think so but I'm not sure.

If I'm correct, what would be the best method to store history of readline? I'm trying to read from a file, line by line, recording to a history file probably after every line (then doing something with the line) and I'd liked it to be saved to a history file so I can easily stop and start my script. As a quick test, what I'm trying at the moment is:

file_dir = "c:\\Temp\\"
listfile=open(file_dir + "list.txt")
while True:
 #listfile.readline.read_history_file(file_dir + "listfilehistory.txt")
 var1 = listfile.readline()
 var2 = listfile.readline()
 var3 = listfile.readline()
 #listfile.readline.writehistoryfile(file_dir + "listfilehistory.txt")
 print var3
 print var2
 print var1
 wait(11)
listfile.close()

This works as I expect except I can't use the history hence why it's commented out. As you may guess from my example, this is on Windows. Specifically Windows XP and I'm using Sikuli X-1.0rc1.

Cheers

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

--- 1.
to use the readline module, I guess you have to say

import readline

before (it is not a built-in support)

--- 2.
I think your usage is not as intended by the module readline
look: http://docs.python.org/release/2.6.6/library/readline.html?highlight=readline#readline.read_history_file

The module seems to implement some support for history files together with the interactive interpreter, so can save interactive sessions and replay them at another time.

generally the functiona have to be used like
readline.read_history_file(filename) (loads and executes the contents of a history file)

--- 3.
In non-interactive scripting, I think, there is no need for such a function: you write down some scripting statements and save them using the Sikuli IDE - and then run them.
If you want to execute scripting statements, that are saved in another file, use execfile(filename).

--- 4.
If you are using interactive Jython, to test some Sikuli features and want to save what you have done, that might be the intended usage, but seems to be a little bit complicated, since you can always open a new tab in the IDE and try out what you want.

Revision history for this message
Tet Yoon Lee (nileinne) said :
#2

Thank for your help although I think I may not have explained what I'm trying to do very well.

The reason I am reading from a file is I have a long list of URLs (500+). I want to visit these URLs one by one doing stuff inbetween visiting each one. It seems easiest to read them from a file. The file only contains the list of URLs. However sometimes I may want to do something else on the computer and stop the script for a while. I may even want to restart the computer or whatever.

I guess I can manually record the current state of the script but that seems a bit unnecessary so I was planning to get the Sikuli script to automatically record the last position it was reading and use that position upon start up. I have another script which does everything else I want which I designed earlier and I added the readline stuff it works as expected except for the history.

I think readline is one way to do what I'm doing at least from http://docs.python.org/tutorial/inputoutput.html although you may be right I'm not understanding the history function properly. I believe line and linecache are other methods but I couldn't work out how to get line working as I wanted at all (didn't try very hard) and linecache isn't provided by Sikuli I think.

Anyway, although line number seemed the easier way to do what I'm trying, perhaps it's best to just save the line to a history file and then when the script starts load the line from the history file and readline until I reach the right line. This is what I've implemented now and it seems to work.

listfile=open(file_dir + "list.txt")
historyfileread = file(file_dir + "historyfile.txt")
historyread = historyfileread.readline()
historyfileread.close()
while True:
 if historyread == "":
  break
 lineread = listfile.readline()
 if lineread == historyread:
  break

Later in my script it will start with the next line (it does readline again). So no need to import anything and it all works well. Again, my coding and methods may seem exceptionally amateurish because that's what I am but I'm not planning to release it so if it works I'm happy :-) (You may notice this script doesn't currently handle the absence of a history file.)

Thanks again, of course even though I've found one way if you do have any other suggestions, I'm still listening.

Cheers

Revision history for this message
Tet Yoon Lee (nileinne) said :
#3

Thanks RaiMan, that solved my question.