From dc7936eeb24a96b51ae8622759add548ed0d79a7 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Wed, 21 Feb 2024 16:55:54 +0100 Subject: [PATCH 1/2] Fjernet next_month la til delta_month --- src/ssb_konjunk/prompts.py | 55 +++++++++++++++++++------------------- tests/test_prompts.py | 38 +++++++++++--------------- 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index 0c9ca89..a5b104c 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 datetime import timedelta def _input_valid_int() -> int: @@ -129,32 +128,6 @@ def extract_start_end_dates(file_name: str) -> tuple[datetime, datetime]: return start_date, end_date -def next_month(desired_year: int, desired_month: int) -> str: - """Get the next month of a given year and month. - - Args: - desired_year: year - desired_month: month - - Returns: - str: next month in the format 'YYYY-MM' - """ - # Create a datetime object for the desired year and month - current_date = datetime(desired_year, desired_month, 1) - - # Add one month to the current date - next_date = current_date + timedelta(days=32) - - # Get the year and month of the next date - next_year = next_date.year - next_month = next_date.month - - # Format the year and month as 'YYYY-MM' - next_month_str = f"{next_year}-{next_month:02d}" - - return next_month_str - - def months_in_term(term: int) -> tuple[int, int]: """Gives out months as ints from term as int. @@ -198,3 +171,31 @@ def find_file_for_month_daily( ): break return file + + +def delta_month(month: int, periods: int) -> int: + """Function to shift month backwards or forward + + Args: + month: Current month you are using. + periods: Periods you want to move, can be positive or negative int. + + Returns: + new_month: Month we have shifted to. + """ + if periods < -11 or periods > 11: + raise ValueError( + "Input periods must be between -11 and 11. If youre doing a going a year back in time, please change year." + ) + elif periods == 0: + raise ValueError("Input periods is 0, you should remove the function!") + else: + # Taking period adding periods. + new_month = month + (periods) + # If new_month is above twelve, i take negative twelve to get month from new year. + if new_month > 12: + new_month = new_month - 12 + # If new_month is below 1, I take 12 negative new_month. This should give the right month from last year. + elif new_month < 1: + new_month = new_month + 12 + return new_month diff --git a/tests/test_prompts.py b/tests/test_prompts.py index 4b87d0a..8ad1e31 100644 --- a/tests/test_prompts.py +++ b/tests/test_prompts.py @@ -3,8 +3,8 @@ import pytest 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 next_month """Test of function days in month""" @@ -73,28 +73,20 @@ def test_extract_start_end_dates_valid() -> None: assert end_date == expected_end_date -"""Tests of function next_month""" +"""Test of function delta_month""" -def test_next_month_valid() -> None: - # Test with a valid year and month - desired_year = 2023 - desired_month = 11 - expected_next_month = "2023-12" - - assert next_month(desired_year, desired_month) == expected_next_month - - -def test_next_month_edge_case() -> None: - # Test with December, expecting January of the next year - desired_year = 2022 - desired_month = 12 - expected_next_month = "2023-01" - - assert next_month(desired_year, desired_month) == expected_next_month - +def test_delta_month() -> None: + # Test for valid change forward in time + assert delta_month(6, 3) == 9 + # Test for valid change backwards in time. + assert delta_month(6, -3) == 3 + # Test for valid change past 12. + assert delta_month(11, 3) == 2 + # Test for valid change past 1. + assert delta_month(1, -2) == 11 -def test_next_month_invalid() -> None: - # Test with an invalid month (13), expecting an error - with pytest.raises(ValueError, match="month must be in 1..12"): - next_month(2022, 13) + with pytest.raises(ValueError): + delta_month(12, -12) + with pytest.raises(ValueError): + delta_month(12, 0) From 1829abb0258710de6617f9423c429a961dcfcb76 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Wed, 21 Feb 2024 17:24:56 +0100 Subject: [PATCH 2/2] naa funker nox --- src/ssb_konjunk/prompts.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ssb_konjunk/prompts.py b/src/ssb_konjunk/prompts.py index a5b104c..23375d3 100644 --- a/src/ssb_konjunk/prompts.py +++ b/src/ssb_konjunk/prompts.py @@ -174,14 +174,18 @@ def find_file_for_month_daily( def delta_month(month: int, periods: int) -> int: - """Function to shift month backwards or forward + """Function to shift month backwards or forward. Args: month: Current month you are using. periods: Periods you want to move, can be positive or negative int. Returns: - new_month: Month we have shifted to. + int: Month we have shifted to. + + Raises: + ValueError: If periods are are more or less than a year. + ValueError: If period is 0. """ if periods < -11 or periods > 11: raise ValueError(