Skip to content

refactor: Use compact decimal formatter from Babel #75

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

Merged
merged 5 commits into from
Nov 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 4 additions & 35 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import i18n
import orjson
from babel import Locale, dates, numbers
from babel.dates import format_timedelta
from babel.numbers import format_compact_decimal

i18n.set("filename_format", "{locale}.{format}")
i18n.set("enable_memoization", True)
Expand All @@ -15,7 +16,7 @@
def format_relative_time(timestamp: float, lang: str = "en") -> str:
"""Get relative time from unix timestamp (ex. "3 hours ago")"""
delta = timedelta(seconds=timestamp - datetime.now().timestamp())
return dates.format_timedelta(delta=delta, add_direction=True, locale=lang)
return format_timedelta(delta=delta, add_direction=True, locale=lang)


def data_uri_from_bytes(*, data: bytes, mime_type: str) -> str:
Expand Down Expand Up @@ -62,38 +63,6 @@ def trim_text(text: str, max_length: int) -> str:
return text[: max_length - 1].strip() + "…"


def format_decimal_compact(number: float, lang: str = "en") -> str:
"""Format number with compact notation (ex. "1.2K")

TODO: This can be refactored once it is supported by Babel
Sees https://github.com/python-babel/babel/pull/909
"""
locale = Locale.parse(lang)
compact_format = locale._data["compact_decimal_formats"]["short"]
number_format = None
for magnitude in sorted([int(m) for m in compact_format["other"]], reverse=True):
if abs(number) >= magnitude:
# check the pattern using "other" as the amount
number_format = compact_format["other"][str(magnitude)]
pattern = numbers.parse_pattern(number_format).pattern
# if the pattern is "0", we do not divide the number
if pattern == "0":
break
# otherwise, we need to divide the number by the magnitude but remove zeros
# equal to the number of 0's in the pattern minus 1
number = number / (magnitude / (10 ** (pattern.count("0") - 1)))
# round to the number of fraction digits requested
number = round(number, 1)
# if the remaining number is like "one", use the singular format
plural_form = locale.plural_form(abs(number))
plural_form = plural_form if plural_form in compact_format else "other"
number_format = compact_format[plural_form][str(magnitude)]
break
return numbers.format_decimal(
number=number, format=number_format, locale=lang, decimal_quantization=False
)


def parse_metric_value(value: str) -> int:
"""Parse a metric value (ex. "1.2K" => 1200)

Expand All @@ -111,7 +80,7 @@ def format_views_value(value: str, lang: str = "en") -> str:
int_value = parse_metric_value(value)
if int_value == 1:
return i18n.t("view", locale=lang)
formatted_value = format_decimal_compact(int_value, lang=lang)
formatted_value = format_compact_decimal(int_value, locale=lang, fraction_digits=1)
return i18n.t("views", number=formatted_value, locale=lang)


Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest
black
pre-commit
taskipy
pyright
pyright
types-babel
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Flask==2.2.2
gunicorn>=20.1.0
orjson==3.8.1
python-i18n[yaml]==0.3.9
Babel==2.10.3
Babel==2.11.0
23 changes: 0 additions & 23 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
data_uri_from_url,
estimate_duration_width,
fetch_views,
format_decimal_compact,
format_relative_time,
format_views_value,
parse_metric_value,
Expand Down Expand Up @@ -50,28 +49,6 @@ def test_format_relative_time_i18n():
assert format_relative_time(datetime.now().timestamp() - 3600, "fr") == "il y a 1 heure"


def test_format_decimal_compact():
assert format_decimal_compact(1) == "1"
assert format_decimal_compact(500) == "500"
assert format_decimal_compact(1000) == "1K"
assert format_decimal_compact(1500) == "1.5K"
assert format_decimal_compact(1000000) == "1M"
assert format_decimal_compact(1500000) == "1.5M"
assert format_decimal_compact(1000000000) == "1B"
assert format_decimal_compact(1500000000) == "1.5B"


def test_format_decimal_compact_i18n():
assert format_decimal_compact(1, "fr") == "1"
assert format_decimal_compact(500, "fr") == "500"
assert format_decimal_compact(1000, "fr") == "1\xa0k"
assert format_decimal_compact(1500, "fr") == "1,5\xa0k"
assert format_decimal_compact(1000000, "fr") == "1\xa0M"
assert format_decimal_compact(1500000, "fr") == "1,5\xa0M"
assert format_decimal_compact(1000000000, "fr") == "1\xa0Md"
assert format_decimal_compact(1500000000, "fr") == "1,5\xa0Md"


def test_parse_metric_value():
assert parse_metric_value("1") == 1
assert parse_metric_value("100") == 100
Expand Down