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 inclusiveRange
 
def is_floatstr
 
def is_intstr
 
def pack
 
def packArraytoBlob
 
def packListstrtoCLOB
 
def packToString
 
def pairwise
 
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)
 
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 43 of file CommonUtil.py.

Referenced by lumiQueryAPI.allruns(), and lumiInstPlot.getInstLumiPerLS().

43 
44 def count_dups(l):
45  """
46  report the number of duplicates in a python list
47  """
48  from collections import defaultdict
49  tally=defaultdict(int)
50  for x in l:
51  tally[x]+=1
return tally.items()
def count_dups
Definition: CommonUtil.py:43
def CommonUtil.findInList (   mylist,
  element 
)
check if an element is in the list

Definition at line 16 of file CommonUtil.py.

16 
17 def findInList(mylist,element):
18  """
19  check if an element is in the list
20  """
21  pos=-1
22  try:
23  pos=mylist.index(element)
24  except ValueError:
25  pos=-1
return pos!=-1
def findInList
Definition: CommonUtil.py:16
def CommonUtil.inclusiveRange (   start,
  stop,
  step 
)
return range including the stop value

Definition at line 95 of file CommonUtil.py.

Referenced by matplotRender.matplotRender.plotSumX_Run().

95 
96 def inclusiveRange(start,stop,step):
97  """return range including the stop value
98  """
99  v=start
100  while v<stop:
101  yield v
102  v+=step
103  if v>=stop:
104  yield stop
def inclusiveRange
Definition: CommonUtil.py:95
def CommonUtil.is_floatstr (   s)
test if a string can be converted to a float

Definition at line 34 of file CommonUtil.py.

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

34 
35 def is_floatstr(s):
36  """
37  test if a string can be converted to a float
38  """
39  try:
40  float(s)
41  return True
42  except ValueError:
return False
def is_floatstr
Definition: CommonUtil.py:34
def CommonUtil.is_intstr (   s)
test if a string can be converted to a int

Definition at line 26 of file CommonUtil.py.

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

26 
27 def is_intstr(s):
28  """test if a string can be converted to a int
29  """
30  try:
31  int(s)
32  return True
33  except ValueError:
return False
def is_intstr
Definition: CommonUtil.py:26
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 59 of file CommonUtil.py.

Referenced by packToString().

59 
60 def pack(high,low):
61  """pack high,low 32bit unsigned int to one unsigned 64bit long long
62  Note:the print value of result number may appear signed, if the sign bit is used.
63  """
64  h=high<<32
return (h|low)
def CommonUtil.packArraytoBlob (   iarray)
Inputs:
inputarray: a python array

Definition at line 125 of file CommonUtil.py.

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

126 def packArraytoBlob(iarray):
127  '''
128  Inputs:
129  inputarray: a python array
130  '''
131  result=coral.Blob()
132  result.write(iarray.tostring())
133  return result
def packArraytoBlob
Definition: CommonUtil.py:125
def CommonUtil.packListstrtoCLOB (   iListstr,
  separator = ' 
)
pack list of string of comma separated large string CLOB

Definition at line 146 of file CommonUtil.py.

147 def packListstrtoCLOB(iListstr,separator=','):
148  '''
149  pack list of string of comma separated large string CLOB
150  '''
151  return separator.join(iListstr)
def packListstrtoCLOB
Definition: CommonUtil.py:146
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 65 of file CommonUtil.py.

References pack().

65 
66 def packToString(high,low):
67  """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
68  Note:the print value of result number may appear signed, if the sign bit is used.
69  """
70  fmt="%u"
return fmt%pack(high,low)
def packToString
Definition: CommonUtil.py:65
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 4 of file CommonUtil.py.

4 
5 def pairwise(lst):
6  """
7  yield item i and item i+1 in lst. e.g.
8  (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
9 
10  from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
11  """
12  if not len(lst): return
13  #yield None, lst[0]
14  for i in range(len(lst)-1):
15  yield lst[i], lst[i+1]
yield lst[-1], None
def pairwise
Definition: CommonUtil.py:4
def CommonUtil.timeStamptoDate (   i)
convert 64bit timestamp to local date in string format

Definition at line 81 of file CommonUtil.py.

References unpack().

81 
82 def timeStamptoDate(i):
83  """convert 64bit timestamp to local date in string format
84  """
return time.ctime(unpack(i)[0])
def timeStamptoDate
Definition: CommonUtil.py:81
def unpack
Definition: CommonUtil.py:71
def CommonUtil.timeStamptoUTC (   i)
convert 64bit timestamp to Universal Time in string format

