1 '''This module collects some frequently used helper functions
3 import time,ast,re,json,coral,array
6 yield item i and item i+1 in lst. e.g.
7 (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
9 from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
11 if not len(lst):
return
13 for i
in range(len(lst)-1):
14 yield lst[i], lst[i+1]
18 check if an element is in the list
22 pos=mylist.index(element)
27 """test if a string can be converted to a int
36 test if a string can be converted to a float
45 report the number of duplicates in a python list
47 from collections
import defaultdict
48 tally=defaultdict(int)
54 transposing list of lists
55 from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
57 if not lists:
return []
58 return map(
lambda *row: [elem
or defaultval
for elem
in row], *lists)
60 """pack high,low 32bit unsigned int to one unsigned 64bit long long
61 Note:the print value of result number may appear signed, if the sign bit is used.
66 """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
67 Note:the print value of result number may appear signed, if the sign bit is used.
70 return fmt%
pack(high,low)
72 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
78 """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low)
82 """convert 64bit timestamp to local date in string format
84 return time.ctime(
unpack(i)[0])
86 """convert 64bit timestamp to Universal Time in string format
89 return time.strftime(
"%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
91 """unpack 64bit lumiid to dictionary {'run','lumisection'}
94 return {
'run':j[0],
'lumisection':j[1]}
96 """return range including the stop value
107 convert json like string to legal json string
108 add double quote around json keys if they are not there, change single quote to double quote around keys
110 strresult=inputstring.strip()
111 strresult=re.sub(
"\s+",
"",strresult)
113 mydict=ast.literal_eval(strresult)
115 print 'error in converting string to dict'
118 for k,v
in mydict.items():
119 if not isinstance(k,str):
123 return re.sub(
"'",
'"',str(result))
128 inputarray: a python array
131 result.write(iarray.tostring())
138 itemtypecode: python array type code
140 if itemtypecode
not in [
'c',
'b',
'B',
'u','h','H','i','I','l','L','f','d']:
141 raise RuntimeError(
'unsupported typecode '+itemtypecode)
142 result=array.array(itemtypecode)
143 result.fromstring(iblob.readline())
148 pack list of string of comma separated large string CLOB
150 return separator.join(iListstr)
154 unpack a large string to list of string
156 return [i.strip()
for i
in iStr.strip().
split(separator)]
158 if __name__==
'__main__':
163 lst = [
'I1',
'I2',
'I1',
'I3',
'I4',
'I4',
'I7',
'I7',
'I7',
'I7',
'I7']
165 seqbag=[[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]
166 print 'before ',seqbag
172 pp=json.loads(result)
176 pp=json.loads(result)
180 pp=json.loads(result)
194 a=[
'aa_f',
'bb',
'dfc']