Skip to content

Commit

Permalink
Merge pull request #183 from hugovk/i18n-intcomma
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Dec 12, 2020
2 parents 89b73e0 + 6ca44f7 commit 97fc008
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 26 deletions.
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

0 comments on commit 97fc008

Please sign in to comment.