From 08e65bfc0ac2d562a0f6c4940dbfa5bb7225f01b Mon Sep 17 00:00:00 2001 From: killian-scalian Date: Mon, 14 Oct 2024 11:03:21 +0200 Subject: [PATCH] lint with ruff --- .../service/local_services/area_local.py | 40 ++--- .../service/local_services/study_local.py | 16 +- src/antares/service/service_factory.py | 158 ++++-------------- src/antares/tools/ini_tool.py | 1 - src/antares/tools/time_series_tool.py | 1 + .../local_services/test_study_read.py | 89 +++++----- 6 files changed, 89 insertions(+), 216 deletions(-) diff --git a/src/antares/service/local_services/area_local.py b/src/antares/service/local_services/area_local.py index 8a11b512..25eb5a64 100644 --- a/src/antares/service/local_services/area_local.py +++ b/src/antares/service/local_services/area_local.py @@ -265,12 +265,12 @@ def _line_exists_in_file(file_content: str, line_to_add: str) -> bool: areas_ini.add_section({"unserverdenergycost": {}}) areas_ini.add_section({"spilledenergycost": {}}) areas_ini.write_ini_file() - areas_ini.parsed_ini["unserverdenergycost"][ - area_name - ] = local_properties.nodal_optimization["average-unsupplied-energy-cost"] - areas_ini.parsed_ini["spilledenergycost"][ - area_name - ] = local_properties.nodal_optimization["average-spilled-energy-cost"] + areas_ini.parsed_ini["unserverdenergycost"][area_name] = local_properties.nodal_optimization[ + "average-unsupplied-energy-cost" + ] + areas_ini.parsed_ini["spilledenergycost"][area_name] = local_properties.nodal_optimization[ + "average-spilled-energy-cost" + ] areas_ini.write_ini_file() local_ui = AreaUiLocal(ui) if ui else AreaUiLocal() @@ -325,13 +325,9 @@ def read_thermal_cluster( thermal_name: str, properties: Optional[ThermalClusterProperties] = None, ) -> ThermalCluster: - local_thermal_properties = ThermalClusterPropertiesLocal( - thermal_name, properties - ) + local_thermal_properties = ThermalClusterPropertiesLocal(thermal_name, properties) - list_ini = IniFile( - self.config.study_path, IniFileTypes.THERMAL_LIST_INI, area_name=area_id - ) + list_ini = IniFile(self.config.study_path, IniFileTypes.THERMAL_LIST_INI, area_name=area_id) list_ini.add_section(local_thermal_properties.list_ini_fields) list_ini.write_ini_file(sort_sections=True) @@ -369,9 +365,7 @@ def read_st_storage( ) -> STStorage: local_st_storage_properties = STStoragePropertiesLocal(st_storage_name, properties) - list_ini = IniFile( - self.config.study_path, IniFileTypes.ST_STORAGE_LIST_INI, area_name=area_id - ) + list_ini = IniFile(self.config.study_path, IniFileTypes.ST_STORAGE_LIST_INI, area_name=area_id) list_ini.add_section(local_st_storage_properties.list_ini_fields) list_ini.write_ini_file(sort_sections=True) @@ -438,9 +432,7 @@ def _line_exists_in_file(file_content: str, line_to_add: str) -> bool: existing_path = self.config.local_path study_path = existing_path / self.study_name - optimization_ini = IniFile( - study_path, IniFileTypes.AREA_OPTIMIZATION_INI, area_name - ).parsed_ini + optimization_ini = IniFile(study_path, IniFileTypes.AREA_OPTIMIZATION_INI, area_name).parsed_ini dict_optimization = { section: {key: f"{value}" for key, value in optimization_ini.items(section)} @@ -448,17 +440,11 @@ def _line_exists_in_file(file_content: str, line_to_add: str) -> bool: } ui_ini = IniFile(study_path, IniFileTypes.AREA_UI_INI, area_name).parsed_ini - dict_ui = { - section: {key: f"{value}" for key, value in ui_ini.items(section)} - for section in ui_ini.sections() - } + dict_ui = {section: {key: f"{value}" for key, value in ui_ini.items(section)} for section in ui_ini.sections()} - patch_ini = IniFile( - study_path, IniFileTypes.AREA_ADEQUACY_PATCH_INI, area_name - ).parsed_ini + patch_ini = IniFile(study_path, IniFileTypes.AREA_ADEQUACY_PATCH_INI, area_name).parsed_ini dict_adequacy_patch = { - section: {key: f"{value}" for key, value in patch_ini.items(section)} - for section in patch_ini.sections() + section: {key: f"{value}" for key, value in patch_ini.items(section)} for section in patch_ini.sections() } return [dict_optimization, dict_ui, dict_adequacy_patch] diff --git a/src/antares/service/local_services/study_local.py b/src/antares/service/local_services/study_local.py index ec15b905..465cc227 100644 --- a/src/antares/service/local_services/study_local.py +++ b/src/antares/service/local_services/study_local.py @@ -22,9 +22,7 @@ class StudyLocalService(BaseStudyService): - def __init__( - self, config: LocalConfiguration, study_name: Path, **kwargs: Any - ) -> None: + def __init__(self, config: LocalConfiguration, study_name: Path, **kwargs: Any) -> None: super().__init__(**kwargs) self._config = config self._study_name = study_name @@ -51,25 +49,19 @@ def read_areas(self) -> json: patch_path = local_path / self._study_name / Path("patch.json") if not os.path.exists(patch_path): - return json.loads( - f"Le fichier {patch_path} n'existe pas dans le dossier {local_path / self._study_name}" - ) + return json.loads(f"Le fichier {patch_path} n'existe pas dans le dossier {local_path / self._study_name}") try: with open(patch_path, "r") as file: content = file.read() try: data = json.loads(content) except json.JSONDecodeError: - return json.loads( - f"Le fichier {patch_path} ne contient pas du JSON valide" - ) + return json.loads(f"Le fichier {patch_path} ne contient pas du JSON valide") if "areas" in data: areas = data["areas"] if isinstance(areas, dict): return list(areas.keys()) else: - return json.loads( - f"The key 'areas' n'existe pas dans le fichier JSON" - ) + return json.loads(f"The key 'areas' n'existe pas dans le fichier JSON") except IOError: return f"Impossible de lire le fichier {patch_path}" diff --git a/src/antares/service/service_factory.py b/src/antares/service/service_factory.py index 188932c6..e01e6b96 100644 --- a/src/antares/service/service_factory.py +++ b/src/antares/service/service_factory.py @@ -134,9 +134,7 @@ def create_st_storage_service(self) -> BaseShortTermStorageService: class ServiceReader: - def __init__( - self, config: BaseConfiguration, study_name: Path = "", study_id: str = "" - ): + def __init__(self, config: BaseConfiguration, study_name: Path = "", study_id: str = ""): self.config = config self.study_id = study_id self.study_name = study_name @@ -162,11 +160,7 @@ def _directory_exists(local_path: Path) -> None: @staticmethod def _lister_dossiers(dossier): try: - dossiers = [ - d - for d in os.listdir(dossier) - if os.path.isdir(os.path.join(dossier, d)) - ] + dossiers = [d for d in os.listdir(dossier) if os.path.isdir(os.path.join(dossier, d))] return dossiers except FileNotFoundError: return [] @@ -174,9 +168,7 @@ def _lister_dossiers(dossier): # we can have read area service here, for just one area def read_study_service(self) -> BaseStudyService: if isinstance(self.config, LocalConfiguration): - study_service: BaseStudyService = StudyLocalService( - self.config, self.study_name - ) + study_service: BaseStudyService = StudyLocalService(self.config, self.study_name) areas = study_service.read_areas() study_path = self.config.local_path / self.study_name @@ -185,8 +177,7 @@ def read_study_service(self) -> BaseStudyService: sets_ini = IniFile(study_path, IniFileTypes.AREAS_SETS_INI).parsed_ini self._areas = { - section: {key: f"{value}" for key, value in sets_ini.items(section)} - for section in sets_ini.sections() + section: {key: f"{value}" for key, value in sets_ini.items(section)} for section in sets_ini.sections() } if not self._directory_exists(study_path): return {} @@ -194,36 +185,24 @@ def read_study_service(self) -> BaseStudyService: # Areas for area_name in areas: # Get everything inside input/areas/{area_name} - area_service: BaseStudyService = AreaLocalService( - self.config, self.study_name - ) + area_service: BaseStudyService = AreaLocalService(self.config, self.study_name) area = area_service.read_area(area_name) self._areas[area_name] = area # Load # Get everything inside input/load/prepro/ - load_correlation_ini = IniFile( - study_path, IniFileTypes.LOAD_CORRELATION_INI - ).parsed_ini + load_correlation_ini = IniFile(study_path, IniFileTypes.LOAD_CORRELATION_INI).parsed_ini self._load["correlation"] = { - section: { - key: f"{value}" - for key, value in load_correlation_ini.items(section) - } + section: {key: f"{value}" for key, value in load_correlation_ini.items(section)} for section in load_correlation_ini.sections() } for area_name in areas: self._load[area_name] = {} # Get everything inside input/load/prepro/{area_name} - load_settings_ini = IniFile( - study_path, IniFileTypes.LOAD_SETTINGS_INI, area_name - ).parsed_ini + load_settings_ini = IniFile(study_path, IniFileTypes.LOAD_SETTINGS_INI, area_name).parsed_ini self._load[area_name]["settings"] = { - section: { - key: f"{value}" - for key, value in load_settings_ini.items(section) - } + section: {key: f"{value}" for key, value in load_settings_ini.items(section)} for section in load_settings_ini.sections() } @@ -253,22 +232,14 @@ def read_study_service(self) -> BaseStudyService: self._load[area_name]["k"] = load_module # Get everything inside input/load/series/{area_name} - load_series = TimeSeriesFile( - TimeSeriesFileType.LOAD_DATA_SERIES, - study_path, - area_name - ).time_series + load_series = TimeSeriesFile(TimeSeriesFileType.LOAD_DATA_SERIES, study_path, area_name).time_series self._load[area_name]["series"] = load_series # misc-gen for area_name in areas: self._misc[area_name] = {} # Get everything inside input/misc-gen/{area_name} - miscgen_series = TimeSeriesFile( - TimeSeriesFileType.MISC_GEN, - study_path, - area_name - ).time_series + miscgen_series = TimeSeriesFile(TimeSeriesFileType.MISC_GEN, study_path, area_name).time_series self._misc[area_name]["series"] = miscgen_series # Renewables @@ -276,24 +247,14 @@ def read_study_service(self) -> BaseStudyService: self._renewables[area_name] = {} # Get everything inside input/renewables/clusters/{area_name} - renewables_list_ini = IniFile( - study_path, IniFileTypes.RENEWABLES_LIST_INI, area_name - ).parsed_ini + renewables_list_ini = IniFile(study_path, IniFileTypes.RENEWABLES_LIST_INI, area_name).parsed_ini self._renewables[area_name]["list"] = { - section: { - key: f"{value}" - for key, value in renewables_list_ini.items(section) - } + section: {key: f"{value}" for key, value in renewables_list_ini.items(section)} for section in renewables_list_ini.sections() } # Get everything inside input/renewables/series/{area_name} - prefix_path = ( - study_path - / TimeSeriesFileType.RENEWABLE_SERIES_PREFIX.value.format( - area_id=area_name - ) - ) + prefix_path = study_path / TimeSeriesFileType.RENEWABLE_SERIES_PREFIX.value.format(area_id=area_name) groups = self._lister_dossiers(prefix_path) for group_name in groups: renewable_series = TimeSeriesFile( @@ -306,29 +267,19 @@ def read_study_service(self) -> BaseStudyService: # Solar # Get everything inside input/solar/prepro/ - solar_correlation_ini = IniFile( - study_path, IniFileTypes.SOLAR_CORRELATION_INI - ).parsed_ini + solar_correlation_ini = IniFile(study_path, IniFileTypes.SOLAR_CORRELATION_INI).parsed_ini self._solar["correlation"] = { - section: { - key: f"{value}" - for key, value in solar_correlation_ini.items(section) - } + section: {key: f"{value}" for key, value in solar_correlation_ini.items(section)} for section in solar_correlation_ini.sections() } for area_name in areas: self._solar[area_name] = {} # Get everything inside input/solar/prepro/{area_name} - solar_settings_ini = IniFile( - study_path, IniFileTypes.SOLAR_SETTINGS_INI, area_name - ).parsed_ini + solar_settings_ini = IniFile(study_path, IniFileTypes.SOLAR_SETTINGS_INI, area_name).parsed_ini self._solar[area_name]["settings"] = { - section: { - key: f"{value}" - for key, value in solar_settings_ini.items(section) - } + section: {key: f"{value}" for key, value in solar_settings_ini.items(section)} for section in solar_settings_ini.sections() } @@ -358,60 +309,36 @@ def read_study_service(self) -> BaseStudyService: self._solar[area_name]["k"] = solar_module # Get everything inside input/solar/series/{area_name} - solar_series = TimeSeriesFile( - TimeSeriesFileType.SOLAR, - study_path, - area_name - ).time_series + solar_series = TimeSeriesFile(TimeSeriesFileType.SOLAR, study_path, area_name).time_series self._solar[area_name]["series"] = solar_series # St-storage for area_name in areas: self._storage[area_name] = {} # Get everything inside input/st-storage/clusters/ - storage_list_ini = IniFile( - study_path, IniFileTypes.ST_STORAGE_LIST_INI, area_name - ).parsed_ini + storage_list_ini = IniFile(study_path, IniFileTypes.ST_STORAGE_LIST_INI, area_name).parsed_ini self._storage[area_name]["list"] = { - section: { - key: f"{value}" - for key, value in storage_list_ini.items(section) - } + section: {key: f"{value}" for key, value in storage_list_ini.items(section)} for section in storage_list_ini.sections() } # Thermals - thermal_list_ini = IniFile( - study_path, IniFileTypes.THERMAL_AREAS_INI - ).parsed_ini + thermal_list_ini = IniFile(study_path, IniFileTypes.THERMAL_AREAS_INI).parsed_ini self._thermals["areas"] = { - section: { - key: f"{value}" - for key, value in thermal_list_ini.items(section) - } + section: {key: f"{value}" for key, value in thermal_list_ini.items(section)} for section in thermal_list_ini.sections() } for area_name in areas: self._thermals[area_name] = {} # Get everything inside input/thermal/clusters/{area_name} - thermal_list_ini = IniFile( - study_path, IniFileTypes.THERMAL_LIST_INI, area_name - ).parsed_ini + thermal_list_ini = IniFile(study_path, IniFileTypes.THERMAL_LIST_INI, area_name).parsed_ini self._thermals[area_name]["list"] = { - section: { - key: f"{value}" - for key, value in thermal_list_ini.items(section) - } + section: {key: f"{value}" for key, value in thermal_list_ini.items(section)} for section in thermal_list_ini.sections() } # Get everything inside input/thermal/prepro/{area_name} - prefix_path = ( - study_path - / TimeSeriesFileType.THERMAL_PREFIX.value.format( - area_id=area_name - ) - ) + prefix_path = study_path / TimeSeriesFileType.THERMAL_PREFIX.value.format(area_id=area_name) groups = self._lister_dossiers(prefix_path) for group_name in groups: thermal_prepro = TimeSeriesFile( @@ -430,12 +357,7 @@ def read_study_service(self) -> BaseStudyService: self._thermals[area_name][f"{group_name}-modulation"] = thermal_module # Get everything inside input/thermal/series/{area_name} - prefix_path = ( - study_path - / TimeSeriesFileType.THERMAL_SERIES_PREFIX.value.format( - area_id=area_name - ) - ) + prefix_path = study_path / TimeSeriesFileType.THERMAL_SERIES_PREFIX.value.format(area_id=area_name) groups = self._lister_dossiers(prefix_path) for group_name in groups: thermal_series = TimeSeriesFile( @@ -446,32 +368,21 @@ def read_study_service(self) -> BaseStudyService: ).time_series self._thermals[area_name][f"{group_name}-series"] = thermal_series - # Wind # Get everything inside input/wind/prepro/ - wind_correlation_ini = IniFile( - study_path, IniFileTypes.WIND_CORRELATION_INI - ).parsed_ini + wind_correlation_ini = IniFile(study_path, IniFileTypes.WIND_CORRELATION_INI).parsed_ini self._wind["correlation"] = { - section: { - key: f"{value}" - for key, value in wind_correlation_ini.items(section) - } + section: {key: f"{value}" for key, value in wind_correlation_ini.items(section)} for section in wind_correlation_ini.sections() } for area_name in areas: self._wind[area_name] = {} # Get everything inside input/wind/prepro/{area_name} - wind_settings_ini = IniFile( - study_path, IniFileTypes.WIND_SETTINGS_INI, area_name - ).parsed_ini + wind_settings_ini = IniFile(study_path, IniFileTypes.WIND_SETTINGS_INI, area_name).parsed_ini self._wind[area_name]["settings"] = { - section: { - key: f"{value}" - for key, value in wind_settings_ini.items(section) - } + section: {key: f"{value}" for key, value in wind_settings_ini.items(section)} for section in wind_settings_ini.sections() } @@ -504,11 +415,7 @@ def read_study_service(self) -> BaseStudyService: self._wind[area_name]["k"] = wind_module # Get everything inside input/wind/series/{area_name} - wind_series = TimeSeriesFile( - TimeSeriesFileType.WIND, - study_path, - area_name - ).time_series + wind_series = TimeSeriesFile(TimeSeriesFileType.WIND, study_path, area_name).time_series self._wind[area_name]["series"] = wind_series ## Hydro @@ -564,7 +471,6 @@ def read_study_service(self) -> BaseStudyService: ).time_series self._hydro[area_name]["watervalues"] = hydro_module - # Get everything inside input/hydro/prepro/{area_name} hydro_module = TimeSeriesFile( TimeSeriesFileType.HYDRO_ENERGY, diff --git a/src/antares/tools/ini_tool.py b/src/antares/tools/ini_tool.py index 39e4b6f5..02044539 100644 --- a/src/antares/tools/ini_tool.py +++ b/src/antares/tools/ini_tool.py @@ -34,7 +34,6 @@ class IniFileTypes(Enum): AREA_ADEQUACY_PATCH_INI = "input/areas/{area_name}/adequacy_patch.ini" BINDING_CONSTRAINTS_INI = "input/bindingconstraints/bindingconstraints.ini" - LINK_PROPERTIES_INI = "input/links/{area_name}/properties.ini" RENEWABLES_LIST_INI = "input/renewables/clusters/{area_name}/list.ini" diff --git a/src/antares/tools/time_series_tool.py b/src/antares/tools/time_series_tool.py index 0903d2cd..1fc3eede 100644 --- a/src/antares/tools/time_series_tool.py +++ b/src/antares/tools/time_series_tool.py @@ -82,6 +82,7 @@ class TimeSeriesFileType(Enum): HYDRO_MOD_SERIES = "input/hydro/series/{area_id}/mod.txt" HYDRO_ROR_SERIES = "input/hydro/series/{area_id}/ror.txt" + class TimeSeriesFile: """ Handling time series files reading and writing locally. diff --git a/tests/antares/services/local_services/test_study_read.py b/tests/antares/services/local_services/test_study_read.py index d7234f9d..becdd7d6 100644 --- a/tests/antares/services/local_services/test_study_read.py +++ b/tests/antares/services/local_services/test_study_read.py @@ -43,12 +43,14 @@ def test_read_study_service(self, caplog): content = read_study_local(study_name, "880", LocalConfiguration(local_path, study_name)) study = content._study_service - expected_keys = ['areas', 'hydro', 'load', 'misc', 'renewables', 'solar', 'storage', 'thermals', 'wind'] + expected_keys = ["areas", "hydro", "load", "misc", "renewables", "solar", "storage", "thermals", "wind"] for key in expected_keys: assert key in study, f"La clé '{key}' est absente du dictionnaire 'study'" not_expected_key = "fake_key" - assert not_expected_key not in study, f"La clé '{not_expected_key}' ne devrait pas être présente dans le dictionnaire 'study'" + assert ( + not_expected_key not in study + ), f"La clé '{not_expected_key}' ne devrait pas être présente dans le dictionnaire 'study'" def test_directory_renewable_thermique(self, caplog): local_path = r"../../studies_samples/" @@ -89,34 +91,34 @@ def test_directory_renewable_thermique(self, caplog): "op2": "0.0", "op3": "0.0", "op4": "0.0", - "op5": "0.0" + "op5": "0.0", } } assert content._study_service["renewables"].get("zone_rt").get("list") == { - "onshore": { - "group": "Wind Onshore", - "name": "onshore", - "enabled": "True", - "unitcount": "1", - "nominalcapacity": "0.0", - "ts-interpretation": "power-generation" - }, - "offshore": { - "group": "Wind Offshore", - "name": "offshore", - "enabled": "True", - "unitcount": "1", - "nominalcapacity": "0.0", - "ts-interpretation": "power-generation" - }, - "solar": { - "group": "Solar PV", - "name": "solar", - "enabled": "True", - "unitcount": "1", - "nominalcapacity": "0.0", - "ts-interpretation": "power-generation" - } + "onshore": { + "group": "Wind Onshore", + "name": "onshore", + "enabled": "True", + "unitcount": "1", + "nominalcapacity": "0.0", + "ts-interpretation": "power-generation", + }, + "offshore": { + "group": "Wind Offshore", + "name": "offshore", + "enabled": "True", + "unitcount": "1", + "nominalcapacity": "0.0", + "ts-interpretation": "power-generation", + }, + "solar": { + "group": "Solar PV", + "name": "solar", + "enabled": "True", + "unitcount": "1", + "nominalcapacity": "0.0", + "ts-interpretation": "power-generation", + }, } def test_directory_hydro_stockage(self, caplog): @@ -125,28 +127,15 @@ def test_directory_hydro_stockage(self, caplog): content = read_study_local(study_name, "880", LocalConfiguration(local_path, study_name)) assert content._study_service["storage"].get("zone_hs").get("list") == { - 'batterie': {'group': 'Battery', 'name': 'batterie'}} + "batterie": {"group": "Battery", "name": "batterie"} + } assert content._study_service["hydro"].get("hydro") == { - "inter-daily-breakdown": { - "zone_hs": "1" - }, - "intra-daily-modulation": { - "zone_hs": "24" - }, - "inter-monthly-breakdown": { - "zone_hs": "1" - }, - "initialize reservoir date": { - "zone_hs": "0" - }, - "leeway low": { - "zone_hs": "1" - }, - "leeway up": { - "zone_hs": "1" - }, - "pumping efficiency": { - "zone_hs": "1" - } - } \ No newline at end of file + "inter-daily-breakdown": {"zone_hs": "1"}, + "intra-daily-modulation": {"zone_hs": "24"}, + "inter-monthly-breakdown": {"zone_hs": "1"}, + "initialize reservoir date": {"zone_hs": "0"}, + "leeway low": {"zone_hs": "1"}, + "leeway up": {"zone_hs": "1"}, + "pumping efficiency": {"zone_hs": "1"}, + }