[Tutor] improve the code

lina lina.lastname at gmail.com
Tue Nov 1 15:11:14 CET 2011


Hi,

The following code (luckily) partial achieved what I wanted, but I
still have few questions:


#!/usr/bin/python3

import os.path

INFILEEXT=".txt"
OUTFILEEXT=".new"
DICTIONARYFILE="dictionary.pdb"
orig_dictionary={}
new_dictionary={}
abetaABresidues={}

def processonefiledata(infilename):
    with open(infilename,"r") as f:
        for line in f:
            parts=line.strip().split()
            orig_dictionary[parts[0]]=parts[1]


def build_abetadictionary(infilename,olddict):
    with open(infilename,"r") as f:
        for line in f:
            parts=line.strip().split()
            if parts[0] != "85CUR" and (parts[0] not in abetaABresidues.keys()):
                abetaABresidues[parts[0]]=0
        for residues, numbers in abetaABresidues.items():
            if residues in olddict.keys():
                new_dictionary[residues]=olddict[residues]
            else:
                new_dictionary[residues]=0
        with open(base+OUTFILEEXT,"w") as f:
            for residues, numbers in new_dictionary.items():
                print(residues,numbers,file=f)
## Q1: How can I sort the results, like the effect of | sort -g

from something like:
84ALA 12
:
:
83ILE 28
:
:

to
:
83ILE 28
84ALA 12
:


if __name__=="__main__":
    for filename in os.listdir("."):
        base, ext =os.path.splitext(filename)
        if ext == INFILEEXT:
            print(filename)
            processonefiledata(filename)
            build_abetadictionary(DICTIONARYFILE,orig_dictionary)

Thanks for any comments or suggestions you may give.

The relevant testing file are attached below links:

https://docs.google.com/open?id=0B93SVRfpVVg3YjdhNjlmYTAtMTdkYy00ZTNjLThkOWEtOGMyNTM1YTBiMmU4
https://docs.google.com/open?id=0B93SVRfpVVg3OWNiZmUwODktMDU4Ny00ZDUyLWExYzQtM2E2ZmY5NGJhNzgz
https://docs.google.com/open?id=0B93SVRfpVVg3MTBmNTM3M2UtYjdiMC00N2UwLWE1YTQtNmU3OGQzOGYwNDc3

Best regards,


More information about the Tutor mailing list