Example #1
0
def test_repr_html():
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    expect = """<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('\n')):
        eq_(l1, l2)
Example #2
0
def test_values_container_convenience_methods():
    table = etl.wrap((('foo', 'bar'),
                      ('a', 1),
                      ('b', 2),
                      ('c', 2)))
    
    actual = table.values('foo').set()
    expect = {'a', 'b', 'c'}
    eq_(expect, actual)
    
    actual = table.values('foo').list()
    expect = ['a', 'b', 'c']
    eq_(expect, actual)
    
    actual = table.values('foo').tuple()
    expect = ('a', 'b', 'c')
    eq_(expect, actual)
    
    actual = table.values('bar').sum()
    expect = 5
    eq_(expect, actual)
    
    actual = table.data().dict()
    expect = {'a': 1, 'b': 2, 'c': 2}
    eq_(expect, actual)
Example #3
0
def test_appendcsv():

    data = (
        u"name,id\n"
        u"Արամ Խաչատրյան,1\n"
        u"Johann Strauß,2\n"
        u"Вагиф Сәмәдоғлу,3\n"
        u"章子怡,4\n"
    )
    fn = NamedTemporaryFile().name
    uf = io.open(fn, encoding='utf-8', mode='wt')
    uf.write(data)
    uf.close()

    tbl = ((u'name', u'id'),
           (u'ኃይሌ ገብረሥላሴ', 5),
           (u'ედუარდ შევარდნაძე', 6))
    appendcsv(tbl, fn, encoding='utf-8', lineterminator='\n')

    expect = (
        u"name,id\n"
        u"Արամ Խաչատրյան,1\n"
        u"Johann Strauß,2\n"
        u"Вагиф Сәмәдоғлу,3\n"
        u"章子怡,4\n"
        u"ኃይሌ ገብረሥላሴ,5\n"
        u"ედუარდ შევარდნაძე,6\n"
    )
    uf = io.open(fn, encoding='utf-8', mode='rt')
    actual = uf.read()
    eq_(expect, actual)
Example #4
0
def test_fieldnames():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = fieldnames(table)
    expect = ('foo', 'bar')
    eq_(expect, actual)

    class CustomField(object):

        def __init__(self, key, description):
            self.key = key
            self.description = description

        def __str__(self):
            return self.key

        def __repr__(self):
            return 'CustomField(%r, %r)' % (self.key, self.description)

    table = ((CustomField('foo', 'Get some foo.'),
              CustomField('bar', 'A lot of bar.')),
             ('a', 1),
             ('b', 2))
    actual = fieldnames(table)
    expect = ('foo', 'bar')
    eq_(expect, actual)
Example #5
0
def test_stringsource():
    tbl1 = (('foo', 'bar'),
            ('a', '1'),
            ('b', '2'),
            ('c', '2'))

    # test writing to a string buffer
    ss = StringSource()
    etl.tocsv(tbl1, ss)
    expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n"
    if not PY2:
        expect = expect.encode('ascii')
    actual = ss.getvalue()
    eq_(expect, actual)

    # test reading from a string buffer
    tbl2 = etl.fromcsv(StringSource(actual))
    ieq(tbl1, tbl2)
    ieq(tbl1, tbl2)

    # test appending
    etl.appendcsv(tbl1, 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"
    if not PY2:
        expect = expect.encode('ascii')
    eq_(expect, actual)
Example #6
0
def test_tohtml_caption():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', (1, 2)))
    f = NamedTemporaryFile(delete=False)
    tohtml(table, f.name, encoding='ascii', caption='my table',
           lineterminator='\n')

    # check what it did
    with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
        actual = o.read()
        expect = (
            u"<table class='petl'>\n"
            u"<caption>my table</caption>\n"
            u"<thead>\n"
            u"<tr>\n"
            u"<th>foo</th>\n"
            u"<th>bar</th>\n"
            u"</tr>\n"
            u"</thead>\n"
            u"<tbody>\n"
            u"<tr>\n"
            u"<td>a</td>\n"
            u"<td style='text-align: right'>1</td>\n"
            u"</tr>\n"
            u"<tr>\n"
            u"<td>b</td>\n"
            u"<td>(1, 2)</td>\n"
            u"</tr>\n"
            u"</tbody>\n"
            u"</table>\n"
        )
        eq_(expect, actual)
Example #7
0
def test_look_truncate():

    table = (('foo', 'bar'), ('abcd', 1234), ('bcde', 2345))

    actual = repr(look(table, truncate=3))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| 'ab | 123 |
+-----+-----+
| 'bc | 234 |
+-----+-----+
"""
    eq_(expect, actual)

    actual = repr(look(table, truncate=3, vrepr=str))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| abc | 123 |
