'While' question

Scott David Daniels Scott.Daniels at Acm.Org
Sat Aug 23 14:37:09 EDT 2008


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
Scott.Daniels at Acm.Org



More information about the Python-list mailing list