From 457994835ae1d27d8fcbe7218857c428d0ebba79 Mon Sep 17 00:00:00 2001 From: NoamNol Date: Mon, 5 Jul 2021 13:41:47 +0300 Subject: [PATCH 1/2] Move to v.0.2.0 and restyle code and usage --- README.md | 9 ++++---- gematriapy/__init__.py | 6 +++++- gematriapy/gematria.py | 22 ++++++++++--------- setup.py | 5 +++-- tests/test_gematria.py | 48 ----------------------------------------- tests/test_to_hebrew.py | 46 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 66 deletions(-) delete mode 100644 tests/test_gematria.py create mode 100644 tests/test_to_hebrew.py diff --git a/README.md b/README.md index 584e867..91ce49f 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,16 @@ pip install gematriapy ## Usage ```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. diff --git a/gematriapy/__init__.py b/gematriapy/__init__.py index e5d0752..40267cc 100644 --- a/gematriapy/__init__.py +++ b/gematriapy/__init__.py @@ -1 +1,5 @@ -from .gematria import Gematria +"""Convert numbers to Hebrew letters""" + +__version__ = "0.2.0" + +from .gematria import to_hebrew diff --git a/gematriapy/gematria.py b/gematriapy/gematria.py index 1feffc8..767d05b 100644 --- a/gematriapy/gematria.py +++ b/gematriapy/gematria.py @@ -1,5 +1,7 @@ -class Gematria: - _HEBREW_LETTERS = [ +from collections import OrderedDict + + +HEBREW_LETTERS: OrderedDict = OrderedDict([ ("א", 1), ("ב", 2), ("ג", 3), @@ -22,26 +24,26 @@ class Gematria: ("ר", 200), ("ש", 300), ("ת", 400) - ] - _REVERSED_HEBREW_LETTERS = _HEBREW_LETTERS[::-1] +]) + - def number_to_hebrew(self, number: int) -> str: +def to_hebrew(number: int) -> str: """ - Convert number to Hebrew letter(s) + Convert number to Hebrew letter(s). Examples: - 2 = "ב" - 47 = "מז" + 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 supported yet") + raise Exception("Number bigger than 999 is not yet supported") remainder = number heb_sum = "" while remainder > 0: - for heb_letter, val in self._REVERSED_HEBREW_LETTERS: + for heb_letter, val in reversed(HEBREW_LETTERS.items()): if (val <= remainder): if remainder == 15: heb_sum += "טו" diff --git a/setup.py b/setup.py index 1cfff4b..df00737 100644 --- a/setup.py +++ b/setup.py @@ -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="noamnol19@gmail.com", - 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", diff --git a/tests/test_gematria.py b/tests/test_gematria.py deleted file mode 100644 index acd345d..0000000 --- a/tests/test_gematria.py +++ /dev/null @@ -1,48 +0,0 @@ -import unittest -from gematriapy import Gematria - - -class Test_Gematria(unittest.TestCase): - gematria = Gematria() - - def test_simple_num(self): - self.assertEqual(self.gematria.number_to_hebrew(3), "ג") - - def test_15_num(self): - self.assertEqual(self.gematria.number_to_hebrew(15), "טו") - - def test_16_num(self): - self.assertEqual(self.gematria.number_to_hebrew(16), "טז") - - def test_random_tens_num_v1(self): - self.assertEqual(self.gematria.number_to_hebrew(24), "כד") - - def test_random_tens_num_v2(self): - self.assertEqual(self.gematria.number_to_hebrew(77), "עז") - - def test_random_hundreds_num_v1(self): - self.assertEqual(self.gematria.number_to_hebrew(250), "רנ") - - def test_random_hundreds_num_v2(self): - self.assertEqual(self.gematria.number_to_hebrew(499), "תצט") - - def test_bigger_than_800_num(self): - self.assertEqual(self.gematria.number_to_hebrew(822), "תתכב") - - def test_hundreds_num_that_ends_with_15(self): - self.assertEqual(self.gematria.number_to_hebrew(515), "תקטו") - - def test_hundreds_num_that_ends_with_16(self): - self.assertEqual(self.gematria.number_to_hebrew(516), "תקטז") - - def test_zero(self): - with self.assertRaises(Exception): - self.gematria.number_to_hebrew(0) - - def test_negative(self): - with self.assertRaises(Exception): - self.gematria.number_to_hebrew(-1) - - def test_too_big(self): - with self.assertRaises(Exception): - self.gematria.number_to_hebrew(1000) diff --git a/tests/test_to_hebrew.py b/tests/test_to_hebrew.py new file mode 100644 index 0000000..99cd4be --- /dev/null +++ b/tests/test_to_hebrew.py @@ -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) From c30a2c01a5ab9fbe5c4c3ed86d13ebac7849a2f2 Mon Sep 17 00:00:00 2001 From: NoamNol Date: Mon, 5 Jul 2021 14:04:52 +0300 Subject: [PATCH 2/2] Add to_number function --- README.md | 19 +++++++- gematriapy/__init__.py | 4 +- gematriapy/gematria.py | 100 +++++++++++++++++++++++----------------- tests/test_to_number.py | 39 ++++++++++++++++ 4 files changed, 116 insertions(+), 46 deletions(-) create mode 100644 tests/test_to_number.py diff --git a/README.md b/README.md index 91ce49f..a8cb4f6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![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 @@ -10,6 +10,7 @@ pip install gematriapy ``` ## Usage +**`to_hebrew`**: ```python import gematriapy gematriapy.to_hebrew(3) # => "ג" @@ -24,3 +25,19 @@ gematriapy.to_hebrew(822) # => "תתכב" ``` > **NOTE**: Numbers greater than 999 are not supported yet. + +
+ +**`to_number`**: +```python +import gematriapy +gematriapy.to_number("ג") # => 3 +``` + +```python +gematriapy.to_number("טו") # => 15 +``` + +```python +gematriapy.to_number("אבא") # => 4 +``` diff --git a/gematriapy/__init__.py b/gematriapy/__init__.py index 40267cc..04d67a5 100644 --- a/gematriapy/__init__.py +++ b/gematriapy/__init__.py @@ -1,5 +1,5 @@ -"""Convert numbers to Hebrew letters""" +"""Convert number to Hebrew or Hebrew to number""" __version__ = "0.2.0" -from .gematria import to_hebrew +from .gematria import to_hebrew, to_number diff --git a/gematriapy/gematria.py b/gematriapy/gematria.py index 767d05b..65c0f87 100644 --- a/gematriapy/gematria.py +++ b/gematriapy/gematria.py @@ -2,57 +2,71 @@ 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) + ("א", 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: + Examples: to_hebrew(2) # => "ב" to_hebrew(47) # => "מז" - """ - if number <= 0: - raise Exception("Number must be bigger than zero") - if number >= 1000: + """ + 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: + 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 + 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 diff --git a/tests/test_to_number.py b/tests/test_to_number.py new file mode 100644 index 0000000..e889423 --- /dev/null +++ b/tests/test_to_number.py @@ -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)