Skip to content

Commit

Permalink
Add add_gershayim
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamNol committed Sep 16, 2024
1 parent e54b0ba commit 75adb16
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,7 +46,7 @@ pip install gematriapy
```

```python
>>> gematriapy.to_number("טו")
>>> gematriapy.to_number("ט״ו")
15
```

Expand Down
24 changes: 23 additions & 1 deletion gematriapy/gematria.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_to_hebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,33 @@ 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)

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), "תשס״ו")
3 changes: 3 additions & 0 deletions tests/test_to_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 75adb16

Please sign in to comment.