Exemplo n.º 1
0
def test_default_values_w_class() -> None:
    d = autotui.prompt_namedtuple(Def)
    assert d == Def(x=5, y="default")

    # override with kwarg
    d = autotui.prompt_namedtuple(Def, type_use_values={str: "something"})
    assert d == Def(x=5, y="something")
Exemplo n.º 2
0
def test_default_values() -> None:

    now = datetime.now()
    current_datetime_func = lambda: now

    # shouldnt prompt interactively because were setting defaults
    val = autotui.prompt_namedtuple(
        P,
        attr_use_values={
            "a": 5,
            "b": 10.0
        },
        type_use_values={
            str: "something",
            datetime: current_datetime_func
        },
    )

    assert P(a=5, b=10.0, c="something", d=now) == val
Exemplo n.º 3
0
def save_from(nt: Type[NamedTuple],
              use_input: TextIO,
              partial: bool = False) -> None:
    json_text: str = use_input.read()
    p = datafile(namedtuple_func_name(nt))
    items: List[NamedTuple] = load_from(nt, p, allow_empty=True)
    new_items: List[NamedTuple] = []
    if partial:
        # load the list as json blobs
        os.environ["AUTOTUI_DISABLE_WARNINGS"] = "1"  # ignore null warnings
        blobs: List[Dict[str, Any]] = []
        for b in namedtuple_sequence_loads(json_text, nt):
            blobs.append(
                {k: v
                 for k, v in b._asdict().items() if v is not None})
        del os.environ["AUTOTUI_DISABLE_WARNINGS"]
        for bd in blobs:
            new_nt = prompt_namedtuple(nt, attr_use_values=bd)
            new_items.append(new_nt)
    else:
        new_items.extend(namedtuple_sequence_loads(json_text, nt))
    items.extend(new_items)
    dump_to(items, p)
from datetime import datetime
from typing import NamedTuple, Optional, List

from autotui import prompt_namedtuple


# describe a meeting with one or more people
class Meeting(NamedTuple):
    when: datetime
    where: Optional[str]  # asks if you want to add this
    people: List[str]  # asks if you want to add another item


m = prompt_namedtuple(Meeting)
print(m)
)

# Note: validators are of type
# Dict[Type, AutoHandler]
# serializer/deserializers are
# Dict[Type, Callable]
# the Callable accepts one argument,
# which is either the python value being serialized
# or the JSON value being deserialized

# use the validator to prompt the user for the NamedTuple data
# name: str automatically uses a generic string prompt
# duration: timedelta gets handled by the type_validator
a = prompt_namedtuple(
    Action,
    type_validators={
        timedelta: timedelta_handler,
    },
)

# Note: this specifies timedelta as the type,
# not int. It uses what the NamedTuple
# specifies as the type for that field, not
# the type of the value thats loaded from JSON

# dump to JSON
a_str: str = namedtuple_sequence_dumps(
    [a],
    type_serializers={
        timedelta: to_seconds,
    },
    indent=None,
Exemplo n.º 6
0
def test_no_way_to_handle_propting() -> None:
    with pytest.raises(AutoTUIException,
                       match=r"no way to handle prompting timedelta"):
        autotui.prompt_namedtuple(Action)
Exemplo n.º 7
0
import autotui
from typing import NamedTuple
from datetime import datetime

# something to persist to a file
class Water(NamedTuple):
    at: datetime
    glass_count: float


w = autotui.prompt_namedtuple(Water)
print(w)

s = autotui.namedtuple_sequence_dumps([w], indent=None)
print(s)

b = autotui.namedtuple_sequence_loads(s, to=Water)
print(b[0])