/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes | Properties
DybDbi::mapper::Mapper Class Reference

List of all members.

Public Member Functions

def __init__
def check_kv
def is_consistent
def is_complete
def is_valid
def smry
def automap
def convert_csv2dbi
def __call__
def __str__
def __repr__

Public Attributes

 cls
 csv_fields
 dbi_attrs
 remove names beginning "_"
 dbi2csv
 avoid the TObjString
 inconsistent
 key name reverse mapping
 csv2type

Properties

 miss = property(lambda self:filter( lambda _:self[_]==None, self ))

Detailed Description

Establish the mapping between sets of fields (such as csv fields) and dbi attributes, usage::
 
    ckf = ['status', '_srcline', 'afterPulse', 'sigmaSpe', 'pmtID', 'efficiency', 'darkRate', '_hasblank', 'prePulse', 'speLow', 'timeOffset', 'timeSpread', 'speHigh', 'description']
    mpr = Mapper( GCalibPmtSpec, ckf , afterPulse="AfterPulseProb", sigmaSpe="SigmaSpeHigh", prePulse="PrePulseProb", description="Describ" )
    print mpr

If a mapping cannot be made, an exception is thrown that reports the partial mapping constructed.

The automapping performed is dumb by design, only case insensitively identical names are
auto mapped. Other differences between csv field names and dbi attributes must be manually provided
in the keyword arguments.

The string codetype from the spec is promoted into the corresponding python type, to enable
conversion of the csv dict (comprised of all strings) into a dbi dict with appropriate types
for the values.   

Definition at line 3 of file mapper.py.


Constructor & Destructor Documentation

def DybDbi::mapper::Mapper::__init__ (   self,
  cls,
  csv_fields,
  kwargs 
)

Definition at line 22 of file mapper.py.

00023                                                   :
00024         self.cls = cls
00025 
00026         self.csv_fields = filter(lambda _:not _.startswith("_"), csv_fields)       ## remove names beginning "_" 
00027         self.dbi_attrs = map(str, cls.SpecKeys().aslist() )   ## avoid the TObjString   
00028         
00029         self.automap()
00030         self.update(kwargs)
00031 
00032         self.dbi2csv = dict([(kdbi,kcsv) for kcsv,kdbi in self.items() ])   ## key name reverse mapping
00033         
00034         sk = cls.SpecKeys().aslist()
00035         sm = cls.SpecMap().asdod()
00036 
00037         self.inconsistent = []
00038         self.check_kv( self.keys()   , self.csv_fields , "csv_fields" )
00039         self.check_kv( self.values() , self.dbi_attrs ,  "dbi_attrs")
00040  
00041         if not self.is_consistent():
00042             print self
00043             raise Exception("inconsistent info supplied to construct mapper: %r ",self)
00044         if not self.is_complete():
00045             print self
00046             raise Exception("insufficient info to construct mapper, partial mapping: %r ",self)
00047 
00048         def typename2type( typename ):
00049            """
00050            Converts the string representation of a type into a callable that can convert 
00051            the string representation of a value of the type into an instance of the type
00052            """
00053            if typename == "int":
00054                return int
00055            elif typename == "float" or typename == "double":
00056                return float
00057            elif typename.startswith('DayaBay::'):
00058                typ = getattr( DybDbi , typename[9:] )
00059                return lambda _:typ(int(_))
00060            else:
00061                return str
00062 
00063         self.csv2type = dict([(self.dbi2csv[k],typename2type(str(sm[k]['codetype']))) for k in sk ])


Member Function Documentation

def DybDbi::mapper::Mapper::check_kv (   self,
  kvl,
  expect,
  name 
)
Check the keys/values are in the expected list 

Definition at line 64 of file mapper.py.

00065                                            :
00066         """
00067         Check the keys/values are in the expected list 
00068         """
00069         for _ in kvl:
00070             if _ not in expect:
00071                  self.inconsistent.append(" %s is not a %s " % ( _, name )  )  

def DybDbi::mapper::Mapper::is_consistent (   self)

Definition at line 72 of file mapper.py.

00073                            :
        return len(self.inconsistent) == 0  
def DybDbi::mapper::Mapper::is_complete (   self)

Definition at line 74 of file mapper.py.

00075                          :
        return len(self.miss) == 0 
def DybDbi::mapper::Mapper::is_valid (   self)

Definition at line 76 of file mapper.py.

00077                       :
00078         return self.is_consistent() and self.is_complete()

def DybDbi::mapper::Mapper::smry (   self)

Definition at line 79 of file mapper.py.

00080                   :
00081         print repr(self)
00082         print self

def DybDbi::mapper::Mapper::automap (   self)
Basic auto mapping, using case insensitive comparison 
and yielding case sensitive mapping from csv fields to dbi attributes

The index of the csv fieldname in the dbi attribute list is 
found with case insensitive string comparison 

Definition at line 83 of file mapper.py.

00084                      :
00085         """
00086         Basic auto mapping, using case insensitive comparison 
00087         and yielding case sensitive mapping from csv fields to dbi attributes
00088 
00089         The index of the csv fieldname in the dbi attribute list is 
00090         found with case insensitive string comparison 
00091 
00092         """
00093         allcaps = lambda _:_.upper()
00094         for ck in self.csv_fields:
00095            try:
00096                idx = map(allcaps, self.dbi_attrs).index( allcaps(ck) )
00097            except ValueError:
00098                idx = -1
00099            if idx > -1: 
00100                self[ck] = self.dbi_attrs[idx]     
00101            else:
00102                self[ck] = None

def DybDbi::mapper::Mapper::convert_csv2dbi (   self,
  dcsv 
)
Translate dict with csv fieldnames into dict with dbi attr names
and appropiate types for insertion into the DBI Row cls instance 

Definition at line 105 of file mapper.py.

00106                                    :
00107         """
00108         Translate dict with csv fieldnames into dict with dbi attr names
00109         and appropiate types for insertion into the DBI Row cls instance 
00110         """
00111         return dict([(self[k], self.csv2type[k](dcsv[k]) ) for k in filter( lambda _:not _.startswith("_"), dcsv) ])

def DybDbi::mapper::Mapper::__call__ (   self,
  dcsv 
)

Definition at line 112 of file mapper.py.

00113                             :
00114         return self.convert_csv2dbi(dcsv)

def DybDbi::mapper::Mapper::__str__ (   self)

Definition at line 115 of file mapper.py.

00116                      :
00117          return "\n".join( ["  csv fields\n   %r " % self.csv_fields , 
00118                             "    dbi atts\n   %r " % self.dbi_attrs  , 
00119                             "     csv2dbi\n   %r " % self  ,
00120                             "     missing\n   %r " % self.miss ,
00121                             "inconsistencies:\n%s" % "\n   ".join(self.inconsistent) ])

def DybDbi::mapper::Mapper::__repr__ (   self)

Definition at line 122 of file mapper.py.

00123                       :
00124          return ",".join(["%s=%s " % (k,v) for k,v in self.items()])
00125 
00126 


Member Data Documentation

Definition at line 22 of file mapper.py.

Definition at line 22 of file mapper.py.

remove names beginning "_"

Definition at line 23 of file mapper.py.

avoid the TObjString

Definition at line 24 of file mapper.py.

key name reverse mapping

Definition at line 25 of file mapper.py.

Definition at line 28 of file mapper.py.


Property Documentation

DybDbi::mapper::Mapper::miss = property(lambda self:filter( lambda _:self[_]==None, self )) [static]

Definition at line 103 of file mapper.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:57:24 for DybDbi by doxygen 1.7.4