| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

DybDbi::csvrw::CSV Class Reference

List of all members.

Public Member Functions

def __init__
def read
def is_valid
def blanks
def smry
def __repr__
def write

Public Attributes

 src

Static Public Attributes

tuple defaults = dict( delimiter="\t", prefix="#Table", descmarker="#[]", synth="_srcline" )
tuple path = property(lambda self:os.path.expanduser(os.path.expandvars(self._path)))
tuple delimiter = property(lambda self:self.kwargs.get('delimiter',self.defaults.get('delimiter',None)))
tuple fieldnames = property(_fieldnames, doc=_fieldnames.__doc__)
tuple descline = property(lambda self:"#" + self.delimiter.join(self.fieldnames) )

Private Member Functions

def _fieldnames

Detailed Description

Reader/writer for .csv files. The contents are stored as a list of dicts.

:param delimiter: csv field divider
:param prefix:  string start of comment lines to be ignored, default #Table
:param descmarker: strings used to identify the field description line 
:param synth: when defined, add extra field with this name to hold the csv source line number 


Read usage example::

    src = CSV("$DBWRITERROOT/share/DYB_MC_AD1.txt", delimiter="\\t" )
    src.read()
    for d in src:
        print d

    len(src)
    src[0]
    src[-1]
    src.fieldnames


On reading an invalid CSV an exception, with error report, is raised::

    src = CSV("$DBWRITERROOT/share/DYB_SAB_AD1.txt", delimiter="\\t" )
    src.read()


Handling of common csv incorrectnesses is made:

#. description line fixed up to conform to the delimiter
#. description line extraneous characters removed (other than fieldnames and delimiters)
#. removes comments 
 
Write usage example, field names are obtained from the dict keys::

     out = CSV("/tmp/demo.csv", delimiter="\\t" )
     for d in list_of_dict_datasource:
         out.append(d)
     out.write()

Definition at line 3 of file csvrw.py.


Member Function Documentation

def DybDbi::csvrw::CSV::__init__ (   self,
  path,
  kwargs 
)

       

Definition at line 7 of file csvrw.py.

00007     :param delimiter: csv field divider
00008     :param prefix:  string start of comment lines to be ignored, default #Table
00009     :param descmarker: strings used to identify the field description line 
00010     :param synth: when defined, add extra field with this name to hold the csv source line number 
00011 
00012 
00013     Read usage example::

def DybDbi::csvrw::CSV::_fieldnames (   self  )  [private]

If fieldnames keyword argument is supplied return that otherwise
return the names of the keys in the first contained dict.  In order to
control the order of fields, the argument has to be specified.

Definition at line 14 of file csvrw.py.

00017                     :
00018             print d
00019 
00020         len(src)
00021         src[0]
00022         src[-1]
00023         src.fieldnames
00024 
00025 
00026     On reading an invalid CSV an exception, with error report, is raised::

def DybDbi::csvrw::CSV::read (   self  ) 

Definition at line 32 of file csvrw.py.

00032                                                   :
00033 
00034     #. description line fixed up to conform to the delimiter
00035     #. description line extraneous characters removed (other than fieldnames and delimiters)
00036     #. removes comments 
00037  
00038     Write usage example, field names are obtained from the dict keys::
00039 
00040          out = CSV("/tmp/demo.csv", delimiter="\\t" )
00041          for d in list_of_dict_datasource:
00042              out.append(d)
00043          out.write()
00044 
00045     """
00046     defaults = dict( delimiter="\t", prefix="#Table", descmarker="#[]", synth="_srcline" )
00047 
00048     def __init__(self, path, **kwargs ):
00049         """
00050        """ 
00051         self._path = path
00052         self.kwargs = kwargs
00053         self.stat = dict( hasblank=0 )
00054 
    def _fieldnames(self):

def DybDbi::csvrw::CSV::is_valid (   self  ) 

Definition at line 55 of file csvrw.py.

00055                          :
00056         """
00057         If fieldnames keyword argument is supplied return that otherwise
        return the names of the keys in the first contained dict.  In order to

def DybDbi::csvrw::CSV::blanks (   self  ) 

Definition at line 58 of file csvrw.py.

00062               :
            return fns

def DybDbi::csvrw::CSV::smry (   self  ) 

Definition at line 61 of file csvrw.py.

00062               :
00063             return fns
00064         if len(self)>0:
00065             return self[0].keys()     
00066         return None
00067 
00068     path = property(lambda self:os.path.expanduser(os.path.expandvars(self._path)))
00069     delimiter = property(lambda self:self.kwargs.get('delimiter',self.defaults.get('delimiter',None)))
    fieldnames = property(_fieldnames, doc=_fieldnames.__doc__)

def DybDbi::csvrw::CSV::__repr__ (   self  ) 

Definition at line 70 of file csvrw.py.

00071                                    :"#" + self.delimiter.join(self.fieldnames) )
00072 

def DybDbi::csvrw::CSV::write (   self  ) 

Definition at line 73 of file csvrw.py.

00073                   :
00074         kwargs = self.kwargs
00075         delimiter = kwargs.pop('delimiter', self.defaults['delimiter'])
00076         prefix =    kwargs.pop('prefix', self.defaults['prefix'])
00077         descmarker = kwargs.pop('descmarker',self.defaults['descmarker'])
00078         synth =     kwargs.pop('synth',self.defaults['synth'])
00079 
00080         src = Source(open(self.path,"r"), delimiter=delimiter, prefix=prefix, descmarker=descmarker, synth=synth )
00081 
00082         for i,r in enumerate(csv.DictReader(src,delimiter=delimiter)):
00083             row = Entry(r)
00084             hasblank = row.hasblank
00085             if hasblank:
                self.stat['hasblank'] += 1 


Member Data Documentation

tuple DybDbi::csvrw::CSV::defaults = dict( delimiter="\t", prefix="#Table", descmarker="#[]", synth="_srcline" ) [static]

Definition at line 5 of file csvrw.py.

tuple DybDbi::csvrw::CSV::path = property(lambda self:os.path.expanduser(os.path.expandvars(self._path))) [static]

Definition at line 27 of file csvrw.py.

tuple DybDbi::csvrw::CSV::delimiter = property(lambda self:self.kwargs.get('delimiter',self.defaults.get('delimiter',None))) [static]

Definition at line 28 of file csvrw.py.

tuple DybDbi::csvrw::CSV::fieldnames = property(_fieldnames, doc=_fieldnames.__doc__) [static]

Definition at line 29 of file csvrw.py.

tuple DybDbi::csvrw::CSV::descline = property(lambda self:"#" + self.delimiter.join(self.fieldnames) ) [static]

Definition at line 30 of file csvrw.py.

DybDbi::csvrw::CSV::src

Definition at line 48 of file csvrw.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 Mon Apr 11 20:17:17 2011 for DybDbi by doxygen 1.4.7