CMS 3D CMS Logo

Functions | Variables
CommonUtil Namespace Reference

Functions

def count_dups (l)
 
def findInList (mylist, element)
 
def flatten (obj)
 
def guessUnit (inverseubval)
 
def inclusiveRange (start, stop, step)
 
def is_floatstr (s)
 
def is_intstr (s)
 
def lumiUnitForPrint (t)
 
def pack (high, low)
 
def packArraytoBlob (iarray)
 
def packListstrtoCLOB (iListstr, separator=')
 
def packToString (high, low)
 
def pairwise (lst)
 
def parselumicorrector (correctorStr)
 
def parseTime (iTime)
 
def splitlistToRangeString (inPut)
 
def timeStamptoDate (i)
 
def timeStamptoUTC (i)
 
def tolegalJSON (inputstring)
 
def transposed (lists, defaultval=None)
 
def unpack (i)
 
def unpackBlobtoArray (iblob, itemtypecode)
 
def unpackCLOBtoListstr (iStr, separator=')
 
def unpackFromString (i)
 
def unpackLumiid (i)
 

Variables

 a
 
 b
 
 lst
 
 myblob
 
 nested
 
 pp
 
 result
 
 seqbag
 

Detailed Description

This module collects some frequently used helper functions

Function Documentation

def CommonUtil.count_dups (   l)
report the number of duplicates in a python list

Definition at line 139 of file CommonUtil.py.

Referenced by lumiQueryAPI.allruns().

139 def count_dups(l):
140  """
141  report the number of duplicates in a python list
142  """
143  from collections import defaultdict
144  tally=defaultdict(int)
145  for x in l:
146  tally[x]+=1
147  return tally.items()
def count_dups(l)
Definition: CommonUtil.py:139
def CommonUtil.findInList (   mylist,
  element 
)
check if an element is in the list

Definition at line 112 of file CommonUtil.py.

112 def findInList(mylist,element):
113  """
114  check if an element is in the list
115  """
116  pos=-1
117  try:
118  pos=mylist.index(element)
119  except ValueError:
120  pos=-1
121  return pos!=-1
def findInList(mylist, element)
Definition: CommonUtil.py:112
def CommonUtil.flatten (   obj)
Given nested lists or tuples, returns a single flattened list

Definition at line 6 of file CommonUtil.py.

Referenced by lumiReport.toCSVLumiByLSXing().

6 def flatten(obj):
7  '''Given nested lists or tuples, returns a single flattened list'''
8  result = []
9  for piece in obj:
10  if hasattr (piece, '__iter__') and not isinstance (piece, str):
11  result.extend( flatten (piece) )
12  else:
13  result.append (piece)
14  return result
15 
def flatten(obj)
Definition: CommonUtil.py:6
def CommonUtil.guessUnit (   inverseubval)
input:
    float value in 1/ub
output:
    printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb]

Definition at line 61 of file CommonUtil.py.

References objects.autophobj.float.

Referenced by lumiReport.toScreenLumiByLS(), lumiReport.toScreenOverview(), lumiReport.toScreenTotDelivered(), and lumiReport.toScreenTotEffective().

61 def guessUnit(inverseubval):
62  '''
63  input:
64  float value in 1/ub
65  output:
66  printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb]
67  '''
68  if inverseubval>=1.0e-09 and inverseubval<1.0e-06:
69  denomitor=1.0e-09
70  unitstring='/kb'
71  return (float(inverseubval)/float(denomitor),unitstring)
72  if inverseubval>=1.0e-06 and inverseubval<1.0e-03:
73  denomitor=1.0e-06
74  unitstring='/b'
75  return (float(inverseubval)/float(denomitor),unitstring)
76  if inverseubval>=1.0e-03 and inverseubval<1.0:
77  denomitor=1.0e-03
78  unitstring='/mb'
79  return (float(inverseubval)/float(denomitor),unitstring)
80  if inverseubval>=1.0 and inverseubval<1.0e3:
81  unitstring='/ub'
82  return (inverseubval,unitstring)
83  if inverseubval>=1.0e3 and inverseubval<1.0e06:
84  denomitor=1.0e3
85  unitstring='/nb'
86  return (float(inverseubval)/float(denomitor),unitstring)
87  if inverseubval>=1.0e6 and inverseubval<1.0e9:
88  denomitor=1.0e6
89  unitstring='/pb'
90  return (float(inverseubval)/float(denomitor),unitstring)
91  if inverseubval>=1.0e9 and inverseubval<1.0e12:
92  denomitor=1.0e9
93  unitstring='/fb'
94  return (float(inverseubval)/float(denomitor),unitstring)
95  if inverseubval>=1.0e12 and inverseubval<1.0e15:
96  denomitor=1.0e12
97  unitstring='/ab'
98  return (float(inverseubval)/float(denomitor),unitstring)
99  return (float(inverseubval),'/ub')
def guessUnit(inverseubval)
Definition: CommonUtil.py:61
def CommonUtil.inclusiveRange (   start,
  stop,
  step 
)
return range including the stop value

Definition at line 192 of file CommonUtil.py.

192 def inclusiveRange(start,stop,step):
193  """return range including the stop value
194  """
195  v=start
196  while v<stop:
197  yield v
198  v+=step
199  if v>=stop:
200  yield stop
201 
def inclusiveRange(start, stop, step)
Definition: CommonUtil.py:192
def CommonUtil.is_floatstr (   s)
test if a string can be converted to a float

Definition at line 130 of file CommonUtil.py.

References objects.autophobj.float.

Referenced by inputFilesetParser.inputFilesetParser.fieldtotal(), and inputFilesetParser.inputFilesetParser.fieldvalues().

130 def is_floatstr(s):
131  """
132  test if a string can be converted to a float
133  """
134  try:
135  float(s)
136  return True
137  except ValueError:
138  return False
def is_floatstr(s)
Definition: CommonUtil.py:130
def CommonUtil.is_intstr (   s)
test if a string can be converted to a int

Definition at line 122 of file CommonUtil.py.

References createfilelist.int.

Referenced by inputFilesetParser.inputFilesetParser.fieldtotal(), inputFilesetParser.inputFilesetParser.fieldvalues(), and inputFilesetParser.inputFilesetParser.runsWithresult().

122 def is_intstr(s):
123  """test if a string can be converted to a int
124  """
125  try:
126  int(s)
127  return True
128  except ValueError:
129  return False
def is_intstr(s)
Definition: CommonUtil.py:122
def CommonUtil.lumiUnitForPrint (   t)
input : largest lumivalue
output: (unitstring,denomitor)

Definition at line 32 of file CommonUtil.py.

Referenced by lumiReport.toScreenLSEffective(), and lumiReport.toScreenLumiByLS().

33  '''
34  input : largest lumivalue
35  output: (unitstring,denomitor)
36  '''
37  unitstring='/ub'
38  denomitor=1.0
39  if t>=1.0e3 and t<1.0e06:
40  denomitor=1.0e3
41  unitstring='/nb'
42  elif t>=1.0e6 and t<1.0e9:
43  denomitor=1.0e6
44  unitstring='/pb'
45  elif t>=1.0e9 and t<1.0e12:
46  denomitor=1.0e9
47  unitstring='/fb'
48  elif t>=1.0e12 and t<1.0e15:
49  denomitor=1.0e12
50  unitstring='/ab'
51  elif t<1.0 and t>=1.0e-3: #left direction
52  denomitor=1.0e-03
53  unitstring='/mb'
54  elif t<1.0e-03 and t>=1.0e-06:
55  denomitor=1.0e-06
56  unitstring='/b'
57  elif t<1.0e-06 and t>=1.0e-09:
58  denomitor=1.0e-9
59  unitstring='/kb'
60  return (unitstring,denomitor)
def lumiUnitForPrint(t)
Definition: CommonUtil.py:32
def CommonUtil.pack (   high,
  low 
)
pack high,low 32bit unsigned int to one unsigned 64bit long long
   Note:the print value of result number may appear signed, if the sign bit is used.

Definition at line 156 of file CommonUtil.py.

Referenced by packToString().

156 def pack(high,low):
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.
159  """
160  h=high<<32
161  return (h|low)
def pack(high, low)
Definition: CommonUtil.py:156
def CommonUtil.packArraytoBlob (   iarray)
Inputs:
inputarray: a python array

Definition at line 222 of file CommonUtil.py.

Referenced by generateDummyData.hlt(), queryDataSource.hltFromOldLumi(), queryDataSource.hltFromRuninfoV2(), generateDummyData.lumiSummary(), generateDummyData.trg(), and queryDataSource.trgFromOldLumi().

222 def packArraytoBlob(iarray):
223  '''
224  Inputs:
225  inputarray: a python array
226  '''
227  result=coral.Blob()
228  result.write(iarray.tostring())
229  return result
230 
def packArraytoBlob(iarray)
Definition: CommonUtil.py:222
def CommonUtil.packListstrtoCLOB (   iListstr,
  separator = ' 
)
pack list of string of comma separated large string CLOB

Definition at line 246 of file CommonUtil.py.

246 def packListstrtoCLOB(iListstr,separator=','):
247  '''
248  pack list of string of comma separated large string CLOB
249  '''
250  return separator.join(iListstr)
251 
def packListstrtoCLOB(iListstr, separator=')
Definition: CommonUtil.py:246
def CommonUtil.packToString (   high,
  low 
)
pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
   Note:the print value of result number may appear signed, if the sign bit is used.

Definition at line 162 of file CommonUtil.py.

References pack().

162 def packToString(high,low):
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.
165  """
166  fmt="%u"
167  return fmt%pack(high,low)
def packToString(high, low)
Definition: CommonUtil.py:162
def pack(high, low)
Definition: CommonUtil.py:156
def CommonUtil.pairwise (   lst)
yield item i and item i+1 in lst. e.g.
(lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)

from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration

Definition at line 100 of file CommonUtil.py.

100 def pairwise(lst):
101  """
102  yield item i and item i+1 in lst. e.g.
103  (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
104 
105  from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
106  """
107  if not len(lst): return
108  #yield None, lst[0]
109  for i in range(len(lst)-1):
110  yield lst[i], lst[i+1]
111  yield lst[-1], None
def pairwise(lst)
Definition: CommonUtil.py:100
def CommonUtil.parselumicorrector (   correctorStr)
output: (functionname,parametersinuppercase[])

Definition at line 273 of file CommonUtil.py.

Referenced by normDML.normValueById().

273 def parselumicorrector(correctorStr):
274  '''
275  output: (functionname,parametersinuppercase[])
276  '''
277  cleancorrectorStr=correctorStr.replace(' ','')#in case of whitespace by mistake
278  [correctorFunc,paramsStr]=cleancorrectorStr.split(':')
279  params=paramsStr.split(',')
280  return (correctorFunc,params)
281 
def parselumicorrector(correctorStr)
Definition: CommonUtil.py:273
def CommonUtil.parseTime (   iTime)
input string of the ("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$" format
output (runnum,fillnum,timeStr)

Definition at line 16 of file CommonUtil.py.

References createfilelist.int.

16 def parseTime(iTime):
17  '''
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)
20  '''
21  if not iTime: return (None,None,None)
22  p=re.compile('^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
23  if re.match(p,iTime):
24  return (None,None,iTime)
25  p=re.compile('^\d{6}$')
26  if re.match(p,iTime):
27  return (int(iTime),None,None)
28  p=re.compile('^\d{4}$')
29  if re.match(p,iTime):
30  return (None,int(iTime),None)
31 
def parseTime(iTime)
Definition: CommonUtil.py:16
def CommonUtil.splitlistToRangeString (   inPut)

Definition at line 258 of file CommonUtil.py.

Referenced by lumiReport.toScreenOverview(), and lumiReport.toScreenTotEffective().

259  result = []
260  first = inPut[0]
261  last = inPut[0]
262  result.append ([inPut[0]])
263  counter = 0
264  for i in inPut[1:]:
265  if i == last+1:
266  result[counter].append (i)
267  else:
268  counter += 1
269  result.append ([i])
270  last = i
271  return ', '.join (['['+str (min (x))+'-'+str (max (x))+']' for x in result])
272 
def splitlistToRangeString(inPut)
Definition: CommonUtil.py:258
def CommonUtil.timeStamptoDate (   i)
convert 64bit timestamp to local date in string format

Definition at line 178 of file CommonUtil.py.

References unpack().

179  """convert 64bit timestamp to local date in string format
180  """
181  return time.ctime(unpack(i)[0])
def timeStamptoDate(i)
Definition: CommonUtil.py:178
def unpack(i)
Definition: CommonUtil.py:168
def CommonUtil.timeStamptoUTC (   i)
convert 64bit timestamp to Universal Time in string format

Definition at line 182 of file CommonUtil.py.

References unpack().

183  """convert 64bit timestamp to Universal Time in string format
184  """
185  t=unpack(i)[0]
186  return time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
def timeStamptoUTC(i)
Definition: CommonUtil.py:182
def unpack(i)
Definition: CommonUtil.py:168
def CommonUtil.tolegalJSON (   inputstring)
convert json like string to legal json string
add double quote around json keys if they are not there, change single quote to double quote around keys

Definition at line 202 of file CommonUtil.py.

References edm.print(), and str.

202 def tolegalJSON(inputstring):
203  '''
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
206  '''
207  strresult=inputstring.strip()
208  strresult=re.sub("\s+","",strresult)
209  try:
210  mydict=ast.literal_eval(strresult)
211  except SyntaxError:
212  print('error in converting string to dict')
213  raise
214  result={}
215  for k,v in mydict.items():
216  if not isinstance(k,str):
217  result[str(k)]=v
218  else:
219  result[k]=v
220  return re.sub("'",'"',str(result))
221 
def tolegalJSON(inputstring)
Definition: CommonUtil.py:202
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
#define str(s)
def CommonUtil.transposed (   lists,
  defaultval = None 
)
transposing list of lists
from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/

Definition at line 148 of file CommonUtil.py.

References genParticles_cff.map.

Referenced by TkRotation2D< Scalar >.rotate(), specificLumi.specificlumiTofile(), and lumiReport.toCSVLumiByLSXing().

148 def transposed(lists, defaultval=None):
149  """
150  transposing list of lists
151  from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
152  """
153  if not lists: return []
154  #return map(lambda *row: [elem or defaultval for elem in row], *lists)
155  return map(lambda *row: [elem for elem in row or defaultval], *lists)
def transposed(lists, defaultval=None)
Definition: CommonUtil.py:148
def CommonUtil.unpack (   i)
unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)

Definition at line 168 of file CommonUtil.py.

References mathSSE.return().

Referenced by timeStamptoDate(), timeStamptoUTC(), unpackFromString(), and unpackLumiid().

168 def unpack(i):
169  """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
170  """
171  high=i>>32
172  low=i&0xFFFFFFFF
173  return(high,low)
return((rh^lh)&mask)
def unpack(i)
Definition: CommonUtil.py:168
def CommonUtil.unpackBlobtoArray (   iblob,
  itemtypecode 
)
Inputs:
iblob: coral.Blob
itemtypecode: python array type code 

Definition at line 231 of file CommonUtil.py.

Referenced by dataDML.beamInfoById(), lumiQueryAPI.beamIntensityForRun(), dataDML.hltLSById(), dataDML.lumiLSById(), and dataDML.trgLSById().

231 def unpackBlobtoArray(iblob,itemtypecode):
232  '''
233  Inputs:
234  iblob: coral.Blob
235  itemtypecode: python array type code
236  '''
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()
241  if not blobstr :
242  return None
243  result.fromstring(blobstr)
244  return result
245 
def unpackBlobtoArray(iblob, itemtypecode)
Definition: CommonUtil.py:231
def CommonUtil.unpackCLOBtoListstr (   iStr,
  separator = ' 
)
unpack a large string to list of string

Definition at line 252 of file CommonUtil.py.

References split.

252 def unpackCLOBtoListstr(iStr,separator=','):
253  '''
254  unpack a large string to list of string
255  '''
256  return [i.strip() for i in iStr.strip().split(separator)]
257 
def unpackCLOBtoListstr(iStr, separator=')
Definition: CommonUtil.py:252
double split
Definition: MVATrainer.cc:139
def CommonUtil.unpackFromString (   i)
unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low)

Definition at line 174 of file CommonUtil.py.

References createfilelist.int, and unpack().

175  """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low)
176  """
177  return unpack(int(i))
def unpackFromString(i)
Definition: CommonUtil.py:174
def unpack(i)
Definition: CommonUtil.py:168
def CommonUtil.unpackLumiid (   i)
unpack 64bit lumiid to dictionary {'run','lumisection'}

Definition at line 187 of file CommonUtil.py.

References unpack().

188  """unpack 64bit lumiid to dictionary {'run','lumisection'}
189  """
190  j=unpack(i)
191  return {'run':j[0],'lumisection':j[1]}
def unpackLumiid(i)
Definition: CommonUtil.py:187
def unpack(i)
Definition: CommonUtil.py:168

Variable Documentation

CommonUtil.a

Definition at line 285 of file CommonUtil.py.

CommonUtil.b

Definition at line 317 of file CommonUtil.py.

CommonUtil.lst

Definition at line 289 of file CommonUtil.py.

CommonUtil.myblob

Definition at line 314 of file CommonUtil.py.

CommonUtil.nested

Definition at line 283 of file CommonUtil.py.

Referenced by edm.contextual_find().

CommonUtil.pp

Definition at line 298 of file CommonUtil.py.

CommonUtil.result

Definition at line 296 of file CommonUtil.py.

CommonUtil.seqbag

Definition at line 291 of file CommonUtil.py.