From 9ebee282fcf686c7fbb99ac914263ec78f61bd6b Mon Sep 17 00:00:00 2001 From: pengWang002 <3363461465@qq.com> Date: Fri, 5 Dec 2025 16:27:37 +0800 Subject: [PATCH 1/2] feat: implementation by trae-04 model --- src/humanize/__init__.py | 2 ++ src/humanize/bitrate.py | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/humanize/bitrate.py diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index 6fb9959..afbb6f3 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from humanize.filesize import naturalsize +from humanize.bitrate import natural_bitrate from humanize.i18n import activate, deactivate, decimal_separator, thousands_separator from humanize.lists import natural_list from humanize.number import ( @@ -41,6 +42,7 @@ "naturalday", "naturaldelta", "naturalsize", + "natural_bitrate", "naturaltime", "ordinal", "precisedelta", diff --git a/src/humanize/bitrate.py b/src/humanize/bitrate.py new file mode 100644 index 0000000..65a5bb5 --- /dev/null +++ b/src/humanize/bitrate.py @@ -0,0 +1,50 @@ +"""Bitrate related humanization.""" + +from __future__ import annotations + +from math import log + +def natural_bitrate(value: float | str, format: str = "%.1f") -> str: + """Format a number of bits per second like a human-readable bitrate (e.g. 1 kbps). + + Uses SI standard (1000-based) prefixes. + + Examples: + ```pycon + >>> natural_bitrate(1000) + '1.0 kbps' + >>> natural_bitrate(5200) + '5.2 kbps' + >>> natural_bitrate(1000000) + '1.0 Mbps' + >>> natural_bitrate(1500000000) + '1.5 Gbps' + >>> natural_bitrate(1000000000000) + '1.0 Tbps' + >>> natural_bitrate(500) + '500 bps' + >>> natural_bitrate(0) + '0 bps' + >>> natural_bitrate(-1000) + '-1.0 kbps' + + ``` + + Args: + value (int, float, str): Integer to convert. + format (str): Custom formatter. + + Returns: + str: Human readable representation of a bitrate. + """ + suffixes = (" kbps", " Mbps", " Gbps", " Tbps") + base = 1000 + bps = float(value) + abs_bps = abs(bps) + + if abs_bps < base: + return f"{int(bps)} bps" + + exp = int(min(log(abs_bps, base), len(suffixes))) + ret: str = format % (bps / (base ** exp)) + suffixes[exp - 1] + return ret From f9052081d3ef74908e3748eebd9940c4f15e435f 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:30:48 +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 | 4 ++-- src/humanize/bitrate.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index afbb6f3..ab3aaef 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -2,8 +2,8 @@ from __future__ import annotations -from humanize.filesize import naturalsize 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 from humanize.number import ( @@ -37,12 +37,12 @@ "intcomma", "intword", "metric", + "natural_bitrate", "natural_list", "naturaldate", "naturalday", "naturaldelta", "naturalsize", - "natural_bitrate", "naturaltime", "ordinal", "precisedelta", diff --git a/src/humanize/bitrate.py b/src/humanize/bitrate.py index 65a5bb5..ba0655c 100644 --- a/src/humanize/bitrate.py +++ b/src/humanize/bitrate.py @@ -4,6 +4,7 @@ from math import log + def natural_bitrate(value: float | str, format: str = "%.1f") -> str: """Format a number of bits per second like a human-readable bitrate (e.g. 1 kbps). @@ -46,5 +47,5 @@ def natural_bitrate(value: float | str, format: str = "%.1f") -> str: return f"{int(bps)} bps" exp = int(min(log(abs_bps, base), len(suffixes))) - ret: str = format % (bps / (base ** exp)) + suffixes[exp - 1] + ret: str = format % (bps / (base**exp)) + suffixes[exp - 1] return ret