make duration formatting more forgiving

This commit is contained in:
Maximilian Hils 2019-11-07 18:19:50 +01:00
parent 3af4647804
commit 80963966b2
2 changed files with 4 additions and 1 deletions

View File

@ -48,12 +48,14 @@ def parse_size(s: typing.Optional[str]) -> typing.Optional[int]:
raise ValueError("Invalid size specification.")
def pretty_duration(secs):
def pretty_duration(secs: typing.Optional[float]) -> str:
formatters = [
(100, "{:.0f}s"),
(10, "{:2.1f}s"),
(1, "{:1.2f}s"),
]
if secs is None:
return ""
for limit, formatter in formatters:
if secs >= limit:

View File

@ -47,6 +47,7 @@ def test_pretty_duration():
assert human.pretty_duration(10000) == "10000s"
assert human.pretty_duration(1.123) == "1.12s"
assert human.pretty_duration(0.123) == "123ms"
assert human.pretty_duration(None) == ""
def test_format_address():