From 75adb162c973606d1cec86a298474aacc2d7a46c Mon Sep 17 00:00:00 2001 From: Noam Nol Date: Mon, 16 Sep 2024 23:02:22 +0300 Subject: [PATCH] Add add_gershayim --- .flake8 | 1 + README.md | 11 ++++++++--- gematriapy/gematria.py | 24 +++++++++++++++++++++++- tests/test_to_hebrew.py | 23 +++++++++++++++++++++++ tests/test_to_number.py | 3 +++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/.flake8 b/.flake8 index b1ec3fa..5898cc9 100644 --- a/.flake8 +++ b/.flake8 @@ -3,3 +3,4 @@ exclude = .git,__pycache__,docs/conf.py,old,build,dist,.venv,.env per-file-ignores = # ignore "imported but unused" and "no newline at end of file" __init__.py: F401,W292 +max-line-length = 90 diff --git a/README.md b/README.md index db51b7c..a10fb1b 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,13 @@ pip install gematriapy ``` ```python ->>> gematriapy.to_hebrew(822) -"תתכב" +>>> gematriapy.to_hebrew(3, add_gershayim=True) +"ג׳" +``` + +```python +>>> gematriapy.to_hebrew(766, add_gershayim=True) +"תשס״ו" ``` > **NOTE**: Numbers greater than 999 are not supported yet. @@ -41,7 +46,7 @@ pip install gematriapy ``` ```python ->>> gematriapy.to_number("טו") +>>> gematriapy.to_number("ט״ו") 15 ``` diff --git a/gematriapy/gematria.py b/gematriapy/gematria.py index 2623242..c001f76 100644 --- a/gematriapy/gematria.py +++ b/gematriapy/gematria.py @@ -37,7 +37,11 @@ } -def to_hebrew(number: int) -> str: +GERESH = "׳" +GERSHAYIM = "״" + + +def to_hebrew(number: int, *, add_gershayim: bool = False) -> str: """ Convert a number to Hebrew letter(s). @@ -47,7 +51,25 @@ def to_hebrew(number: int) -> str: 'ב' >>> to_hebrew(47) 'מז' + + With flag `add_gershayim`: + + >>> to_hebrew(2, add_gershayim=True) + 'ב׳' + >>> to_hebrew(16, add_gershayim=True) + 'ט״ז' """ + letters = _simple_to_hebrew(number) + if add_gershayim: + if len(letters) == 1: + letters += GERESH + elif len(letters) > 1: + index = len(letters) - 1 + letters = letters[:index] + GERSHAYIM + letters[index:] + return letters + + +def _simple_to_hebrew(number: int) -> str: if number <= 0: raise ValueError("Number must be bigger than zero") if number >= 1000: diff --git a/tests/test_to_hebrew.py b/tests/test_to_hebrew.py index 89576ee..ec3ec25 100644 --- a/tests/test_to_hebrew.py +++ b/tests/test_to_hebrew.py @@ -48,6 +48,10 @@ def test_zero(self): with self.assertRaises(ValueError): gematriapy.to_hebrew(0) + def test_zero_with_add_gershayim(self): + with self.assertRaises(ValueError): + gematriapy.to_hebrew(0, add_gershayim=True) + def test_negative(self): with self.assertRaises(ValueError): gematriapy.to_hebrew(-1) @@ -55,3 +59,22 @@ def test_negative(self): def test_too_big(self): with self.assertRaises(ValueError): gematriapy.to_hebrew(1000) + + def test_add_gershayim_one_letter(self): + self.assertEqual(gematriapy.to_hebrew(3, add_gershayim=True), 'ג׳') + + def test_add_gershayim_two_letters(self): + self.assertEqual(gematriapy.to_hebrew(13, add_gershayim=True), 'י״ג') + + def test_add_gershayim_two_letters_value_16_and_17(self): + """ + 15 and 16 are a special case in Hebrew. + """ + self.assertEqual(gematriapy.to_hebrew(15, add_gershayim=True), 'ט״ו') + self.assertEqual(gematriapy.to_hebrew(16, add_gershayim=True), 'ט״ז') + + def test_add_gershayim_three_letters(self): + self.assertEqual(gematriapy.to_hebrew(115, add_gershayim=True), "קט״ו") + + def test_add_gershayim_four_letters(self): + self.assertEqual(gematriapy.to_hebrew(766, add_gershayim=True), "תשס״ו") diff --git a/tests/test_to_number.py b/tests/test_to_number.py index ccd2859..9ad8534 100644 --- a/tests/test_to_number.py +++ b/tests/test_to_number.py @@ -55,3 +55,6 @@ def test_hebrew_and_non_hebrew(self): def test_hebrew_with_nikud(self): self.assertEqual(gematriapy.to_number("שלום"), 376) self.assertEqual(gematriapy.to_number("שָׁלוֹם"), 376) + + def test_with_gershayim(self): + self.assertEqual(gematriapy.to_number("תשס״ו"), 766)