Example #1
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)
    # key only
    actual = dictlookupone(cut(t1, 'foo'), 'foo')
    expect = {'a': {'foo': 'a'},
              '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 = 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 #2
0
def test_dictlookupone():
    """Test the dictlookupone function."""

    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)
    expect = {"a": {"foo": "a", "bar": 1}, "b": {"foo": "b", "bar": 2}}  # first wins
    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 #3
0
def load_devices(file):
    """
    File header:
    id;additional_info;customer_id;type;name;label;search_text;tenant_id

    Output:
        dictionary like {'id_1': {name:'Device 1'}, ...}

    """
    tbl_devices = petl.io.csv.fromcsv(file, delimiter=';', encoding='utf-8')
    tbl_devices = petl.cutout(tbl_devices, 'customer_id', 'search_text', 'tenant_id')
    # PETL docs:
    # https://petl.readthedocs.io/en/stable/util.html#petl.util.lookups.dictlookupone
    devices = petl.dictlookupone(tbl_devices, 'id')
    return devices
Example #4
0
lkp = shelve.open('example.dat', flag='r')
lkp['a']
lkp['b']


# dictlookupone()
#################

import petl as etl
table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['b', 3]]
# if the specified key is not unique and strict=False (default),
# the first value wins
lkp = etl.dictlookupone(table1, 'foo')
lkp['a']
lkp['b']
# if the specified key is not unique and strict=True, will raise
# DuplicateKeyError
try:
    lkp = etl.dictlookupone(table1, 'foo', strict=True)
except etl.errors.DuplicateKeyError as e:
    print(e)

# compound keys are supported
table2 = [['foo', 'bar', 'baz'],
          ['a', 1, True],
          ['b', 2, False],
          ['b', 3, True],
          ['b', 3, False]]
Example #5
0
def load_keys(file):
    tbl_keys = petl.io.csv.fromcsv(file, delimiter=';', encoding='utf-8', header=['key', 'key_id'])
    keys = petl.dictlookupone(tbl_keys, 'key_id')
    return keys
Example #6
0
import shelve
lkp = shelve.open('example.dat', flag='n')
lkp = etl.dictlookup(table1, 'foo', lkp)
lkp.close()
lkp = shelve.open('example.dat', flag='r')
lkp['a']
lkp['b']

# dictlookupone()
#################

import petl as etl
table1 = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]]
# if the specified key is not unique and strict=False (default),
# the first value wins
lkp = etl.dictlookupone(table1, 'foo')
lkp['a']
lkp['b']
# if the specified key is not unique and strict=True, will raise
# DuplicateKeyError
try:
    lkp = etl.dictlookupone(table1, 'foo', strict=True)
except etl.errors.DuplicateKeyError as e:
    print(e)

# compound keys are supported
table2 = [['foo', 'bar', 'baz'], ['a', 1, True], ['b', 2, False],
          ['b', 3, True], ['b', 3, False]]
lkp = etl.dictlookupone(table2, ('foo', 'bar'))
lkp[('a', 1)]
lkp[('b', 2)]