def test_ordering(): a = Enumap("forward", names=["n" + str(i) for i in range(100)]) b = Enumap("backward", names=["n" + str(i) for i in range(99, -1, -1)]) expected_a = list(range(100)) expected_a[42] = 9000 assert list(a.tuple(*range(100), n42=9000)) == expected_a expected_b = list(range(100)) expected_b[57] = 9000 assert list(b.tuple(*range(100), n42=9000)) == expected_b
def test_smallish_sparse_tuple(): data = "1 2 3 4 5 6 7 8 9 10 11".split() incomplete_data = data[:-6] sparse_spec = SparseEnumap("ThingSparse", "a b c d e f g h i j k") spec = Enumap("Thing", sparse_spec.names()) print() print(spec.tuple(*data)) print(spec.tuple(*data, d="override")) print(sparse_spec.tuple(*incomplete_data)) # time Enumap.tuple() when all data is given enumap_tuple_time = timeit("spec.tuple(*data)", globals=dict(data=data, spec=spec), number=N_RUNS) # time Enumap.tuple() when all data is given as kwargs kwarg_data = dict(zip(spec.names(), data)) enumap_kwargs_tuple_time = timeit("spec.tuple(**data)", globals=dict(data=kwarg_data, spec=spec), number=N_RUNS) # time Enumap.tuple() when data is given with overrides enumap_override_tuple_time = timeit("spec.tuple(*data, d='override')", globals=dict(data=data, spec=spec), number=N_RUNS) # time SparseEnumap.tuple() when partial data is given enumap_sparse_tuple_time = timeit("spec.tuple(*data)", globals=dict(data=incomplete_data, spec=sparse_spec), number=N_RUNS) # time a regular tuple(iterable) call regular_tuple_time = timeit("tuple(data)", globals=dict(data=data), number=N_RUNS) # time a regular namedtuple(*args) call ntuple = namedtuple("ntuple", list(spec.names())) named_tuple_time = timeit("ntuple(*data)", globals=dict(data=data, ntuple=ntuple), number=N_RUNS) print(f"{'Enumap.tuple':<40} {enumap_tuple_time:.2f}") print(f"{'Enumap.tuple (with kwargs)':<40} {enumap_kwargs_tuple_time:.2f}") print( f"{'Enumap.tuple (with override)':<40} {enumap_override_tuple_time:.2f}" ) print(f"{'Enumap.tuple (sparse)':<40} {enumap_sparse_tuple_time:.2f}") print(f"{'tuple':<40} {regular_tuple_time:.2f}") print(f"{'namedtuple':<40} {named_tuple_time:.2f}")
def test_missing_key(): a = Enumap("a", names="b c e") with pytest.raises(KeyError) as ke: assert a.tuple(*"1 3".split()) assert "missing keys {'e'}" in str(ke)
def test_tuple(): a = Enumap("a", names="b c e") assert a.tuple(1, 2, 3, e=33) == (1, 2, 33)
def test_bad_key(): a = Enumap("a", names="b c e") with pytest.raises(KeyError) as ke: assert a.tuple(*"1 3 4".split(), f="nope") assert "invalid keys {'f'}" in str(ke)