Write to same file from different modules

Asked by shaihulud

I have a test suite which is a collection of tests running from different modules. At different points in different tests I want to verify that something exists and write into a separate txt file that it does. But I can only get the first module called to write to the file, the second module does not. I guess one dirty solution is to have separate files for each module.
Another strange thing is that the following code runs one time, but when I run it a second time i get the error:
[error] script [ testsuite] stopped with error in line 2
[error] ValueError ( I/O operation on closed file )

Here is a condensed version of what i´ve done so far:

(Module: testsuite.sikuli)
Import Method1
Method1.tst1()

(Module:Method1.sikuli)
import Method2
from sikuli import *
logfile = file("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt","w")
def tst1():
    logfile.write("This is tst1" + "\n")
    logfile.close
    Method2.tst2()

(Module :Method2.sikuli)
from sikuli import *
logfile = file("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt","w")
def tst2():
    logfile.write("this is tst2" + "\n")
    logfile.close

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

Since code on the indent level 0 (not in def()s or classes) is run at import time and ONLY ONCE at import time, the statement
logfile = file("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt","w")

is executed at import Method1 (and it is executed twice, since Method1 imports Method2 at import) and it contains an implicit open of the file.

then you call
Method1.tst1()

which writes to the file and then closes it.

at rerun, the file is closed and not opened again, since
logfile = file("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt","w")
is not rerun (index level 0 code !)

so the easiest correction is to simply put the
logfile = file("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt","w")
as 1st statement into the def()s and everything is fine ;-)

and you have to use "a" instead of "w", to open the file in append mode ("w" will always clear the file and only write the one record)

But what you are doing is against the DRY design pattern: don't repeat yourself:
the log-open-write-close should be a global def with the message to write as parameter
def myLog(msg):
    logfile = file("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt","a")
    logfile.write(msg + "\n")
    logfile.close

But why are you inventing the wheel again:
version 1.0.1 has a user log feature:
Debug.user(msg)

or
Debug.user(format_string, var1, var2, …) according to the Java String format feature

The messages will even have timestamps at the beginning ;-)

when saying once at the beginning
Debug.setUserLogFile("C:\\Users\\caan\\Documents\\SikuliTests\\TwoImports\\output.txt")

you will get all your messages to that file.

… and if you want more features: Python has a nice logging feature ;-)

Revision history for this message
shaihulud (cal-a) said :
#2

wow you are such a star RaiMan, thanks so much for all your help. Hope I one day will be able to contribute back to the community! :)

Revision history for this message
shaihulud (cal-a) said :
#3

Thanks RaiMan, that solved my question.

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

Thanks for kind feedback. Glad to help.