Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
parametrized arguments to the task class.
- :pull:`228` removes ``task.pytaskmark`` and moves the information to
:attr:`_pytask.models.CollectionMetadata.markers`.
- :pull:`230` implements :class:`_pytask.logging._TimeUnit` as a
:class:`typing.NamedTuple` for better typing.


0.1.9 - 2022-02-23
Expand Down
54 changes: 27 additions & 27 deletions src/_pytask/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import warnings
from typing import Any
from typing import NamedTuple
from typing import TYPE_CHECKING

import _pytask
Expand All @@ -28,14 +29,6 @@


if TYPE_CHECKING and sys.version_info >= (3, 8):
from typing import TypedDict

class _TimeUnit(TypedDict):
singular: str
plural: str
short: str
in_seconds: int

if sys.version_info >= (3, 8):
from typing import Literal
else:
Expand All @@ -44,6 +37,13 @@ class _TimeUnit(TypedDict):
_ShowTraceback = Literal["no", "yes"]


class _TimeUnit(NamedTuple):
singular: str
plural: str
short: str
in_seconds: int


@hookimpl
def pytask_extend_command_line_interface(cli: click.Group) -> None:
show_locals_option = click.Option(
Expand Down Expand Up @@ -156,10 +156,10 @@ def pytask_log_session_footer(


_TIME_UNITS: list[_TimeUnit] = [
{"singular": "day", "plural": "days", "short": "d", "in_seconds": 86400},
{"singular": "hour", "plural": "hours", "short": "h", "in_seconds": 3600},
{"singular": "minute", "plural": "minutes", "short": "m", "in_seconds": 60},
{"singular": "second", "plural": "seconds", "short": "s", "in_seconds": 1},
_TimeUnit(singular="day", plural="days", short="d", in_seconds=86400),
_TimeUnit(singular="hour", plural="hours", short="h", in_seconds=3600),
_TimeUnit(singular="minute", plural="minutes", short="m", in_seconds=60),
_TimeUnit(singular="second", plural="seconds", short="s", in_seconds=1),
]


Expand Down Expand Up @@ -199,45 +199,45 @@ def _humanize_time(

"""
index = None
for i, entry in enumerate(_TIME_UNITS):
if unit in [entry["singular"], entry["plural"]]:
for i, time_unit in enumerate(_TIME_UNITS):
if unit in [time_unit.singular, time_unit.plural]:
index = i
break
else:
raise ValueError(f"The time unit {unit!r} is not known.")

seconds = amount * _TIME_UNITS[index]["in_seconds"]
seconds = amount * _TIME_UNITS[index].in_seconds
result: list[tuple[float, str]] = []
remaining_seconds = seconds

for entry in _TIME_UNITS:
whole_units = int(remaining_seconds / entry["in_seconds"])
for time_unit in _TIME_UNITS:
whole_units = int(remaining_seconds / time_unit.in_seconds)

if entry["singular"] == "second" and remaining_seconds:
if time_unit.singular == "second" and remaining_seconds:
last_seconds = round(remaining_seconds, 2)
if short_label:
label = entry["short"]
label = time_unit.short
elif last_seconds == 1:
label = entry["singular"]
label = time_unit.singular
else:
label = entry["plural"]
label = time_unit.plural
result.append((last_seconds, label))

elif whole_units >= 1:
if entry["singular"] != "seconds":
if time_unit.singular != "seconds":
if short_label:
label = entry["short"]
label = time_unit.short
elif whole_units == 1:
label = entry["singular"]
label = time_unit.singular
else:
label = entry["plural"]
label = time_unit.plural

result.append((whole_units, label))
remaining_seconds -= whole_units * entry["in_seconds"]
remaining_seconds -= whole_units * time_unit.in_seconds

if not result:
result.append(
(0, _TIME_UNITS[-1]["short"] if short_label else _TIME_UNITS[-1]["plural"])
(0, _TIME_UNITS[-1].short if short_label else _TIME_UNITS[-1].plural)
)

return result