From fd6fc3f459b132b4f088a0ab91faff20c93bc59a Mon Sep 17 00:00:00 2001 From: alphahm Date: Sat, 28 Jan 2023 15:13:34 +0000 Subject: [PATCH] fix when method is none in calculation parameters object (#17) * fix when method is none in calculation parameters object * use typing optional to support older versions --- .gitignore | 3 +- src/adhanpy/calculation/CalculationMethod.py | 24 ++++++------ .../calculation/CalculationParameters.py | 28 +++++++------- src/adhanpy/calculation/MethodsParameters.py | 1 + .../calculation/test_CalculationParameters.py | 37 +++++++++++++++++++ 5 files changed, 67 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 5a4d78b..646eedf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ venv *.egg-info/ dist build -.coverage \ No newline at end of file +.coverage +.idea \ No newline at end of file diff --git a/src/adhanpy/calculation/CalculationMethod.py b/src/adhanpy/calculation/CalculationMethod.py index d6b0a9d..aea2b80 100644 --- a/src/adhanpy/calculation/CalculationMethod.py +++ b/src/adhanpy/calculation/CalculationMethod.py @@ -3,69 +3,71 @@ class CalculationMethod(Enum): - MUSLIM_WORLD_LEAGUE = 0 + NONE = 0 + + MUSLIM_WORLD_LEAGUE = 1 """ Muslim World League Uses Fajr angle of 18 and an Isha angle of 17 """ - EGYPTIAN = 1 + EGYPTIAN = 2 """ Egyptian General Authority of Survey Uses Fajr angle of 19.5 and an Isha angle of 17.5 """ - KARACHI = 2 + KARACHI = 3 """ University of Islamic Sciences, Karachi Uses Fajr angle of 18 and an Isha angle of 18 """ - UMM_AL_QURA = 3 + UMM_AL_QURA = 4 """ Umm al-Qura University, Makkah Uses a Fajr angle of 18.5 and an Isha angle of 90. Note: You should add a +30 minute custom adjustment of Isha during Ramadan. """ - DUBAI = 4 + DUBAI = 5 """ The Gulf Region Uses Fajr and Isha angles of 18.2 degrees. """ - MOON_SIGHTING_COMMITTEE = 5 + MOON_SIGHTING_COMMITTEE = 6 """ Moonsighting Committee Uses a Fajr angle of 18 and an Isha angle of 18. Also uses seasonal adjustment values. """ - NORTH_AMERICA = 6 + NORTH_AMERICA = 7 """ Referred to as the ISNA method This method is included for completeness, but is not recommended. Uses a Fajr angle of 15 and an Isha angle of 15. """ - KUWAIT = 7 + KUWAIT = 8 """ Kuwait Uses a Fajr angle of 18 and an Isha angle of 17.5 """ - QATAR = 8 + QATAR = 9 """ Qatar Modified version of Umm al-Qura that uses a Fajr angle of 18. """ - SINGAPORE = 9 + SINGAPORE = 10 """ Singapore Uses a Fajr angle of 20 and an Isha angle of 18 """ - UOIF = 10 + UOIF = 11 """ UOIF Uses a Fajr angle of 12 and an Isha angle of 12 diff --git a/src/adhanpy/calculation/CalculationParameters.py b/src/adhanpy/calculation/CalculationParameters.py index 970e119..7aca4b0 100644 --- a/src/adhanpy/calculation/CalculationParameters.py +++ b/src/adhanpy/calculation/CalculationParameters.py @@ -1,3 +1,4 @@ +from typing import Optional from adhanpy.calculation.CalculationMethod import CalculationMethod from adhanpy.calculation.MethodsParameters import methods_parameters from adhanpy.calculation.Madhab import Madhab @@ -9,13 +10,13 @@ class CalculationParameters: def __init__( self, - method: CalculationMethod = None, - adjustments: PrayerAdjustments = None, - method_adjustments: PrayerAdjustments = None, + method: Optional[CalculationMethod] = None, + adjustments: Optional[PrayerAdjustments] = None, + method_adjustments: Optional[PrayerAdjustments] = None, isha_interval: int = 0, fajr_angle: float = 0.0, isha_angle: float = 0.0, - ): + ) -> None: # The madhab used to calculate Asr self.madhab = Madhab.SHAFI @@ -25,10 +26,8 @@ def __init__( # Minutes after Maghrib (if set, the time for Isha will be Maghrib plus isha_interval) self.isha_interval = isha_interval - # angle for calculating fajr + # fajr and isha angles self.fajr_angle = fajr_angle - - # angle for calculating isha self.isha_angle = isha_angle # Used to optionally add or subtract a set amount of time from each prayer time @@ -36,6 +35,11 @@ def __init__( adjustments if adjustments is not None else PrayerAdjustments() ) + # method is last assigned and has precedence and will overwrite other parameters + self.method = ( + method if isinstance(method, CalculationMethod) else CalculationMethod.NONE + ) + # Used for method adjustments self.method_adjustments = ( method_adjustments @@ -43,11 +47,7 @@ def __init__( else PrayerAdjustments() ) - # The method used to do the calculation - # method is last to be assigned, it has precedence and will overwrite some of the other parameters - if isinstance(method, CalculationMethod): - self.method = method - self._set_parameters_using_method(method) + self._set_parameters_using_method() def night_portions(self) -> NightPortions: if self.high_latitude_rule == HighLatitudeRule.MIDDLE_OF_THE_NIGHT: @@ -61,7 +61,7 @@ def night_portions(self) -> NightPortions: raise ValueError("Invalid high latitude rule") - def _set_parameters_using_method(self, calculation_method: CalculationMethod): - method_parameters = methods_parameters[calculation_method] + def _set_parameters_using_method(self) -> None: + method_parameters = methods_parameters[self.method] for key, value in method_parameters.items(): setattr(self, key, value) diff --git a/src/adhanpy/calculation/MethodsParameters.py b/src/adhanpy/calculation/MethodsParameters.py index f30345b..e0d2967 100644 --- a/src/adhanpy/calculation/MethodsParameters.py +++ b/src/adhanpy/calculation/MethodsParameters.py @@ -3,6 +3,7 @@ methods_parameters = { + CalculationMethod.NONE: {}, CalculationMethod.MUSLIM_WORLD_LEAGUE: { "fajr_angle": 18.0, "isha_angle": 17.0, diff --git a/tests/calculation/test_CalculationParameters.py b/tests/calculation/test_CalculationParameters.py index 3f409ce..539cd01 100644 --- a/tests/calculation/test_CalculationParameters.py +++ b/tests/calculation/test_CalculationParameters.py @@ -63,3 +63,40 @@ def test_night_portion_with_invalid_high_latitude_rule(): # Act, Assert with pytest.raises(ValueError, match="Invalid high latitude rule"): parameters.night_portions() + + +def test_method_is_always_set(): + # Arrange + params_method_none = CalculationParameters( + method=None, fajr_angle=18, isha_angle=18 + ) + params_no_method = CalculationParameters(fajr_angle=18, isha_angle=18) + + # Act, Assert + assert params_method_none.method is CalculationMethod.NONE + assert params_no_method.method is CalculationMethod.NONE + + +def test_when_method_is_not_other_parameters_are_not_overwritten(): + # Arrange + params_method_none = CalculationParameters(method=None, fajr_angle=18) + params_no_method = CalculationParameters( + fajr_angle=18, isha_angle=18, isha_interval=90 + ) + + # Act, Assert + assert params_method_none.fajr_angle == 18 + assert params_no_method.fajr_angle == 18 + assert params_no_method.isha_angle == 18 + assert params_no_method.isha_interval == 90 + + +def test_method_has_precedence_over_other_parameters(): + # Arrange + params = CalculationParameters( + method=CalculationMethod.MOON_SIGHTING_COMMITTEE, fajr_angle=10 + ) + + # Act, Assert + # MOON_SIGHTING_COMMITTEE has a fajr_angle of 18 and should overwrite fajr_angle provided + assert params.fajr_angle == 18