Exemple #1
0
 def _to_csv(self,fd,sep=None,with_headers=True,with_data=True):
   dialect = self.get_csv_dialect()
   csv_args = {}
   if sep is not None:
     csv_args['delimiter'] = sep
   writer = csv.writer(fd,dialect=dialect,**csv_args)
   if with_headers:
     writer.writerow(self.keys())
   if with_data:
     values = [export_string(value) for value in self.values()]
     lentbl = max([len(col) for col in values])
     for rownum in range(lentbl):
       out = [str(col[rownum]) for col in values]
       writer.writerow(out)
Exemple #2
0
 def _to_xml_elementtree(self,**kwargs):
   cfg = {
     'tbl_element':  'table',
     'row_element':  'row',
   }
   cfg.update(kwargs)
   from xml.etree.cElementTree import Element, SubElement, ElementTree
   out = Element(cfg['tbl_element'])
   out.text = out.tail = '\n'
   values = [export_string(value) for value in self.values()]
   cols = []
   for col in values:
     if col is None:
       col = [col]*len(self)
     cols.append(col)
   row_element = cfg['row_element']
   keys = self.keys()
   for rownum in range(len(self)):
     datarow = SubElement(out,row_element)
     datarow.tail = '\n'
     for key,col in zip(keys,cols):
       datacell = SubElement(datarow,key)
       datacell.text = str(col[rownum])
   return ElementTree(out)
Exemple #3
0
 def _to_html_elementtree(self,tblattr=None,method=str):
   if tblattr is None:
     tblattr = {'border':'1'}
   from xml.etree.cElementTree import Element, SubElement, ElementTree
   out = Element('table',**tblattr)
   out.text = out.tail = '\n'
   headings = SubElement(out,'tr')
   headings.tail = '\n'
   for key in self.keys():
     heading = SubElement(headings,'th')
     heading.text=method(key)
   values = [export_string(value) for value in self.values()]
   cols = []
   for col in values:
     if col is None:
       col = [col]*len(self)
     cols.append(col)
   for rownum in range(len(self)):
     datarow = SubElement(out,'tr')
     datarow.tail = '\n'
     for col in cols:
       datacell = SubElement(datarow,'td')
       datacell.text = method(col[rownum])
   return ElementTree(out)
Exemple #4
0
  def _iter_pptbl(self,sep='|',xsep='+',xfill='-',method=str,text='right',rhead=0):
    sizes = []
    values = [export_string(value) for value in self.values()]
    for (name,col) in zip(self.keys(),values):
      # this is a lookahead on entire dataset to find max
      # print size for each field - try to short circuit where possible
      namesize = len(method(name))
      fldsize = None
      try:
        if is_char_array(col):
          fldsize = col.maxLen()
        elif col is None:
          fldsize = len(method(None))
        else:
          natype = get_format(col)
          is_float = natype.startswith('Float')
          if not is_float:
            nasize = col.itemsize()
            if nasize*3 <= namesize:
              fldsize = namesize
            else:
              minmax = min(col),max(col)
              fldsize = max(len(prtfld) for prtfld in it.imap(method,minmax))
      except:
        pass

      if fldsize is None:
        # no shortcut found, convert entire column
        try:
          fldsize = max(len(prtfld) for prtfld in it.imap(method,col))
        except:
          fldsize = 0
      sizes.append(max(namesize,fldsize))

    xbar = xsep + xsep.join([w*xfill for w in sizes]) + xsep
    out = [name.center(w) for (w,name) in zip(sizes,self.keys())]
    headline = sep + sep.join(out) + sep

    if text == 'left':
      for i,col in enumerate(values):
        try:
          if is_char_array(col):
            sizes[i] = -sizes[i]
        except:
          pass
    formats = ['%%%ss' % s for s in sizes]

    cols = []
    for col in values:
      if col is None:
        col = [col]*len(self)
      cols.append(col)

    # finally, yield the result

    if not rhead:
      # if not repeating header, yield it first
      yield xbar
      yield headline
      yield xbar

    for rownum in range(len(self)):
      if rhead and rownum % rhead == 0:
        # repeat header at specified interval
        yield xbar
        yield headline
        yield xbar
      out = [(fmt % method(col[rownum])) for (fmt,col) in zip(formats,cols)]
      yield sep + sep.join(out) + sep
    yield xbar