I'm participating in online judge contests and I want to test my code with a .in file full of testcases to time my algorithm. How can I get my script to take input from this .in file?
+1
A:
PyUnit "the standard unit testing framework for Python" might be what you are looking for.
Doing a small script that does something like this:
#!/usr/bin/env python
import sys
def main():
in_file = open('path_to_file')
for line in in_file:
sys.stdout.write(line)
if __name__ == "__main__":
main()
And run as
this_script.py | your_app.py
Macarse
2009-06-21 19:06:44
Not exactly what I'm looking for, as I want my script to take the .in file as if its input was coming from stdin.
fmartin
2009-06-21 19:12:22
Oh, check my edit.
Macarse
2009-06-21 19:21:55
+5
A:
So the script normally takes test cases from stdin, and now you want to test using test cases from a file?
If that is the case, use the <
redirection operation on the cmd line:
my_script < testcases.in
codeape
2009-06-21 19:31:23
+2
A:
Read from file(s) and/or stdin:
import fileinput
for line in fileinput.input():
process(line)
J.F. Sebastian
2009-06-21 19:57:07
A:
You can do this in a separate file.
testmyscript.py
import sys
someFile= open( "somefile.in", "r" )
sys.stdin= someFile
execfile( "yourscript.py" )
S.Lott
2009-06-21 20:48:35