+-----+-----+
| bcd | 234 |
+-----+-----+
"""
    eq_(expect, actual)
Example #8
0
def test_dictlookupone():

    t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3))

    try:
        dictlookupone(t1, 'foo', strict=True)
    except DuplicateKeyError:
        pass  # expected
    else:
        assert False, 'expected error'

    # relax
    actual = dictlookupone(t1, 'foo', strict=False)
    # first wins
    expect = {'a': {'foo': 'a', 'bar': 1}, 'b': {'foo': 'b', 'bar': 2}}
    eq_(expect, actual)

    t2 = (('foo', 'bar', 'baz'),
          ('a', 1, True),
          ('b', 2, False),
          ('b', 3, True),
          ('b', 3, False))

    # test compound key
    actual = dictlookupone(t2, ('foo', 'bar'), strict=False)
    expect = {('a', 1): {'foo': 'a', 'bar': 1, 'baz': True},
              ('b', 2): {'foo': 'b', 'bar': 2, 'baz': False},
              ('b', 3): {'foo': 'b', 'bar': 3, 'baz': True}}  # first wins
    eq_(expect, actual)
Example #9
0
def test_recordlookup():

    t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3))

    lkp = recordlookup(t1, 'foo')
    eq_([1], [r.bar for r in lkp['a']])
    eq_([2, 3], [r.bar for r in lkp['b']])
Example #10
0
def test_lookup():

    t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3))

    # lookup one column on another
    actual = lookup(t1, 'foo', 'bar')
    expect = {'a': [1], 'b': [2, 3]}
    eq_(expect, actual)

    # test default value - tuple of whole row
    actual = lookup(t1, 'foo')  # no value selector
    expect = {'a': [('a', 1)], 'b': [('b', 2), ('b', 3)]}
    eq_(expect, actual)
    # test default value - key only
    actual = lookup(cut(t1, 'foo'), 'foo')
    expect = {'a': [('a',)], 'b': [('b',), ('b',)]}
    eq_(expect, actual)

    t2 = (('foo', 'bar', 'baz'),
          ('a', 1, True),
          ('b', 2, False),
          ('b', 3, True),
          ('b', 3, False))

    # test value selection
    actual = lookup(t2, 'foo', ('bar', 'baz'))
    expect = {'a': [(1, True)], 'b': [(2, False), (3, True), (3, False)]}
    eq_(expect, actual)

    # test compound key
    actual = lookup(t2, ('foo', 'bar'), 'baz')
    expect = {('a', 1): [True], ('b', 2): [False], ('b', 3): [True, False]}
    eq_(expect, actual)
Example #11
0
def test_repr_html_limit():
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))

    # lower limit
    etl.config.display_limit = 2

    expect = """<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_()
    print(actual)
    for l1, l2 in zip(expect.split('\n'), actual.split('\n')):
        eq_(l1, l2)
Example #12
0
def test_repr():
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    expect = str(etl.look(table))
    actual = repr(etl.wrap(table))
    eq_(expect, actual)
