Exemplo n.º 1
0
def test_dictlookup():

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

    actual = dictlookup(t1, 'foo')
    expect = {'a': [{'foo': 'a', 'bar': 1}],
              'b': [{'foo': 'b', 'bar': 2}, {'foo': 'b', 'bar': 3}]}
    eq_(expect, actual)
    # key only
    actual = dictlookup(cut(t1, 'foo'), 'foo')
    expect = {'a': [{'foo': 'a'}],
              'b': [{'foo': 'b'}, {'foo': 'b'}]}
    eq_(expect, actual)

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

    # test compound key
    actual = dictlookup(t2, ('foo', 'bar'))
    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},
                         {'foo': 'b', 'bar': 3, 'baz': False}]}
    eq_(expect, actual)
Exemplo n.º 2
0
def test_dictlookup():
    """Test the dictlookup function."""

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

    actual = dictlookup(t1, 'foo')
    expect = {
        'a': [{
            'foo': 'a',
            'bar': 1
        }],
        'b': [{
            'foo': 'b',
            'bar': 2
        }, {
            'foo': 'b',
            'bar': 3
        }]
    }
    eq_(expect, actual)

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

    # test compound key
    actual = dictlookup(t2, ('foo', 'bar'))
    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
        }, {
            'foo': 'b',
            'bar': 3,
            'baz': False
        }]
    }
    eq_(expect, actual)
Exemplo n.º 3
0
def test_dictlookup():
    """Test the dictlookup function."""

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

    actual = dictlookup(t1, "foo")
    expect = {"a": [{"foo": "a", "bar": 1}], "b": [{"foo": "b", "bar": 2}, {"foo": "b", "bar": 3}]}
    eq_(expect, actual)

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

    # test compound key
    actual = dictlookup(t2, ("foo", "bar"))
    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}, {"foo": "b", "bar": 3, "baz": False}],
    }
    eq_(expect, actual)
Exemplo n.º 4
0
lkp = etl.lookupone(table1, 'foo', 'bar', lkp)
lkp.close()
lkp = shelve.open('example.dat', flag='r')
lkp['a']
lkp['b']


# dictlookup()
##############

import petl as etl
table1 = [['foo', 'bar'], 
          ['a', 1], 
          ['b', 2], 
          ['b', 3]]
lkp = etl.dictlookup(table1, 'foo')
lkp['a']
lkp['b']
# compound keys are supported
table2 = [['foo', 'bar', 'baz'],
          ['a', 1, True],
          ['b', 2, False],
          ['b', 3, True],
          ['b', 3, False]]
lkp = etl.dictlookup(table2, ('foo', 'bar'))
lkp[('a', 1)]
lkp[('b', 2)]
lkp[('b', 3)]
# data can be loaded into an existing dictionary-like 
# object, including persistent dictionaries created via the 
# shelve module
Exemplo n.º 5
0
# object, including persistent dictionaries created via the
# shelve module
import shelve
lkp = shelve.open('example.dat', flag='n')
lkp = etl.lookupone(table1, 'foo', 'bar', lkp)
lkp.close()
lkp = shelve.open('example.dat', flag='r')
lkp['a']
lkp['b']

# dictlookup()
##############

import petl as etl
table1 = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]]
lkp = etl.dictlookup(table1, 'foo')
lkp['a']
lkp['b']
# compound keys are supported
table2 = [['foo', 'bar', 'baz'], ['a', 1, True], ['b', 2, False],
          ['b', 3, True], ['b', 3, False]]
lkp = etl.dictlookup(table2, ('foo', 'bar'))
lkp[('a', 1)]
lkp[('b', 2)]
lkp[('b', 3)]
# data can be loaded into an existing dictionary-like
# object, including persistent dictionaries created via the
# shelve module
import shelve
lkp = shelve.open('example.dat', flag='n')
lkp = etl.dictlookup(table1, 'foo', lkp)