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

00122                  :
00123     """
00124     report the number of duplicates in a python list
00125     """
00126     from collections import defaultdict
00127     tally=defaultdict(int)
00128     for x in l:
00129         tally[x]+=1
    return tally.items()
def CommonUtil::findInList (   mylist,
  element 
)
check if an element is in the list

Definition at line 94 of file CommonUtil.py.

00095                               :
00096     """
00097     check if an element is in the list
00098     """
00099     pos=-1
00100     try:
00101         pos=mylist.index(element)
00102     except ValueError:
00103         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 43 of file CommonUtil.py.

00044                            :
00045     '''
00046     input:
00047         float value in 1/ub
00048     output:
00049         printable value (value(float),unit(str)) unit in [1/kb,1/b,1/mb,1/ub,1/nb,1/pb,1/fb]
00050     '''
00051     if inverseubval>=1.0e-09 and inverseubval<1.0e-06:
00052         denomitor=1.0e-09
00053         unitstring='/kb'
00054         return (float(inverseubval)/float(denomitor),unitstring)
00055     if inverseubval>=1.0e-06 and inverseubval<1.0e-03:
00056         denomitor=1.0e-06
00057         unitstring='/b'
00058         return (float(inverseubval)/float(denomitor),unitstring)
00059     if inverseubval>=1.0e-03 and inverseubval<1.0:
00060         denomitor=1.0e-03
00061         unitstring='/mb'
00062         return (float(inverseubval)/float(denomitor),unitstring)
00063     if inverseubval>=1.0 and inverseubval<1.0e3:
00064         unitstring='/ub'
00065         return (inverseubval,unitstring)
00066     if inverseubval>=1.0e3 and inverseubval<1.0e06:
00067         denomitor=1.0e3
00068         unitstring='/nb'
00069         return (float(inverseubval)/float(denomitor),unitstring)
00070     if inverseubval>=1.0e6 and inverseubval<1.0e9:
00071         denomitor=1.0e6
00072         unitstring='/pb'
00073         return (float(inverseubval)/float(denomitor),unitstring)
00074     if inverseubval>=1.0e9 and inverseubval<1.0e12:
00075         denomitor=1.0e9
00076         unitstring='/fb'
00077         return (float(inverseubval)/float(denomitor),unitstring)
00078     if inverseubval>=1.0e12 and inverseubval<1.0e15:
00079         denomitor=1.0e12
00080         unitstring='/ab'
00081         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 174 of file CommonUtil.py.

00175                                    :
00176     """return range including the stop value
00177     """
00178     v=start
00179     while v<stop:
00180         yield v
00181         v+=step
00182     if v>=stop:
00183         yield stop
        
def CommonUtil::is_floatstr (   s)
test if a string can be converted to a float

Definition at line 112 of file CommonUtil.py.

00113                   :
00114     """
00115     test if a string can be converted to a float
00116     """
00117     try:
00118         float(s)
00119         return True
00120     except ValueError:
        return False
def CommonUtil::is_intstr (   s)
test if a string can be converted to a int

Definition at line 104 of file CommonUtil.py.

00105                 :
00106     """test if a string can be converted to a int
00107     """
00108     try:
00109         int(s)
00110         return True
00111     except ValueError:
        return False
def CommonUtil::lumiUnitForPrint (   t)
input : largest lumivalue
output: (unitstring,denomitor)

Definition at line 14 of file CommonUtil.py.

00015                        :
00016     '''
00017     input : largest lumivalue
00018     output: (unitstring,denomitor)
00019     '''
00020     unitstring='/ub'
00021     denomitor=1.0
00022     if t>=1.0e3 and t<1.0e06:
00023         denomitor=1.0e3
00024         unitstring='/nb'
00025     elif t>=1.0e6 and t<1.0e9:
00026         denomitor=1.0e6
00027         unitstring='/pb'
00028     elif t>=1.0e9 and t<1.0e12:
00029         denomitor=1.0e9
00030         unitstring='/fb'
00031     elif t>=1.0e12 and t<1.0e15:
00032         denomitor=1.0e12
00033         unitstring='/ab'
00034     elif t<1.0 and t>=1.0e-3: #left direction
00035         denomitor=1.0e-03
00036         unitstring='/mb'
00037     elif t<1.0e-03 and t>=1.0e-06:
00038         denomitor=1.0e-06
00039         unitstring='/b'
00040     elif t<1.0e-06 and t>=1.0e-09:
00041         denomitor=1.0e-9
00042         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 138 of file CommonUtil.py.

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

