473,394 Members | 2,063 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,394 software developers and data experts.

Strange loop behavior

Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.

--
Arimaz SA
Av. du 24 Janvier 11
Ateliers de la Ville de Renens, Atelier 5
1020 Renens, Switzerland
www.mydeskfriend.com
Mob: +41-(0)79-539-0069

Mar 26 '08 #1
4 1330
Gabriel Rossetti schrieb:
Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.
But you used a superfluous repr. Look at this:
>>repr("")
"''"
>>repr("") == ""
False
>>>
So - get rid of the useless repr, then things should work.
Regarding the behavior in the debugger: that's an artifact of the
debugger that it doesn't step into that line, it doesn't change the way
the code works.

Diez
Mar 26 '08 #2
Gabriel Rossetti wrote:
I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.
d may be an empty string, but repr(d) isn't:
>>d = ""
print repr(d)
''
>>print d
Remove the two repr() calls and you should be OK.

Peter
Mar 26 '08 #3
On Mar 26, 8:35*am, Gabriel Rossetti
<gabriel.rosse...@mydeskfriend.comwrote:
Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

* * d = repr(f.read(DEFAULT_BUFFER_SIZE))
* * while d != "":
* * * * file_str.write(d)
* * * * d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.
No, it isn't the empty string, it is the printable representation of
the empty string (because you wrap your f.read() calls in repr()),
which is the string "''":
>>print "''"
''

Why do you wrap f.read(...) in the repr() function? If you remove the
two repr() I suspect your code will work.

OTOH do you know that the read() method of file objects does what you
want? You could simply write:

file_str = f.read()

Or are there some other factors that prevent you from doing this?

--
Arnaud

Mar 26 '08 #4
Arnaud Delobelle wrote:
On Mar 26, 8:35 am, Gabriel Rossetti
<gabriel.rosse...@mydeskfriend.comwrote:
>Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.

No, it isn't the empty string, it is the printable representation of
the empty string (because you wrap your f.read() calls in repr()),
which is the string "''":
>>print "''"
''

Why do you wrap f.read(...) in the repr() function? If you remove the
two repr() I suspect your code will work.

OTOH do you know that the read() method of file objects does what you
want? You could simply write:

file_str = f.read()

Or are there some other factors that prevent you from doing this?

--
Arnaud

Ok, like I mentioned in my second msg, I had put repr() there because I
was getting errors because the ascii range (128) was exceeded, I tried
cheating and telling it it was unicode but that didn't work, so I tried
repr() and it gave me a string rep. of my data, that was ok. After that
I tried using StringIO instead of what I had before, a list of strings
that I later joined with an empty string. I just re-tried removing the
repr() and it works with the StringIO version.

Thanks for all your answers,
Gabriel
Mar 26 '08 #5

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

Similar topics

2
by: simon place | last post by:
while playing about with inheriting from list to be able to cache macro properties i noticed this, the rate of summing a list seems to be over linear? its nearly 3 times faster to sum the sums of...
0
by: John Hunter | last post by:
I have a class that uses some extension code I have written and I am trying to track down some memory leaks, which I presume to be in my extension code. The class is question is in a python...
3
by: curi42 | last post by:
I am running two functions in a row that do the same thing. One runs in .14 seconds, the other 56. I'm confused. I wrote another version of the program and couldn't get the slow behavior again,...
4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
2
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions...
2
by: maxim khesin | last post by:
I was working on some code that compiled with vc6, eg.g for(int i;i<x;); if(i<j) {// etc which I rewrote to int i = 0; for(i = n;i<x;);
2
by: Jim Bancroft | last post by:
Hi everyone, I have a DropDownList I populate as outlined below. This is from my code-behind file: private void Page_Load(object sender, System.EventArgs e) { BindMyData(); DataBind(); }
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.