Skip to content
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

Internationalise intcomma and add fr_FR #183

Merged
merged 3 commits into from
Dec 12, 2020
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
16 changes: 11 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ repos:
- id: black
args: ["--target-version", "py36"]

- repo: https://github.com/PyCQA/isort
rev: 5.6.4
hooks:
- id: isort

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
additional_dependencies: [flake8-2020, flake8-implicit-str-concat]

- repo: https://github.com/timothycrosley/isort
rev: 5.6.4
hooks:
- id: isort

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.7.0
hooks:
Expand All @@ -33,6 +33,7 @@ repos:
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer

- repo: https://github.com/PyCQA/pydocstyle
rev: 5.1.1
Expand All @@ -41,6 +42,11 @@ repos:
args: ["--convention", "google"]
files: "src/"

- repo: https://github.com/tox-dev/tox-ini-fmt
rev: 0.5.0
hooks:
- id: tox-ini-fmt

- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.15.1
hooks:
Expand Down
7 changes: 1 addition & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ max_line_length = 88
convention = google

[tool:isort]
known_third_party = freezegun,humanize,pkg_resources,pytest,setuptools
force_grid_wrap = 0
include_trailing_comma = True
line_length = 88
multi_line_output = 3
use_parentheses = True
profile = black

[tool:pytest]
addopts = --color=yes
4 changes: 3 additions & 1 deletion src/humanize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Main package for humanize."""
import pkg_resources

from humanize.filesize import naturalsize
from humanize.i18n import activate, deactivate
from humanize.i18n import activate, deactivate, thousands_separator
from humanize.number import apnumber, fractional, intcomma, intword, ordinal, scientific
from humanize.time import (
naturaldate,
Expand Down Expand Up @@ -30,5 +31,6 @@
"ordinal",
"precisedelta",
"scientific",
"thousands_separator",
"VERSION",
]
21 changes: 20 additions & 1 deletion src/humanize/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import os.path
from threading import local

__all__ = ["activate", "deactivate", "gettext", "ngettext"]
__all__ = ["activate", "deactivate", "gettext", "ngettext", "thousands_separator"]

_TRANSLATIONS = {None: gettext_module.NullTranslations()}
_CURRENT = local()


# Mapping of locale to thousands separator
_THOUSANDS_SEPARATOR = {
"fr_FR": " ",
}


def _get_default_locale_path():
try:
if __file__ is None:
Expand Down Expand Up @@ -129,3 +135,16 @@ def num_name(n):
str: Original text, unchanged.
"""
return message


def thousands_separator() -> str:
"""Return the thousands separator for a locale, default to comma.

Returns:
str: Thousands separator.
"""
try:
sep = _THOUSANDS_SEPARATOR[_CURRENT.locale]
except (AttributeError, KeyError):
sep = ","
return sep
6 changes: 4 additions & 2 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .i18n import gettext as _
from .i18n import gettext_noop as N_
from .i18n import pgettext as P_
from .i18n import thousands_separator


def ordinal(value):
Expand Down Expand Up @@ -97,9 +98,10 @@ def intcomma(value, ndigits=None):
Returns:
str: string containing commas every three digits.
"""
sep = thousands_separator()
try:
if isinstance(value, str):
float(value.replace(",", ""))
float(value.replace(sep, ""))
else:
float(value)
except (TypeError, ValueError):
Expand All @@ -110,7 +112,7 @@ def intcomma(value, ndigits=None):
else:
orig = str(value)

new = re.sub(r"^(-?\d+)(\d{3})", r"\g<1>,\g<2>", orig)
new = re.sub(r"^(-?\d+)(\d{3})", fr"\g<1>{sep}\g<2>", orig)
if orig == new:
return new
else:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

"""Tests for filesize humanizing."""

import humanize
import pytest

import humanize


@pytest.mark.parametrize(
"test_args, expected",
Expand Down
17 changes: 16 additions & 1 deletion tests/test_i18n.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime as dt
import importlib

import humanize
import pytest

import humanize


def test_i18n():
three_seconds = dt.timedelta(seconds=3)
Expand All @@ -26,6 +27,20 @@ def test_i18n():
assert humanize.precisedelta(one_min_three_seconds) == "1 minute and 7 seconds"


def test_intcomma():
number = 10_000_000

assert humanize.intcomma(number) == "10,000,000"

try:
humanize.i18n.activate("fr_FR")
assert humanize.intcomma(number) == "10 000 000"

finally:
humanize.i18n.deactivate()
assert humanize.intcomma(number) == "10,000,000"


def test_default_locale_path_defined__file__():
i18n = importlib.import_module("humanize.i18n")
assert i18n._get_default_locale_path() is not None
Expand Down
3 changes: 2 additions & 1 deletion tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

"""Number tests."""

import humanize
import pytest

import humanize
from humanize import number


Expand Down
3 changes: 2 additions & 1 deletion tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import datetime as dt

import humanize
import pytest
from freezegun import freeze_time

import humanize
from humanize import time

ONE_DAY_DELTA = dt.timedelta(days=1)
Expand Down
19 changes: 12 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
py{36, 37, 38, 39, py3}
py{py3, 39, 38, 37, 36}

[testenv]
extras =
Expand All @@ -9,14 +9,19 @@ commands =
{envpython} -m pytest --cov humanize --cov tests --cov-report xml {posargs}

[testenv:docs]
deps = -r docs/requirements.txt
commands = mkdocs build
deps =
-rdocs/requirements.txt
commands =
mkdocs build

[testenv:lint]
deps = pre-commit
commands = pre-commit run --all-files --show-diff-on-failure
passenv =
PRE_COMMIT_COLOR
skip_install = true
passenv = PRE_COMMIT_COLOR
deps =
pre-commit
commands =
pre-commit run --all-files --show-diff-on-failure

[pytest]
addopts = --doctest-modules
addopts = --doctest-modules