Definition at line 204 of file CommonUtil.py.

00205                            :
00206     '''
00207     Inputs:
00208     inputarray: a python array
00209     '''
00210     result=coral.Blob()
00211     result.write(iarray.tostring())
00212     return result

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

Definition at line 228 of file CommonUtil.py.

00229                                              :
00230     '''
00231     pack list of string of comma separated large string CLOB
00232     '''
00233     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 144 of file CommonUtil.py.

00145                           :
00146     """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
00147        Note:the print value of result number may appear signed, if the sign bit is used.
00148     """
00149     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 82 of file CommonUtil.py.

00083                  :
00084     """
00085     yield item i and item i+1 in lst. e.g.
00086     (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
00087     
00088     from https://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
00089     """
00090     if not len(lst): return
00091     #yield None, lst[0]
00092     for i in range(len(lst)-1):
00093         yield lst[i], lst[i+1]
    yield lst[-1], None
def CommonUtil::splitlistToRangeString (   inPut)

Definition at line 240 of file CommonUtil.py.

00241                                   :
00242     result = []
00243     first = inPut[0]
00244     last = inPut[0]
00245     result.append ([inPut[0]])
00246     counter = 0
00247     for i in inPut[1:]:
00248         if i == last+1:
00249             result[counter].append (i)
00250         else:
00251             counter += 1
00252             result.append ([i])
00253         last = i
00254     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 160 of file CommonUtil.py.

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

Definition at line 164 of file CommonUtil.py.

00165                      :
00166     """convert 64bit timestamp to Universal Time in string format
00167     """
00168     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 184 of file CommonUtil.py.

00185                             :
00186    '''
00187    convert json like string to legal json string
00188    add double quote around json keys if they are not there, change single quote to double quote around keys
00189    '''
00190    strresult=inputstring.strip()
00191    strresult=re.sub("\s+","",strresult)
00192    try:
00193        mydict=ast.literal_eval(strresult)
00194    except SyntaxError:
00195        print 'error in converting string to dict'
00196        raise
00197    result={}
00198    for k,v in mydict.items():
00199        if not isinstance(k,str):
00200            result[str(k)]=v
00201        else:
00202            result[k]=v
00203    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 130 of file CommonUtil.py.

00131                                       :
00132     """
00133     transposing list of lists
00134     from https://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
00135     """
00136     if not lists: return []
00137     #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 150 of file CommonUtil.py.

00151              :
00152     """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
00153     """
00154     high=i>>32
00155     low=i&0xFFFFFFFF
    return(high,low)
def CommonUtil::unpackBlobtoArray (   iblob,
  itemtypecode 
)
Inputs:
iblob: coral.Blob
itemtypecode: python array type code 

Definition at line 213 of file CommonUtil.py.

00214                                          :
00215     '''
00216     Inputs:
00217     iblob: coral.Blob
00218     itemtypecode: python array type code 
00219     '''
00220     if itemtypecode not in ['c','b','B','u','h','H','i','I','l','L','f','d']:
00221         raise RuntimeError('unsupported typecode '+itemtypecode)
00222     result=array.array(itemtypecode)
00223     blobstr=iblob.readline()
00224     if not blobstr :
00225         return None
00226     result.fromstring(blobstr)
00227     return result

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

Definition at line 234 of file CommonUtil.py.

00235                                            :
00236     '''
00237     unpack a large string to list of string
00238     '''
00239     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 156 of file CommonUtil.py.

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

Definition at line 169 of file CommonUtil.py.

00170                    :
00171     """unpack 64bit lumiid to dictionary {'run','lumisection'}
00172     """
00173     j=unpack(i)
    return {'run':j[0],'lumisection':j[1]}

Variable Documentation

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

Definition at line 258 of file CommonUtil.py.

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

Definition at line 290 of file CommonUtil.py.

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

Definition at line 262 of file CommonUtil.py.

tuple CommonUtil::myblob = packArraytoBlob(a)

Definition at line 287 of file CommonUtil.py.

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

Definition at line 256 of file CommonUtil.py.

Referenced by edm::contextual_find().

tuple CommonUtil::pp = json.loads(result)

Definition at line 271 of file CommonUtil.py.

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

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