Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update annotations: naturadelta and naturaltime can also accept a float #29

Merged
merged 2 commits into from Jun 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/humanize/filesize.py
Expand Up @@ -55,22 +55,22 @@ def naturalsize(
suffix = suffixes["decimal"]

base = 1024 if (gnu or binary) else 1000
bytes = float(value)
abs_bytes = abs(bytes)
bytes_ = float(value)
abs_bytes = abs(bytes_)

if abs_bytes == 1 and not gnu:
return "%d Byte" % bytes
return "%d Byte" % bytes_
elif abs_bytes < base and not gnu:
return "%d Bytes" % bytes
return "%d Bytes" % bytes_
elif abs_bytes < base and gnu:
return "%dB" % bytes
return "%dB" % bytes_

for i, s in enumerate(suffix):
unit = base ** (i + 2)
if abs_bytes < unit and not gnu:
return (format + " %s") % ((base * bytes / unit), s)
return (format + " %s") % ((base * bytes_ / unit), s)
elif abs_bytes < unit and gnu:
return (format + "%s") % ((base * bytes / unit), s)
return (format + "%s") % ((base * bytes_ / unit), s)
if gnu:
return (format + "%s") % ((base * bytes / unit), s)
return (format + " %s") % ((base * bytes / unit), s)
return (format + "%s") % ((base * bytes_ / unit), s)
return (format + " %s") % ((base * bytes_ / unit), s)
8 changes: 4 additions & 4 deletions src/humanize/time.py
Expand Up @@ -88,7 +88,7 @@ def _date_and_delta(


def naturaldelta(
value: dt.timedelta | int,
value: dt.timedelta | float,
months: bool = True,
minimum_unit: str = "seconds",
) -> str:
Expand All @@ -97,7 +97,7 @@ def naturaldelta(
This is similar to `naturaltime`, but does not add tense to the result.

Args:
value (datetime.timedelta or int): A timedelta or a number of seconds.
value (datetime.timedelta, int or float): A timedelta or a number of seconds.
months (bool): If `True`, then a number of months (based on 30.5 days) will be
used for fuzziness between years.
minimum_unit (str): The lowest unit that can be used.
Expand Down Expand Up @@ -206,7 +206,7 @@ def naturaldelta(


def naturaltime(
value: dt.datetime | int,
value: dt.datetime | float,
future: bool = False,
months: bool = True,
minimum_unit: str = "seconds",
Expand All @@ -217,7 +217,7 @@ def naturaltime(
This is more or less compatible with Django's `naturaltime` filter.

Args:
value (datetime.datetime, int): A `datetime` or a number of seconds.
value (datetime.datetime, int or float): A `datetime` or a number of seconds.
future (bool): Ignored for `datetime`s, where the tense is always figured out
based on the current time. For integers, the return value will be past tense
by default, unless future is `True`.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_time.py
Expand Up @@ -93,6 +93,7 @@ def test_naturaldelta_nomonths(test_input: dt.timedelta, expected: str) -> None:
[
(0, "a moment"),
(1, "a second"),
(23.5, "23 seconds"),
(30, "30 seconds"),
(dt.timedelta(minutes=1, seconds=30), "a minute"),
(dt.timedelta(minutes=2), "2 minutes"),
Expand Down Expand Up @@ -156,6 +157,7 @@ def test_naturaldelta(test_input: int | dt.timedelta, expected: str) -> None:
# regression tests for bugs in post-release humanize
(NOW + dt.timedelta(days=10000), "27 years from now"),
(NOW - dt.timedelta(days=365 + 35), "1 year, 1 month ago"),
(23.5, "23 seconds ago"),
(30, "30 seconds ago"),
(NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
(NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"),
Expand Down