Example #13
0
def test_header():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = header(table)
    expect = ('foo', 'bar')
    eq_(expect, actual)
    table = (['foo', 'bar'], ['a', 1], ['b', 2])
    actual = header(table)
    eq_(expect, actual)
Example #14
0
def test_see():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(see(table))
    expect = """foo: 'a', 'b'
bar: 1, 2
"""
    eq_(expect, actual)
Example #15
0
def test_see_index_header():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(see(table, index_header=True))
    expect = """0|foo: 'a', 'b'
1|bar: 1, 2
"""
    eq_(expect, actual)
Example #16
0
def test_see_duplicateheader():

    table = (('foo', 'bar', 'foo'), ('a', 1, 'a_prime'), ('b', 2, 'b_prime'))
    actual = repr(see(table))
    expect = """foo: 'a', 'b'
bar: 1, 2
foo: 'a_prime', 'b_prime'
"""
    eq_(expect, actual)
Example #17
0
def test_diffheaders():

    table1 = (('foo', 'bar', 'baz'),
              ('a', 1, .3))

    table2 = (('baz', 'bar', 'quux'),
              ('a', 1, .3))

    add, sub = diffheaders(table1, table2)
    eq_(set(['quux']), add)
    eq_(set(['foo']), sub)
Example #18
0
def test_look_width():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, width=10))
    expect = ("+-----+---\n"
              "| foo | ba\n"
              "+=====+===\n"
              "| 'a' |   \n"
              "+-----+---\n"
              "| 'b' |   \n"
              "+-----+---\n")
    eq_(expect, actual)
Example #19
0
def test_look_style_minimal():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, style='minimal'))
    expect = """foo  bar
'a'    1
'b'    2
"""
    eq_(expect, actual)
    etl.config.look_style = 'minimal'
    actual = repr(look(table))
    eq_(expect, actual)
    etl.config.look_style = 'grid'
Example #20
0
def test_diffvalues():

    table1 = (('foo', 'bar'),
              ('a', 1),
              ('b', 3))

    table2 = (('bar', 'foo'),
              (1, 'a'),
              (3, 'c'))

    add, sub = diffvalues(table1, table2, 'foo')
    eq_(set(['c']), add)
    eq_(set(['b']), sub)
Example #21
0
def test_wrap_tuple_return():
    tablea = etl.wrap((('foo', 'bar'),
                       ('A', 1),
                       ('C', 7)))
    tableb = etl.wrap((('foo', 'bar'),
                       ('B', 5),
                       ('C', 7)))

    added, removed = tablea.diff(tableb)
    eq_(('foo', 'bar'), added.header())
    eq_(('foo', 'bar'), removed.header())
    ieq(etl.data(added), added.data())
    ieq(etl.data(removed), removed.data())
Example #22
0
def test_look_irregular_rows():

    table = (('foo', 'bar'), ('a',), ('b', 2, True))
    actual = repr(look(table))
    expect = """+-----+-----+------+
| foo | bar |      |
+=====+=====+======+
| 'a' |     |      |
+-----+-----+------+
| 'b' |   2 | True |
+-----+-----+------+
"""
    eq_(expect, actual)
Example #23
0
def test_lookstr():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(lookstr(table))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| a   |   1 |
+-----+-----+
| b   |   2 |
+-----+-----+
"""
    eq_(expect, actual)
Example #24
0
def test_look_index_header():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, index_header=True))
    expect = """+-------+-------+
| 0|foo | 1|bar |
+=======+=======+
| 'a'   |     1 |
+-------+-------+
| 'b'   |     2 |
+-------+-------+
"""
    eq_(expect, actual)
Example #25
0
def test_look_bool():

    table = (('foo', 'bar'), ('a', True), ('b', False))
    actual = repr(look(table))
    expect = """+-----+-------+
