Exemplo n.º 1
0
 def _repr_html_(self):
     if InteractiveWrapper.repr_index_header:
         indexed_header = ['%s|%s' % (i, f) for (i, f) in enumerate(petl.util.header(self))]
         target = petl.transform.setheader(self, indexed_header)
     else:
         target = self
     buf = StringSource()
     tohtml(target, buf)
     return buf.getvalue()
Exemplo n.º 2
0
def repr_html(tbl, index_header=None, representation=unicode, caption=None, encoding='utf-8'):
    if index_header is None:
        index_header = repr_index_header  # use default
    if index_header:
        indexed_header = [u'%s|%s' % (i, f) for (i, f) in enumerate(petl.util.header(tbl))]
        target = petl.transform.setheader(tbl, indexed_header)
    else:
        target = tbl
    buf = StringSource()
    if representation is unicode:
        touhtml(target, buf, caption=caption, encoding=encoding)
    else:
        tohtml(target, buf, representation=representation, caption=caption)
    return buf.getvalue()
Exemplo n.º 3
0
def repr_html(tbl,
              limit=None,
              index_header=None,
              representation=unicode,
              caption=None,
              encoding='utf-8'):

    # add column indices to header?
    if index_header is None:
        index_header = repr_index_header  # use default
    if index_header:
        indexed_header = [
            u'%s|%s' % (i, f) for (i, f) in enumerate(petl.util.header(tbl))
        ]
        target = petl.transform.setheader(tbl, indexed_header)
    else:
        target = tbl

    # limit number of rows output?
    # N.B., limit is max number of data rows (not including header)
    if limit is None:
        # use default
        limit = repr_html_limit

    overflow = False
    if limit > 0:
        # try reading one more than the limit, to see if there are more rows
        target = list(islice(target, 0, limit + 2))
        if len(target) > limit + 1:
            overflow = True
            target = target[:-1]
    else:
        # render the entire table
        pass

    # write to html string
    buf = StringSource()
    if representation is unicode:
        touhtml(target, buf, caption=caption, encoding=encoding)
    else:
        tohtml(target, buf, representation=representation, caption=caption)

    if overflow:
        return buf.getvalue() + u'<p><strong>...</strong></p>'
    else:
        return buf.getvalue()
Exemplo n.º 4
0
def repr_html(tbl,
              index_header=None,
              representation=unicode,
              caption=None,
              encoding='utf-8'):
    if index_header is None:
        index_header = repr_index_header  # use default
    if index_header:
        indexed_header = [
            u'%s|%s' % (i, f) for (i, f) in enumerate(petl.util.header(tbl))
        ]
        target = petl.transform.setheader(tbl, indexed_header)
    else:
        target = tbl
    buf = StringSource()
    if representation is unicode:
        touhtml(target, buf, caption=caption, encoding=encoding)
    else:
        tohtml(target, buf, representation=representation, caption=caption)
    return buf.getvalue()
Exemplo n.º 5
0
def test_StringSource():

    table1 = (('foo', 'bar'), ('a', '1'), ('b', '2'), ('c', '2'))

    # test writing to a string buffer
    ss = StringSource()
    tocsv(table1, ss)
    expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n"
    actual = ss.getvalue()
    eq_(expect, actual)

    # test reading from a string buffer
    table2 = fromcsv(StringSource(actual))
    ieq(table1, table2)
    ieq(table1, table2)

    # test appending
    appendcsv(table1, ss)
    actual = ss.getvalue()
    expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n"
    eq_(expect, actual)
Exemplo n.º 6
0
def repr_html(tbl, limit=None, index_header=None, representation=unicode,
              caption=None, encoding='utf-8'):

    # add column indices to header?
    if index_header is None:
        index_header = repr_index_header  # use default
    if index_header:
        indexed_header = [u'%s|%s' % (i, f) for (i, f) in enumerate(petl.util.header(tbl))]
        target = petl.transform.setheader(tbl, indexed_header)
    else:
        target = tbl

    # limit number of rows output?
    # N.B., limit is max number of data rows (not including header)
    if limit is None:
        # use default
        limit = repr_html_limit

    overflow = False
    if limit > 0:
        # try reading one more than the limit, to see if there are more rows
        target = list(islice(target, 0, limit+2))
        if len(target) > limit+1:
            overflow = True
            target = target[:-1]
    else:
        # render the entire table
        pass

    # write to html string
    buf = StringSource()
    if representation is unicode:
        touhtml(target, buf, caption=caption, encoding=encoding)
    else:
        tohtml(target, buf, representation=representation, caption=caption)

    if overflow:
        return buf.getvalue() + u'<p><strong>...</strong></p>'
    else:
        return buf.getvalue()
Exemplo n.º 7
0
def test_StringSource():
    
    table1 = (('foo', 'bar'),
             ('a', '1'),
             ('b', '2'),
             ('c', '2'))

    # test writing to a string buffer
    ss = StringSource()
    tocsv(table1, ss)
    expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n"
    actual = ss.getvalue()
    eq_(expect, actual)

    # test reading from a string buffer
    table2 = fromcsv(StringSource(actual))
    ieq(table1, table2)
    ieq(table1, table2)

    # test appending
    appendcsv(table1, ss)
    actual = ss.getvalue()
    expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n"
    eq_(expect, actual)
Exemplo n.º 8
0
 def _repr_html_(self):
     buf = StringSource()
     tohtml(self, buf)
     return buf.getvalue()
Exemplo n.º 9
0
Apples
Cortland,0.30,24
Red Delicious,0.40,24
Oranges
Navel,0.50,12
"""

# <codecell>

import petl.interactive as etl
from petl.io import StringSource

# <codecell>

tbl1 = (etl
    .fromcsv(StringSource(data))
)
tbl1

# <headingcell level=2>

# Option 1 - using existing petl functions

# <codecell>

def make_room_for_category(row):
    if len(row) == 1:
        return (row[0], 'X', 'X', 'X')
    else:
        return (None,) + tuple(row)
Exemplo n.º 10
0
 def _repr_html_(self):
     buf = StringSource()
     tohtml(self, buf)
     return buf.getvalue()