Ejemplo n.º 1
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)
Ejemplo n.º 2
0
def test_smallish_casted_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")
    sparse_spec.set_types(a=int, e=int)
    spec = Enumap("Thing", sparse_spec.names())
    spec.set_types(a=int, e=int)
    sparse_typeless_spec = SparseEnumap("ThingSparseTypless",
                                        "a b c d e f g h i j k")

    print()
    print(spec.tuple_casted(*data))
    print(spec.tuple_casted(*data, d="9999999"))
    print(sparse_spec.tuple_casted(*incomplete_data))
    print(sparse_typeless_spec.tuple_casted(*incomplete_data))

    # time Enumap.tuple() when all data is given
    enumap_tuple_time = timeit("spec.tuple_casted(*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_casted(**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_casted(*data, d='99999')",
                                        globals=dict(data=data, spec=spec),
                                        number=N_RUNS)

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

    # time SparseEnumap.tuple() when partial data is given
    enumap_sparse_typeless_tuple_time = timeit("spec.tuple_casted(*data)",
                                               globals=dict(
                                                   data=incomplete_data,
                                                   spec=sparse_typeless_spec),
                                               number=N_RUNS)

    # time a regular tuple(iterable) call
    regular_tuple_time = timeit("tuple(map(int, 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(*map(int, data))",
                              globals=dict(data=data, ntuple=ntuple),
                              number=N_RUNS)

    print(f"{'Enumap.tuple_casted':<40} {enumap_tuple_time:.2f}")
    print(
        f"{'Enumap.tuple_casted (with kwargs)':<40} {enumap_kwargs_tuple_time:.2f}"
    )
    print(
        f"{'Enumap.tuple_casted (with override)':<40} {enumap_override_tuple_time:.2f}"
    )
    print(
        f"{'Enumap.tuple_casted (sparse)':<40} {enumap_sparse_tuple_time:.2f}")
    print(
        f"{'Enumap.tuple_casted (sparse, typeless)':<40} {enumap_sparse_typeless_tuple_time:.2f}"
    )
    print(f"{'tuple(map(int, ...))':<40} {regular_tuple_time:.2f}")
    print(f"{'namedtuple(map(int, ...))':<40} {named_tuple_time:.2f}")
Ejemplo n.º 3
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)