From b1f8793d24b579d16a898f6f3f5a6d5992b95378 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 25 Jun 2022 11:31:20 +0300 Subject: [PATCH 1/2] Do not shadow 'bytes' builtin --- src/humanize/filesize.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index 1449600..050bed0 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -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) From 7cc024567ebf1a4197db3613010226b1b12e65e5 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sat, 25 Jun 2022 11:41:59 +0300 Subject: [PATCH 2/2] naturadelta and naturaltime can also accept a float --- src/humanize/time.py | 8 ++++---- tests/test_time.py | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index 373657d..32bfda3 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -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: @@ -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. @@ -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", @@ -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`. diff --git a/tests/test_time.py b/tests/test_time.py index 30539f9..9916865 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -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"), @@ -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"),