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