CMS 3D CMS Logo

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.

00044                  :
00045     """
00046     report the number of duplicates in a python list
00047     """
00048     from collections import defaultdict
00049     tally=defaultdict(int)
00050     for x in l:
00051         tally[x]+=1
    return tally.items()
def CommonUtil::findInList (   mylist,
  element 
)
check if an element is in the list

Definition at line 16 of file CommonUtil.py.

00017                               :
00018     """
00019     check if an element is in the list
00020     """
00021     pos=-1
00022     try:
00023         pos=mylist.index(element)
00024     except ValueError:
00025         pos=-1
    return pos!=-1
def CommonUtil::inclusiveRange (   start,
  stop,
  step 
)
return range including the stop value

Definition at line 95 of file CommonUtil.py.

00096                                    :
00097     """return range including the stop value
00098     """
00099     v=start
00100     while v<stop:
00101         yield v
00102         v+=step
00103     if v>=stop:
00104         yield stop
        
def CommonUtil::is_floatstr (   s)
test if a string can be converted to a float

Definition at line 34 of file CommonUtil.py.

00035                   :
00036     """
00037     test if a string can be converted to a float
00038     """
00039     try:
00040         float(s)
00041         return True
00042     except ValueError:
        return False
def CommonUtil::is_intstr (   s)
test if a string can be converted to a int

Definition at line 26 of file CommonUtil.py.

00027                 :
00028     """test if a string can be converted to a int
00029     """
00030     try:
00031         int(s)
00032         return True
00033     except ValueError:
        return False
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.

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

Definition at line 125 of file CommonUtil.py.

00126                            :
00127     '''
00128     Inputs:
00129     inputarray: a python array
00130     '''
00131     result=coral.Blob()
00132     result.write(iarray.tostring())
00133     return result

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

Definition at line 146 of file CommonUtil.py.

00147                                              :
00148     '''
00149     pack list of string of comma separated large string CLOB
00150     '''
00151     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 65 of file CommonUtil.py.

00066                           :
00067     """pack high,low 32bit unsigned int to one unsigned 64bit long long in string format
00068        Note:the print value of result number may appear signed, if the sign bit is used.
00069     """
00070     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 http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration

Definition at line 4 of file CommonUtil.py.

00005                  :
00006     """
00007     yield item i and item i+1 in lst. e.g.
00008     (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
00009     
00010     from http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration
00011     """
00012     if not len(lst): return
00013     #yield None, lst[0]
00014     for i in range(len(lst)-1):
00015         yield lst[i], lst[i+1]
    yield lst[-1], None
def CommonUtil::timeStamptoDate (   i)
convert 64bit timestamp to local date in string format

Definition at line 81 of file CommonUtil.py.

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

Definition at line 85 of file CommonUtil.py.

00086                      :
00087     """convert 64bit timestamp to Universal Time in string format
00088     """
00089     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 105 of file CommonUtil.py.

00106                             :
00107    '''
00108    convert json like string to legal json string
00109    add double quote around json keys if they are not there, change single quote to double quote around keys
00110    '''
00111    strresult=inputstring.strip()
00112    strresult=re.sub("\s+","",strresult)
00113    try:
00114        mydict=ast.literal_eval(strresult)
00115    except SyntaxError:
00116        print 'error in converting string to dict'
00117        raise
00118    result={}
00119    for k,v in mydict.items():
00120        if not isinstance(k,str):
00121            result[str(k)]=v
00122        else:
00123            result[k]=v
00124    return re.sub("'",'"',str(result))

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.

00053                                       :
00054     """
00055     transposing list of lists
00056     from http://code.activestate.com/recipes/410687-transposing-a-list-of-lists-with-different-lengths/
00057     """
00058     if not lists: return []
    return map(lambda *row: [elem or defaultval for elem in row], *lists)
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.

00072              :
00073     """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
00074     """
00075     high=i>>32
00076     low=i&0xFFFFFFFF
    return(high,low)
def CommonUtil::unpackBlobtoArray (   iblob,
  itemtypecode 
)
Inputs:
iblob: coral.Blob
itemtypecode: python array type code 

Definition at line 134 of file CommonUtil.py.

00135                                          :
00136     '''
00137     Inputs:
00138     iblob: coral.Blob
00139     itemtypecode: python array type code 
00140     '''
00141     if itemtypecode not in ['c','b','B','u','h','H','i','I','l','L','f','d']:
00142         raise RuntimeError('unsupported typecode '+itemtypecode)
00143     result=array.array(itemtypecode)
00144     result.fromstring(iblob.readline())
00145     return result

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

Definition at line 152 of file CommonUtil.py.

00153                                            :
00154     '''
00155     unpack a large string to list of string
00156     '''
00157     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 77 of file CommonUtil.py.

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

Definition at line 90 of file CommonUtil.py.

00091                    :
00092     """unpack 64bit lumiid to dictionary {'run','lumisection'}
00093     """
00094     j=unpack(i)
    return {'run':j[0],'lumisection':j[1]}

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']
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.