From a2b140528c1f777e5c6385f8e0bafda45a937b17 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 11 Mar 2024 13:06:27 +0100 Subject: [PATCH 1/5] Versjon 0.0.4 lagt til ny funk --- pyproject.toml | 2 +- src/ssb_konjunk/prompts.py | 34 +++++++++++++++++++++++ tests/test_prompts.py | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 339f39d..9882505 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ssb-konjunk" -version = "0.0.3" +version = "0.0.4" description = "SSB Konjunk" authors = ["Edvard Garmannslund "] license = "MIT" diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index 23375d3..d9338b0 100644 --- a/src/ssb_konjunk/prompts.py +++ b/src/ssb_konjunk/prompts.py @@ -203,3 +203,37 @@ def delta_month(month: int, periods: int) -> int: elif new_month < 1: new_month = new_month + 12 return new_month + + +def iterate_years_months( + start_year: int, end_year: int, start_month: int, end_month: int +) -> tuple: + """Function to iterate over years and month. + + Allows you to select start year, start month, end year and end month + + Args: + start_year: Int for start year. + start_month: Int for start month. + end_year: Int for end year. + end_month: Int for end month. + + Yields: + tuple: A tuple containing the year and month for each combination. + """ + if start_year > end_year: + raise ValueError("Start year must be less than or equal to end year") + if start_month < 1 or start_month > 12 or end_month < 1 or end_month > 12: + raise ValueError("Month must be between 1 and 12") + if start_year == end_year and start_month > end_month: + raise ValueError( + "If iterating in same year start month must be less than end month." + ) + + for year in range(start_year, end_year + 1): + for month in range(1, 13): + if (year == start_year and month < start_month) or ( + year == end_year and month > end_month + ): + continue + yield year, month diff --git a/tests/test_prompts.py b/tests/test_prompts.py index 8ad1e31..2428745 100644 --- a/tests/test_prompts.py +++ b/tests/test_prompts.py @@ -5,6 +5,7 @@ from ssb_konjunk.prompts import days_in_month from ssb_konjunk.prompts import delta_month from ssb_konjunk.prompts import extract_start_end_dates +from ssb_konjunk.prompts import iterate_years_months """Test of function days in month""" @@ -90,3 +91,58 @@ def test_delta_month() -> None: delta_month(12, -12) with pytest.raises(ValueError): delta_month(12, 0) + + +"""Test of function iterate_years_months""" + + +def test_iterate_years_months_full_range(): + # Test when providing a full range of years and months + expected_output = [ + (2024, 1), + (2024, 2), + (2024, 3), + (2024, 4), + (2024, 5), + (2024, 6), + (2024, 7), + (2024, 8), + (2024, 9), + (2024, 10), + (2024, 11), + (2024, 12), + (2025, 1), + ] + assert list(iterate_years_months(2024, 2025, 1, 1)) == expected_output + + +def test_iterate_years_months_partial_range(): + # Test when providing a partial range of years and months + expected_output = [(2023, 11), (2023, 12), (2024, 1), (2024, 2)] + assert list(iterate_years_months(2023, 2024, 11, 2)) == expected_output + + +def test_iterate_years_months_one_period(): + # Test when providing a partial range of years and months + expected_output = [(2024, 2)] + assert list(iterate_years_months(2024, 2024, 2, 2)) == expected_output + + +def test_iterate_years_months_invalid_range(): + # Test when providing an invalid range where start year > end year + with pytest.raises(ValueError): + list(iterate_years_months(2024, 2022, 1, 12)) + + # Test when providing an invalid range where start month > end month + with pytest.raises(ValueError): + list(iterate_years_months(2024, 2024, 6, 1)) + + +def test_iterate_years_months_invalid_month(): + # Test when providing an invalid month (greater than 12) + with pytest.raises(ValueError): + list(iterate_years_months(2022, 2024, 1, 13)) + + # Test when providing an invalid month (less than 1) + with pytest.raises(ValueError): + list(iterate_years_months(2022, 2024, 0, 12)) From da79921ae7955495b191268e0ee1693c56994790 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 11 Mar 2024 13:12:36 +0100 Subject: [PATCH 2/5] fikset darglint --- src/ssb_konjunk/prompts.py | 3 ++- tests/test_prompts.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index d9338b0..9ee924e 100644 --- a/src/ssb_konjunk/prompts.py +++ b/src/ssb_konjunk/prompts.py @@ -6,6 +6,7 @@ import re from calendar import monthrange from datetime import datetime +from typing import Generator, Tuple def _input_valid_int() -> int: @@ -207,7 +208,7 @@ def delta_month(month: int, periods: int) -> int: def iterate_years_months( start_year: int, end_year: int, start_month: int, end_month: int -) -> tuple: +) -> Generator[Tuple[int, int], None, None]: """Function to iterate over years and month. Allows you to select start year, start month, end year and end month diff --git a/tests/test_prompts.py b/tests/test_prompts.py index 2428745..61fbd05 100644 --- a/tests/test_prompts.py +++ b/tests/test_prompts.py @@ -96,7 +96,7 @@ def test_delta_month() -> None: """Test of function iterate_years_months""" -def test_iterate_years_months_full_range(): +def test_iterate_years_months_full_range() -> None: # Test when providing a full range of years and months expected_output = [ (2024, 1), @@ -116,19 +116,19 @@ def test_iterate_years_months_full_range(): assert list(iterate_years_months(2024, 2025, 1, 1)) == expected_output -def test_iterate_years_months_partial_range(): +def test_iterate_years_months_partial_range() -> None: # Test when providing a partial range of years and months expected_output = [(2023, 11), (2023, 12), (2024, 1), (2024, 2)] assert list(iterate_years_months(2023, 2024, 11, 2)) == expected_output -def test_iterate_years_months_one_period(): +def test_iterate_years_months_one_period() -> None: # Test when providing a partial range of years and months expected_output = [(2024, 2)] assert list(iterate_years_months(2024, 2024, 2, 2)) == expected_output -def test_iterate_years_months_invalid_range(): +def test_iterate_years_months_invalid_range() -> None: # Test when providing an invalid range where start year > end year with pytest.raises(ValueError): list(iterate_years_months(2024, 2022, 1, 12)) @@ -138,7 +138,7 @@ def test_iterate_years_months_invalid_range(): list(iterate_years_months(2024, 2024, 6, 1)) -def test_iterate_years_months_invalid_month(): +def test_iterate_years_months_invalid_month() -> None: # Test when providing an invalid month (greater than 12) with pytest.raises(ValueError): list(iterate_years_months(2022, 2024, 1, 13)) From 107ef20d58ee5ba3a8e91cf953046e18459a1472 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 11 Mar 2024 13:15:20 +0100 Subject: [PATCH 3/5] Ny fiks darglint --- src/ssb_konjunk/prompts.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index 9ee924e..19389d7 100644 --- a/src/ssb_konjunk/prompts.py +++ b/src/ssb_konjunk/prompts.py @@ -6,7 +6,6 @@ import re from calendar import monthrange from datetime import datetime -from typing import Generator, Tuple def _input_valid_int() -> int: @@ -208,7 +207,7 @@ def delta_month(month: int, periods: int) -> int: def iterate_years_months( start_year: int, end_year: int, start_month: int, end_month: int -) -> Generator[Tuple[int, int], None, None]: +) -> any: """Function to iterate over years and month. Allows you to select start year, start month, end year and end month @@ -220,7 +219,7 @@ def iterate_years_months( end_month: Int for end month. Yields: - tuple: A tuple containing the year and month for each combination. + any: A tuple containing the year and month for each combination. """ if start_year > end_year: raise ValueError("Start year must be less than or equal to end year") From a63139b4a0ae19fc6ad605389ebf8a229dbf7ac6 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 11 Mar 2024 13:19:17 +0100 Subject: [PATCH 4/5] Ny fiks darglint --- src/ssb_konjunk/prompts.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index 19389d7..d4d2e15 100644 --- a/src/ssb_konjunk/prompts.py +++ b/src/ssb_konjunk/prompts.py @@ -220,6 +220,11 @@ def iterate_years_months( Yields: any: A tuple containing the year and month for each combination. + + Raises: + ValueError: If start year is bigger than end year. + ValueError: If month is invalid number. + ValueError: If end month is bigger than start and only iterating on one year. """ if start_year > end_year: raise ValueError("Start year must be less than or equal to end year") From 2c6a2ebf30cbb062a05bb3e109867e1117d0f25e Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 11 Mar 2024 13:21:08 +0100 Subject: [PATCH 5/5] Oppdatert typehints --- src/ssb_konjunk/prompts.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index d4d2e15..04f0d05 100644 --- a/src/ssb_konjunk/prompts.py +++ b/src/ssb_konjunk/prompts.py @@ -6,6 +6,7 @@ import re from calendar import monthrange from datetime import datetime +from typing import Any def _input_valid_int() -> int: @@ -207,7 +208,7 @@ def delta_month(month: int, periods: int) -> int: def iterate_years_months( start_year: int, end_year: int, start_month: int, end_month: int -) -> any: +) -> Any: """Function to iterate over years and month. Allows you to select start year, start month, end year and end month @@ -219,7 +220,7 @@ def iterate_years_months( end_month: Int for end month. Yields: - any: A tuple containing the year and month for each combination. + Any: A tuple containing the year and month for each combination. Raises: ValueError: If start year is bigger than end year.