Python 2.2 code continues running before list comprehension is completed?
Peter Otten
__peter__ at web.de
Mon Jul 19 12:42:03 EDT 2004
Chris P. wrote:
> Hi. I've made a program that logs onto a telnet server, enters a
> command, and then creates a list of useful information out of the
> information that is dumped to the screen as a result of the command.
> Here's a generic version of the code in question:
>
> #####
>
> # Prior code opens telnet connection "tn" and logs in.
> tn.read_until('> ')
> tn.write('THE COMMAND IS HERE\n')
> dump = tn.read_very_eager()
You could try tn.read_all() instead (just a guess).
assert "string1\r" in dump # would do no harm
assert ""string2\r" in dump
> dump_lines = dump.split('\n')
> dump_info = [x for x in dump_lines if (VARIOUS CONDITIONS ON x)]
>
> # string1 and string2 are strings that are known to
> # always appear in the dump.
Bold claim :-)
> A = dump_info.index('string1\r')
> B = dump_info.index('string2\r')
> C = A - B - 1
>
> #####
>
> The thing is, I can step through this code in the debugger and it
> works fine. When run from the command line, however, I get the
> following error in response to my dump_info.index lines:
>
> ValueError: list.index(x): x not in list
>
> So I tried putting (for x in dump_info: print x) right after I make
> the list comprehension and, when I run that, it shows that the list is
> incomplete... so it SEEMS like Python begins to create the list
> comprehension and then continues to execute commands before the list
> comprehension is finished!
I'm pretty sure that the list comprehension is finished. Look out for
something else.
Peter
More information about the Python-list
mailing list