473,382 Members | 1,791 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,382 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 2251
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.