CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CommonUtil.py
Go to the documentation of this file.
1 '''This module collects some frequently used helper functions
2 '''
3 import time,ast,re,json,coral,array
4 def flatten(obj):
5  '''Given nested lists or tuples, returns a single flattened list'''
6  result = []
7  for piece in obj:
8  if hasattr (piece, '__iter__') and not isinstance (piece, basestring):
9  result.extend( flatten (piece) )
10  else:
11  result.append (piece)
12  return result
13 
14 def parseTime(iTime):
15  '''
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)
18  '''
19  if not iTime: return (None,None,None)
20  p=re.compile('^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
21  if re.match(p,iTime):
22  return (None,None,iTime)
23  p=re.compile('^\d{6}$')
24  if re.match(p,iTime):
25  return (int(iTime),None,None)
26  p=re.compile('^\d{4}$')
27  if re.match(p,iTime):
28  return (None,int(iTime),None)
29 
31  '''
32  input : largest lumivalue
33  output: (unitstring,denomitor)
34  '''
35  unitstring='/ub'
36  denomitor=1.0
37  if t>=1.0e3 and t<1.0e06:
38  denomitor=1.0e3
39  unitstring='/nb'
40  elif t>=1.0e6 and t<1.0e9:
41  denomitor=1.0e6
42  unitstring='/pb'
43  elif t>=1.0e9 and t<1.0e12:
44  denomitor=1.0e9
45  unitstring='/fb'
46  elif t>=1.0e12 and t<1.0e15:
47  denomitor=1.0e12
48  unitstring='/ab'
49  elif t<1.0 and t>=1.0e-3: #left direction
50  denomitor=1.0e-03
51  unitstring='/mb'
52  elif t<1.0e-03 and t>=1.0e-06:
53  denomitor=1.0e-06
54  unitstring='/b'
55  elif t<1.0e-06 and t>=1.0e-09:
56  denomitor=1.0e-9
57  unitstring='/kb'
58  return (unitstring,denomitor)
59 def guessUnit(inverseubval):
60  '''
61  input:
62  float value in 1/ub
63  output:
64  printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb]
65  '''
66  if inverseubval>=1.0e-09 and inverseubval<1.0e-06:
67  denomitor=1.0e-09
68  unitstring='/kb'
69  return (float(inverseubval)/float(denomitor),unitstring)
70  if inverseubval>=1.0e-06 and inverseubval<1.0e-03:
71  denomitor=1.0e-06
72  unitstring='/b'
73  return (float(inverseubval)/float(denomitor),unitstring)
74  if inverseubval>=1.0e-03 and inverseubval<1.0:
75  denomitor=1.0e-03
76  unitstring='/mb'
77  return (float(inverseubval)/float(denomitor),unitstring)
78  if inverseubval>=1.0 and inverseubval<1.0e3:
79  unitstring='/ub'
80  return (inverseubval,unitstring)
81  if inverseubval>=1.0e3 and inverseubval<1.0e06:
82  denomitor=1.0e3
83  unitstring='/nb'
84  return (float(inverseubval)/float(denomitor),unitstring)
85  if inverseubval>=1.0e6 and inverseubval<1.0e9:
86  denomitor=1.0e6
87  unitstring='/pb'
88  return (float(inverseubval)/float(denomitor),unitstring)
89  if inverseubval>=1.0e9 and inverseubval<1.0e12:
90  denomitor=1.0e9
91  unitstring='/fb'
92  return (float(inverseubval)/float(denomitor),unitstring)
93  if inverseubval>=1.0e12 and inverseubval<1.0e15:
94  denomitor=1.0e12
95  unitstring='/ab'
96  return (float(inverseubval)/float(denomitor),unitstring)
97  return (float(inverseubval),'/ub')
98 def pairwise(lst):
99  """
100  yield item i and item i+1 in lst. e.g.
101  (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
102 
103  from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
104  """
105  if not len(lst): return
106  #yield None, lst[0]
107  for i in range(len(lst)-1):
108  yield lst[i], lst[i+1]
109  yield lst[-1], None
110 def findInList(mylist,element):
111  """
112  check if an element is in the list
113  """
114  pos=-1
115  try:
116  pos=mylist.index(element)
117  except ValueError:
118  pos=-1
119  return pos!=-1
120 def is_intstr(s):
121  """test if a string can be converted to a int
122  """
123  try:
124  int(s)
125  return True
126  except ValueError:
127  return False
128 def is_floatstr(s):
129  """
130  test if a string can be converted to a float
131  """
132  try:
133  float(s)
134  return True
135  except ValueError:
136  return False
137 def count_dups(l):
138  """
139  report the number of duplicates in a python list
140  """
141  from collections import defaultdict
142  tally=defaultdict(int)
143  for x in l:
144  tally[x]+=1
145  return tally.items()
146 def transposed(lists, defaultval=None):
147  """
148  transposing list of lists
149  from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
150  """
151  if not lists: return []
152  #return map(lambda *row: [elem or defaultval for elem in row], *lists)
153  return map(lambda *row: [elem for elem in row or defaultval], *lists)
154 def pack(high,low):
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.
157  """
158  h=high<<32
159  return (h|low)
160 def packToString(high,low):
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.
163  """
164  fmt="%u"
165  return fmt%pack(high,low)
166 def unpack(i):
167  """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
168  """
169  high=i>>32
170  low=i&0xFFFFFFFF
171  return(high,low)
173  """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low)
174  """
175  return unpack(int(i))
177  """convert 64bit timestamp to local date in string format
178  """
179  return time.ctime(unpack(i)[0])
181  """convert 64bit timestamp to Universal Time in string format
182  """
183  t=unpack(i)[0]
184  return time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
186  """unpack 64bit lumiid to dictionary {'run','lumisection'}
187  """
188  j=unpack(i)
189  return {'run':j[0],'lumisection':j[1]}
190 def inclusiveRange(start,stop,step):
191  """return range including the stop value
192  """
193  v=start
194  while v<stop:
195  yield v
196  v+=step
197  if v>=stop:
198  yield stop
199 
200 def tolegalJSON(inputstring):
201  '''
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
204  '''
205  strresult=inputstring.strip()
206  strresult=re.sub("\s+","",strresult)
207  try:
208  mydict=ast.literal_eval(strresult)
209  except SyntaxError:
210  print 'error in converting string to dict'
211  raise
212  result={}
213  for k,v in mydict.items():
214  if not isinstance(k,str):
215  result[str(k)]=v
216  else:
217  result[k]=v
218  return re.sub("'",'"',str(result))
219 
220 def packArraytoBlob(iarray):
221  '''
222  Inputs:
223  inputarray: a python array
224  '''
225  result=coral.Blob()
226  result.write(iarray.tostring())
227  return result
228 
229 def unpackBlobtoArray(iblob,itemtypecode):
230  '''
231  Inputs:
232  iblob: coral.Blob
233  itemtypecode: python array type code
234  '''
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()
239  if not blobstr :
240  return None
241  result.fromstring(blobstr)
242  return result
243 
244 def packListstrtoCLOB(iListstr,separator=','):
245  '''
246  pack list of string of comma separated large string CLOB
247  '''
248  return separator.join(iListstr)
249 
250 def unpackCLOBtoListstr(iStr,separator=','):
251  '''
252  unpack a large string to list of string
253  '''
254  return [i.strip() for i in iStr.strip().split(separator)]
255 
257  result = []
258  first = inPut[0]
259  last = inPut[0]
260  result.append ([inPut[0]])
261  counter = 0
262  for i in inPut[1:]:
263  if i == last+1:
264  result[counter].append (i)
265  else:
266  counter += 1
267  result.append ([i])
268  last = i
269  return ', '.join (['['+str (min (x))+'-'+str (max (x))+']' for x in result])
270 
271 def parselumicorrector(correctorStr):
272  '''
273  output: (functionname,parametersinuppercase[])
274  '''
275  cleancorrectorStr=correctorStr.replace(' ','')#in case of whitespace by mistake
276  [correctorFunc,paramsStr]=cleancorrectorStr.split(':')
277  params=paramsStr.split(',')
278  return (correctorFunc,params)
279 
280 if __name__=='__main__':
281  nested=[[[1,2],[6,6,8]],[[3,4,5],[4,5]]]
282  print 'flattened ',flatten(nested)
283  a=[1,2,3,4,5]
284  for i,j in pairwise(a):
285  if j :
286  print i,j
287  lst = ['I1','I2','I1','I3','I4','I4','I7','I7','I7','I7','I7']
288  print count_dups(lst)
289  seqbag=[[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]
290  print 'before ',seqbag
291  print 'after ',transposed(seqbag,None)
292  print [i for i in inclusiveRange(1,3,1)]
293 
294  result=tolegalJSON('{1:[],2:[[1,3],[4,5]]}')
295  print result
296  pp=json.loads(result)
297  print pp["2"]
298  result=tolegalJSON("{'1':[],'2':[[1,3],[4,5]]}")
299  print result
300  pp=json.loads(result)
301  print pp["2"]
302  result=tolegalJSON('{"1":[],"2":[[1,3],[4,5]]}')
303  print result
304  pp=json.loads(result)
305  print pp["2"]
306 
307  a=array.array('f')
308  a.append(1.3)
309  a.append(1.4)
310  a.append(2.3)
311  a.append(6.3)
312  myblob=packArraytoBlob(a)
313  print myblob.size()
314  print unpackBlobtoArray(myblob,'f')
315  b=array.array('f')
316  myblob=packArraytoBlob(b)
317  print myblob.size()
318  a=['aa_f', 'bb', 'dfc']
319  print packListstrtoCLOB(a)
def unpackCLOBtoListstr
Definition: CommonUtil.py:250
def unpackFromString
Definition: CommonUtil.py:172
def timeStamptoDate
Definition: CommonUtil.py:176
def packListstrtoCLOB
Definition: CommonUtil.py:244
def timeStamptoUTC
Definition: CommonUtil.py:180
def tolegalJSON
Definition: CommonUtil.py:200
def parselumicorrector
Definition: CommonUtil.py:271
def splitlistToRangeString
Definition: CommonUtil.py:256
def transposed
Definition: CommonUtil.py:146
def packToString
Definition: CommonUtil.py:160
def unpackBlobtoArray
Definition: CommonUtil.py:229
def findInList
Definition: CommonUtil.py:110
def packArraytoBlob
Definition: CommonUtil.py:220
def inclusiveRange
Definition: CommonUtil.py:190
def is_floatstr
Definition: CommonUtil.py:128
def count_dups
Definition: CommonUtil.py:137
def parseTime
Definition: CommonUtil.py:14
def lumiUnitForPrint
Definition: CommonUtil.py:30
def is_intstr
Definition: CommonUtil.py:120
def pairwise
Definition: CommonUtil.py:98
def unpackLumiid
Definition: CommonUtil.py:185
def guessUnit
Definition: CommonUtil.py:59
double split
Definition: MVATrainer.cc:139
def flatten
Definition: CommonUtil.py:4