Пример #1
0
def test_smallish_sparse_map():
    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.map(*data))
    print(spec.map(*data, d="override"))
    print(sparse_spec.map(*incomplete_data))

    # time Enumap.map() when all data is given
    enumap_map_time = timeit("spec.map(*data)",
                             globals=dict(data=data, spec=spec),
                             number=N_RUNS)

    # time Enumap.map() when all data is given as kwargs
    kwarg_data = dict(zip(spec.names(), data))
    enumap_kwargs_map_time = timeit("spec.map(**data)",
                                    globals=dict(data=kwarg_data, spec=spec),
                                    number=N_RUNS)

    # time Enumap.map() when data is given with overrides
    enumap_override_map_time = timeit("spec.map(*data, d='override')",
                                      globals=dict(data=data, spec=spec),
                                      number=N_RUNS)

    # time SparseEnumap.map() when partial data is given
    enumap_sparse_map_time = timeit("spec.map(*data)",
                                    globals=dict(data=incomplete_data,
                                                 spec=sparse_spec),
                                    number=N_RUNS)

    # time a regular dict(zip(...)) call
    regular_dict_time = timeit("dict(zip(spec.names(), data))",
                               globals=dict(data=data, spec=spec),
                               number=N_RUNS)

    # time a regular OrderedDict(zip(...)) call
    ordered_dict_time = timeit("OrderedDict(zip(spec.names(), data))",
                               globals=dict(data=data,
                                            OrderedDict=OrderedDict,
                                            spec=spec),
                               number=N_RUNS)

    print(f"{'Enumap.map':<40} {enumap_map_time:.2f}")
    print(f"{'Enumap.map (with kwargs)':<40} {enumap_kwargs_map_time:.2f}")
    print(f"{'Enumap.map (with override)':<40} {enumap_override_map_time:.2f}")
    print(f"{'Enumap.map (sparse)':<40} {enumap_sparse_map_time:.2f}")
    print(f"{'dict':<40} {regular_dict_time:.2f}")
    print(f"{'OrderedDict':<40} {ordered_dict_time:.2f}")
Пример #2
0
def test_sparse_types():
    """Check that SparseEnumap's types can be sparse.
    Missing type callables won't be called on values."""
    a = SparseEnumap("a", names="a b c e")
    a.set_types(int, int, int)  # sparse types; only two of four set
    a.set_defaults(b=3000, c="heyo")
    assert a.tuple_casted("1", "1") == (1, 1, "heyo", None)
    a = Enumap("a", "a b c")
    a.set_types(a=int, b=int)
    assert a.types() == dict(a=int, b=int)
Пример #3
0
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
Пример #4
0
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}")
Пример #5
0
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)
Пример #6
0
def test_map():
    a = Enumap("a", names="b c e")
    assert (a.map(1, 2, 3, e=33) ==
            OrderedDict([('b', 1), ('c', 2), ('e', 33)]))
Пример #7
0
def test_member_types():
    Pastry = Enumap("Pastry", names="croissant donut muffin")
    Pastry.set_types(int, int, int, donut=float)  # override donut with kwarg
    assert (Pastry.types() ==
            {'croissant': int, 'donut': float, 'muffin': int})
Пример #8
0
def test_tuple_class():
    a = Enumap("a", names="b c e")
    assert a.tuple_class()._fields == ("b", "c", "e")
Пример #9
0
def test_names():
    assert list(Enumap("a", names="b c e").names()) == ["b", "c", "e"]
Пример #10
0
def test_tuple_casted_1():
    a = Enumap("a", names="b c e")
    a.set_types(to_int, to_int, float, e=to_int)
    assert a.tuple_casted(*"1 2.2 3.3".split(), b=2.2) == (2, 2, 3.0)
Пример #11
0
def test_tuple():
    a = Enumap("a", names="b c e")
    assert a.tuple(1, 2, 3, e=33) == (1, 2, 33)
Пример #12
0
def test_map_casted_1():
    a = Enumap("a", names="b c e")
    a.set_types(to_int, to_int, float, e=to_int)
    assert a.map_casted(*"1 2.2 3.3".split()) == dict(b=1, c=2, e=3)
Пример #13
0
def test_str():
    """Check that EnumapMeta's __str__ method works"""
    a = Enumap("a", "b c d")
    assert str(a) == "a(b, c, d)"
Пример #14
0
def test_repr():
    """Make sure that EnumapMeta's __repr___ method works"""
    a = Enumap("a", "b c d")
    assert repr(a) == """a(
Пример #15
0
def test_copy_from_names():
    """Check that Enumap.names() can be used to construct another Enumap"""
    a = Enumap("a", "b c d")
    b = Enumap("b", a.names())
    assert a.map(*range(3)) == b.map(*range(3))
Пример #16
0
def test_typless():
    """Make sure types are allowed to be blank"""
    a = Enumap("A", "a b c".split())
    b = SparseEnumap("B", "a b c".split())
    assert a.types() == {}
    assert b.types() == {}
Пример #17
0
def test_tuple_casted_0():
    a = Enumap("a", names="b c e")
    a.set_types(to_int, to_int, float)
    assert a.tuple_casted(*"1 2.2 3.3".split()) == (1, 2, 3.3)
Пример #18
0
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)