1 '''This module collects some frequently used helper functions 3 from __future__
import print_function
4 from builtins
import range
5 import time,ast,re,json,coral,array
7 '''Given nested lists or tuples, returns a single flattened list''' 10 if hasattr (piece,
'__iter__')
and not isinstance (piece, str):
11 result.extend( flatten (piece) )
18 input string of the ("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$" format 19 output (runnum,fillnum,timeStr) 21 if not iTime:
return (
None,
None,
None)
22 p=re.compile(
'^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
24 return (
None,
None,iTime)
25 p=re.compile(
'^\d{6}$')
27 return (
int(iTime),
None,
None)
28 p=re.compile(
'^\d{4}$')
30 return (
None,
int(iTime),
None)
34 input : largest lumivalue 35 output: (unitstring,denomitor) 39 if t>=1.0e3
and t<1.0e06:
42 elif t>=1.0e6
and t<1.0e9:
45 elif t>=1.0e9
and t<1.0e12:
48 elif t>=1.0e12
and t<1.0e15:
51 elif t<1.0
and t>=1.0e-3:
54 elif t<1.0e-03
and t>=1.0e-06:
57 elif t<1.0e-06
and t>=1.0e-09:
60 return (unitstring,denomitor)
66 printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb] 68 if inverseubval>=1.0e-09
and inverseubval<1.0e-06:
71 return (
float(inverseubval)/
float(denomitor),unitstring)
72 if inverseubval>=1.0e-06
and inverseubval<1.0e-03:
75 return (
float(inverseubval)/
float(denomitor),unitstring)
76 if inverseubval>=1.0e-03
and inverseubval<1.0:
79 return (
float(inverseubval)/
float(denomitor),unitstring)
80 if inverseubval>=1.0
and inverseubval<1.0e3:
82 return (inverseubval,unitstring)
83 if inverseubval>=1.0e3
and inverseubval<1.0e06:
86 return (
float(inverseubval)/
float(denomitor),unitstring)
87 if inverseubval>=1.0e6
and inverseubval<1.0e9:
90 return (
float(inverseubval)/
float(denomitor),unitstring)
91 if inverseubval>=1.0e9
and inverseubval<1.0e12:
94 return (
float(inverseubval)/
float(denomitor),unitstring)
95 if inverseubval>=1.0e12
and inverseubval<1.0e15:
98 return (
float(inverseubval)/
float(denomitor),unitstring)
99 return (
float(inverseubval),
'/ub')
102 yield item i and item i+1 in lst. e.g. 103 (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None) 105 from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration 107 if not len(lst):
return 109 for i
in range(len(lst)-1):
110 yield lst[i], lst[i+1]
114 check if an element is in the list 118 pos=mylist.index(element)
123 """test if a string can be converted to a int 132 test if a string can be converted to a float 141 report the number of duplicates in a python list 143 from collections
import defaultdict
144 tally=defaultdict(int)
150 transposing list of lists 151 from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/ 153 if not lists:
return []
155 return map(
lambda *row: [elem
for elem
in row
or defaultval], *lists)
157 """pack high,low 32bit unsigned int to one unsigned 64bit long long 158 Note:the print value of result number may appear signed, if the sign bit is used. 163 """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format 164 Note:the print value of result number may appear signed, if the sign bit is used. 167 return fmt%
pack(high,low)
169 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low) 175 """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low) 179 """convert 64bit timestamp to local date in string format 181 return time.ctime(
unpack(i)[0])
183 """convert 64bit timestamp to Universal Time in string format 186 return time.strftime(
"%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
188 """unpack 64bit lumiid to dictionary {'run','lumisection'} 191 return {
'run':j[0],
'lumisection':j[1]}
193 """return range including the stop value 204 convert json like string to legal json string 205 add double quote around json keys if they are not there, change single quote to double quote around keys 207 strresult=inputstring.strip()
208 strresult=re.sub(
"\s+",
"",strresult)
210 mydict=ast.literal_eval(strresult)
212 print(
'error in converting string to dict')
215 for k,v
in mydict.items():
216 if not isinstance(k,str):
220 return re.sub(
"'",
'"',
str(result))
225 inputarray: a python array 228 result.write(iarray.tostring())
235 itemtypecode: python array type code 237 if itemtypecode
not in [
'c',
'b',
'B',
'u','h','H','i','I','l','L','f','d']:
238 raise RuntimeError(
'unsupported typecode '+itemtypecode)
239 result=array.array(itemtypecode)
240 blobstr=iblob.readline()
243 result.fromstring(blobstr)
248 pack list of string of comma separated large string CLOB 250 return separator.join(iListstr)
254 unpack a large string to list of string 256 return [i.strip()
for i
in iStr.strip().
split(separator)]
262 result.append ([inPut[0]])
266 result[counter].append (i)
271 return ', '.join ([
'['+str (min (x))+
'-'+str (max (x))+
']' for x
in result])
275 output: (functionname,parametersinuppercase[]) 277 cleancorrectorStr=correctorStr.replace(
' ',
'')
278 [correctorFunc,paramsStr]=cleancorrectorStr.split(
':')
279 params=paramsStr.split(
',')
280 return (correctorFunc,params)
282 if __name__==
'__main__':
283 nested=[[[1,2],[6,6,8]],[[3,4,5],[4,5]]]
289 lst = [
'I1',
'I2',
'I1',
'I3',
'I4',
'I4',
'I7',
'I7',
'I7',
'I7',
'I7']
291 seqbag=[[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]
292 print(
'before ',seqbag)
298 pp=json.loads(result)
302 pp=json.loads(result)
306 pp=json.loads(result)
320 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)