Skip to content

Commit ba77fd5

Browse files
ENH: replace if elif else chains with match statement (#921)
* ENH: replace ifelif else chains with match statement * Update rocketpy/environment/environment.py Co-authored-by: Gui-FernandesBR <[email protected]> * fix CI tests and add changes to CHANGELOG.md * update CHANGELOG.md * fix linters --------- Co-authored-by: Gui-FernandesBR <[email protected]>
1 parent 05d7f90 commit ba77fd5

File tree

9 files changed

+442
-397
lines changed

9 files changed

+442
-397
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Attention: The newest changes should be on top -->
4949

5050
### Changed
5151

52+
- ENH: replace if elif else chains with match statement [#921](https://github.com/RocketPy-Team/RocketPy/pull/921/changes)
5253
- ENH: Refactor Flight class to improve time node handling and sensor/controllers [#843](https://github.com/RocketPy-Team/RocketPy/pull/843)
5354

5455
### Fixed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exclude_also = [
7979

8080

8181
[tool.ruff]
82-
target-version = "py39"
82+
target-version = "py310"
8383
line-length = 88
8484
indent-width = 4
8585

rocketpy/environment/environment.py

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,31 +1277,31 @@ def set_atmospheric_model( # pylint: disable=too-many-statements
12771277
self.atmospheric_model_type = type
12781278
type = type.lower()
12791279

1280-
# Handle each case # TODO: use match case when python 3.9 is no longer supported
1281-
if type == "standard_atmosphere":
1282-
self.process_standard_atmosphere()
1283-
elif type == "wyoming_sounding":
1284-
self.process_wyoming_sounding(file)
1285-
elif type == "custom_atmosphere":
1286-
self.process_custom_atmosphere(pressure, temperature, wind_u, wind_v)
1287-
elif type == "windy":
1288-
self.process_windy_atmosphere(file)
1289-
elif type in ["forecast", "reanalysis", "ensemble"]:
1290-
dictionary = self.__validate_dictionary(file, dictionary)
1291-
try:
1292-
fetch_function = self.__atm_type_file_to_function_map[type][file]
1293-
except KeyError:
1294-
fetch_function = None
1295-
1296-
# Fetches the dataset using OpenDAP protocol or uses the file path
1297-
dataset = fetch_function() if fetch_function is not None else file
1298-
1299-
if type in ["forecast", "reanalysis"]:
1300-
self.process_forecast_reanalysis(dataset, dictionary)
1301-
else:
1302-
self.process_ensemble(dataset, dictionary)
1303-
else: # pragma: no cover
1304-
raise ValueError(f"Unknown model type '{type}'.")
1280+
match type:
1281+
case "standard_atmosphere":
1282+
self.process_standard_atmosphere()
1283+
case "wyoming_sounding":
1284+
self.process_wyoming_sounding(file)
1285+
case "custom_atmosphere":
1286+
self.process_custom_atmosphere(pressure, temperature, wind_u, wind_v)
1287+
case "windy":
1288+
self.process_windy_atmosphere(file)
1289+
case "forecast" | "reanalysis" | "ensemble":
1290+
dictionary = self.__validate_dictionary(file, dictionary)
1291+
try:
1292+
fetch_function = self.__atm_type_file_to_function_map[type][file]
1293+
except KeyError:
1294+
fetch_function = None
1295+
1296+
# Fetches the dataset using OpenDAP protocol or uses the file path
1297+
dataset = fetch_function() if fetch_function is not None else file
1298+
1299+
if type in ["forecast", "reanalysis"]:
1300+
self.process_forecast_reanalysis(dataset, dictionary)
1301+
else:
1302+
self.process_ensemble(dataset, dictionary)
1303+
case _: # pragma: no cover
1304+
raise ValueError(f"Unknown model type '{type}'.")
13051305

13061306
if type not in ["ensemble"]:
13071307
# Ensemble already computed these values
@@ -2696,51 +2696,52 @@ def from_dict(cls, data): # pylint: disable=too-many-statements
26962696
)
26972697
atmospheric_model = data["atmospheric_model_type"]
26982698

2699-
if atmospheric_model == "standard_atmosphere":
2700-
env.set_atmospheric_model("standard_atmosphere")
2701-
elif atmospheric_model == "custom_atmosphere":
2702-
env.set_atmospheric_model(
2703-
type="custom_atmosphere",
2704-
pressure=data["pressure"],
2705-
temperature=data["temperature"],
2706-
wind_u=data["wind_velocity_x"],
2707-
wind_v=data["wind_velocity_y"],
2708-
)
2709-
else:
2710-
env.__set_pressure_function(data["pressure"])
2711-
env.__set_temperature_function(data["temperature"])
2712-
env.__set_wind_velocity_x_function(data["wind_velocity_x"])
2713-
env.__set_wind_velocity_y_function(data["wind_velocity_y"])
2714-
env.__set_wind_heading_function(data["wind_heading"])
2715-
env.__set_wind_direction_function(data["wind_direction"])
2716-
env.__set_wind_speed_function(data["wind_speed"])
2717-
env.elevation = data["elevation"]
2718-
env.max_expected_height = data["max_expected_height"]
2719-
2720-
if atmospheric_model in ("windy", "forecast", "reanalysis", "ensemble"):
2721-
env.atmospheric_model_init_date = data["atmospheric_model_init_date"]
2722-
env.atmospheric_model_end_date = data["atmospheric_model_end_date"]
2723-
env.atmospheric_model_interval = data["atmospheric_model_interval"]
2724-
env.atmospheric_model_init_lat = data["atmospheric_model_init_lat"]
2725-
env.atmospheric_model_end_lat = data["atmospheric_model_end_lat"]
2726-
env.atmospheric_model_init_lon = data["atmospheric_model_init_lon"]
2727-
env.atmospheric_model_end_lon = data["atmospheric_model_end_lon"]
2728-
2729-
if atmospheric_model == "ensemble":
2730-
env.level_ensemble = data["level_ensemble"]
2731-
env.height_ensemble = data["height_ensemble"]
2732-
env.temperature_ensemble = data["temperature_ensemble"]
2733-
env.wind_u_ensemble = data["wind_u_ensemble"]
2734-
env.wind_v_ensemble = data["wind_v_ensemble"]
2735-
env.wind_heading_ensemble = data["wind_heading_ensemble"]
2736-
env.wind_direction_ensemble = data["wind_direction_ensemble"]
2737-
env.wind_speed_ensemble = data["wind_speed_ensemble"]
2738-
env.num_ensemble_members = data["num_ensemble_members"]
2739-
2740-
env.__reset_barometric_height_function()
2741-
env.calculate_density_profile()
2742-
env.calculate_speed_of_sound_profile()
2743-
env.calculate_dynamic_viscosity()
2699+
match atmospheric_model:
2700+
case "standard_atmosphere":
2701+
env.set_atmospheric_model("standard_atmosphere")
2702+
case "custom_atmosphere":
2703+
env.set_atmospheric_model(
2704+
type="custom_atmosphere",
2705+
pressure=data["pressure"],
2706+
temperature=data["temperature"],
2707+
wind_u=data["wind_velocity_x"],
2708+
wind_v=data["wind_velocity_y"],
2709+
)
2710+
case _:
2711+
env.__set_pressure_function(data["pressure"])
2712+
env.__set_temperature_function(data["temperature"])
2713+
env.__set_wind_velocity_x_function(data["wind_velocity_x"])
2714+
env.__set_wind_velocity_y_function(data["wind_velocity_y"])
2715+
env.__set_wind_heading_function(data["wind_heading"])
2716+
env.__set_wind_direction_function(data["wind_direction"])
2717+
env.__set_wind_speed_function(data["wind_speed"])
2718+
env.elevation = data["elevation"]
2719+
env.max_expected_height = data["max_expected_height"]
2720+
2721+
if atmospheric_model in ("windy", "forecast", "reanalysis", "ensemble"):
2722+
env.atmospheric_model_init_date = data["atmospheric_model_init_date"]
2723+
env.atmospheric_model_end_date = data["atmospheric_model_end_date"]
2724+
env.atmospheric_model_interval = data["atmospheric_model_interval"]
2725+
env.atmospheric_model_init_lat = data["atmospheric_model_init_lat"]
2726+
env.atmospheric_model_end_lat = data["atmospheric_model_end_lat"]
2727+
env.atmospheric_model_init_lon = data["atmospheric_model_init_lon"]
2728+
env.atmospheric_model_end_lon = data["atmospheric_model_end_lon"]
2729+
2730+
if atmospheric_model == "ensemble":
2731+
env.level_ensemble = data["level_ensemble"]
2732+
env.height_ensemble = data["height_ensemble"]
2733+
env.temperature_ensemble = data["temperature_ensemble"]
2734+
env.wind_u_ensemble = data["wind_u_ensemble"]
2735+
env.wind_v_ensemble = data["wind_v_ensemble"]
2736+
env.wind_heading_ensemble = data["wind_heading_ensemble"]
2737+
env.wind_direction_ensemble = data["wind_direction_ensemble"]
2738+
env.wind_speed_ensemble = data["wind_speed_ensemble"]
2739+
env.num_ensemble_members = data["num_ensemble_members"]
2740+
2741+
env.__reset_barometric_height_function()
2742+
env.calculate_density_profile()
2743+
env.calculate_speed_of_sound_profile()
2744+
env.calculate_dynamic_viscosity()
27442745

27452746
return env
27462747

0 commit comments

Comments
 (0)