| foo | bar   |
+=====+=======+
| 'a' | True  |
+-----+-------+
| 'b' | False |
+-----+-------+
"""
    eq_(expect, actual)
Example #26
0
def test_container():
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    actual = etl.wrap(table)[0]
    expect = ('foo', 'bar')
    eq_(expect, actual)
    actual = etl.wrap(table)['bar']
    expect = (1, 2, 2)
    ieq(expect, actual)
    actual = len(etl.wrap(table))
    expect = 4
    eq_(expect, actual)
Example #27
0
def test_fromdb_mkcursor():

    # initial data
    data = (("a", 1), ("b", 2), ("c", 2.0))
    connection = sqlite3.connect(":memory:")
    c = connection.cursor()
    c.execute("create table foobar (foo, bar)")
    for row in data:
        c.execute("insert into foobar values (?, ?)", row)
    connection.commit()
    c.close()

    # test the function
    mkcursor = lambda: connection.cursor()
    actual = fromdb(mkcursor, "select * from foobar")
    expect = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2.0))
    ieq(expect, actual)
    ieq(expect, actual)  # verify can iterate twice

    # test iterators are isolated
    i1 = iter(actual)
    i2 = iter(actual)
    eq_(("foo", "bar"), next(i1))
    eq_(("a", 1), next(i1))
    eq_(("foo", "bar"), next(i2))
    eq_(("b", 2), next(i1))
Example #28
0
def test_tocsv_appendcsv_gz():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False)
    fn = f.name + '.gz'
    f.close()
    tocsv(table, fn, encoding='ascii', lineterminator='\n')

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        data = [b'foo,bar',
                b'a,1',
                b'b,2',
                b'c,2']
        # don't forget final terminator
        expect = b'\n'.join(data) + b'\n'
        actual = o.read()
        eq_(expect, actual)
    finally:
        o.close()

    # check appending
    table2 = (('foo', 'bar'),
              ('d', 7),
              ('e', 9),
              ('f', 1))
    appendcsv(table2, fn, encoding='ascii', lineterminator='\n')

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        data = [b'foo,bar',
                b'a,1',
                b'b,2',
                b'c,2',
                b'd,7',
                b'e,9',
                b'f,1']
        # don't forget final terminator
        expect = b'\n'.join(data) + b'\n'
        actual = o.read()
        eq_(expect, actual)
    finally:
        o.close()
Example #29
0
def test_totext_gz():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    fn = f.name + '.gz'
    os.rename(f.name, fn)
    prologue = (
        "{| class='wikitable'\n"
        "|-\n"
        "! foo\n"
        "! bar\n"
    )
    template = (
        "|-\n"
        "| {foo}\n"
        "| {bar}\n"
    )
    epilogue = "|}\n"
    totext(table, fn, encoding='ascii', template=template, prologue=prologue,
           epilogue=epilogue)

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        actual = o.read()
        expect = (
            b"{| class='wikitable'\n"
            b"|-\n"
            b"! foo\n"
            b"! bar\n"
            b"|-\n"
            b"| a\n"
            b"| 1\n"
            b"|-\n"
            b"| b\n"
            b"| 2\n"
            b"|-\n"
            b"| c\n"
            b"| 2\n"
            b"|}\n"
        )
        eq_(expect, actual)
    finally:
        o.close()
Example #30
0
def test_recordlookupone():

    t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3))

    try:
        recordlookupone(t1, 'foo', strict=True)
    except DuplicateKeyError:
        pass  # expected
    else:
        assert False, 'expected error'

    # relax
    lkp = recordlookupone(t1, 'foo', strict=False)
    eq_(1, lkp['a'].bar)
    eq_(2, lkp['b'].bar)  # first wins
Example #31
0
    def test_helper_smb_url_parse():
        from petl.io.remotes import _parse_smb_url

        url = r"smb://workgroup;user:password@server:444/share/folder/file.csv"
        domain, host, port, user, passwd, server_path = _parse_smb_url(url)
        print("Parsed:", domain, host, port, user, passwd, server_path)
        eq_(domain, r"workgroup")
        eq_(host, r"server")
        eq_(port, 444)
        eq_(user, r"user")
        eq_(passwd, r"password")
        eq_(server_path, "\\\\server\\share\\folder\\file.csv")
Example #32
0
def test_capture_nonmatching():

    table = (('id', 'variable', 'value'), ('1', 'A1', '12'), ('2', 'A2', '15'),
             ('3', 'B1', '18'), ('4', 'C12', '19'))

    expectation = (('id', 'value', 'treat', 'time'), ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'), ('3', '18', 'B', '1'))

    # default behaviour, raise exception
    result = capture(table, 'variable', r'([A-B])(\d+)', ('treat', 'time'))
    it = iter(result)
    eq_(expectation[0], next(it))  # header
    eq_(expectation[1], next(it))
    eq_(expectation[2], next(it))
    eq_(expectation[3], next(it))
    try:
        next(it)  # doesn't match
    except TransformError:
        pass  # expected
    else:
        assert False, 'expected exception'

    # explicit fill
    result = capture(table,
                     'variable',
                     r'([A-B])(\d+)',
                     newfields=('treat', 'time'),
                     fill=['', 0])
    it = iter(result)
    eq_(expectation[0], next(it))  # header
    eq_(expectation[1], next(it))
    eq_(expectation[2], next(it))
    eq_(expectation[3], next(it))
    eq_(('4', '19', '', 0), next(it))
Example #33
0
 def test_toarray_dictdtype():
     t = [('foo', 'bar', 'baz'), ('apples', 1, 2.5), ('oranges', 3, 4.4),
          ('pears', 7, .1)]
     a = toarray(t, dtype={'foo': 'U4'})  # specify partial dtype
     assert isinstance(a, np.ndarray)
     assert isinstance(a['foo'], np.ndarray)
     assert isinstance(a['bar'], np.ndarray)
     assert isinstance(a['baz'], np.ndarray)
     eq_('appl', a['foo'][0])
     eq_('oran', a['foo'][1])
     eq_('pear', a['foo'][2])
     eq_(1, a['bar'][0])
     eq_(3, a['bar'][1])
     eq_(7, a['bar'][2])
     assert_almost_equal(2.5, a['baz'][0])
     assert_almost_equal(4.4, a['baz'][1])
     assert_almost_equal(.1, a['baz'][2])
Example #34
0
def test_records():
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 3))
    actual = records(table)
    # access items
    it = iter(actual)
    o = next(it)
    eq_('a', o['foo'])
    eq_(1, o['bar'])
    o = next(it)
    eq_('b', o['foo'])
    eq_(2, o['bar'])
    # access attributes
    it = iter(actual)
    o = next(it)
    eq_('a', o.foo)
    eq_(1, o.bar)
    o = next(it)
    eq_('b', o.foo)
    eq_(2, o.bar)
    # access with get() method
    it = iter(actual)
    o = next(it)
    eq_('a', o.get('foo'))
    eq_(1, o.get('bar'))
    eq_(None, o.get('baz'))
    eq_('qux', o.get('baz', default='qux'))
Example #35
0
def test_sort_buffered_independent():

    table = (('foo', 'bar'), ('C', 2), ('A', 9), ('A', 6), ('F', 1), ('D', 10))
    expectation = (('foo', 'bar'), ('F', 1), ('C', 2), ('A', 6), ('A', 9),
                   ('D', 10))

    result = sort(table, 'bar', buffersize=4)
    nrows(result)  # cause data to be cached
    # check that two row iterators are independent, i.e., consuming rows
    # from one does not affect the other
    it1 = iter(result)
    it2 = iter(result)
    eq_(expectation[0], next(it1))
    eq_(expectation[1], next(it1))
    eq_(expectation[0], next(it2))
    eq_(expectation[1], next(it2))
    eq_(expectation[2], next(it2))
    eq_(expectation[2], next(it1))
Example #36
0
def test_rowgroupby():

    table = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, True), ('b', 3))

    # simplest form

    g = rowgroupby(table, 'foo')

    key, vals = next(g)
    vals = list(vals)
    eq_('a', key)
    eq_(1, len(vals))
    eq_(('a', 1, True), vals[0])

    key, vals = next(g)
    vals = list(vals)
    eq_('b', key)
    eq_(2, len(vals))
    eq_(('b', 2, True), vals[0])
    eq_(('b', 3), vals[1])

    # specify value

    g = rowgroupby(table, 'foo', 'bar')

    key, vals = next(g)
    vals = list(vals)
    eq_('a', key)
    eq_(1, len(vals))
    eq_(1, vals[0])

    key, vals = next(g)
    vals = list(vals)
    eq_('b', key)
    eq_(2, len(vals))
    eq_(2, vals[0])
    eq_(3, vals[1])

    # callable key

    g = rowgroupby(table, lambda r: r['foo'], lambda r: r['baz'])

    key, vals = next(g)
    vals = list(vals)
    eq_('a', key)
    eq_(1, len(vals))
    eq_(True, vals[0])

    key, vals = next(g)
    vals = list(vals)
    eq_('b', key)
    eq_(2, len(vals))
    eq_(True, vals[0])
    eq_(None, vals[1])  # gets padded
Example #37
0
 def test_torecarray():
     t = [('foo', 'bar', 'baz'), ('apples', 1, 2.5), ('oranges', 3, 4.4),
          ('pears', 7, .1)]
     a = torecarray(t)
     assert isinstance(a, np.ndarray)
     assert isinstance(a.foo, np.ndarray)
     assert isinstance(a.bar, np.ndarray)
     assert isinstance(a.baz, np.ndarray)
     eq_('apples', a.foo[0])
     eq_('oranges', a.foo[1])
     eq_('pears', a.foo[2])
     eq_(1, a.bar[0])
     eq_(3, a.bar[1])
     eq_(7, a.bar[2])
     assert_almost_equal(2.5, a.baz[0], places=6)
     assert_almost_equal(4.4, a.baz[1], places=6)
     assert_almost_equal(.1, a.baz[2], places=6)
Example #38
0
def test_records_unevenrows():
    table = (('foo', 'bar'), ('a', 1, True), ('b', ))
    actual = records(table)
    # access items
    it = iter(actual)
    o = next(it)
    eq_('a', o['foo'])
    eq_(1, o['bar'])
    o = next(it)
    eq_('b', o['foo'])
    eq_(None, o['bar'])
    # access attributes
    it = iter(actual)
    o = next(it)
    eq_('a', o.foo)
    eq_(1, o.bar)
    o = next(it)
    eq_('b', o.foo)
    eq_(None, o.bar)
Example #39
0
def test_facet_empty():
    table = (('foo', 'bar'),)
    actual = facet(table, 'foo')
    eq_(list(), list(actual.keys()))
Example #40
0
def test_repr():
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2))
    expect = str(etl.look(table))
    actual = repr(etl.wrap(table))
    eq_(expect, actual)
Example #41
0
def test_columns():

    table = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]]
    cols = columns(table)
    eq_(['a', 'b', 'b'], cols['foo'])
    eq_([1, 2, 3], cols['bar'])
Example #42
0
def test_facetcolumns():

    table = [['foo', 'bar', 'baz'], ['a', 1, True], ['b', 2, True], ['b', 3]]

    fc = facetcolumns(table, 'foo')
    eq_(['a'], fc['a']['foo'])
    eq_([1], fc['a']['bar'])
    eq_([True], fc['a']['baz'])
    eq_(['b', 'b'], fc['b']['foo'])
    eq_([2, 3], fc['b']['bar'])
    eq_([True, None], fc['b']['baz'])
Example #43
0
 def test_toarray_stringdtype():
     t = [('foo', 'bar', 'baz'), ('apples', 1, 2.5), ('oranges', 3, 4.4),
          ('pears', 7, .1)]
     a = toarray(t, dtype='U4, i2, f4')
     assert isinstance(a, np.ndarray)
     assert isinstance(a['foo'], np.ndarray)
     assert isinstance(a['bar'], np.ndarray)
     assert isinstance(a['baz'], np.ndarray)
     eq_('appl', a['foo'][0])
     eq_('oran', a['foo'][1])
     eq_('pear', a['foo'][2])
     eq_(1, a['bar'][0])
     eq_(3, a['bar'][1])
     eq_(7, a['bar'][2])
     assert_almost_equal(2.5, a['baz'][0], places=6)
     assert_almost_equal(4.4, a['baz'][1], places=6)
     assert_almost_equal(.1, a['baz'][2], places=6)
Example #44
0
def test_comparable():

    # bools
    d = [True, False]
    a = sorted(d, key=Comparable)
    e = [False, True]
    eq_(e, a)

    # ints
    d = [3, 1, 2]
    a = sorted(d, key=Comparable)
    e = [1, 2, 3]
    eq_(e, a)

    # floats
    d = [3., 1.2, 2.5]
    a = sorted(d, key=Comparable)
    e = [1.2, 2.5, 3.]
    eq_(e, a)

    # mixed numeric
    d = [3., 1, 2.5, Decimal('1.5')]
    a = sorted(d, key=Comparable)
    e = [1, Decimal('1.5'), 2.5, 3.]
    eq_(e, a)

    # mixed numeric and bool
    d = [True, False, -1.2, 2, .5]
    a = sorted(d, key=Comparable)
    e = [-1.2, False, .5, True, 2]
    eq_(e, a)

    # mixed numeric and None
    d = [3, None, 2.5]
    a = sorted(d, key=Comparable)
    e = [None, 2.5, 3.]
    eq_(e, a)

    # bytes
    d = [b'b', b'ccc', b'aa']
    a = sorted(d, key=Comparable)
    e = [b'aa', b'b', b'ccc']
    eq_(e, a)

    # text
    d = [u'b', u'ccc', u'aa']
    a = sorted(d, key=Comparable)
    e = [u'aa', u'b', u'ccc']
    eq_(e, a)

    # mixed bytes and text
    d = [u'b', b'ccc', b'aa']
    a = sorted(d, key=Comparable)
    # N.B., expect always bytes < unicode
    e = [b'aa', b'ccc', u'b']
    eq_(e, a)

    # mixed bytes and None
    d = [b'b', b'ccc', None, b'aa']
    a = sorted(d, key=Comparable)
    e = [None, b'aa', b'b', b'ccc']
    eq_(e, a)

    # mixed text and None
    d = [u'b', u'ccc', None, u'aa']
    a = sorted(d, key=Comparable)
    e = [None, u'aa', u'b', u'ccc']
    eq_(e, a)

    # mixed bytes, text and None
    d = [u'b', b'ccc', None, b'aa']
    a = sorted(d, key=Comparable)
    # N.B., expect always bytes < unicode
    e = [None, b'aa', b'ccc', u'b']
    eq_(e, a)

    # mixed bytes, text, numbers and None
    d = [u'b', True, b'ccc', False, None, b'aa', -1, 3.4]
    a = sorted(d, key=Comparable)
    e = [None, -1, False, True, 3.4, b'aa', b'ccc', u'b']
    eq_(e, a)
Example #45
0
 def test_toarray_nodtype():
     t = [('foo', 'bar', 'baz'), ('apples', 1, 2.5), ('oranges', 3, 4.4),
          ('pears', 7, .1)]
     a = toarray(t)
     assert isinstance(a, np.ndarray)
     assert isinstance(a['foo'], np.ndarray)
     assert isinstance(a['bar'], np.ndarray)
     assert isinstance(a['baz'], np.ndarray)
     eq_('apples', a['foo'][0])
     eq_('oranges', a['foo'][1])
     eq_('pears', a['foo'][2])
     eq_(1, a['bar'][0])
     eq_(3, a['bar'][1])
     eq_(7, a['bar'][2])
     assert_almost_equal(2.5, a['baz'][0])
     assert_almost_equal(4.4, a['baz'][1])
     assert_almost_equal(.1, a['baz'][2])
Example #46
0
def test_comparable_nested():

    # lists
    d = [[3], [1], [2]]
    a = sorted(d, key=Comparable)
    e = [[1], [2], [3]]
    eq_(e, a)

    # tuples
    d = [(3, ), (1, ), (2, )]
    a = sorted(d, key=Comparable)
    e = [(1, ), (2, ), (3, )]
    eq_(e, a)

    # mixed lists and numeric
    d = [3, 1, [2]]
    a = sorted(d, key=Comparable)
    e = [1, 3, [2]]
    eq_(e, a)

    # lists containing None
    d = [[3], [None], [2]]
    a = sorted(d, key=Comparable)
    e = [[None], [2], [3]]
    eq_(e, a)

    # mixed lists and tuples
    d = [[3], [1], (2, )]
    a = sorted(d, key=Comparable)
    e = [[1], (2, ), [3]]
    eq_(e, a)

    # length 2 lists
    d = [[3, 2], [3, 1], [2]]
    a = sorted(d, key=Comparable)
    e = [[2], [3, 1], [3, 2]]
    eq_(e, a)

    dt = datetime.now().replace

    # mixed everything
    d = [
        dt(hour=12), None, (dt(hour=3), 'b'), True, [b'aa', False],
        (b'aa', -1), 3.4
    ]
    a = sorted(d, key=Comparable)
    e = [
        None, True, 3.4,
        dt(hour=12), (dt(hour=3), 'b'), (b'aa', -1), [b'aa', False]
    ]
    eq_(e, a)
Example #47
0
 def test_toarray_explicitdtype():
     t = [('foo', 'bar', 'baz'), ('apples', 1, 2.5), ('oranges', 3, 4.4),
          ('pears', 7, .1)]
     a = toarray(t, dtype=[('A', 'U4'), ('B', 'i2'), ('C', 'f4')])
     assert isinstance(a, np.ndarray)
     assert isinstance(a['A'], np.ndarray)
     assert isinstance(a['B'], np.ndarray)
     assert isinstance(a['C'], np.ndarray)
     eq_('appl', a['A'][0])
     eq_('oran', a['A'][1])
     eq_('pear', a['A'][2])
     eq_(1, a['B'][0])
     eq_(3, a['B'][1])
     eq_(7, a['B'][2])
     assert_almost_equal(2.5, a['C'][0], places=6)
     assert_almost_equal(4.4, a['C'][1], places=6)
     assert_almost_equal(.1, a['C'][2], places=6)
Example #48
0
 def test_toarray_lists():
     t = [['foo', 'bar', 'baz'], ['apples', 1, 2.5], ['oranges', 3, 4.4],
          ['pears', 7, .1]]
     a = toarray(t)
     assert isinstance(a, np.ndarray)
     assert isinstance(a['foo'], np.ndarray)
     assert isinstance(a['bar'], np.ndarray)
     assert isinstance(a['baz'], np.ndarray)
     eq_('apples', a['foo'][0])
     eq_('oranges', a['foo'][1])
     eq_('pears', a['foo'][2])
     eq_(1, a['bar'][0])
     eq_(3, a['bar'][1])
     eq_(7, a['bar'][2])
     assert_almost_equal(2.5, a['baz'][0], places=6)
     assert_almost_equal(4.4, a['baz'][1], places=6)
     assert_almost_equal(.1, a['baz'][2], places=6)