1 '''This module collects some frequently used helper functions 3 from __future__
import print_function
4 import time,ast,re,json,coral,array
6 '''Given nested lists or tuples, returns a single flattened list''' 9 if hasattr (piece,
'__iter__')
and not isinstance (piece, str):
10 result.extend( flatten (piece) )
17 input string of the ("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$" format 18 output (runnum,fillnum,timeStr) 20 if not iTime:
return (
None,
None,
None)
21 p=re.compile(
'^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
23 return (
None,
None,iTime)
24 p=re.compile(
'^\d{6}$')
26 return (
int(iTime),
None,
None)
27 p=re.compile(
'^\d{4}$')
29 return (
None,
int(iTime),
None)
33 input : largest lumivalue 34 output: (unitstring,denomitor) 38 if t>=1.0e3
and t<1.0e06:
41 elif t>=1.0e6
and t<1.0e9:
44 elif t>=1.0e9
and t<1.0e12:
47 elif t>=1.0e12
and t<1.0e15:
50 elif t<1.0
and t>=1.0e-3:
53 elif t<1.0e-03
and t>=1.0e-06:
56 elif t<1.0e-06
and t>=1.0e-09:
59 return (unitstring,denomitor)
65 printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb] 67 if inverseubval>=1.0e-09
and inverseubval<1.0e-06:
70 return (
float(inverseubval)/
float(denomitor),unitstring)
71 if inverseubval>=1.0e-06
and inverseubval<1.0e-03:
74 return (
float(inverseubval)/
float(denomitor),unitstring)
75 if inverseubval>=1.0e-03
and inverseubval<1.0:
78 return (
float(inverseubval)/
float(denomitor),unitstring)
79 if inverseubval>=1.0
and inverseubval<1.0e3:
81 return (inverseubval,unitstring)
82 if inverseubval>=1.0e3
and inverseubval<1.0e06:
85 return (
float(inverseubval)/
float(denomitor),unitstring)
86 if inverseubval>=1.0e6
and inverseubval<1.0e9:
89 return (
float(inverseubval)/
float(denomitor),unitstring)
90 if inverseubval>=1.0e9
and inverseubval<1.0e12:
93 return (
float(inverseubval)/
float(denomitor),unitstring)
94 if inverseubval>=1.0e12
and inverseubval<1.0e15:
97 return (
float(inverseubval)/
float(denomitor),unitstring)
98 return (
float(inverseubval),
'/ub')
101 yield item i and item i+1 in lst. e.g. 102 (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None) 104 from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration 106 if not len(lst):
return 108 for i
in range(len(lst)-1):
109 yield lst[i], lst[i+1]
113 check if an element is in the list 117 pos=mylist.index(element)
122 """test if a string can be converted to a int 131 test if a string can be converted to a float 140 report the number of duplicates in a python list 142 from collections
import defaultdict
143 tally=defaultdict(int)
149 transposing list of lists 150 from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/ 152 if not lists:
return []
154 return map(
lambda *row: [elem
for elem
in row
or defaultval], *lists)
156 """pack high,low 32bit unsigned int to one unsigned 64bit long long 157 Note:the print value of result number may appear signed, if the sign bit is used. 162 """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format 163 Note:the print value of result number may appear signed, if the sign bit is used. 166 return fmt%
pack(high,low)
168 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low) 174 """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low) 178 """convert 64bit timestamp to local date in string format 180 return time.ctime(
unpack(i)[0])
182 """convert 64bit timestamp to Universal Time in string format 185 return time.strftime(
"%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
187 """unpack 64bit lumiid to dictionary {'run','lumisection'} 190 return {
'run':j[0],
'lumisection':j[1]}
192 """return range including the stop value 203 convert json like string to legal json string 204 add double quote around json keys if they are not there, change single quote to double quote around keys 206 strresult=inputstring.strip()
207 strresult=re.sub(
"\s+",
"",strresult)
209 mydict=ast.literal_eval(strresult)
211 print(
'error in converting string to dict')
214 for k,v
in mydict.items():
215 if not isinstance(k,str):
219 return re.sub(
"'",
'"',
str(result))
224 inputarray: a python array 227 result.write(iarray.tostring())
234 itemtypecode: python array type code 236 if itemtypecode
not in [
'c',
'b',
'B',
'u','h','H','i','I','l','L','f','d']:
237 raise RuntimeError(
'unsupported typecode '+itemtypecode)
238 result=array.array(itemtypecode)
239 blobstr=iblob.readline()
242 result.fromstring(blobstr)
247 pack list of string of comma separated large string CLOB 249 return separator.join(iListstr)
253 unpack a large string to list of string 255 return [i.strip()
for i
in iStr.strip().
split(separator)]
261 result.append ([inPut[0]])
265 result[counter].append (i)
270 return ', '.join ([
'['+str (min (x))+
'-'+str (max (x))+
']' for x
in result])
274 output: (functionname,parametersinuppercase[]) 276 cleancorrectorStr=correctorStr.replace(
' ',
'')
277 [correctorFunc,paramsStr]=cleancorrectorStr.split(
':')
278 params=paramsStr.split(
',')
279 return (correctorFunc,params)
281 if __name__==
'__main__':
282 nested=[[[1,2],[6,6,8]],[[3,4,5],[4,5]]]
288 lst = [
'I1',
'I2',
'I1',
'I3',
'I4',
'I4',
'I7',
'I7',
'I7',
'I7',
'I7']
290 seqbag=[[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]
291 print(
'before ',seqbag)
297 pp=json.loads(result)
301 pp=json.loads(result)
305 pp=json.loads(result)
319 a=[
'aa_f',
'bb',
'dfc']
def tolegalJSON(inputstring)
def unpackBlobtoArray(iblob, itemtypecode)
def findInList(mylist, element)
def packListstrtoCLOB(iListstr, separator=')
def guessUnit(inverseubval)
S & print(S &os, JobReport::InputFile const &f)
def splitlistToRangeString(inPut)
def unpackCLOBtoListstr(iStr, separator=')
def transposed(lists, defaultval=None)
def packArraytoBlob(iarray)
def packToString(high, low)
def parselumicorrector(correctorStr)
def inclusiveRange(start, stop, step)