Package obitools :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module obitools.utils

  1  import sys 
  2  import sys 
  3  import gzip 
  4  import time 
  5  import getopt 
  6   
7 -def universalOpen(file):
8 if isinstance(file,str): 9 if file[-3:] == '.gz': 10 rep = gzip.open(file) 11 else: 12 rep = open(file) 13 else: 14 rep = file 15 return rep
16
17 -def universalTell(file):
18 if isinstance(file, gzip.GzipFile): 19 file=file.myfileobj 20 return file.tell()
21
22 -def fileSize(file):
23 if isinstance(file, gzip.GzipFile): 24 file=file.myfileobj 25 pos = file.tell() 26 file.seek(0,2) 27 length = file.tell() 28 file.seek(pos,0) 29 return length
30
31 -def progressBar(pos,max,reset=False,delta=[]):
32 if reset: 33 del delta[:] 34 if not delta: 35 delta.append(time.time()) 36 delta.append(time.time()) 37 38 delta[1]=time.time() 39 elapsed = delta[1]-delta[0] 40 percent = float(pos)/max * 100 41 remain = time.strftime('%H:%M:%S',time.gmtime(elapsed / percent * (100-percent))) 42 bar = '#' * int(percent/2) 43 bar+= '|/-\\-'[pos % 5] 44 bar+= ' ' * (50 - int(percent/2)) 45 sys.stderr.write('\r%5.1f %% |%s] remain : %s' %(percent,bar,remain))
46
47 -def endLessIterator(endedlist):
48 for x in endedlist: 49 yield x 50 while(1): 51 yield endedlist[-1]
52 53
54 -class ColumnFile(object):
55
56 - def __init__(self,stream,sep=None,strip=True,types=None):
57 if isinstance(stream,str): 58 self._stream = open(stream) 59 elif hasattr(stream,'next'): 60 self._stream = stream 61 else: 62 raise ValueError,'stream must be string or an iterator' 63 self._delimiter=sep 64 self._strip=strip 65 if types: 66 self._types=[x for x in types] 67 for i in xrange(len(self._types)): 68 if self._types[i] is bool: 69 self._types[i]=ColumnFile.str2bool 70 else: 71 self._types=None
72
73 - def str2bool(x):
74 return bool(eval(x.strip()[0].upper(),{'T':True,'V':True,'F':False}))
75 76 str2bool = staticmethod(str2bool) 77 78
79 - def __iter__(self):
80 return self
81
82 - def next(self):
83 ligne = self._stream.next() 84 data = ligne.split(self._delimiter) 85 if self._strip or self._types: 86 data = [x.strip() for x in data] 87 if self._types: 88 it = endLessIterator(self._types) 89 data = [x[1](x[0]) for x in ((y,it.next()) for y in data)] 90 return data
91 92
93 -def checkHelpOption(help):
94 ''' 95 Check if the help option (-h or --help) is set and print 96 print help message if true. 97 98 @param help: the help message 99 @type help: text 100 ''' 101 o,data = getopt.getopt(sys.argv[1:], 'h', ('help')) 102 for name,value in o: 103 if name in ('-h','--help'): 104 print help 105 sys.exit()
106