473,414 Members | 1,665 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,414 software developers and data experts.

Re: 'While' question

Thanks. I tried to use 'for' instead of 'while' as both of you
suggested. It's running well as my previous version but breaks
completely instead of just skipping the empty file. I suspect the
reason is that this part is inside another 'for' so it stops
everything. I just want to it to break only one 'for', that is go back
to 5th line in the example code (directory C has one empty file):

receptors = ['A', 'B', 'C']
for x in receptors:
print x
for i in range(3):
for r in (7, 9, 11, 13, 15, 17):
f =
open('c:/Linux/Dock_method_validation/%s/validation/ligand_ran_line_%s_%sA_secondary_scored.mol2'
%(x,i,r), 'r')
line = f.readline()[:-1]
out_file =
open('c:/Linux/Dock_method_validation/%s/validation/pockets.out' %(x),'a')
out_file.write('%s ' %i)
out_file.write('%s ' %r)
# skip to scores
j=0
for line in f:
line = line.rstrip()
if "PRIMARY" not in line:
j += 1
if j == 20:
break
else:
for line in f:
if "TRIPOS" not in line:
line = line.rstrip()
out_file.write(line)
else:
break
f.close()
out_file.close()

Any suggestions as for how to control the "extent of break"? should I do
something else instead? Thank you!
Aug 22 '08 #1
3 1077
On Fri, 22 Aug 2008 10:42:13 -0400, Ben Keshet wrote:
Thanks. I tried to use 'for' instead of 'while' as both of you
suggested. It's running well as my previous version but breaks
completely instead of just skipping the empty file. I suspect the
reason is that this part is inside another 'for' so it stops
everything. I just want to it to break only one 'for', that is go back
to 5th line in the example code (directory C has one empty file):
for line in f:
^^^
line = line.rstrip()
if "PRIMARY" not in line:
j += 1
if j == 20:
break
else:
for line in f:
^^^
You're iterating through the same value in inner and outer loop.
Don't do that. It's hard to predict the behavior of such a code.

Regarding break statement, it breaks only the inner loop
and returns to the outer loop/block.

It would be great if you could reduce your code to a short piece
that illustrates your problem and that we could all run.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Aug 22 '08 #2
Wojtek Walczak wrote:
On Fri, 22 Aug 2008 10:42:13 -0400, Ben Keshet wrote:
>Thanks. I tried to use 'for' instead of 'while' as both of you
suggested. It's running well as my previous version but breaks
completely instead of just skipping the empty file. I suspect the
reason is that this part is inside another 'for' so it stops
everything. I just want to it to break only one 'for', that is go back
to 5th line in the example code (directory C has one empty file):

> for line in f:
^^^
> line = line.rstrip()
if "PRIMARY" not in line:
j += 1
if j == 20:
break
else:
for line in f:
^^^
You're iterating through the same value in inner and outer loop.
Don't do that. It's hard to predict the behavior of such a code.

Regarding break statement, it breaks only the inner loop
and returns to the outer loop/block.

It would be great if you could reduce your code to a short piece
that illustrates your problem and that we could all run.

I ended up using another method as someone suggested to me. I am still
not sure why the previous version got stuck on empty files, while this
one doesn't:

receptors = ['A' 'B']
for x in receptors:
# open out_file for appending for each 'x' in receptors, close at
same level
out_file =
open('c:/Linux/Dock_method_validation/%s/validation/pockets.out' %(x),'a')
for i in range(10):
for r in (7, 9, 11, 13, 15, 17):
f =
open('c:/Linux/Dock_method_validation/%s/validation/ligand_ran_line_%s_%sA_secondary_scored.mol2'
%(x,i,r), 'r')
# assume 'PRIMARY' should be found first
# set flag for string 'PRIMARY'
primary = False
# iterate on file object, empty files will be skipped
for line in f:
if 'PRIMARY' in line:
primary = True
out_file.write(line.strip())
# copy scores
elif 'TRIPOS' not in line and primary:
out_file.write(line.strip())
out_file.write(' ')
elif 'TRIPOS' in line and primary:
break
print
out_file.write('\n')
f.close()
out_file.close()
Aug 23 '08 #3
Ben Keshet wrote:
....
I ended up using another method as someone suggested to me. I am still
not sure why the previous version got stuck on empty files, while this
one doesn't:

receptors = ['A' 'B']
*** Alarm bells *** Do you mean ['AB'], or do you mean ['A', 'B']?
...(more code one way) ...
Don't be afraid of defining functions, you are nested too deeply to
easily understand, and hence likely to make mistakes. For similar
reasons, I don't like names like x and i unless there are no better
names. Also, since you don't seem to "really" need to write, I used
print. The comments would be better if I knew the field a bit (or
your code had had better names).
Try something like (obviously I couldn't test it, so untested code):

OUTPUT = 'c:/Linux/Dock_method_validation/%s/validation/pockets.out'
INPUT = ('c:/Linux/Dock_method_validation/%s/validation/'
'ligand_ran_line_%s_%sA_secondary_scored.mol2')

def extract_dist(dest, receptor, line, ligand):
'''Get distances after "PRIMARY" from the appropriate file
'''
source = open(INPUT % (receptor, line, ligand), 'r')
gen = iter(source) # get a name for walking through the file.
try:
# Find the start
for j, text in enumerate(gen):
if 'PRIMARY' in text:
print >>dest, text.strip(),
break
if j == 19: # Stop looking after 20 lines.
return # nothing here, go on to the next one
# copy scores up to TRIPOS
for text in gen:
if 'TRIPOS' in text:
break
print >>dest, text.strip(),
print
print >>dest
finally:
source.close()

for receptor in 'A', 'B':
# open out_file for appending per receptor, close at same level
out_file = open(OUTPUT % receptor, 'a')
for line in range(10):
for ligand in (7, 9, 11, 13, 15, 17):
extract_dist(out_file, receptor, line, ligand)
out_file.close()
--Scott David Daniels
Sc***********@Acm.Org
Aug 23 '08 #4

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

Similar topics

1
by: jjliu | last post by:
Could someone point me out that why the following perl program only print 'title'. I expect the contents in h1, keywords and description to print out as well. thanks. JJL #!/usr/bin/Perl ...
19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
5
by: J. Yuan | last post by:
Hi, I am working on a checkout/inventory system. How can I make a button that when pressed, would update the previous fields transaction number to a table (for example, -3 printers, so that...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
8
by: jvb | last post by:
Hey all, I figure it's Wednesday, why not put a question up for debate. Beyond personal preference, is there any benefit (performance or otherwise) to using one loop over the other? For example,...
9
by: morpheus | last post by:
Hi Group, When I run this: # include <stdio.h> int main(){ int c=0; while ( (c=getchar()) != EOF && c != ' ' && c != '\t' ) printf("foo"); if (c == '8') putchar(c);
11
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the...
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.