From a4deafa41019223005d9f837e31811f74136a604 Mon Sep 17 00:00:00 2001 From: Jeanne Cheneby Date: Fri, 26 Jan 2024 11:24:32 +0100 Subject: [PATCH] Add from_string function --- climate_health/time_period/__init__.py | 33 ++++++++++++++++++++++++-- tests/time_period/test_data_objects.py | 3 +-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/climate_health/time_period/__init__.py b/climate_health/time_period/__init__.py index 0c60e811..cb311b62 100644 --- a/climate_health/time_period/__init__.py +++ b/climate_health/time_period/__init__.py @@ -1,6 +1,35 @@ class TimePeriod: - pass + def __init__(self): + pass + @classmethod + def from_string(cls, time_string): + year, month = time_string.split('-') + return Month(year=year, month=month) class Month: - pass \ No newline at end of file + def __init__(self, year: int|str, month: int|str) -> None: + """ + :param year: + :param month: Starting from 1 + """ + self.year = int(year) + self.month = int(month) + + + def __str__(self) -> str: + dict_month = { + 1: 'January', + 2: 'February', + 3: 'March', + 4: 'April', + 5: 'May', + 6: 'June', + 7: 'July', + 8: 'August', + 9: 'September', + 10: 'October', + 11: 'November', + 12: 'December', + } + return f'{dict_month[self.month]} {self.year}' \ No newline at end of file diff --git a/tests/time_period/test_data_objects.py b/tests/time_period/test_data_objects.py index 597d1457..8086397c 100644 --- a/tests/time_period/test_data_objects.py +++ b/tests/time_period/test_data_objects.py @@ -3,7 +3,6 @@ from climate_health.time_period import TimePeriod, Month -@pytest.mark.xfail def test_time_period(): - month = TimePeriod.from_str('2013-01') + month = TimePeriod.from_string('2013-01') assert str(month) == 'January 2013' \ No newline at end of file