Package Bio :: Package GFF :: Module GenericTools
[hide private]
[frames] | no frames]

Source Code for Module Bio.GFF.GenericTools

  1  #!/usr/bin/env python 
  2  # 
  3  # Copyright 2002 by Michael Hoffman.  All rights reserved. 
  4  # This code is part of the Biopython distribution and governed by its 
  5  # license.  Please see the LICENSE file that should have been included 
  6  # as part of this package. 
  7   
  8  """ 
  9  A set of generic bits of code under Bio.GFF (possibly obsolete). 
 10  """ 
 11   
 12  __version__ = "$Revision: 1.6 $" 
 13  # $Source: /home/repository/biopython/biopython/Bio/GFF/GenericTools.py,v $ 
 14   
 15  import exceptions 
 16  import os 
 17  import sys 
 18  import tempfile 
 19   
20 -class AppendableListDictionary(dict):
21 """ 22 a dictionary of lists 23 """
24 - def append_to(self, key, value):
25 try: 26 dict.__getitem__(self, key).append(value) 27 except KeyError: 28 self[key] = [value]
29
30 -class ForgivingDictionary(AppendableListDictionary):
31 - def __getitem__(self, key):
32 try: 33 return dict.__getitem__(self, key) 34 except KeyError: 35 return None
36
37 -class VerboseDict(dict):
38 - def __str__(self):
39 dict_copy = {} 40 for key in self: 41 dict_copy[key] = str(self[key]) 42 return str(dict_copy)
43
44 -class VerboseList(list):
45 - def __str__(self):
46 return str(map(lambda x: str(x), self))
47
48 -class TempFile(file):
49 - def __init__(self, suffix = ".python-temp", keep = 0):
50 self.removed = 0 51 self.keep = keep 52 # XXX: this is a race condition: 53 file.__init__(self, tempfile.mktemp(suffix), "w")
54
55 - def __del__(self):
56 self.remove()
57
58 - def remove(self):
59 if self.keep == 0: 60 if self.removed == 0: 61 try: 62 try: 63 self.close() 64 os.remove(self.name) 65 finally: 66 self.removed = 1 67 except exceptions.OSError: 68 pass
69
70 -class SurrogateNotInitedError(exceptions.AttributeError):
71 pass
72
73 -class Surrogate(object):
74 """ 75 the data is stored in _data 76 """
77 - def __init__(self, data):
78 self._data = data
79
80 - def __getattr__(self, name):
81 if name == "_data": 82 raise SurrogateNotInitedError(name) 83 else: 84 try: 85 return getattr(self._data, name) 86 except SurrogateNotInitedError: 87 raise SurrogateNotInitedError(name)
88 89
90 -def defline_text(defline):
91 if defline[0] == ">": 92 return defline[1:] 93 else: 94 return defline
95
96 -def is_nestable(x):
97 """ 98 Returns 1 if x is a tuple or list (sequence types that can nest) 99 Returns 0 otherwise 100 101 >>> is_nestable("string") 102 0 103 >>> is_nestable((0,)) 104 1 105 >>> is_nestable(range(5)) 106 1 107 """ 108 return isinstance(x, (tuple, list))
109
110 -def dump_list(l):
111 """ 112 returns strings of list 113 """ 114 try: 115 return '[%s]' % ', '.join(map(str, l)) 116 except TypeError: 117 return str(l)
118
119 -def reverse_text(text):
120 """ 121 >>> reverse_text('abracadabra') 122 'arbadacarba' 123 """ 124 l = list(text) 125 l.reverse() 126 return ''.join(l)
127
128 -class ArgsParser(object):
129 """ 130 >>> unparsed_args = ["moocow"] 131 >>> args = ArgsParser(unparsed_args, [('infile', 'defaultin'), ('outfile', 'defaultout')]) 132 >>> args.infile 133 'moocow' 134 >>> args.outfile 135 'defaultout' 136 """
137 - def __init__(self, args, defaults):
138 for i, default in enumerate(defaults): 139 try: 140 self.__dict__[default[0]] = args[i] 141 continue 142 except TypeError: 143 pass 144 except IndexError: 145 pass 146 self.__dict__[default[0]] = default[1]
147
148 -def all(iterator):
149 return [item for item in iterator]
150
151 -def _test(*args, **keywds):
152 import doctest, sys 153 doctest.testmod(sys.modules[__name__], *args, **keywds)
154 155 if __name__ == "__main__": 156 if __debug__: 157 _test() 158