473,287 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Python 2.2 code continues running before list comprehension is completed?

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()
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.
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!

Any help with a solution or workaround will be greatly appreciated,

- Chris
Jul 18 '05 #1
7 2250
Am Montag, 19. Juli 2004 16:25 schrieb Chris P.:
Any help with a solution or workaround will be greatly appreciated,


IIRC, Python 2.2 didn't support list comprehensions (feature new as of 2.3),
so you'll have to work around that by creating some form of simple for loop
which does what you want, esp. considering that VARIOUS CONDITIONS ON x seems
to be a lot more than just a simple check (which will make the code a
helluvalot cleaner if spelled out).

Heiko.
Jul 18 '05 #2
Heiko Wundram wrote:
IIRC, Python 2.2 didn't support list comprehensions (feature new as of
2.3), so you'll have to work around that by creating some form of simple
for loop which does what you want, esp. considering that VARIOUS
CONDITIONS ON x seems to be a lot more than just a simple check (which
will make the code a helluvalot cleaner if spelled out).


Python 2.2.1 (#1, Sep 10 2002, 17:49:17)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[c for c in "you are wrong"][-5:] ['w', 'r', 'o', 'n', 'g']


(but only with regard to list comprehensions)

Peter
Jul 18 '05 #3
Chris P. <ch**************@utoronto.ca> wrote:
# Prior code opens telnet connection "tn" and logs in.
tn.read_until('> ')
tn.write('THE COMMAND IS HERE\n')
dump = tn.read_very_eager()
dump_lines = dump.split('\n')
dump_info = [x for x in dump_lines if (VARIOUS CONDITIONS ON x)]
[ ... ]
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!


Did you try putting a print in *before* the list comprehension
(for x in dump_lines: print x) to make sure you're getting all
the lines you think you're getting? Documentation says
read_very_eager() "Read[s] everything that's possible without
blocking in I/O" -- my first thought is that your client
running outside the debugger is reading faster than the server
can feed it data, it blocks part way through the execution of
the command, and read_very_eager() returns without the complete
output.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jul 18 '05 #4
Peter Otten wrote:
(but only with regard to list comprehensions)

....not being supported by 2.2
Jul 18 '05 #5
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
Jul 18 '05 #6
Am Montag, 19. Juli 2004 18:31 schrieb Peter Otten:
Python 2.2.1 (#1, Sep 10 2002, 17:49:17)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[c for c in "you are wrong"][-5:]


['w', 'r', 'o', 'n', 'g']


Humm... Okay, I don't have an installation of 2.2 around anymore, so I
couldn't check before I posted... ;) Forgive my faux pas. ;)

Heiko.
Jul 18 '05 #7
Hey. Just thought I'd give an update that I figured out how to make
my script work. The original code was:
tn.read_until('> ')
tn.write('THE COMMAND IS HERE\n')
dump = tn.read_very_eager()
dump_lines = dump.split('\n')
dump_info = [x for x in dump_lines if (VARIOUS CONDITIONS ON x)]


Sion had suggested that the client is reading faster than the server,
and I might want to try tn.read_all() instead of tn.read_very_eager().
That actually didn't work for me - read_all() documentation says:
"Read all data until EOF; block until connection closed" and that's a
problem... it makes my system hang, I'm guessing because the command I
send doesn't close the connection directly after it runs.

SO, I should have just looked to myself for inspriation: replacing the
tn.read_very_eager() with tn.read_until('> ') ensures that everything
up to the next command prompt is read in.

- Chris
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Raymond Hettinger | last post by:
Here are four more mini-mysteries for your amusement and edification. In this episode, the program output is not shown. Your goal is to predict the output and, if anything mysterious occurs,...
0
by: Irmen de Jong | last post by:
QOTW: "To make the instructions even friendlier it would also help if 'but Whatever You Do DON'T UNZIP THE FREAKIN' THING - This Means YOU John Latter!' were in large, bold, and underlined type. ...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
0
by: Cameron Laird | last post by:
QOTW: "n other languages, by the time you get the bloody thing working it's time to ship, and you don't have to bother worrying about making it optimal." -- Simon Brunning "One of the best...
0
by: Josiah Carlson | last post by:
QOTW: "XML with elementtree is what makes me never have think about XML again." -- Istvan Albert "'Plays well with others' was a strong motivator for Python's design, and that often means...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.