From 32ce6fa4de0ba4743b2b63285a2b8ac3884d888c Mon Sep 17 00:00:00 2001 From: Minku Koo Date: Sun, 17 Nov 2024 22:14:07 +0900 Subject: [PATCH 1/6] Refactor: Replace hardcoded time units with named constants - Replaced hardcoded time-related values with named constants. - Facilitates easier adjustments of these values and reduces potential for errors.| --- .../formatting/difference_formatter.py | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/pendulum/formatting/difference_formatter.py b/src/pendulum/formatting/difference_formatter.py index 588c0727..e0325f5d 100644 --- a/src/pendulum/formatting/difference_formatter.py +++ b/src/pendulum/formatting/difference_formatter.py @@ -4,6 +4,17 @@ from pendulum.locales.locale import Locale +DAYS_THRESHOLD_FOR_HALF_WEEK = 3 +DAYS_THRESHOLD_FOR_HALF_MONTH = 15 +MONTHS_THRESHOLD_FOR_HALF_YEAR = 6 + +HOURS_IN_NEARLY_A_DAY = 22 +DAYS_IN_NEARLY_A_MONTH = 27 +MONTHS_IN_NEARLY_A_YEAR = 11 + +DAYS_OF_WEEK = 7 +SECONDS_OF_MINUTE = 60 +FEW_SECONDS_MAX = 10 if t.TYPE_CHECKING: from pendulum import Duration @@ -14,6 +25,11 @@ class DifferenceFormatter: Handles formatting differences in text. """ + KEY_FUTURE = ".future" + KEY_PAST = ".past" + KEY_AFTER = ".after" + KEY_BEFORE = ".before" + def __init__(self, locale: str = "en") -> None: self._locale = Locale.load(locale) @@ -38,28 +54,29 @@ def format( unit = "year" count = diff.years - if diff.months > 6: + if diff.months > MONTHS_THRESHOLD_FOR_HALF_YEAR: count += 1 - elif diff.months == 11 and (diff.weeks * 7 + diff.remaining_days) > 15: + elif (diff.months == MONTHS_IN_NEARLY_A_YEAR + and (diff.weeks * DAYS_OF_WEEK + diff.remaining_days) > DAYS_THRESHOLD_FOR_HALF_MONTH): unit = "year" count = 1 elif diff.months > 0: unit = "month" count = diff.months - if (diff.weeks * 7 + diff.remaining_days) >= 27: + if (diff.weeks * DAYS_OF_WEEK + diff.remaining_days) >= DAYS_IN_NEARLY_A_MONTH: count += 1 elif diff.weeks > 0: unit = "week" count = diff.weeks - if diff.remaining_days > 3: + if diff.remaining_days > DAYS_THRESHOLD_FOR_HALF_WEEK: count += 1 elif diff.remaining_days > 0: unit = "day" count = diff.remaining_days - if diff.hours >= 22: + if diff.hours >= HOURS_IN_NEARLY_A_DAY: count += 1 elif diff.hours > 0: unit = "hour" @@ -67,7 +84,7 @@ def format( elif diff.minutes > 0: unit = "minute" count = diff.minutes - elif 10 < diff.remaining_seconds <= 59: + elif self.FEW_SECONDS_MAX < diff.remaining_seconds < SECONDS_OF_MINUTE: unit = "second" count = diff.remaining_seconds else: @@ -86,10 +103,10 @@ def format( key += ".ago" else: if is_future: - key += ".after" + key += self.KEY_AFTER else: - key += ".before" - + key += self.KEY_BEFORE + return t.cast(str, locale.get(key).format(time)) else: unit = "second" @@ -109,9 +126,9 @@ def format( key = f"translations.relative.{unit}" if is_future: - key += ".future" + key += self.KEY_FUTURE else: - key += ".past" + key += self.KEY_PAST else: # Absolute comparison # So we have to use the custom locale data @@ -119,9 +136,9 @@ def format( # Checking for special pluralization rules key = "custom.units_relative" if is_future: - key += f".{unit}.future" + key += f".{unit}{self.KEY_FUTURE}" else: - key += f".{unit}.past" + key += f".{unit}{self.KEY_PAST}" trans = locale.get(key) if not trans: @@ -133,10 +150,9 @@ def format( key = "custom" if is_future: - key += ".after" + key += self.KEY_AFTER else: - key += ".before" - + key += self.KEY_BEFORE return t.cast(str, locale.get(key).format(time)) key += f".{locale.plural(count)}" From eee1ebceeec9adad232f3d5741fccc461e1e4d5f Mon Sep 17 00:00:00 2001 From: Minku Koo Date: Wed, 27 Nov 2024 23:02:47 +0900 Subject: [PATCH 2/6] Refactor: Use direct reference to constants instead of `self` - Constants declared outside the class should be directly accessed without using `self`. --- src/pendulum/formatting/difference_formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pendulum/formatting/difference_formatter.py b/src/pendulum/formatting/difference_formatter.py index e0325f5d..1ab46f3b 100644 --- a/src/pendulum/formatting/difference_formatter.py +++ b/src/pendulum/formatting/difference_formatter.py @@ -84,7 +84,7 @@ def format( elif diff.minutes > 0: unit = "minute" count = diff.minutes - elif self.FEW_SECONDS_MAX < diff.remaining_seconds < SECONDS_OF_MINUTE: + elif FEW_SECONDS_MAX < diff.remaining_seconds < SECONDS_OF_MINUTE: unit = "second" count = diff.remaining_seconds else: From 1a779636015bcb9e47e52cf57612ac483d6bdbbb Mon Sep 17 00:00:00 2001 From: Minku Koo Date: Mon, 9 Dec 2024 23:51:23 +0900 Subject: [PATCH 3/6] Fix: Resolve code style issues using pre-commit hooks - Ensured all files pass pre-commit checks - Fixed line length violations (E501) in `difference_formatter.py` --- src/pendulum/formatting/difference_formatter.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/pendulum/formatting/difference_formatter.py b/src/pendulum/formatting/difference_formatter.py index 1ab46f3b..05b18282 100644 --- a/src/pendulum/formatting/difference_formatter.py +++ b/src/pendulum/formatting/difference_formatter.py @@ -4,6 +4,7 @@ from pendulum.locales.locale import Locale + DAYS_THRESHOLD_FOR_HALF_WEEK = 3 DAYS_THRESHOLD_FOR_HALF_MONTH = 15 MONTHS_THRESHOLD_FOR_HALF_YEAR = 6 @@ -56,15 +57,19 @@ def format( if diff.months > MONTHS_THRESHOLD_FOR_HALF_YEAR: count += 1 - elif (diff.months == MONTHS_IN_NEARLY_A_YEAR - and (diff.weeks * DAYS_OF_WEEK + diff.remaining_days) > DAYS_THRESHOLD_FOR_HALF_MONTH): + elif (diff.months == MONTHS_IN_NEARLY_A_YEAR) and ( + (diff.weeks * DAYS_OF_WEEK + diff.remaining_days) + > DAYS_THRESHOLD_FOR_HALF_MONTH + ): unit = "year" count = 1 elif diff.months > 0: unit = "month" count = diff.months - if (diff.weeks * DAYS_OF_WEEK + diff.remaining_days) >= DAYS_IN_NEARLY_A_MONTH: + if ( + diff.weeks * DAYS_OF_WEEK + diff.remaining_days + ) >= DAYS_IN_NEARLY_A_MONTH: count += 1 elif diff.weeks > 0: unit = "week" @@ -106,7 +111,7 @@ def format( key += self.KEY_AFTER else: key += self.KEY_BEFORE - + return t.cast(str, locale.get(key).format(time)) else: unit = "second" From 6095771129f3df5f9da13d9e56b691ce53777916 Mon Sep 17 00:00:00 2001 From: Minku Koo Date: Tue, 10 Dec 2024 23:36:44 +0900 Subject: [PATCH 4/6] Refactor: Move define declarations from class level to class methods - solve impl problem --- .../formatting/difference_formatter.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/pendulum/formatting/difference_formatter.py b/src/pendulum/formatting/difference_formatter.py index 05b18282..0d8b95bf 100644 --- a/src/pendulum/formatting/difference_formatter.py +++ b/src/pendulum/formatting/difference_formatter.py @@ -26,11 +26,6 @@ class DifferenceFormatter: Handles formatting differences in text. """ - KEY_FUTURE = ".future" - KEY_PAST = ".past" - KEY_AFTER = ".after" - KEY_BEFORE = ".before" - def __init__(self, locale: str = "en") -> None: self._locale = Locale.load(locale) @@ -49,6 +44,11 @@ def format( :param absolute: Whether it's an absolute difference or not :param locale: The locale to use """ + KEY_FUTURE = ".future" + KEY_PAST = ".past" + KEY_AFTER = ".after" + KEY_BEFORE = ".before" + locale = self._locale if locale is None else Locale.load(locale) if diff.years > 0: @@ -108,9 +108,9 @@ def format( key += ".ago" else: if is_future: - key += self.KEY_AFTER + key += KEY_AFTER else: - key += self.KEY_BEFORE + key += KEY_BEFORE return t.cast(str, locale.get(key).format(time)) else: @@ -131,9 +131,9 @@ def format( key = f"translations.relative.{unit}" if is_future: - key += self.KEY_FUTURE + key += KEY_FUTURE else: - key += self.KEY_PAST + key += KEY_PAST else: # Absolute comparison # So we have to use the custom locale data @@ -141,9 +141,9 @@ def format( # Checking for special pluralization rules key = "custom.units_relative" if is_future: - key += f".{unit}{self.KEY_FUTURE}" + key += f".{unit}{KEY_FUTURE}" else: - key += f".{unit}{self.KEY_PAST}" + key += f".{unit}{KEY_PAST}" trans = locale.get(key) if not trans: @@ -155,9 +155,9 @@ def format( key = "custom" if is_future: - key += self.KEY_AFTER + key += KEY_AFTER else: - key += self.KEY_BEFORE + key += KEY_BEFORE return t.cast(str, locale.get(key).format(time)) key += f".{locale.plural(count)}" From fcd45df64ea4477be8df96b66898d5d1fe077693 Mon Sep 17 00:00:00 2001 From: Minku Koo Date: Tue, 10 Dec 2024 23:42:58 +0900 Subject: [PATCH 5/6] Refactor: Move define declarations from global level to class methods --- .../formatting/difference_formatter.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/pendulum/formatting/difference_formatter.py b/src/pendulum/formatting/difference_formatter.py index 0d8b95bf..bfe5dee4 100644 --- a/src/pendulum/formatting/difference_formatter.py +++ b/src/pendulum/formatting/difference_formatter.py @@ -5,18 +5,6 @@ from pendulum.locales.locale import Locale -DAYS_THRESHOLD_FOR_HALF_WEEK = 3 -DAYS_THRESHOLD_FOR_HALF_MONTH = 15 -MONTHS_THRESHOLD_FOR_HALF_YEAR = 6 - -HOURS_IN_NEARLY_A_DAY = 22 -DAYS_IN_NEARLY_A_MONTH = 27 -MONTHS_IN_NEARLY_A_YEAR = 11 - -DAYS_OF_WEEK = 7 -SECONDS_OF_MINUTE = 60 -FEW_SECONDS_MAX = 10 - if t.TYPE_CHECKING: from pendulum import Duration @@ -44,6 +32,18 @@ def format( :param absolute: Whether it's an absolute difference or not :param locale: The locale to use """ + DAYS_THRESHOLD_FOR_HALF_WEEK = 3 + DAYS_THRESHOLD_FOR_HALF_MONTH = 15 + MONTHS_THRESHOLD_FOR_HALF_YEAR = 6 + + HOURS_IN_NEARLY_A_DAY = 22 + DAYS_IN_NEARLY_A_MONTH = 27 + MONTHS_IN_NEARLY_A_YEAR = 11 + + DAYS_OF_WEEK = 7 + SECONDS_OF_MINUTE = 60 + FEW_SECONDS_MAX = 10 + KEY_FUTURE = ".future" KEY_PAST = ".past" KEY_AFTER = ".after" From c8f097b8e03d2517b2a880eefe50314cce93f799 Mon Sep 17 00:00:00 2001 From: Minku Koo Date: Wed, 11 Dec 2024 00:36:14 +0900 Subject: [PATCH 6/6] Fix release builds --- .github/workflows/release.yml | 89 +++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 35 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 58214074..6e220862 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,43 +18,62 @@ jobs: include: - os: ubuntu platform: linux - - os: windows - ls: dir - interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 pypy3.8 pypy3.9 pypy3.10 - - os: windows - ls: dir - target: aarch64 - interpreter: 3.11 3.12 - - os: macos - target: aarch64 - interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 pypy3.8 pypy3.9 pypy3.10 - os: ubuntu platform: linux target: aarch64 # mimalloc not supported on manylinux2014 cross-compile container extra-build-args: --no-default-features - # musllinux - - os: ubuntu - platform: linux - target: x86_64 - manylinux: musllinux_1_1 - - os: ubuntu - platform: linux - target: aarch64 - manylinux: musllinux_1_1 - os: ubuntu platform: linux target: ppc64le - interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 3.13 # mimalloc not supported on manylinux2014 cross-compile container extra-build-args: --no-default-features - os: ubuntu platform: linux target: s390x - interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 3.13 # mimalloc not supported on manylinux2014 cross-compile container extra-build-args: --no-default-features + # musllinux + - os: ubuntu + platform: linux + target: x86_64 + manylinux: musllinux_1_1 + - os: ubuntu + platform: linux + target: aarch64 + manylinux: musllinux_1_1 + + # - os: windows + # ls: dir + # interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.8 pypy3.9 pypy3.10 + # - os: windows + # ls: dir + # target: aarch64 + # interpreter: 3.11 3.12 + + - os: windows + target: x86_64 + interpreter: pypy3.9 pypy3.10 + - os: windows + target: i686 + python-architecture: x86 + interpreter: 3.8 3.9 3.10 3.11 3.12 3.13 + - os: windows + target: aarch64 + interpreter: 3.11 3.12 3.13 + + + - os: macos + target: x86_64 + - os: macos + target: aarch64 + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.8 pypy3.9 pypy3.10 + + + runs-on: ${{ matrix.os }}-latest steps: - uses: actions/checkout@v4 @@ -71,7 +90,7 @@ jobs: target: ${{ matrix.target }} manylinux: ${{ matrix.manylinux || 'auto' }} container: ${{ matrix.container }} - args: --release --out dist --interpreter ${{ matrix.interpreter || '3.7 3.8 3.9 3.10 3.11 3.12 pypy3.7 pypy3.8 pypy3.9 pypy3.10' }} ${{ matrix.extra-build-args }} + args: --release --out dist --interpreter ${{ matrix.interpreter || '3.7 3.8 3.9 3.10 3.11 3.12 3.13 pypy3.7 pypy3.8 pypy3.9 pypy3.10' }} ${{ matrix.extra-build-args }} rust-toolchain: stable docker-options: -e CI @@ -130,16 +149,16 @@ jobs: [[ "${GITHUB_REF#refs/tags/}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] \ || echo ::set-output name=prerelease::true - - name: Create Release - uses: ncipollo/release-action@v1 - with: - artifacts: "dist/*" - token: ${{ secrets.GITHUB_TOKEN }} - draft: false - prerelease: steps.check-version.outputs.prerelease == 'true' - - - name: Publish to PyPI - env: - POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }} - run: | - poetry publish + # - name: Create Release + # uses: ncipollo/release-action@v1 + # with: + # artifacts: "dist/*" + # token: ${{ secrets.GITHUB_TOKEN }} + # draft: false + # prerelease: steps.check-version.outputs.prerelease == 'true' + + # - name: Publish to PyPI + # env: + # POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }} + # run: | + # poetry publish \ No newline at end of file