I am making a simple test function that claims that the output from the interpreter that I am developing is correct by reading from the file an expression for the evaluation and the expected result, similar to the python doctrine. This is for the circuit, so an example input file will be
> 42 42 > (+ 1 2 3) 6
My first attempt for a function that can parse such a file is as follows: it works as expected:
def run_test(filename): interp = Interpreter() response_next = False num_tests = 0 with open(filename) as f: for line in f: if response_next: assert response == line.rstrip('\n') response_next = False elif line.startswith('> '): num_tests += 1 response = interp.eval(line[2:]) response = str(response) if response else '' response_next = True print "{:20} Ran {} tests successfully".format(os.path.basename(filename), num_tests)
I wanted to improve it a bit by removing the response_next flag, since I am not a fan of such flags and instead read the next line in the elif block with next(f) . I had a little unrelated question regarding what I asked on IRC on freenode. I got the help I wanted, but I was also asked to use f.readlines() instead, and then use indexing in the resulting list. (I was also told that I can use groupby() in itertools for paired strings, but I will explore this approach later.)
Now, to the question, I was very curious why this approach would be better, but my Internet connection was a whisper in the train, and I could not ask, so I will ask for it here. Why is it better to read everything with readlines() instead of parsing each line when they are read on the fly?
I really wonder how my feelings are opposite, I think it seems clean to parse the lines one at a time, so that everything is finished at a time. I usually avoid using indexes in arrays in Python and prefer working with iterators and generators. It may not be possible to answer and guess what the person thought if it was a subjective opinion, but if there is any general recommendation, I would be glad to hear about it.
source share