[Tutor] improve the code

lina lina.lastname at gmail.com
Tue Nov 1 16:11:01 CET 2011


On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel <d at davea.name> wrote:
> On 11/01/2011 10:11 AM, lina wrote:
>>
>> 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
>> :
>
> Just use the sort() method of the list object.  In particular, items()
> returns an unordered list, so it's ready to be sorted.
>
>            for residues, numbers in new_dictionary.items().sort():
>
> That will sort such that residues are in sorted order.

Thanks, but still something went wrong here,

Traceback (most recent call last):
  File "fill-gap.py", line 41, in <module>
    build_abetadictionary(DICTIONARYFILE,orig_dictionary)
  File "fill-gap.py", line 31, in build_abetadictionary
    for residues, numbers in new_dictionary.items().sort():
AttributeError: 'dict_items' object has no attribute 'sort'

I have another concerns,
is it possible to append the output file content as a sing one,
such as a.new is
A 1
B 3

b.new is
A 3
B 5

I wish the final one like:

A 1 3
B 3 5

I will think about it. Thanks,
> --
>
> DaveA
>
>


More information about the Tutor mailing list