def test_container(): table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) actual = etl.wrap(table)[0] expect = ('foo', 'bar') eq_(expect, actual) actual = len(etl.wrap(table)) expect = 4 eq_(expect, actual)
def test_repr_html(): table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) expect = u"""<table class='petl'> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td style='text-align: right'>2</td> </tr> <tr> <td>c</td> <td style='text-align: right'>2</td> </tr> </tbody> </table> """ actual = etl.wrap(table)._repr_html_() for l1, l2 in zip(expect.split('\n'), actual.split('\r\n')): eq_(l1, l2)
def test_repr_html_limit(): table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) # lower repr limit etl.repr_html_limit = 2 expect = u"""<table class='petl'> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td style='text-align: right'>2</td> </tr> </tbody> </table> <p><strong>...</strong></p> """ actual = etl.wrap(table)._repr_html_() for l1, l2 in zip(expect.split('\n'), actual.split('\r\n')): eq_(l1, l2)
def test_repr_html_limit(): table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) # lower repr limit etl.repr_html_limit = 2 expect = u"""<table class='petl'> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td style='text-align: right'>2</td> </tr> </tbody> </table> <p><strong>...</strong></p> """ actual = etl.wrap(table)._repr_html_() for l1, l2 in zip(expect.split('\n'), actual.split('\r\n')): print l1, l2 eq_(l1, l2)
def test_repr_html(): table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) expect = u"""<table class='petl'> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td style='text-align: right'>2</td> </tr> <tr> <td>c</td> <td style='text-align: right'>2</td> </tr> </tbody> </table> """ actual = etl.wrap(table)._repr_html_() for l1, l2 in zip(expect.split('\n'), actual.split('\r\n')): print l1, l2 eq_(l1, l2)
def test_basics(): t1 = (('foo', 'bar'), ('A', 1), ('B', 2)) w1 = etl.wrap(t1) eq_(('foo', 'bar'), w1.header()) eq_(petl.header(w1), w1.header()) ieq((('A', 1), ('B', 2)), w1.data()) ieq(petl.data(w1), w1.data()) w2 = w1.cut('bar', 'foo') expect2 = (('bar', 'foo'), (1, 'A'), (2, 'B')) ieq(expect2, w2) ieq(petl.cut(w1, 'bar', 'foo'), w2) w3 = w1.cut('bar', 'foo').cut('foo', 'bar') ieq(t1, w3)
# -*- coding: utf-8 -*- # <nbformat>3.0</nbformat> # <markdowncell> # Notes supporting [issue #256](https://github.com/alimanfoo/petl/issues/256). # <codecell> import petl.interactive as etl # <codecell> t1 = etl.wrap([['foo', 'bar'], [1, 'a'], [2, 'b']]) t1 # <codecell> t2 = etl.wrap([['foo', 'bar'], [1, 'a'], [2, 'c']]) t2 # <codecell> t3 = etl.merge(t1, t2, key='foo') t3 # <markdowncell> # The problem with the above is that you cannot tell from inspecting *t3* alone which conflicting value comes from which source. # # A workaround as suggested by [@pawl](https://github.com/pawl) is to use the [*conflicts()*](http://petl.readthedocs.org/en/latest/#petl.conflicts) function, e.g.:
# -*- coding: utf-8 -*- # <nbformat>3.0</nbformat> # <markdowncell> # Notes supporting [issue #256](https://github.com/alimanfoo/petl/issues/256). # <codecell> import petl.interactive as etl # <codecell> t1 = etl.wrap([["foo", "bar"], [1, "a"], [2, "b"]]) t1 # <codecell> t2 = etl.wrap([["foo", "bar"], [1, "a"], [2, "c"]]) t2 # <codecell> t3 = etl.merge(t1, t2, key="foo") t3 # <markdowncell> # The problem with the above is that you cannot tell from inspecting *t3* alone which conflicting value comes from which source. # # A workaround as suggested by [@pawl](https://github.com/pawl) is to use the [*conflicts()*](http://petl.readthedocs.org/en/latest/#petl.conflicts) function, e.g.:
class CustomTransformer(object): def __init__(self, source): self.source = source def __iter__(self): it = iter(self.source) # construct new header source_fields = it.next() out_fields = ('category',) + tuple(source_fields) yield out_fields # transform data current_category = None for row in it: if len(row) == 1: current_category = row[0] else: yield (current_category,) + tuple(row) # <codecell> tbl5 = CustomTransformer(tbl1) # <codecell> # just so it formats nicely as HTML in the notebook... etl.wrap(tbl5)