Skip to content

Commit

Permalink
fix when method is none in calculation parameters object (#17)
Browse files Browse the repository at this point in the history
* fix when method is none in calculation parameters object

* use typing optional to support older versions
  • Loading branch information
alphahm authored Jan 28, 2023
1 parent 2477ee2 commit fd6fc3f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ venv
*.egg-info/
dist
build
.coverage
.coverage
.idea
24 changes: 13 additions & 11 deletions src/adhanpy/calculation/CalculationMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/adhanpy/calculation/CalculationParameters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -25,29 +26,28 @@ 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
self.adjustments = (
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
if method_adjustments is not None
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:
Expand All @@ -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)
1 change: 1 addition & 0 deletions src/adhanpy/calculation/MethodsParameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


methods_parameters = {
CalculationMethod.NONE: {},
CalculationMethod.MUSLIM_WORLD_LEAGUE: {
"fajr_angle": 18.0,
"isha_angle": 17.0,
Expand Down
37 changes: 37 additions & 0 deletions tests/calculation/test_CalculationParameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fd6fc3f

Please sign in to comment.