CMS 3D CMS Logo

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.

00138                  :
00139     """
00140     report the number of duplicates in a python list
00141     """
00142     from collections import defaultdict
00143     tally=defaultdict(int)
00144     for x in l:
00145         tally[x]+=1
    return tally.items()
def CommonUtil::findInList (   mylist,
  element 
)
check if an element is in the list

Definition at line 110 of file CommonUtil.py.

00111                               :
00112     """
00113     check if an element is in the list
00114     """
00115     pos=-1
00116     try:
00117         pos=mylist.index(element)
00118     except ValueError:
00119         pos=-1
    return pos!=-1
def CommonUtil::flatten (   obj)
Given nested lists or tuples, returns a single flattened list

Definition at line 4 of file CommonUtil.py.

00005                 :
00006     '''Given nested lists or tuples, returns a single flattened list'''
00007     result = []
00008     for piece in obj:
00009         if hasattr (piece, '__iter__') and not isinstance (piece, basestring):
00010             result.extend( flatten (piece) )
00011         else:
00012             result.append (piece)
00013     return result

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.

00060                            :
00061     '''
00062     input:
00063         float value in 1/ub
00064     output:
00065         printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb]
00066     '''
00067     if inverseubval>=1.0e-09 and inverseubval<1.0e-06:
00068         denomitor=1.0e-09
00069         unitstring='/kb'
00070         return (float(inverseubval)/float(denomitor),unitstring)
00071     if inverseubval>=1.0e-06 and inverseubval<1.0e-03:
00072         denomitor=1.0e-06
00073         unitstring='/b'
00074         return (float(inverseubval)/float(denomitor),unitstring)
00075     if inverseubval>=1.0e-03 and inverseubval<1.0:
00076         denomitor=1.0e-03
00077         unitstring='/mb'
00078         return (float(inverseubval)/float(denomitor),unitstring)
00079     if inverseubval>=1.0 and inverseubval<1.0e3:
00080         unitstring='/ub'
00081         return (inverseubval,unitstring)
00082     if inverseubval>=1.0e3 and inverseubval<1.0e06:
00083         denomitor=1.0e3
00084         unitstring='/nb'
00085         return (float(inverseubval)/float(denomitor),unitstring)
00086     if inverseubval>=1.0e6 and inverseubval<1.0e9:
00087         denomitor=1.0e6
00088         unitstring='/pb'
00089         return (float(inverseubval)/float(denomitor),unitstring)
00090     if inverseubval>=1.0e9 and inverseubval<1.0e12:
00091         denomitor=1.0e9
00092         unitstring='/fb'
00093         return (float(inverseubval)/float(denomitor),unitstring)
00094     if inverseubval>=1.0e12 and inverseubval<1.0e15:
00095         denomitor=1.0e12
00096         unitstring='/ab'
00097         return (float(inverseubval)/float(denomitor),unitstring)
    return (float(inverseubval),'/ub')
def CommonUtil::inclusiveRange (   start,
  stop,
  step 
)
return range including the stop value

Definition at line 190 of file CommonUtil.py.

00191                                    :
00192     """return range including the stop value
00193     """
00194     v=start
00195     while v<stop:
00196         yield v
00197         v+=step
00198     if v>=stop:
00199         yield stop
        
def CommonUtil::is_floatstr (   s)
test if a string can be converted to a float

Definition at line 128 of file CommonUtil.py.

00129                   :
00130     """
00131     test if a string can be converted to a float
00132     """
00133     try:
00134         float(s)
00135         return True
00136     except ValueError:
        return False
def CommonUtil::is_intstr (   s)
test if a string can be converted to a int

Definition at line 120 of file CommonUtil.py.

00121                 :
00122     """test if a string can be converted to a int
00123     """
00124     try:
00125         int(s)
00126         return True
00127     except ValueError:
        return False
def CommonUtil::lumiUnitForPrint (   t)
input : largest lumivalue
output: (unitstring,denomitor)

Definition at line 30 of file CommonUtil.py.

00031                        :
00032     '''
00033     input : largest lumivalue
00034     output: (unitstring,denomitor)
00035     '''
00036     unitstring='/ub'
00037     denomitor=1.0
00038     if t>=1.0e3 and t<1.0e06:
00039         denomitor=1.0e3
00040         unitstring='/nb'
00041     elif t>=1.0e6 and t<1.0e9:
00042         denomitor=1.0e6
00043         unitstring='/pb'
00044     elif t>=1.0e9 and t<1.0e12:
00045         denomitor=1.0e9
00046         unitstring='/fb'
00047     elif t>=1.0e12 and t<1.0e15:
00048         denomitor=1.0e12
00049         unitstring='/ab'
00050     elif t<1.0 and t>=1.0e-3: #left direction
00051         denomitor=1.0e-03
00052         unitstring='/mb'
00053     elif t<1.0e-03 and t>=1.0e-06:
00054         denomitor=1.0e-06
00055         unitstring='/b'
00056     elif t<1.0e-06 and t>=1.0e-09:
00057         denomitor=1.0e-9
00058         unitstring='/kb'
    return (unitstring,denomitor)
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.

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

Definition at line 220 of file CommonUtil.py.

00221                            :
00222     '''
00223     Inputs:
00224     inputarray: a python array
00225     '''
00226     result=coral.Blob()
00227     result.write(iarray.tostring())
00228     return result

def CommonUtil::packListstrtoCLOB (   iListstr,
  separator = ' 
)
pack list of string of comma separated large string CLOB

