From 62c02c232363ecf85c57a2973ec62d410e726ac7 Mon Sep 17 00:00:00 2001 From: pengWang002 <3363461465@qq.com> Date: Fri, 5 Dec 2025 16:35:08 +0800 Subject: [PATCH 1/2] feat: implementation by trae-09 model --- src/humanize/__init__.py | 2 ++ src/humanize/bitrate.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/humanize/bitrate.py diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index 6fb9959..7b0c614 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -22,6 +22,7 @@ naturaltime, precisedelta, ) +from humanize.bitrate import natural_bitrate from ._version import __version__ @@ -36,6 +37,7 @@ "intcomma", "intword", "metric", + "natural_bitrate", "natural_list", "naturaldate", "naturalday", diff --git a/src/humanize/bitrate.py b/src/humanize/bitrate.py new file mode 100644 index 0000000..99432f6 --- /dev/null +++ b/src/humanize/bitrate.py @@ -0,0 +1,23 @@ +from typing import Union + +def natural_bitrate(value: Union[int, float]) -> str: + """ + Format a bitrate value (bps) into a human-readable string using SI units (1000-based). + + Args: + value: Bitrate in bits per second (bps). + + Returns: + Human-readable bitrate string (e.g., "1 kbps", "5.2 Mbps", "10 Gbps"). + """ + units = ["bps", "kbps", "Mbps", "Gbps", "Tbps"] + unit_index = 0 + + while value >= 1000 and unit_index < len(units) - 1: + value /= 1000 + unit_index += 1 + + if unit_index == 0: + return f"{value:.0f} {units[unit_index]}" + else: + return f"{value:.1f} {units[unit_index]}" \ No newline at end of file From b51680087189a982dd8b13c65b30ddd457959000 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 5 Dec 2025 08:38:04 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/humanize/__init__.py | 2 +- src/humanize/bitrate.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index 7b0c614..ab3aaef 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -2,6 +2,7 @@ from __future__ import annotations +from humanize.bitrate import natural_bitrate from humanize.filesize import naturalsize from humanize.i18n import activate, deactivate, decimal_separator, thousands_separator from humanize.lists import natural_list @@ -22,7 +23,6 @@ naturaltime, precisedelta, ) -from humanize.bitrate import natural_bitrate from ._version import __version__ diff --git a/src/humanize/bitrate.py b/src/humanize/bitrate.py index 99432f6..63ebed8 100644 --- a/src/humanize/bitrate.py +++ b/src/humanize/bitrate.py @@ -1,23 +1,23 @@ -from typing import Union +from __future__ import annotations + + +def natural_bitrate(value: float) -> str: + """Format a bitrate value (bps) into a human-readable string using SI units (1000-based). -def natural_bitrate(value: Union[int, float]) -> str: - """ - Format a bitrate value (bps) into a human-readable string using SI units (1000-based). - Args: value: Bitrate in bits per second (bps). - + Returns: Human-readable bitrate string (e.g., "1 kbps", "5.2 Mbps", "10 Gbps"). """ units = ["bps", "kbps", "Mbps", "Gbps", "Tbps"] unit_index = 0 - + while value >= 1000 and unit_index < len(units) - 1: value /= 1000 unit_index += 1 - + if unit_index == 0: return f"{value:.0f} {units[unit_index]}" else: - return f"{value:.1f} {units[unit_index]}" \ No newline at end of file + return f"{value:.1f} {units[unit_index]}"