Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/humanize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,6 +37,7 @@
"intcomma",
"intword",
"metric",
"natural_bitrate",
"natural_list",
"naturaldate",
"naturalday",
Expand Down
51 changes: 51 additions & 0 deletions src/humanize/bitrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""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