Skip to content

Commit

Permalink
Merge pull request #2 from NoamNol/v0.2.0
Browse files Browse the repository at this point in the history
V0.2.0: Restyle and add to_number function
  • Loading branch information
NoamNol authored Jul 5, 2021
2 parents 17e2501 + c30a2c0 commit c342081
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 113 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,42 @@
![CI](https://github.com/NoamNol/gematriapy/workflows/CI/badge.svg)
![PyPI Deploy](https://github.com/NoamNol/gematriapy/workflows/PyPI%20Deploy/badge.svg)

Convert numbers to Hebrew letters
Convert number to Hebrew or Hebrew to number

## Install
```bash
pip install gematriapy
```

## Usage
**`to_hebrew`**:
```python
from gematriapy import Gematria
gematria = Gematria()
gematria.number_to_hebrew(3) # => "ג"
import gematriapy
gematriapy.to_hebrew(3) # => "ג"
```

```python
gematria.number_to_hebrew(15) # => "טו"
gematriapy.to_hebrew(15) # => "טו"
```

```python
gematria.number_to_hebrew(822) # => "תתכב"
gematriapy.to_hebrew(822) # => "תתכב"
```

> **NOTE**: Numbers greater than 999 are not supported yet.
<br/>

**`to_number`**:
```python
import gematriapy
gematriapy.to_number("ג") # => 3
```

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

```python
gematriapy.to_number("אבא") # => 4
```
6 changes: 5 additions & 1 deletion gematriapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .gematria import Gematria
"""Convert number to Hebrew or Hebrew to number"""

__version__ = "0.2.0"

from .gematria import to_hebrew, to_number
128 changes: 72 additions & 56 deletions gematriapy/gematria.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,72 @@
class Gematria:
_HEBREW_LETTERS = [
("א", 1),
("ב", 2),
("ג", 3),
("ד", 4),
("ה", 5),
("ו", 6),
("ז", 7),
("ח", 8),
("ט", 9),
("י", 10),
("כ", 20),
("ל", 30),
("מ", 40),
("נ", 50),
("ס", 60),
("ע", 70),
("פ", 80),
("צ", 90),
("ק", 100),
("ר", 200),
("ש", 300),
("ת", 400)
]
_REVERSED_HEBREW_LETTERS = _HEBREW_LETTERS[::-1]

def number_to_hebrew(self, number: int) -> str:
"""
Convert number to Hebrew letter(s)
Examples:
2 = "ב"
47 = "מז"
"""
if number <= 0:
raise Exception("Number must be bigger than zero")
if number >= 1000:
raise Exception("Number bigger than 999 is not supported yet")

remainder = number
heb_sum = ""
while remainder > 0:
for heb_letter, val in self._REVERSED_HEBREW_LETTERS:
if (val <= remainder):
if remainder == 15:
heb_sum += "טו"
remainder = 0
elif remainder == 16:
heb_sum += "טז"
remainder = 0
else:
heb_sum += heb_letter
remainder -= val
break
return heb_sum
from collections import OrderedDict


HEBREW_LETTERS: OrderedDict = OrderedDict([
("א", 1),
("ב", 2),
("ג", 3),
("ד", 4),
("ה", 5),
("ו", 6),
("ז", 7),
("ח", 8),
("ט", 9),
("י", 10),
("כ", 20),
("ל", 30),
("מ", 40),
("נ", 50),
("ס", 60),
("ע", 70),
("פ", 80),
("צ", 90),
("ק", 100),
("ר", 200),
("ש", 300),
("ת", 400)
])


def to_hebrew(number: int) -> str:
"""
Convert number to Hebrew letter(s).
Examples:
to_hebrew(2) # => "ב"
to_hebrew(47) # => "מז"
"""
if number <= 0:
raise Exception("Number must be bigger than zero")
if number >= 1000:
raise Exception("Number bigger than 999 is not yet supported")

remainder = number
heb_sum = ""
while remainder > 0:
for heb_letter, val in reversed(HEBREW_LETTERS.items()):
if (val <= remainder):
if remainder == 15:
heb_sum += "טו"
remainder = 0
elif remainder == 16:
heb_sum += "טז"
remainder = 0
else:
heb_sum += heb_letter
remainder -= val
break
return heb_sum


def to_number(hebrew: str) -> int:
"""
Convert Hebrew letters to number. Ignore Non-Hebrew letters.
Examples:
to_number("ב") # => 2
to_number("מז") # => 47
"""
sum = 0
for letter in hebrew:
sum += HEBREW_LETTERS.get(letter, 0)
return sum
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import setuptools
import gematriapy

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name="gematriapy",
version="0.1.0",
version=gematriapy.__version__,
author="Noam Nol",
author_email="[email protected]",
description="Convert numbers to Hebrew letters",
description=gematriapy.__doc__,
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/NoamNol/gematriapy",
Expand Down
48 changes: 0 additions & 48 deletions tests/test_gematria.py

This file was deleted.

46 changes: 46 additions & 0 deletions tests/test_to_hebrew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
import gematriapy


class TestGematriapy_ToHebrew(unittest.TestCase):
def test_simple_num(self):
self.assertEqual(gematriapy.to_hebrew(3), "ג")

def test_15_num(self):
self.assertEqual(gematriapy.to_hebrew(15), "טו")

def test_16_num(self):
self.assertEqual(gematriapy.to_hebrew(16), "טז")

def test_random_tens_num_v1(self):
self.assertEqual(gematriapy.to_hebrew(24), "כד")

def test_random_tens_num_v2(self):
self.assertEqual(gematriapy.to_hebrew(77), "עז")

def test_random_hundreds_num_v1(self):
self.assertEqual(gematriapy.to_hebrew(250), "רנ")

def test_random_hundreds_num_v2(self):
self.assertEqual(gematriapy.to_hebrew(499), "תצט")

def test_bigger_than_800_num(self):
self.assertEqual(gematriapy.to_hebrew(822), "תתכב")

def test_hundreds_num_that_ends_with_15(self):
self.assertEqual(gematriapy.to_hebrew(515), "תקטו")

def test_hundreds_num_that_ends_with_16(self):
self.assertEqual(gematriapy.to_hebrew(516), "תקטז")

def test_zero(self):
with self.assertRaises(Exception):
gematriapy.to_hebrew(0)

def test_negative(self):
with self.assertRaises(Exception):
gematriapy.to_hebrew(-1)

def test_too_big(self):
with self.assertRaises(Exception):
gematriapy.to_hebrew(1000)
39 changes: 39 additions & 0 deletions tests/test_to_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
import gematriapy


class TestGematriapy_ToNumber(unittest.TestCase):
def test_single_letter_to_3(self):
self.assertEqual(gematriapy.to_number("ג"), 3)

def test_single_letter_to_9(self):
self.assertEqual(gematriapy.to_number("ט"), 9)

def test_long_hebrew_v1(self):
self.assertEqual(gematriapy.to_number("קכו"), 126)

def test_long_hebrew_v2(self):
self.assertEqual(gematriapy.to_number("רטו"), 215)

def test_illegal_hebrew_number_v1(self):
"""
In Hebrew, letters representing a numerical value should have an order:
big numbers come before small numbers.
So "ת" should come before "י".
to_number should not care about that and just return the 'gematriapy'
value - the sum of the numerical value of the letters.
"""
self.assertEqual(gematriapy.to_number("יתג"), 413)

def test_illegal_hebrew_number_v2(self):
self.assertEqual(gematriapy.to_number("אבא"), 4)

def test_empty(self):
self.assertEqual(gematriapy.to_number(""), 0)

def test_non_hebrew(self):
self.assertEqual(gematriapy.to_number("abc"), 0)

def test_hebrew_and_non_hebrew(self):
self.assertEqual(gematriapy.to_number("aאbהc"), 6)

0 comments on commit c342081

Please sign in to comment.