[BioPython] Some proposed code-cleanup

Andreas Kuntzagk andreas.kuntzagk at mdc-berlin.de
Thu Jun 19 11:29:22 EDT 2003


Hi there,

while surfing the source, I noticed the use of UserDict.UserDict.
The library-doc discourages the use of this and suggests to inherit from
the new dict class instead. Since biopython requires python 2.2 this
should work for everybody. 
So I went and replaced all occurences of UserDict, UserList and
UserString. All tests working. Should improve readability.
You find the diffs in diff.out
More improvement could be done by replacing 

a.member = dict()
 
whith 

a.member = {}

What do you think?

Also in GenBank.__init__.py I found the use of "my_dict.has_key(my_key)"
and "for my_key in my_dict.keys()".
This can be replaced by "my_key in my_dict" and "for my_key in My_dict"
GvR did this in Python source (cvs), so this must be a Good Thing(tm)
:-) 
I'm sure there are other places and I will find them, if people like
this change.

See GenBank.diff

Andreas
-------------- next part --------------
Index: Bio/Index.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/Index.py,v
retrieving revision 1.5
diff -r1.5 Index.py
22d21
< import UserDict
24c23
< class _ShelveIndex(UserDict.UserDict):
---
> class _ShelveIndex(dict):
37c36
<         UserDict.UserDict.__init__(self)
---
>         dict.__init__(self)
68c67
< class _InMemoryIndex(UserDict.UserDict):
---
> class _InMemoryIndex(dict):
82c81
<         UserDict.UserDict.__init__(self)
---
>         dict.__init__(self)
106c105
<         UserDict.UserDict.update(self, dict)
---
>         dict.update(self, dict)
109c108
<         UserDict.UserDict.__setitem__(self, key, value)
---
>         dict.__setitem__(self, key, value)
112c111
<         UserDict.UserDict.__delitem__(self, key)
---
>         dict.__delitem__(self, key)
115c114
<         UserDict.UserDict.clear(self)
---
>         dict.clear(self)
Index: Bio/PropertyManager.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/PropertyManager.py,v
retrieving revision 1.2
diff -r1.2 PropertyManager.py
14d13
< import UserDict
18c17
< class CreateDict(UserDict.UserDict):
---
> class CreateDict(dict):
Index: Bio/Seq.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/Seq.py,v
retrieving revision 1.6
diff -r1.6 Seq.py
1d0
< import UserList
Index: Bio/CDD/Record.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/CDD/Record.py,v
retrieving revision 1.1
diff -r1.1 Record.py
29,30d28
< from UserDict import UserDict
< 
39c37
< class Record( UserDict ):
---
> class Record( dict ):
59c57
<         UserDict.__init__( self )
---
>         dict.__init__( self )
Index: Bio/Crystal/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/Crystal/__init__.py,v
retrieving revision 1.4
diff -r1.4 __init__.py
13,14d12
< import UserList
< import UserDict
Index: Bio/EUtils/POM.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/EUtils/POM.py,v
retrieving revision 1.1
diff -r1.1 POM.py
13,14d12
< from UserList import UserList
< from UserString import UserString
38c36
< class IndentedText(UserString):
---
> class IndentedText(str):
863c861
< class AttributeList(UserList):
---
> class AttributeList(list):
Index: Bio/FSSP/FSSPTools.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/FSSP/FSSPTools.py,v
retrieving revision 1.4
diff -r1.4 FSSPTools.py
1d0
< import UserDict
14c13
< class FSSPMultAlign(UserDict.UserDict):
---
> class FSSPMultAlign(dict):
Index: Bio/FSSP/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/FSSP/__init__.py,v
retrieving revision 1.3
diff -r1.3 __init__.py
5d4
< import UserDict
153c152
< class FSSPAlignDict(UserDict.UserDict):
---
> class FSSPAlignDict(dict):
204c203
< class FSSPSumDict(UserDict.UserDict):
---
> class FSSPSumDict(dict):
Index: Bio/InterPro/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/InterPro/__init__.py,v
retrieving revision 1.4
diff -r1.4 __init__.py
35d34
< import UserDict
37,38c36
< 
< class Record( UserDict.UserDict ):
---
> class Record( dict ):
Index: Bio/LocusLink/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/LocusLink/__init__.py,v
retrieving revision 1.1
diff -r1.1 __init__.py
3d2
< import UserDict
21c20
< class Record( UserDict.UserDict):
---
> class Record( dict):
24c23
<         UserDict.UserDict.__init__( self )
---
>         dict.__init__( self )
48c47
<         elif( isinstance( item, UserDict.UserDict ) ):
---
>         elif( isinstance( item, dict ) ):
Index: Bio/LocusLink/web_parse.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/LocusLink/web_parse.py,v
retrieving revision 1.2
diff -r1.2 web_parse.py
6d5
< import UserDict
160c159
< class Record( UserDict.UserDict):
---
> class Record(dict):
163c162
<         UserDict.UserDict.__init__( self )
---
>         dict.__init__( self )
194c193
<         elif( isinstance( item, UserDict.UserDict ) ):
---
>         elif( isinstance( item, dict ) ):
Index: Bio/MetaTool/Record.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/MetaTool/Record.py,v
retrieving revision 1.3
diff -r1.3 Record.py
8d7
< import UserDict
13c12
< class Metabolite( UserDict.UserDict ):
---
> class Metabolite( dict ):
23c22
< class MetaboliteRole( UserDict.UserDict ):
---
> class MetaboliteRole( dict ):
Index: Bio/Ndb/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/Ndb/__init__.py,v
retrieving revision 1.2
diff -r1.2 __init__.py
40d39
< import UserDict
43c42
< class Record( UserDict.UserDict ):
---
> class Record( dict ):
75c74
<             elif( isinstance( val, UserDict.UserDict ) ):
---
>             elif( isinstance( val, dict ) ):
Index: Bio/Rebase/Rebase.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/Rebase/Rebase.py,v
retrieving revision 1.8
diff -r1.8 Rebase.py
35d34
< import UserDict
132c131
< class EnzymeDict( UserDict.UserDict ):
---
> class EnzymeDict( dict ):
144c143
<         elif( isinstance( item, UserDict.UserDict ) ):
---
>         elif( isinstance( item, dict ) ):
181c180
<         self.enzyme_dict[ 'number_of_sites' ] = UserDict.UserDict()
---
>         self.enzyme_dict[ 'number_of_sites' ] = dict()
Index: Bio/SCOP/FileIndex.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/SCOP/FileIndex.py,v
retrieving revision 1.1
diff -r1.1 FileIndex.py
6,7d5
< from UserDict import UserDict
< 
12c10
< class FileIndex(UserDict) :
---
> class FileIndex(dict) :
31c29
<         UserDict.__init__(self)
---
>         dict.__init__(self)
Index: Bio/SubsMat/FreqTable.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/SubsMat/FreqTable.py,v
retrieving revision 1.1
diff -r1.1 FreqTable.py
1d0
< import UserDict
51c50
< class FreqTable(UserDict.UserDict):
---
> class FreqTable(dict):
Index: Bio/SubsMat/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/SubsMat/__init__.py,v
retrieving revision 1.7
diff -r1.7 __init__.py
11d10
< import UserDict
133c132
< class SeqMat(UserDict.UserDict):
---
> class SeqMat(dict):
173c172
<       assert (type(data) == type({}) or isinstance(data,UserDict.UserDict) or
---
>       assert (type(data) == type({}) or isinstance(data,dict) or
Index: Bio/UniGene/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/UniGene/__init__.py,v
retrieving revision 1.5
diff -r1.5 __init__.py
5d4
< import UserDict
15c14
< class Record( UserDict.UserDict):
---
> class Record( dict):
18c17
<         UserDict.UserDict.__init__( self )
---
>         dict.__init__( self )
41c40
<         elif( isinstance( item, UserDict.UserDict ) ):
---
>         elif( isinstance( item, dict ) ):
150c149
<             self.queue[ key ] = UserDict.UserDict()
---
>             self.queue[ key ] = dict()
152c151
<             self.queue[ key ] = UserDict.UserDict()
---
>             self.queue[ key ] = dict()
220c219
<     print record
\ No newline at end of file
---
>     print record
Index: Martel/LAX.py
===================================================================
RCS file: /home/repository/biopython/biopython/Martel/LAX.py,v
retrieving revision 1.2
diff -r1.2 LAX.py
81c81
< import string, UserDict
---
> import string
90c90
< class LAX(handler.ContentHandler, UserDict.UserDict):
---
> class LAX(handler.ContentHandler, dict):
93c93
<         UserDict.UserDict.__init__(self)
---
>         dict.__init__(self)
-------------- next part --------------
Index: Bio/GenBank/__init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/GenBank/__init__.py,v
retrieving revision 1.39
diff -r1.39 __init__.py
103,112c103
<         all_keys = self._index.keys()
<         keys_to_remove = []
< 
<         for key in all_keys:
<             # if the key is internal (has a __) then add it to the remove list
<             if key[:2] == '__':
<                 keys_to_remove.append(key)
< 
<         for key in keys_to_remove:
<             all_keys.remove(key)
---
>         all_keys = [key for key in self._index.keys() if not key.startswith('__')]
880c871
<             if self._cur_feature.qualifiers.has_key(key):
---
>             if key in self._cur_feature.qualifiers:
1140c1131
<             if self._cur_qualifier.key.find(remove_space_key) >= 0:
---
>             if remove_space_key in self._cur_qualifier.key:
1306c1297
<             if search_db.mindy_data.has_key("file_sizes"):
---
>             if "file_sizes" in search_db.mindy_data:


More information about the BioPython mailing list