Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
majiidd committed Mar 24, 2024
1 parent b6152a4 commit a52ea89
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
GIT_ASK_YESNO: "false"
PYTHONIOENCODING: "utf-8"
run: |
pipenv run pytest -ra --full-trace --cov=persiantools --cov-report xml:coverage.xml tests/
pipenv run pytest -ra --cov=persiantools --cov-report xml:coverage.xml tests/
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
16 changes: 11 additions & 5 deletions persiantools/digits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from persiantools import utils
import re

EN_TO_FA_MAP = {
"0": "۰",
Expand Down Expand Up @@ -48,6 +48,12 @@
"۸": "٨",
"۹": "٩",
}

EN_TO_FA_REGEX = re.compile("|".join(EN_TO_FA_MAP.keys()))
AR_TO_FA_REGEX = re.compile("|".join(AR_TO_FA_MAP.keys()))
FA_TO_EN_REGEX = re.compile("|".join(FA_TO_EN_MAP.keys()))
FA_TO_AR_REGEX = re.compile("|".join(FA_TO_AR_MAP.keys()))

ONES = ("یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه")
TENS = ("بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود")
HUNDREDS = ("یکصد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد")
Expand Down Expand Up @@ -101,7 +107,7 @@ def en_to_fa(string: str) -> str:
:param string: A string, will be converted
:rtype: str
"""
return utils.replace(string, EN_TO_FA_MAP)
return EN_TO_FA_REGEX.sub(lambda x: EN_TO_FA_MAP[x.group()], string)


def ar_to_fa(string: str) -> str:
Expand All @@ -114,7 +120,7 @@ def ar_to_fa(string: str) -> str:
:param string: A string, will be converted
:rtype: str
"""
return utils.replace(string, AR_TO_FA_MAP)
return AR_TO_FA_REGEX.sub(lambda x: AR_TO_FA_MAP[x.group()], string)


def fa_to_en(string: str) -> str:
Expand All @@ -127,7 +133,7 @@ def fa_to_en(string: str) -> str:
:param string: A string, will be converted
:rtype: str
"""
return utils.replace(string, FA_TO_EN_MAP)
return FA_TO_EN_REGEX.sub(lambda x: FA_TO_EN_MAP[x.group()], string)


def fa_to_ar(string: str) -> str:
Expand All @@ -140,7 +146,7 @@ def fa_to_ar(string: str) -> str:
:param string: A string, will be converted
:rtype: str
"""
return utils.replace(string, FA_TO_AR_MAP)
return FA_TO_AR_REGEX.sub(lambda x: FA_TO_AR_MAP[x.group()], string)


def _to_word(number: int, depth: bool) -> str:
Expand Down
18 changes: 13 additions & 5 deletions persiantools/jdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,19 @@ def check_date(cls, year, month, day):

@staticmethod
def is_leap(year):
assert MINYEAR <= year <= MAXYEAR
"""
This function calculates whether a given year in the Persian calendar is a leap year.
It calculates `((year + 2346) * 683) % 2820`. This expression does a few things:
- It offsets the input year by 2346. This is done to align the Persian calendar with the astronomical solar year.
- It multiplies the result by 683, which is the number of leap years in a 2820-year cycle.
- It then takes the result modulo 2820 to get the position of the year within the current 2820-year cycle.
c = 0.24219858156028368 # 683 / 2820
# return ((year + 2346) * 683) % 2820 < 683
return ((year + 2346) * c) % 1 < c
If the result of this calculation is less than 683, the year is a leap year in the Persian calendar.
This is because there are 683 leap years in each 2820-year cycle of the Persian calendar.
"""
assert MINYEAR <= year <= MAXYEAR
return ((year + 2346) * 683) % 2820 < 683

@classmethod
def days_in_month(cls, month, year):
Expand Down Expand Up @@ -1037,7 +1045,7 @@ def __seqToRE(self, to_convert, directive):
else:
return ""
regex = "|".join(re_escape(stuff) for stuff in to_convert)
regex = '(?P<%s>%s' % (directive, regex)
regex = f"(?P<{directive}>{regex}"
return "%s)" % regex

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def readme():
author="Majid Hajiloo",
author_email="majid.hajiloo@gmail.com",
license="MIT",
license_files = ("LICENSE",),
license_files=("LICENSE",),
packages=["persiantools"],
python_requires=">=3.8",
tests_require=["pytest", "pytest-cov"],
Expand Down
14 changes: 14 additions & 0 deletions tests/test_jalalidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def test_additions(self):
JalaliDate(1400, 1, 1, "us")

def test_leap(self):
self.assertEqual(JalaliDate.is_leap(1214), True)
self.assertEqual(JalaliDate.is_leap(1216), False)
self.assertEqual(JalaliDate.is_leap(1218), True)
self.assertEqual(JalaliDate.is_leap(1309), True)
self.assertEqual(JalaliDate.is_leap(1313), True)
self.assertEqual(JalaliDate.is_leap(1321), True)
self.assertEqual(JalaliDate.is_leap(1342), True)
self.assertEqual(JalaliDate.is_leap(1346), True)
self.assertEqual(JalaliDate.is_leap(1358), True)
self.assertEqual(JalaliDate.is_leap(1366), True)
self.assertEqual(JalaliDate.is_leap(1367), False)
Expand All @@ -131,7 +139,13 @@ def test_leap(self):
self.assertEqual(JalaliDate.is_leap(1398), False)
self.assertEqual(JalaliDate.is_leap(1399), True)
self.assertEqual(JalaliDate.is_leap(1400), False)
self.assertEqual(JalaliDate.is_leap(1402), False)
self.assertEqual(JalaliDate.is_leap(1403), True)
self.assertEqual(JalaliDate.is_leap(1407), False)
self.assertEqual(JalaliDate.is_leap(1408), True)
self.assertEqual(JalaliDate.is_leap(1424), True)
self.assertEqual(JalaliDate.is_leap(1474), True)
self.assertEqual(JalaliDate.is_leap(1498), True)

def test_format(self):
j = JalaliDate(date(1988, 5, 4))
Expand Down

0 comments on commit a52ea89

Please sign in to comment.