1 '''This module collects some frequently used helper functions 3 import time,ast,re,json,coral,array
5 '''Given nested lists or tuples, returns a single flattened list''' 8 if hasattr (piece,
'__iter__')
and not isinstance (piece, basestring):
9 result.extend( flatten (piece) )
16 input string of the ("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$" format 17 output (runnum,fillnum,timeStr) 19 if not iTime:
return (
None,
None,
None)
20 p=re.compile(
'^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
22 return (
None,
None,iTime)
23 p=re.compile(
'^\d{6}$')
25 return (
int(iTime),
None,
None)
26 p=re.compile(
'^\d{4}$')
28 return (
None,
int(iTime),
None)
32 input : largest lumivalue 33 output: (unitstring,denomitor) 37 if t>=1.0e3
and t<1.0e06:
40 elif t>=1.0e6
and t<1.0e9:
43 elif t>=1.0e9
and t<1.0e12:
46 elif t>=1.0e12
and t<1.0e15:
49 elif t<1.0
and t>=1.0e-3:
52 elif t<1.0e-03
and t>=1.0e-06:
55 elif t<1.0e-06
and t>=1.0e-09:
58 return (unitstring,denomitor)
64 printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb] 66 if inverseubval>=1.0e-09
and inverseubval<1.0e-06:
69 return (
float(inverseubval)/
float(denomitor),unitstring)
70 if inverseubval>=1.0e-06
and inverseubval<1.0e-03:
73 return (
float(inverseubval)/
float(denomitor),unitstring)
74 if inverseubval>=1.0e-03
and inverseubval<1.0:
77 return (
float(inverseubval)/
float(denomitor),unitstring)
78 if inverseubval>=1.0
and inverseubval<1.0e3:
80 return (inverseubval,unitstring)
81 if inverseubval>=1.0e3
and inverseubval<1.0e06:
84 return (
float(inverseubval)/
float(denomitor),unitstring)
85 if inverseubval>=1.0e6
and inverseubval<1.0e9:
88 return (
float(inverseubval)/
float(denomitor),unitstring)
89 if inverseubval>=1.0e9
and inverseubval<1.0e12:
92 return (
float(inverseubval)/
float(denomitor),unitstring)
93 if inverseubval>=1.0e12
and inverseubval<1.0e15:
96 return (
float(inverseubval)/
float(denomitor),unitstring)
97 return (
float(inverseubval),
'/ub')
100 yield item i and item i+1 in lst. e.g. 101 (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None) 103 from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration 105 if not len(lst):
return 107 for i
in range(len(lst)-1):
108 yield lst[i], lst[i+1]
112 check if an element is in the list 116 pos=mylist.index(element)
121 """test if a string can be converted to a int 130 test if a string can be converted to a float 139 report the number of duplicates in a python list 141 from collections
import defaultdict
142 tally=defaultdict(int)
148 transposing list of lists 149 from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/ 151 if not lists:
return []
153 return map(
lambda *row: [elem
for elem
in row
or defaultval], *lists)
155 """pack high,low 32bit unsigned int to one unsigned 64bit long long 156 Note:the print value of result number may appear signed, if the sign bit is used. 161 """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format 162 Note:the print value of result number may appear signed, if the sign bit is used. 165 return fmt%
pack(high,low)
167 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low) 173 """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low) 177 """convert 64bit timestamp to local date in string format 179 return time.ctime(
unpack(i)[0])
181 """convert 64bit timestamp to Universal Time in string format 184 return time.strftime(
"%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
186 """unpack 64bit lumiid to dictionary {'run','lumisection'} 189 return {
'run':j[0],
'lumisection':j[1]}
191 """return range including the stop value 202 convert json like string to legal json string 203 add double quote around json keys if they are not there, change single quote to double quote around keys 205 strresult=inputstring.strip()
206 strresult=re.sub(
"\s+",
"",strresult)
208 mydict=ast.literal_eval(strresult)
210 print 'error in converting string to dict' 213 for k,v
in mydict.items():
214 if not isinstance(k,str):
218 return re.sub(
"'",
'"',
str(result))
223 inputarray: a python array 226 result.write(iarray.tostring())
233 itemtypecode: python array type code 235 if itemtypecode
not in [
'c',
'b',
'B',
'u','h','H','i','I','l','L','f','d']:
236 raise RuntimeError(
'unsupported typecode '+itemtypecode)
237 result=array.array(itemtypecode)
238 blobstr=iblob.readline()
241 result.fromstring(blobstr)
246 pack list of string of comma separated large string CLOB 248 return separator.join(iListstr)
252 unpack a large string to list of string 254 return [i.strip()
for i
in iStr.strip().
split(separator)]
260 result.append ([inPut[0]])
264 result[counter].append (i)
269 return ', '.join ([
'['+str (min (x))+
'-'+str (max (x))+
']' for x
in result])
273 output: (functionname,parametersinuppercase[]) 275 cleancorrectorStr=correctorStr.replace(
' ',
'')
276 [correctorFunc,paramsStr]=cleancorrectorStr.split(
':')
277 params=paramsStr.split(
',')
278 return (correctorFunc,params)
280 if __name__==
'__main__':
281 nested=[[[1,2],[6,6,8]],[[3,4,5],[4,5]]]
282 print 'flattened ',
flatten(nested)
287 lst = [
'I1',
'I2',
'I1',
'I3',
'I4',
'I4',
'I7',
'I7',
'I7',
'I7',
'I7']
289 seqbag=[[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]
290 print 'before ',seqbag
296 pp=json.loads(result)
300 pp=json.loads(result)
304 pp=json.loads(result)
318 a=[
'aa_f',
'bb',
'dfc']
def tolegalJSON(inputstring)
def unpackBlobtoArray(iblob, itemtypecode)
def findInList(mylist, element)
def packListstrtoCLOB(iListstr, separator=')
def guessUnit(inverseubval)
def splitlistToRangeString(inPut)
def unpackCLOBtoListstr(iStr, separator=')
def transposed(lists, defaultval=None)
def packArraytoBlob(iarray)
return(e1-e2)*(e1-e2)+dp *dp
def packToString(high, low)
def parselumicorrector(correctorStr)
def inclusiveRange(start, stop, step)