Definition at line 85 of file CommonUtil.py.

References unpack().

85 
86 def timeStamptoUTC(i):
87  """convert 64bit timestamp to Universal Time in string format
88  """
89  t=unpack(i)[0]
return time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
def timeStamptoUTC
Definition: CommonUtil.py:85
def unpack
Definition: CommonUtil.py:71
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 105 of file CommonUtil.py.

Referenced by lumiValidate.main().

106 def tolegalJSON(inputstring):
107  '''
108  convert json like string to legal json string
109  add double quote around json keys if they are not there, change single quote to double quote around keys
110  '''
111  strresult=inputstring.strip()
112  strresult=re.sub("\s+","",strresult)
113  try:
114  mydict=ast.literal_eval(strresult)
115  except SyntaxError:
116  print 'error in converting string to dict'
117  raise
118  result={}
119  for k,v in mydict.items():
120  if not isinstance(k,str):
121  result[str(k)]=v
122  else:
123  result[k]=v
124  return re.sub("'",'"',str(result))
def tolegalJSON
Definition: CommonUtil.py:105
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 52 of file CommonUtil.py.

References python.multivaluedict.map().

Referenced by lumiSumPlot.getLumiOrderByLS(), lumiInstPlot.main(), lumiSumPlot.main(), specificLumi.specificlumiTofile(), and specificLumi-2011.specificlumiTofile().

52 
53 def transposed(lists, defaultval=None):
54  """
55  transposing list of lists
56  from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
57  """
58  if not lists: return []
return map(lambda *row: [elem or defaultval for elem in row], *lists)
def transposed
Definition: CommonUtil.py:52
def CommonUtil.unpack (   i)
unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)

Definition at line 71 of file CommonUtil.py.

References mathSSE.return().

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

71 
72 def unpack(i):
73  """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
74  """
75  high=i>>32
76  low=i&0xFFFFFFFF
return(high,low)
return((rh^lh)&mask)
def unpack
Definition: CommonUtil.py:71
def CommonUtil.unpackBlobtoArray (   iblob,
  itemtypecode 
)
Inputs:
iblob: coral.Blob
itemtypecode: python array type code 

Definition at line 134 of file CommonUtil.py.

Referenced by lumiCalcAPI.effectiveLumiForRange(), and lumiCalcAPI.lumiForRange().

135 def unpackBlobtoArray(iblob,itemtypecode):
136  '''
137  Inputs:
138  iblob: coral.Blob
139  itemtypecode: python array type code
140  '''
141  if itemtypecode not in ['c','b','B','u','h','H','i','I','l','L','f','d']:
142  raise RuntimeError('unsupported typecode '+itemtypecode)
143  result=array.array(itemtypecode)
144  result.fromstring(iblob.readline())
145  return result
def unpackBlobtoArray
Definition: CommonUtil.py:134
def CommonUtil.unpackCLOBtoListstr (   iStr,
  separator = ' 
)
unpack a large string to list of string

Definition at line 152 of file CommonUtil.py.

References split.

153 def unpackCLOBtoListstr(iStr,separator=','):
154  '''
155  unpack a large string to list of string
156  '''
157  return [i.strip() for i in iStr.strip().split(separator)]
def unpackCLOBtoListstr
Definition: CommonUtil.py:152
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 77 of file CommonUtil.py.

References unpack().

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

Definition at line 90 of file CommonUtil.py.

References unpack().

90 
91 def unpackLumiid(i):
92  """unpack 64bit lumiid to dictionary {'run','lumisection'}
93  """
94  j=unpack(i)
return {'run':j[0],'lumisection':j[1]}
def unpack
Definition: CommonUtil.py:71
def unpackLumiid
Definition: CommonUtil.py:90

Variable Documentation

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

Definition at line 159 of file CommonUtil.py.

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

Definition at line 191 of file CommonUtil.py.

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

Definition at line 163 of file CommonUtil.py.

Referenced by ParticleKinematicLinearizedTrackState.createRefittedTrackState(), editVectorParameter(), editVInputTag(), evf::FWEPWrapper.getTriggerReport(), FWSummaryManager.newItem(), and evf::FWEPWrapper.updateRollingReport().

tuple CommonUtil.myblob = packArraytoBlob(a)

Definition at line 188 of file CommonUtil.py.

tuple CommonUtil.pp = json.loads(result)

Definition at line 172 of file CommonUtil.py.

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

Definition at line 170 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 165 of file CommonUtil.py.