From 192d7b28091c2b47a6e9e3ea1229eaf513f06f67 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Thu, 6 Jun 2024 12:26:32 +0200 Subject: [PATCH 1/5] add version as function attribute --- src/imf_reader/weo/reader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/imf_reader/weo/reader.py b/src/imf_reader/weo/reader.py index d614039..4d5dc8f 100644 --- a/src/imf_reader/weo/reader.py +++ b/src/imf_reader/weo/reader.py @@ -146,6 +146,7 @@ def fetch_data(version: Optional[Version] = None) -> pd.DataFrame: version = validate_version(version) df = _fetch(version) logger.info(f"Data fetched successfully for version {version[0]} {version[1]}") + fetch_data.last_version_fetched = version # store the version fetched as function attribute return df # if no version is passed, generate the latest version and fetch the data From 449f2fc825cead27e3e792b150e8df55dba224c8 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Thu, 6 Jun 2024 12:30:11 +0200 Subject: [PATCH 2/5] update readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 2ff5749..ff750e5 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,16 @@ be either "April" or "October". df = weo.fetch_data(version=("April", 2020)) ``` +If the version of the data fetched is needed, it can be +retrieved from the function attribute `last_version_fetched`. + +```python +df = weo.fetch_data() +print(weo.fetch_data.last_version_fetched) +# >>> ('April', 2024) or whichever version was just fetched +``` + + Caching is used to avoid multiple requests to the IMF website for the same data and to enhance performance. Caching using the LRU (Least Recently Used) algorithm approach and stores data in RAM. The cache is cleared when the program is terminated. To clear the cache manually, use the `clear_cache` function. From db8c7db4b32ec2718e0bd209110b52b1282a2c2e Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Thu, 6 Jun 2024 12:44:15 +0200 Subject: [PATCH 3/5] format numeric types --- src/imf_reader/weo/parser.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/imf_reader/weo/parser.py b/src/imf_reader/weo/parser.py index df13306..0ce3518 100644 --- a/src/imf_reader/weo/parser.py +++ b/src/imf_reader/weo/parser.py @@ -16,13 +16,13 @@ } # numeric columns and the type to convert them to -SDMX_NUMERIC_COLUMNS = [ - "REF_AREA_CODE", - "OBS_VALUE", - "SCALE_CODE", - "LASTACTUALDATE", - "TIME_PERIOD", -] +SDMX_NUMERIC_COLUMNS = { + "REF_AREA_CODE": "Int16", + "OBS_VALUE": "Float64", + "SCALE_CODE": "Int16", + "LASTACTUALDATE": "Int16", + "TIME_PERIOD": "Int16", +} class SDMXParser: @@ -122,13 +122,12 @@ def check_folder(sdmx_folder: ZipFile) -> None: def clean_numeric_columns(df: pd.DataFrame) -> pd.DataFrame: """Cleans the numeric columns - Replaces "n/a" and "--" with pd.NA and converts the columns to numeric. + Replaces "n/a" and "--" with pd.NA and converts the columns to numeric and the correct type. """ - df[SDMX_NUMERIC_COLUMNS] = ( - df[SDMX_NUMERIC_COLUMNS].replace(["n/a", "--"], pd.NA).apply(pd.to_numeric) - ) + for column, dtype in SDMX_NUMERIC_COLUMNS.items(): + df[column] = df[column].replace(["n/a", "--"], pd.NA).astype(dtype) return df From e324eecab4b60238bf775a5163b3bad1a976d659 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Thu, 6 Jun 2024 12:52:19 +0200 Subject: [PATCH 4/5] add test for fetch_data attribute --- tests/test_weo/test_reader.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_weo/test_reader.py b/tests/test_weo/test_reader.py index b257250..1e176db 100644 --- a/tests/test_weo/test_reader.py +++ b/tests/test_weo/test_reader.py @@ -83,6 +83,25 @@ def test_fetch_data(mock_fetch): mock_fetch.assert_called_with(reader.gen_latest_version()) +@patch("imf_reader.weo.reader.gen_latest_version") +@patch("imf_reader.weo.reader._fetch") +def test_fetch_data_attribute(mock_fetch, mock_gen_latest_version): + """Test for fetch_data method attribute.""" + + mock_data = pd.DataFrame({"column1": [1, 2, 3], "column2": [4, 5, 6]}) + mock_fetch.return_value = mock_data + mock_gen_latest_version.return_value = ("April", 2024) + + # when a version is passed, check that the attribute is set + reader.fetch_data(("April", 2022)) + assert reader.fetch_data.last_version_fetched == ("April", 2022) + + # when no version is passed, check that the attribute is set + reader.fetch_data() + assert reader.fetch_data.last_version_fetched == ("April", 2024) + + + @patch("imf_reader.weo.reader._fetch.cache_clear") def test_clear_cache(mock_cache_clear): """Test for clear_cache method.""" From e72325979bd5e5af93c404cea7519cd541f42721 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Thu, 6 Jun 2024 12:53:12 +0200 Subject: [PATCH 5/5] black formatting --- src/imf_reader/weo/reader.py | 4 +++- tests/test_weo/test_reader.py | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/imf_reader/weo/reader.py b/src/imf_reader/weo/reader.py index 4d5dc8f..a1df35c 100644 --- a/src/imf_reader/weo/reader.py +++ b/src/imf_reader/weo/reader.py @@ -146,7 +146,9 @@ def fetch_data(version: Optional[Version] = None) -> pd.DataFrame: version = validate_version(version) df = _fetch(version) logger.info(f"Data fetched successfully for version {version[0]} {version[1]}") - fetch_data.last_version_fetched = version # store the version fetched as function attribute + fetch_data.last_version_fetched = ( + version # store the version fetched as function attribute + ) return df # if no version is passed, generate the latest version and fetch the data diff --git a/tests/test_weo/test_reader.py b/tests/test_weo/test_reader.py index 1e176db..4837cdb 100644 --- a/tests/test_weo/test_reader.py +++ b/tests/test_weo/test_reader.py @@ -101,7 +101,6 @@ def test_fetch_data_attribute(mock_fetch, mock_gen_latest_version): assert reader.fetch_data.last_version_fetched == ("April", 2024) - @patch("imf_reader.weo.reader._fetch.cache_clear") def test_clear_cache(mock_cache_clear): """Test for clear_cache method."""