Definition at line 244 of file CommonUtil.py.

00245                                              :
00246     '''
00247     pack list of string of comma separated large string CLOB
00248     '''
00249     return separator.join(iListstr)

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.

00161                           :
00162     """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
00163        Note:the print value of result number may appear signed, if the sign bit is used.
00164     """
00165     fmt="%u"
    return fmt%pack(high,low)
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 https://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration

Definition at line 98 of file CommonUtil.py.

00099                  :
00100     """
00101     yield item i and item i+1 in lst. e.g.
00102     (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
00103     
00104     from https://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
00105     """
00106     if not len(lst): return
00107     #yield None, lst[0]
00108     for i in range(len(lst)-1):
00109         yield lst[i], lst[i+1]
    yield lst[-1], None
def CommonUtil::parselumicorrector (   correctorStr)
output: (functionname,parametersinuppercase[])

Definition at line 271 of file CommonUtil.py.

00272                                     :
00273     '''
00274     output: (functionname,parametersinuppercase[])
00275     '''
00276     cleancorrectorStr=correctorStr.replace(' ','')#in case of whitespace by mistake
00277     [correctorFunc,paramsStr]=cleancorrectorStr.split(':')
00278     params=paramsStr.split(',')
00279     return (correctorFunc,params)

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.

00015                     :
00016     '''
00017     input string of the ("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$" format
00018     output (runnum,fillnum,timeStr)
00019     '''
00020     if not iTime: return (None,None,None)
00021     p=re.compile('^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$')
00022     if re.match(p,iTime):
00023         return (None,None,iTime)
00024     p=re.compile('^\d{6}$')
00025     if re.match(p,iTime):
00026         return (int(iTime),None,None)
00027     p=re.compile('^\d{4}$')
00028     if re.match(p,iTime):
00029         return (None,int(iTime),None)
    
def CommonUtil::splitlistToRangeString (   inPut)

Definition at line 256 of file CommonUtil.py.

00257                                   :
00258     result = []
00259     first = inPut[0]
00260     last = inPut[0]
00261     result.append ([inPut[0]])
00262     counter = 0
00263     for i in inPut[1:]:
00264         if i == last+1:
00265             result[counter].append (i)
00266         else:
00267             counter += 1
00268             result.append ([i])
00269         last = i
00270     return ', '.join (['['+str (min (x))+'-'+str (max (x))+']' for x in result])

def CommonUtil::timeStamptoDate (   i)
convert 64bit timestamp to local date in string format

Definition at line 176 of file CommonUtil.py.

00177                       :
00178     """convert 64bit timestamp to local date in string format
00179     """
    return time.ctime(unpack(i)[0])
def CommonUtil::timeStamptoUTC (   i)
convert 64bit timestamp to Universal Time in string format

Definition at line 180 of file CommonUtil.py.

00181                      :
00182     """convert 64bit timestamp to Universal Time in string format
00183     """
00184     t=unpack(i)[0]
    return time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(t))
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.

00201                             :
00202    '''
00203    convert json like string to legal json string
00204    add double quote around json keys if they are not there, change single quote to double quote around keys
00205    '''
00206    strresult=inputstring.strip()
00207    strresult=re.sub("\s+","",strresult)
00208    try:
00209        mydict=ast.literal_eval(strresult)
00210    except SyntaxError:
00211        print 'error in converting string to dict'
00212        raise
00213    result={}
00214    for k,v in mydict.items():
00215        if not isinstance(k,str):
00216            result[str(k)]=v
00217        else:
00218            result[k]=v
00219    return re.sub("'",'"',str(result))

def CommonUtil::transposed (   lists,
  defaultval = None 
)
transposing list of lists
from https://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/

Definition at line 146 of file CommonUtil.py.

00147                                       :
00148     """
00149     transposing list of lists
00150     from https://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
00151     """
00152     if not lists: return []
00153     #return map(lambda *row: [elem or defaultval for elem in row], *lists)
    return map(lambda *row: [elem for elem in row or defaultval], *lists)
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.

00167              :
00168     """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
00169     """
00170     high=i>>32
00171     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.

00230                                          :
00231     '''
00232     Inputs:
00233     iblob: coral.Blob
00234     itemtypecode: python array type code 
00235     '''
00236     if itemtypecode not in ['c','b','B','u','h','H','i','I','l','L','f','d']:
00237         raise RuntimeError('unsupported typecode '+itemtypecode)
00238     result=array.array(itemtypecode)
00239     blobstr=iblob.readline()
00240     if not blobstr :
00241         return None
00242     result.fromstring(blobstr)
00243     return result

def CommonUtil::unpackCLOBtoListstr (   iStr,
  separator = ' 
)
unpack a large string to list of string

Definition at line 250 of file CommonUtil.py.

00251                                            :
00252     '''
00253     unpack a large string to list of string
00254     '''
00255     return [i.strip() for i in iStr.strip().split(separator)]

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.

00173                        :
00174     """unpack 64bit unsigned long long in string format into 2 32bit unsigned int, return tuple(high,low)
00175     """
    return unpack(int(i))
def CommonUtil::unpackLumiid (   i)
unpack 64bit lumiid to dictionary {'run','lumisection'}

Definition at line 185 of file CommonUtil.py.

00186                    :
00187     """unpack 64bit lumiid to dictionary {'run','lumisection'}
00188     """
00189     j=unpack(i)
    return {'run':j[0],'lumisection':j[1]}

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.