Beispiel #1
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}")
Beispiel #2
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}")
Beispiel #3
0
def test_repr_sparse():
    """Make sure that SparseEnumapMeta's __repr___ method works"""
    a = SparseEnumap("a", "b c d")
    assert repr(a) == """a(
Beispiel #4
0
def test_str_sparse():
    """Check that SparseEnumapMeta's __str__ method works"""
    a = SparseEnumap("a", "b c d")
    assert str(a) == "a(b, c, d)"
Beispiel #5
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() == {}
Beispiel #6
0
def test_sparse_bad_key():
    a = SparseEnumap("a", names="b c e")
    with pytest.raises(KeyError) as ke:
        assert a.tuple(*"1 3".split(), f="nope")
    assert "invalid keys {'f'}" in str(ke)
Beispiel #7
0
def test_sparse_casted_map():
    a = SparseEnumap("a", names="a b c e")
    a.set_types(to_int, to_int, float, float)
    casted_map = a.map_casted(*"1.1 2.2 3.3".split())
    assert tuple(casted_map.values()) == (1, 2, 3.3, None)
Beispiel #8
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)
Beispiel #9
0
def test_sparse_casted_tuple_with_default():
    a = SparseEnumap("a", names="a b c e")
    a.set_types(to_int, to_int, float, int)
    a.set_defaults(e="HOOP")
    casted_tuple = a.tuple_casted(*"1.1 2.2".split())
    assert casted_tuple == (1, 2, None, "HOOP")  # missing values aren't casted
Beispiel #10
0
def test_sparse_casted_tuple():
    a = SparseEnumap("a", names="a b c e")
    a.set_types(to_int, to_int, float, float)
    casted_tuple = a.tuple_casted(*"1.1 2.2 3.3".split())
    assert casted_tuple == (1, 2, 3.3, None)  # missing values aren't casted
Beispiel #11
0
def test_sparse_map():
    a = SparseEnumap("a", names="b c e")
    assert (a.map(*"1 3".split(), c="2.2") ==
            OrderedDict([("b", "1"), ("c", "2.2"), ("e", None)]))
Beispiel #12
0
def test_sparse_tuple():
    a = SparseEnumap("a", names="b c d e")
    a.set_defaults(c="WONK", d=0)
    assert (a.tuple(*"1 3".split(), c="2.2") ==
            ("1", "2.2", 0, None))
Beispiel #13
0
def test_sparse_defaults():
    a = SparseEnumap("a", names="b c d e")
    a.set_defaults(c="WONK", d=0)
    assert a.tuple(**a.defaults()) == (None, "WONK", 0, None)