From e26874c196b842a47f3770667155cc81662bfbc1 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 31 Dec 2024 11:05:19 -0600 Subject: [PATCH] Transition to pyproject.toml --- CHANGELOG.md | 5 ++++ MANIFEST.in | 2 -- pylintrc | 3 ++- pyproject.toml | 41 ++++++++++++++++++++++++++++++ requirements_dev.txt | 13 +++++----- script/package | 2 +- setup.py | 46 ---------------------------------- tests/test_de.py | 38 ++++++++++++++++++++++++++++ tests/test_es.py | 1 + tox.ini | 2 +- unicode_rbnf/VERSION | 1 - unicode_rbnf/__init__.py | 7 +++--- unicode_rbnf/decimal_format.py | 1 + unicode_rbnf/engine.py | 12 ++++++--- 14 files changed, 110 insertions(+), 64 deletions(-) delete mode 100644 MANIFEST.in create mode 100644 pyproject.toml delete mode 100644 setup.py delete mode 100644 unicode_rbnf/VERSION diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f78866..fa10a3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 2.2.0 + +- Fix use of optional sub part with zero remainder +- Transition to pyproject.toml + ## 2.1.0 - Ensure all supported languages can load diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 2ba003a..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include LICENSE.md -include unicode_rbnf/rbnf/*.xml diff --git a/pylintrc b/pylintrc index 22a70d0..d29f56a 100644 --- a/pylintrc +++ b/pylintrc @@ -31,7 +31,8 @@ disable= missing-class-docstring, missing-function-docstring, import-error, - consider-using-with + consider-using-with, + too-many-positional-arguments [FORMAT] expected-line-ending-format=LF diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b0d614c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,41 @@ +[build-system] +requires = ["setuptools>=62.3"] +build-backend = "setuptools.build_meta" + +[project] +name = "unicode-rbnf" +version = "2.2.0" +license = {text = "MIT"} +description = "Rule-based number formatting using Unicode CLDR data" +readme = "README.md" +authors = [ + {name = "Michael Hansen", email = "mike@rhasspy.org"} +] +keywords = ["rbnf", "unicode", "number", "format"] +classifiers = [ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "Topic :: Text Processing :: Linguistic", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", +] +requires-python = ">=3.8.0" + +[project.urls] +"Source Code" = "https://github.com/rhasspy/unicode-rbnf" + +[tool.setuptools] +platforms = ["any"] +zip-safe = true +include-package-data = true + +[tool.setuptools.packages.find] +include = ["unicode_rbnf"] + +[tool.setuptools.package-data] +unicode_rbnf = ["rbnf/*.xml"] diff --git a/requirements_dev.txt b/requirements_dev.txt index 18755a4..b9d6fd9 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,6 +1,7 @@ -black==22.12.0 -flake8==6.0.0 -isort==5.11.3 -mypy==0.991 -pylint==2.15.9 -pytest==7.4.3 +build>=1,<2 +black>=24,<25 +flake8>=7,<8 +isort>=5,<6 +mypy>=1,<2 +pylint>=3,<4 +pytest>=7,<8 diff --git a/script/package b/script/package index 524f647..86cc374 100755 --- a/script/package +++ b/script/package @@ -9,5 +9,5 @@ _VENV_DIR = _PROGRAM_DIR / ".venv" context = venv.EnvBuilder().ensure_directories(_VENV_DIR) subprocess.check_call( - [context.env_exe, _PROGRAM_DIR / "setup.py", "sdist", "bdist_wheel"] + [context.env_exe, "-m", "build", "--sdist", "--wheel"] ) diff --git a/setup.py b/setup.py deleted file mode 100644 index d8b1a7f..0000000 --- a/setup.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -from pathlib import Path - -import setuptools -from setuptools import setup - -module_name = "unicode_rbnf" -this_dir = Path(__file__).parent -module_dir = this_dir / module_name -data_files = list((module_dir / "rbnf").glob("*.xml")) - -version_path = module_dir / "VERSION" -data_files.append(version_path) -version = version_path.read_text(encoding="utf-8").strip() - -long_description = (this_dir / "README.md").read_text() - -# ----------------------------------------------------------------------------- - -setup( - name=module_name, - version=version, - description="Rule-based number formatting using Unicode CLDR data", - long_description=long_description, - long_description_content_type="text/markdown", - url="http://github.com/rhasspy/unicode-rbnf", - author="Michael Hansen", - author_email="mike@rhasspy.org", - license="MIT", - packages=setuptools.find_packages(), - package_data={ - module_name: [str(p.relative_to(module_dir)) for p in data_files] + ["py.typed"] - }, - classifiers=[ - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Topic :: Text Processing :: Linguistic", - "License :: OSI Approved :: MIT License", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - ], - keywords="rbnf unicode number format", -) diff --git a/tests/test_de.py b/tests/test_de.py index 0dd7a89..c450767 100644 --- a/tests/test_de.py +++ b/tests/test_de.py @@ -3,8 +3,46 @@ def test_german(): engine = RbnfEngine.for_language("de") + assert engine.format_number(1).text == "eins" + assert engine.format_number(2).text == "zwei" + assert engine.format_number(3).text == "drei" + assert engine.format_number(4).text == "vier" + assert engine.format_number(5).text == "fünf" + assert engine.format_number(6).text == "sechs" + assert engine.format_number(7).text == "sieben" + assert engine.format_number(8).text == "acht" + assert engine.format_number(9).text == "neun" + assert engine.format_number(10).text == "zehn" + assert engine.format_number(11).text == "elf" + assert engine.format_number(12).text == "zwölf" assert engine.format_number(13).text == "dreizehn" + assert engine.format_number(14).text == "vierzehn" + assert engine.format_number(15).text == "fünfzehn" + assert engine.format_number(16).text == "sechzehn" + assert engine.format_number(17).text == "siebzehn" + assert engine.format_number(18).text == "achtzehn" + assert engine.format_number(19).text == "neunzehn" + assert engine.format_number(20).text == "zwanzig" + assert engine.format_number(21).text == "einundzwanzig" + assert engine.format_number(22).text == "zweiundzwanzig" + assert engine.format_number(23).text == "dreiundzwanzig" + assert engine.format_number(24).text == "vierundzwanzig" + assert engine.format_number(25).text == "fünfundzwanzig" + assert engine.format_number(26).text == "sechsundzwanzig" + assert engine.format_number(27).text == "siebenundzwanzig" + assert engine.format_number(28).text == "achtundzwanzig" + assert engine.format_number(29).text == "neunundzwanzig" + assert engine.format_number(30).text == "dreißig" assert engine.format_number(32).text == "zweiunddreißig" + assert engine.format_number(100).text == "einhundert" + assert engine.format_number(101).text == "einhunderteins" + assert engine.format_number(120).text == "einhundertzwanzig" + assert engine.format_number(121).text == "einhunderteinundzwanzig" + assert engine.format_number(200).text == "zweihundert" + assert engine.format_number(1000).text == "eintausend" + assert engine.format_number(1001).text == "eintausendeins" + assert engine.format_number(1100).text == "eintausendeinhundert" + assert engine.format_number(1234).text == "eintausendzweihundertvierunddreißig" # All genders, cases assert set(engine.format_number(1).text_by_ruleset.values()) == { diff --git a/tests/test_es.py b/tests/test_es.py index a653e0e..a724d3e 100644 --- a/tests/test_es.py +++ b/tests/test_es.py @@ -6,6 +6,7 @@ def test_german(): assert engine.format_number(5).text == "cinco" assert engine.format_number(2).text == "dos" assert engine.format_number(5.2).text == "cinco coma dos" + assert engine.format_number(21).text == "veintiuno" # All genders assert set(engine.format_number(1).text_by_ruleset.values()) == { diff --git a/tox.ini b/tox.ini index 73cce90..1164e23 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] env_list = - py{38,39,310,311,312} + py{38,39,310,311,312,313} minversion = 4.12.1 [testenv] diff --git a/unicode_rbnf/VERSION b/unicode_rbnf/VERSION deleted file mode 100644 index 7ec1d6d..0000000 --- a/unicode_rbnf/VERSION +++ /dev/null @@ -1 +0,0 @@ -2.1.0 diff --git a/unicode_rbnf/__init__.py b/unicode_rbnf/__init__.py index 76af864..730a63e 100644 --- a/unicode_rbnf/__init__.py +++ b/unicode_rbnf/__init__.py @@ -1,10 +1,11 @@ -from pathlib import Path +"""Rule-based number formatting using Unicode CLDR data.""" + +import importlib.metadata from .engine import FormatOptions, FormatPurpose, FormatResult, RbnfEngine -_DIR = Path(__file__).parent +__version__ = importlib.metadata.version("unicode_rbnf") -__version__ = (_DIR / "VERSION").read_text(encoding="utf-8").strip() __all__ = [ "__version__", diff --git a/unicode_rbnf/decimal_format.py b/unicode_rbnf/decimal_format.py index 99f97d9..ede23a8 100644 --- a/unicode_rbnf/decimal_format.py +++ b/unicode_rbnf/decimal_format.py @@ -2,6 +2,7 @@ See: https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1DecimalFormat.html """ + from decimal import Decimal from typing import Union diff --git a/unicode_rbnf/engine.py b/unicode_rbnf/engine.py index b461b87..59f84cd 100644 --- a/unicode_rbnf/engine.py +++ b/unicode_rbnf/engine.py @@ -599,8 +599,12 @@ def iter_format_number( if part.text: yield part.text elif isinstance(part, SubRulePart): - if (part.type == SubType.QUOTIENT) and (q > 0): - if (q == 0) and (part.ruleset_name is None): + sub_part: SubRulePart = part + + if part.type == SubType.QUOTIENT: + if (q == 0) and ( + sub_part.is_optional or (part.ruleset_name is None) + ): # Rulesets can use quotients of zero continue @@ -614,7 +618,9 @@ def iter_format_number( if part.text_after: yield part.text_after elif part.type == SubType.REMAINDER: - if (r == 0) and (part.ruleset_name is None): + if (r == 0) and ( + sub_part.is_optional or (part.ruleset_name is None) + ): # Rulesets can use remainders of zero continue