From 5e3bb623285c125dd9fb95a4248e68afcc36b394 Mon Sep 17 00:00:00 2001 From: polochinoc Date: Thu, 15 Aug 2024 18:21:51 +0200 Subject: [PATCH] close #70; Add from_config class method to instantiate AllRainfall class from configuration --- app.py | 6 +----- src/core/models/all_rainfall.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 7ece8c0..39e0a05 100644 --- a/app.py +++ b/app.py @@ -19,16 +19,12 @@ from src.api.utils import ( raise_time_mode_error_or_do_nothing, ) -from src.config import Config from src.core.models.all_rainfall import AllRainfall from src.core.utils.enums.months import Month from src.core.utils.enums.seasons import Season from src.core.utils.enums.time_modes import TimeMode -cfg = Config() -all_rainfall = AllRainfall( - cfg.get_dataset_path(), cfg.get_start_year(), cfg.get_rainfall_precision() -) +all_rainfall = AllRainfall.from_config() app = FastAPI( debug=True, diff --git a/src/core/models/all_rainfall.py b/src/core/models/all_rainfall.py index 0f9c92d..0d44e1c 100644 --- a/src/core/models/all_rainfall.py +++ b/src/core/models/all_rainfall.py @@ -8,6 +8,7 @@ import pandas as pd +from src.config import Config from src.core.models.monthly_rainfall import MonthlyRainfall from src.core.models.seasonal_rainfall import SeasonalRainfall from src.core.models.yearly_rainfall import YearlyRainfall @@ -30,6 +31,7 @@ class AllRainfall: def __init__( self, dataset_url_or_path: str, + *, start_year=1971, round_precision=2, ): @@ -53,6 +55,16 @@ def __init__( for season in Season } + @classmethod + def from_config(cls, from_file=False): + cfg = Config() + + return cls( + cfg.get_dataset_path() if from_file else cfg.get_dataset_url(), + start_year=cfg.get_start_year(), + round_precision=cfg.get_rainfall_precision(), + ) + def export_all_data_to_csv(self, folder_path="csv_data") -> str: """ Export all the different data as CSVs into specified folder path.