Skip to content

refactor: Pythonic cleanup of gematria.py and utils.py (regression-tested)#33

Open
Wolfvin wants to merge 1 commit into
simlist:masterfrom
Wolfvin:pythonic-cleanup-with-regression-testing
Open

refactor: Pythonic cleanup of gematria.py and utils.py (regression-tested)#33
Wolfvin wants to merge 1 commit into
simlist:masterfrom
Wolfvin:pythonic-cleanup-with-regression-testing

Conversation

@Wolfvin

@Wolfvin Wolfvin commented Jun 13, 2026

Copy link
Copy Markdown

Pythonic Cleanup — Verified Safe by Regression Testing

This PR contains behavior-preserving refactoring of gematria.py and utils.py, verified using Regrets — an output-fingerprint regression testing tool.

Changes to gematria.py

  • Added module-level docstring with clear purpose description
  • Extracted _SPECIAL_REPLACEMENTS dict for the divine-name-avoidance rule (15→טו, 16→טז), replacing hidden .replace() calls chained together
  • Extracted _apply_special_replacements() as a dedicated, self-documenting function
  • Named constants: _GERESH, _GERSHAYIM, _TAV — replacing inline Unicode characters that are hard to distinguish
  • Improved variable names in _get_letters(): onesones_digit, tenstens_digit, hundredshundreds_raw, four_hundredstav_count/tav_prefix
  • Added comprehensive docstrings with parameter documentation
  • Simplified _stringify_gematria() with early return for empty input

Changes to utils.py

  • Simplified _is_leap() from redundant if (condition): return True; return False to direct return condition, with docstring explaining the 19-year Metonic cycle
  • Extracted _apply_postponement() from _elapsed_days() — the three dehiyyah (postponement) rules (Molad Zakein, Gatarad, Betutakafot) are now in a separate, well-documented function with named boolean variables instead of a deeply nested conditional
  • Used frozenset for month membership testing in _month_length() (O(1) lookup instead of O(n) list scan)
  • Replaced nested if/return with Pythonic ternary expressions for variable-length months
  • Simplified _month_name() with ternary name list selection
  • Added comprehensive docstrings to all modified functions

3-Way Regression Test Verification

Using Regrets, I fingerprinted 7 behavioral clusters before refactoring, then verified after refactoring:

Cluster Entry Function Fingerprint Status
gematria-convert _num_to_str 69vpdvu PASS
hebrew-leap _is_leap 5wtkimf PASS
month-length _month_length fny25om PASS
month-name _month_name 3pfo2au PASS
elapsed-days _elapsed_days 5hj9vhu PASS
get-letters _get_letters 2lcwua6 PASS
stringify-gematria _stringify_gematria 5etsuwj PASS

3 verifications all passed:

  1. Regrets clusters: All 7 GREEN after refactoring
  2. Raw output: Identical to pre-refactor baseline (KEBENARAN 1)
  3. Fingerprint cross-check: All hashes match KEBENARAN 2 exactly

No behavioral changes — only code clarity and maintainability improvements.

Behavior-preserving refactoring verified by Regrets 3-way regression testing.

## Changes to gematria.py
- Added module-level docstring with clear purpose description
- Extracted _SPECIAL_REPLACEMENTS dict for divine-name-avoidance rule
  (15→טו, 16→טז), replacing hidden .replace() calls
- Extracted _apply_special_replacements() as a dedicated function
- Named constants: _GERESH, _GERSHAYIM, _TAV for Unicode characters
- Improved variable names in _get_letters():
  ones→ones_digit, tens→tens_digit, hundreds→hundreds_raw,
  four_hundreds→tav_count/tav_prefix
- Added comprehensive docstrings with parameter documentation
- Simplified _stringify_gematria() with early return pattern

## Changes to utils.py
- Simplified _is_leap() from if/return True/return False to direct
  boolean return, with docstring explaining the Metonic cycle
- Extracted _apply_postponement() from _elapsed_days() with named
  dehiyyah rules: Molad Zakein, Gatarad, Betutakafot
- Used frozenset for month membership testing in _month_length()
  (O(1) lookup instead of O(n) list scan)
- Replaced nested if/return with Pythonic ternary expressions
- Simplified _month_name() with ternary name list selection
- Added comprehensive docstrings to all modified functions

## Verification
7 Regrets clusters fingerprinted and all passed 3-way verification:
1. Regrets clusters: all GREEN after refactoring
2. Raw output: identical to pre-refactor baseline
3. Fingerprint cross-check: all hashes match exactly

Clusters: gematria-convert (69vpdvu), hebrew-leap (5wtkimf),
month-length (fny25om), month-name (3pfo2au), elapsed-days (5hj9vhu),
get-letters (2lcwua6), stringify-gematria (5etsuwj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant