From 947400e547d7b9b487b519b433b9c6778c20cc8d Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Fri, 9 Aug 2024 14:20:35 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Timestamp=20st=C3=B8tter=20alle=20tidsforma?= =?UTF-8?q?ter=20n=C3=A5,=20pre=20nox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 +- src/ssb_konjunk/timestamp.py | 62 ++++++++++++++++++++++++++++++++---- tests/test_timestamp.py | 42 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6116461..33a0eda 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ssb-konjunk" -version = "0.1.0" +version = "0.1.1" description = "SSB Konjunk" authors = ["Edvard Garmannslund "] license = "MIT" diff --git a/src/ssb_konjunk/timestamp.py b/src/ssb_konjunk/timestamp.py index c27d6bc..53d617e 100644 --- a/src/ssb_konjunk/timestamp.py +++ b/src/ssb_konjunk/timestamp.py @@ -16,6 +16,18 @@ def _check_valid_day(day: int) -> None: raise ValueError( f"The arg for day is smaller than possible min is 1 you have: {day}." ) + + +def _check_valid_week(week: int) -> None: + """Function to check that day arg is valid.""" + if week > 52: + raise ValueError( + f"The arg for week is bigger than possible max is 52 you have: {week}." + ) + if week < 1: + raise ValueError( + f"The arg for week is smaller than possible min is 1 you have: {week}." + ) def _check_valid_month(month: int) -> None: @@ -54,6 +66,30 @@ def _check_valid_quarter(quarter: int) -> None: ) +def _check_valid_trimester(trimester: int) -> None: + """Function to check that day arg is valid.""" + if trimester > 3: + raise ValueError( + f"The arg for trimester is bigger than possible max is 3 you have: {trimester}." + ) + if trimester < 1: + raise ValueError( + f"The arg for trimester is smaller than possible min is 1 you have: {trimester}." + ) + + +def _check_valid_half_year(half_year: int) -> None: + """Function to check that day arg is valid.""" + if half_year > 2: + raise ValueError( + f"The arg for half_year is bigger than possible max is 2 you have: {half_year}." + ) + if half_year < 1: + raise ValueError( + f"The arg for half_year is smaller than possible min is 1 you have: {half_year}." + ) + + def _check_valid_year(year1: int, year2: int | None = None) -> None: """Function to check that year is valid.""" if len(str(year1)) != 4: @@ -76,14 +112,23 @@ def _check_valid_args(*args: int, frequency: str) -> None: """Function to check if valid args.""" if len(args) == 2: _check_valid_year(args[0]) + if frequency == "W": + _check_valid_week(args[1]) if frequency == "M": _check_valid_month(args[1]) if frequency == "B": _check_valid_term(args[1]) if frequency == "Q": _check_valid_quarter(args[1]) + if frequency == "T": + _check_valid_trimester(args[1]) + if frequency == "H": + _check_valid_half_year(args[1]) elif len(args) == 4: _check_valid_year(args[0], args[2]) + if frequency == "W": + _check_valid_week(args[1]) + _check_valid_week(args[3]) if frequency == "M": _check_valid_month(args[1]) _check_valid_month(args[3]) @@ -93,7 +138,12 @@ def _check_valid_args(*args: int, frequency: str) -> None: if frequency == "Q": _check_valid_quarter(args[1]) _check_valid_quarter(args[3]) - + if frequency == "T": + _check_valid_trimester(args[1]) + _check_valid_trimester(args[3]) + if frequency == "H": + _check_valid_half_year(args[1]) + _check_valid_half_year(args[3]) else: raise ValueError( f"You have the wrong number of args, youre args are {args}. You can have 2 or 4 for frequency: {frequency}" @@ -102,9 +152,9 @@ def _check_valid_args(*args: int, frequency: str) -> None: def _check_frequency_suport(frequency: str) -> None: """Function to check if frequency requested is supported.""" - if frequency not in ["Y", "Q", "B", "M", "D"]: + if frequency not in ["Y", "Q", "B", "M", "D","W","T","H"]: raise ValueError( - f"The function does not support frequency: {frequency} yet. Please use one the supported ones: Y,Q,B,M,D" + f"The function does not support frequency: {frequency} yet. Please use one the supported ones: Y,Q,B,M,D,W,T,H" ) @@ -151,9 +201,9 @@ def get_timestamp_special(*args: int, frequency: str) -> str | None: """ _check_valid_args(*args, frequency=frequency) - if frequency == "M": - frequency = "-" - + if frequency in ["M","W"]: + if frequency == "M": + frequency = "-" if len(args) == 2: return f"p{args[0]}{frequency}{args[1]:02}" elif len(args) == 4: diff --git a/tests/test_timestamp.py b/tests/test_timestamp.py index a87a2c9..3fbb8eb 100644 --- a/tests/test_timestamp.py +++ b/tests/test_timestamp.py @@ -3,9 +3,12 @@ import pytest from ssb_konjunk.timestamp import _check_valid_day +from ssb_konjunk.timestamp import _check_valid_week from ssb_konjunk.timestamp import _check_valid_month from ssb_konjunk.timestamp import _check_valid_quarter from ssb_konjunk.timestamp import _check_valid_term +from ssb_konjunk.timestamp import _check_valid_trimester +from ssb_konjunk.timestamp import _check_valid_half_year from ssb_konjunk.timestamp import _check_valid_year from ssb_konjunk.timestamp import check_even from ssb_konjunk.timestamp import get_ssb_timestamp @@ -46,6 +49,9 @@ def test_test_get_timestamp_special() -> None: def test_get_ssb_timestamp() -> None: """Test of function get_ssb_timestamp.""" + # Testing week + assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="W") == "p2020W01_p2021W02" + assert get_ssb_timestamp(2020, 1, frequency="W") == "p2020W01" # Testing month assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="M") == "p2020-01_p2021-02" assert get_ssb_timestamp(2020, 1) == "p2020-01" @@ -55,6 +61,12 @@ def test_get_ssb_timestamp() -> None: # Testing term assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="B") == "p2020B1_p2021B2" assert get_ssb_timestamp(2020, 1, frequency="B") == "p2020B1" + # Testing trimester + assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="T") == "p2020T1_p2021T2" + assert get_ssb_timestamp(2020, 1, frequency="T") == "p2020T1" + # Testing half year + assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="H") == "p2020H1_p2021H2" + assert get_ssb_timestamp(2020, 1, frequency="H") == "p2020H1" # Testing day assert ( get_ssb_timestamp(2020, 1, 1, 2020, 1, 31, frequency="D") @@ -89,6 +101,16 @@ def test_check_valid_day() -> None: ): _check_valid_day(day) + +def test_check_valid_week() -> None: + """Test of function _check_valid_week.""" + week = 53 + with pytest.raises( + ValueError, + match=f"The arg for week is bigger than possible max is 52 you have: {week}.", + ): + _check_valid_week(week) + def test_check_valid_month() -> None: """Test of function _check_valid_month.""" @@ -118,6 +140,26 @@ def test_check_valid_quarter() -> None: match=f"The arg for quarter is bigger than possible max is 4 you have: {quarter}.", ): _check_valid_quarter(quarter) + + +def test_check_valid_trimester() -> None: + """Test of function _check_valid_trimester.""" + trimester = 5 + with pytest.raises( + ValueError, + match=f"The arg for trimester is bigger than possible max is 3 you have: {trimester}.", + ): + _check_valid_trimester(trimester) + + +def test_check_valid_quarter() -> None: + """Test of function _check_valid_half_year.""" + half_year = 5 + with pytest.raises( + ValueError, + match=f"The arg for half_year is bigger than possible max is 2 you have: {half_year}.", + ): + _check_valid_half_year(half_year) def test_check_valid_year() -> None: From b1e946f1383f838f4fd8a100a65c53f3f7fc6bee Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Fri, 9 Aug 2024 14:27:02 +0200 Subject: [PATCH 2/2] =?UTF-8?q?ssb=5Fkonjunk=200.1.1=20st=C3=B8tte=20for?= =?UTF-8?q?=20timestamp=20klar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssb_konjunk/timestamp.py | 10 +++++----- tests/test_timestamp.py | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ssb_konjunk/timestamp.py b/src/ssb_konjunk/timestamp.py index 53d617e..009d7ad 100644 --- a/src/ssb_konjunk/timestamp.py +++ b/src/ssb_konjunk/timestamp.py @@ -16,7 +16,7 @@ def _check_valid_day(day: int) -> None: raise ValueError( f"The arg for day is smaller than possible min is 1 you have: {day}." ) - + def _check_valid_week(week: int) -> None: """Function to check that day arg is valid.""" @@ -76,8 +76,8 @@ def _check_valid_trimester(trimester: int) -> None: raise ValueError( f"The arg for trimester is smaller than possible min is 1 you have: {trimester}." ) - - + + def _check_valid_half_year(half_year: int) -> None: """Function to check that day arg is valid.""" if half_year > 2: @@ -152,7 +152,7 @@ def _check_valid_args(*args: int, frequency: str) -> None: def _check_frequency_suport(frequency: str) -> None: """Function to check if frequency requested is supported.""" - if frequency not in ["Y", "Q", "B", "M", "D","W","T","H"]: + if frequency not in ["Y", "Q", "B", "M", "D", "W", "T", "H"]: raise ValueError( f"The function does not support frequency: {frequency} yet. Please use one the supported ones: Y,Q,B,M,D,W,T,H" ) @@ -201,7 +201,7 @@ def get_timestamp_special(*args: int, frequency: str) -> str | None: """ _check_valid_args(*args, frequency=frequency) - if frequency in ["M","W"]: + if frequency in ["M", "W"]: if frequency == "M": frequency = "-" if len(args) == 2: diff --git a/tests/test_timestamp.py b/tests/test_timestamp.py index 3fbb8eb..f00cf23 100644 --- a/tests/test_timestamp.py +++ b/tests/test_timestamp.py @@ -3,12 +3,12 @@ import pytest from ssb_konjunk.timestamp import _check_valid_day -from ssb_konjunk.timestamp import _check_valid_week +from ssb_konjunk.timestamp import _check_valid_half_year from ssb_konjunk.timestamp import _check_valid_month from ssb_konjunk.timestamp import _check_valid_quarter from ssb_konjunk.timestamp import _check_valid_term from ssb_konjunk.timestamp import _check_valid_trimester -from ssb_konjunk.timestamp import _check_valid_half_year +from ssb_konjunk.timestamp import _check_valid_week from ssb_konjunk.timestamp import _check_valid_year from ssb_konjunk.timestamp import check_even from ssb_konjunk.timestamp import get_ssb_timestamp @@ -101,7 +101,7 @@ def test_check_valid_day() -> None: ): _check_valid_day(day) - + def test_check_valid_week() -> None: """Test of function _check_valid_week.""" week = 53 @@ -109,7 +109,7 @@ def test_check_valid_week() -> None: ValueError, match=f"The arg for week is bigger than possible max is 52 you have: {week}.", ): - _check_valid_week(week) + _check_valid_week(week) def test_check_valid_month() -> None: @@ -140,7 +140,7 @@ def test_check_valid_quarter() -> None: match=f"The arg for quarter is bigger than possible max is 4 you have: {quarter}.", ): _check_valid_quarter(quarter) - + def test_check_valid_trimester() -> None: """Test of function _check_valid_trimester.""" @@ -152,7 +152,7 @@ def test_check_valid_trimester() -> None: _check_valid_trimester(trimester) -def test_check_valid_quarter() -> None: +def test_check_valid_half_year() -> None: """Test of function _check_valid_half_year.""" half_year = 5 with pytest.raises(