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 137 of file CommonUtil.py.

Referenced by lumiQueryAPI.allruns().

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()
def count_dups(l)
Definition: CommonUtil.py:137
def CommonUtil.findInList (   mylist,
  element 
)
check if an element is in the list

Definition at line 110 of file CommonUtil.py.

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
def findInList(mylist, element)
Definition: CommonUtil.py:110
def CommonUtil.flatten (   obj)
Given nested lists or tuples, returns a single flattened list

Definition at line 4 of file CommonUtil.py.

Referenced by python.rootplot.root2matplotlib.Hist2D.box(), python.rootplot.core.divide_axes(), and lumiReport.toCSVLumiByLSXing().

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, str):
9  result.extend( flatten (piece) )
10  else:
11  result.append (piece)
12  return result
13 
def flatten(obj)
Definition: CommonUtil.py:4
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 59 of file CommonUtil.py.

References objects.autophobj.float.

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

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')
def guessUnit(inverseubval)
Definition: CommonUtil.py:59
def CommonUtil.inclusiveRange (   start,
  stop,
  step 
)
return range including the stop value

Definition at line 190 of file CommonUtil.py.

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 
def inclusiveRange(start, stop, step)
Definition: CommonUtil.py:190
def CommonUtil.is_floatstr (   s)
test if a string can be converted to a float

Definition at line 128 of file CommonUtil.py.

References objects.autophobj.float.

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

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
def is_floatstr(s)
Definition: CommonUtil.py:128
def CommonUtil.is_intstr (   s)
test if a string can be converted to a int

Definition at line 120 of file CommonUtil.py.

References createfilelist.int.

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

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
def is_intstr(s)
Definition: CommonUtil.py:120
def CommonUtil.lumiUnitForPrint (   t)
input : largest lumivalue
output: (unitstring,denomitor)

Definition at line 30 of file CommonUtil.py.

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

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)
def lumiUnitForPrint(t)
Definition: CommonUtil.py:30
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 154 of file CommonUtil.py.

Referenced by packToString().

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)
def pack(high, low)
Definition: CommonUtil.py:154
def CommonUtil.packArraytoBlob (   iarray)
Inputs:
inputarray: a python array

Definition at line 220 of file CommonUtil.py.

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

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 
def packArraytoBlob(iarray)
Definition: CommonUtil.py:220
def CommonUtil.packListstrtoCLOB (   iListstr,
  separator = ' 
)
pack list of string of comma separated large string CLOB

Definition at line 244 of file CommonUtil.py.

244 def packListstrtoCLOB(iListstr,separator=','):
245  '''
246  pack list of string of comma separated large string CLOB
247  '''
248  return separator.join(iListstr)
249 
def packListstrtoCLOB(iListstr, separator=')
Definition: CommonUtil.py:244
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 160 of file CommonUtil.py.

References pack().

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)
def packToString(high, low)
Definition: CommonUtil.py:160
def pack(high, low)
Definition: CommonUtil.py:154
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 98 of file CommonUtil.py.

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
def pairwise(lst)
Definition: CommonUtil.py:98
def CommonUtil.parselumicorrector (   correctorStr)
output: (functionname,parametersinuppercase[])

Definition at line 271 of file CommonUtil.py.

Referenced by normDML.normValueById().

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 
def parselumicorrector(correctorStr)
Definition: CommonUtil.py:271
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 14 of file CommonUtil.py.

References createfilelist.int.

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 
def parseTime(iTime)
Definition: CommonUtil.py:14
def CommonUtil.splitlistToRangeString (   inPut)

Definition at line 256 of file CommonUtil.py.

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

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 
def splitlistToRangeString(inPut)
Definition: CommonUtil.py:256
def CommonUtil.timeStamptoDate (   i)
convert 64bit timestamp to local date in string format

Definition at line 176 of file CommonUtil.py.

References unpack().

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

Definition at line 180 of file CommonUtil.py.

References unpack().

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))
def timeStamptoUTC(i)
Definition: CommonUtil.py:180
def unpack(i)
Definition: CommonUtil.py:166
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 200 of file CommonUtil.py.

References str.

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 
def tolegalJSON(inputstring)
Definition: CommonUtil.py:200
#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 146 of file CommonUtil.py.

References genParticles_cff.map.

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

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)
def transposed(lists, defaultval=None)
Definition: CommonUtil.py:146
def CommonUtil.unpack (   i)
unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)

Definition at line 166 of file CommonUtil.py.

References mathSSE.return().

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

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)
return((rh^lh)&mask)
def unpack(i)
Definition: CommonUtil.py:166
def CommonUtil.unpackBlobtoArray (   iblob,
  itemtypecode 
)
Inputs:
iblob: coral.Blob
itemtypecode: python array type code 

Definition at line 229 of file CommonUtil.py.

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

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 
def unpackBlobtoArray(iblob, itemtypecode)
Definition: CommonUtil.py:229
def CommonUtil.unpackCLOBtoListstr (   iStr,
  separator = ' 
)
unpack a large string to list of string

Definition at line 250 of file CommonUtil.py.

References split.

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 
def unpackCLOBtoListstr(iStr, separator=')
Definition: CommonUtil.py:250
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 172 of file CommonUtil.py.

References createfilelist.int, and unpack().

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

Definition at line 185 of file CommonUtil.py.

References unpack().

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

Variable Documentation

CommonUtil.a

Definition at line 283 of file CommonUtil.py.

CommonUtil.b

Definition at line 315 of file CommonUtil.py.

CommonUtil.lst

Definition at line 287 of file CommonUtil.py.

CommonUtil.myblob

Definition at line 312 of file CommonUtil.py.

CommonUtil.nested

Definition at line 281 of file CommonUtil.py.

Referenced by edm.contextual_find().

CommonUtil.pp

Definition at line 296 of file CommonUtil.py.

CommonUtil.result

Definition at line 294 of file CommonUtil.py.

CommonUtil.seqbag

Definition at line 289 of file CommonUtil.py.