CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
CommonUtil Namespace Reference

Functions

def count_dups
 
def findInList
 
def flatten
 
def guessUnit
 
def inclusiveRange
 
def is_floatstr
 
def is_intstr
 
def lumiUnitForPrint
 
def pack
 
def packArraytoBlob
 
def packListstrtoCLOB
 
def packToString
 
def pairwise
 
def parselumicorrector
 
def parseTime
 
def splitlistToRangeString
 
def timeStamptoDate
 
def timeStamptoUTC
 
def tolegalJSON
 
def transposed
 
def unpack
 
def unpackBlobtoArray
 
def unpackCLOBtoListstr
 
def unpackFromString
 
def unpackLumiid
 

Variables

list a = [1,2,3,4,5]
 
tuple b = array.array('f')
 
list lst = ['I1','I2','I1','I3','I4','I4','I7','I7','I7','I7','I7']
 
tuple myblob = packArraytoBlob(a)
 
list nested = [[[1,2],[6,6,8]],[[3,4,5],[4,5]]]
 
tuple pp = json.loads(result)
 
tuple result = tolegalJSON('{1:[],2:[[1,3],[4,5]]}')
 
list seqbag = [[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]
 

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().

138 def count_dups(l):
139  """
140  report the number of duplicates in a python list
141  """
142  from collections import defaultdict
143  tally=defaultdict(int)
144  for x in l:
145  tally[x]+=1
return tally.items()
def count_dups
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.

111 def findInList(mylist,element):
112  """
113  check if an element is in the list
114  """
115  pos=-1
116  try:
117  pos=mylist.index(element)
118  except ValueError:
119  pos=-1
return pos!=-1
def findInList
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 
5 def flatten(obj):
6  '''Given nested lists or tuples, returns a single flattened list'''
7  result = []
8  for piece in obj:
9  if hasattr (piece, '__iter__') and not isinstance (piece, basestring):
10  result.extend( flatten (piece) )
11  else:
12  result.append (piece)
13  return result
def flatten
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.

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

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

Definition at line 190 of file CommonUtil.py.

191 def inclusiveRange(start,stop,step):
192  """return range including the stop value
193  """
194  v=start
195  while v<stop:
196  yield v
197  v+=step
198  if v>=stop:
199  yield stop
def inclusiveRange
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.

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

129 def is_floatstr(s):
130  """
131  test if a string can be converted to a float
132  """
133  try:
134  float(s)
135  return True
136  except ValueError:
return False
def is_floatstr
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.

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

121 def is_intstr(s):
122  """test if a string can be converted to a int
123  """
124  try:
125  int(s)
126  return True
127  except ValueError:
return False
def is_intstr
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().

30 
31 def lumiUnitForPrint(t):
32  '''
33  input : largest lumivalue
34  output: (unitstring,denomitor)
35  '''
36  unitstring='/ub'
37  denomitor=1.0
38  if t>=1.0e3 and t<1.0e06:
39  denomitor=1.0e3
40  unitstring='/nb'
41  elif t>=1.0e6 and t<1.0e9:
42  denomitor=1.0e6
43  unitstring='/pb'
44  elif t>=1.0e9 and t<1.0e12:
45  denomitor=1.0e9
46  unitstring='/fb'
47  elif t>=1.0e12 and t<1.0e15:
48  denomitor=1.0e12
49  unitstring='/ab'
50  elif t<1.0 and t>=1.0e-3: #left direction
51  denomitor=1.0e-03
52  unitstring='/mb'
53  elif t<1.0e-03 and t>=1.0e-06:
54  denomitor=1.0e-06
55  unitstring='/b'
56  elif t<1.0e-06 and t>=1.0e-09:
57  denomitor=1.0e-9
58  unitstring='/kb'
return (unitstring,denomitor)
def lumiUnitForPrint
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().

155 def pack(high,low):
156  """pack high,low 32bit unsigned int to one unsigned 64bit long long
157  Note:the print value of result number may appear signed, if the sign bit is used.
158  """
159  h=high<<32
return (h|low)
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().

221 def packArraytoBlob(iarray):
222  '''
223  Inputs:
224  inputarray: a python array
225  '''
226  result=coral.Blob()
227  result.write(iarray.tostring())
228  return result
def packArraytoBlob
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.

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

161 def packToString(high,low):
162  """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
163  Note:the print value of result number may appear signed, if the sign bit is used.
164  """
165  fmt="%u"
return fmt%pack(high,low)
def packToString
Definition: CommonUtil.py:160
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 
99 def pairwise(lst):
100  """
101  yield item i and item i+1 in lst. e.g.
102  (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
103 
104  from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
105  """
106  if not len(lst): return
107  #yield None, lst[0]
108  for i in range(len(lst)-1):
109  yield lst[i], lst[i+1]
yield lst[-1], None
def pairwise
Definition: CommonUtil.py:98
def CommonUtil.parselumicorrector (   correctorStr)
output: (functionname,parametersinuppercase[])

Definition at line 271 of file CommonUtil.py.

Referenced by normDML.normValueById().

272 def parselumicorrector(correctorStr):
273  '''
274  output: (functionname,parametersinuppercase[])
275  '''
276  cleancorrectorStr=correctorStr.replace(' ','')#in case of whitespace by mistake
277  [correctorFunc,paramsStr]=cleancorrectorStr.split(':')
278  params=paramsStr.split(',')
279  return (correctorFunc,params)
def parselumicorrector
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.

14 
15 def parseTime(iTime):
16  '''
17  input string of the ("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$" format
18  output (runnum,fillnum,timeStr)
19  '''
20  if not iTime: return (None,None,None)
21  p=re.compile('^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
22  if re.match(p,iTime):
23  return (None,None,iTime)
24  p=re.compile('^\d{6}$')
25  if re.match(p,iTime):
26  return (int(iTime),None,None)
27  p=re.compile('^\d{4}$')
28  if re.match(p,iTime):
29  return (None,int(iTime),None)
def parseTime
Definition: CommonUtil.py:14
def CommonUtil.splitlistToRangeString (   inPut)

Definition at line 256 of file CommonUtil.py.

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

257 def splitlistToRangeString (inPut):
258  result = []
259  first = inPut[0]
260  last = inPut[0]
261  result.append ([inPut[0]])
262  counter = 0
263  for i in inPut[1:]:
264  if i == last+1:
265  result[counter].append (i)
266  else:
267  counter += 1
268  result.append ([i])
269  last = i
270  return ', '.join (['['+str (min (x))+'-'+str (max (x))+']' for x in result])
def splitlistToRangeString
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 def timeStamptoDate(i):
178  """convert 64bit timestamp to local date in string format
179  """
return time.ctime(unpack(i)[0])
def timeStamptoDate
Definition: CommonUtil.py:176
def CommonUtil.timeStamptoUTC (   i)
convert 64bit timestamp to Universal Time in string format

Definition at line 180 of file CommonUtil.py.

References unpack().

181 def timeStamptoUTC(i):
182  """convert 64bit timestamp to Universal Time in string format
183  """
184  t=unpack(i)[0]
return time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
def timeStamptoUTC
Definition: CommonUtil.py:180
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.

201 def tolegalJSON(inputstring):
202  '''
203  convert json like string to legal json string
204  add double quote around json keys if they are not there, change single quote to double quote around keys
205  '''
206  strresult=inputstring.strip()
207  strresult=re.sub("\s+","",strresult)
208  try:
209  mydict=ast.literal_eval(strresult)
210  except SyntaxError:
211  print 'error in converting string to dict'
212  raise
213  result={}
214  for k,v in mydict.items():
215  if not isinstance(k,str):
216  result[str(k)]=v
217  else:
218  result[k]=v
219  return re.sub("'",'"',str(result))
def tolegalJSON
Definition: CommonUtil.py:200
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 Association.map.

Referenced by specificLumi.specificlumiTofile(), and lumiReport.toCSVLumiByLSXing().

147 def transposed(lists, defaultval=None):
148  """
149  transposing list of lists
150  from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
151  """
152  if not lists: return []
153  #return map(lambda *row: [elem or defaultval for elem in row], *lists)
return map(lambda *row: [elem for elem in row or defaultval], *lists)
dictionary map
Definition: Association.py:205
def transposed
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 hitfit.return.

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

167 def unpack(i):
168  """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
169  """
170  high=i>>32
171  low=i&0xFFFFFFFF
return(high,low)
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().

230 def unpackBlobtoArray(iblob,itemtypecode):
231  '''
232  Inputs:
233  iblob: coral.Blob
234  itemtypecode: python array type code
235  '''
236  if itemtypecode not in ['c','b','B','u','h','H','i','I','l','L','f','d']:
237  raise RuntimeError('unsupported typecode '+itemtypecode)
238  result=array.array(itemtypecode)
239  blobstr=iblob.readline()
240  if not blobstr :
241  return None
242  result.fromstring(blobstr)
243  return result
def unpackBlobtoArray
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.

251 def unpackCLOBtoListstr(iStr,separator=','):
252  '''
253  unpack a large string to list of string
254  '''
255  return [i.strip() for i in iStr.strip().split(separator)]
def unpackCLOBtoListstr
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 unpack().

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

Definition at line 185 of file CommonUtil.py.

References unpack().

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

Variable Documentation

list CommonUtil.a = [1,2,3,4,5]

Definition at line 283 of file CommonUtil.py.

tuple CommonUtil.b = array.array('f')

Definition at line 315 of file CommonUtil.py.

list CommonUtil.lst = ['I1','I2','I1','I3','I4','I4','I7','I7','I7','I7','I7']

Definition at line 287 of file CommonUtil.py.

tuple CommonUtil.myblob = packArraytoBlob(a)

Definition at line 312 of file CommonUtil.py.

list CommonUtil.nested = [[[1,2],[6,6,8]],[[3,4,5],[4,5]]]

Definition at line 281 of file CommonUtil.py.

Referenced by edm.contextual_find().

tuple CommonUtil.pp = json.loads(result)

Definition at line 296 of file CommonUtil.py.

tuple CommonUtil.result = tolegalJSON('{1:[],2:[[1,3],[4,5]]}')

Definition at line 294 of file CommonUtil.py.

list CommonUtil.seqbag = [[1,2,3],[1,3,3],[1,4,6],[4,5,6,7],[8,9]]

Definition at line 289 of file CommonUtil.py.