Skip to content

Commit

Permalink
Remove useless type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-florentin-charles committed Feb 17, 2024
1 parent 5e63447 commit ff52a4c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 124 deletions.
70 changes: 21 additions & 49 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@app.route(f"{base_path}/rainfall/average")
@swag_from(average_specs.route_specs)
def average_rainfall() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args,
param.time_mode,
param.begin_year,
Expand All @@ -54,9 +54,7 @@ def average_rainfall() -> Response:
param.season,
)

to_return: Response | dict = manage_time_mode_errors(
{}, params[0], params[3], params[4]
)
to_return = manage_time_mode_errors({}, params[0], params[3], params[4])
if isinstance(to_return, Response):
return to_return

Expand All @@ -65,9 +63,7 @@ def average_rainfall() -> Response:
"name": "average rainfall (mm)",
"value": all_rainfall.get_average_rainfall(*params),
"begin_year": params[1],
"end_year": params[2]
if params[2] is not None
else all_rainfall.get_last_year(),
"end_year": params[2] or all_rainfall.get_last_year(),
"time_mode": TimeMode[params[0]],
}
)
Expand All @@ -78,13 +74,11 @@ def average_rainfall() -> Response:
@app.route(f"{base_path}/rainfall/normal")
@swag_from(normal_specs.route_specs)
def normal_rainfall() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args, param.time_mode, param.begin_year, param.month, param.season
)

to_return: Response | dict = manage_time_mode_errors(
{}, params[0], params[2], params[3]
)
to_return = manage_time_mode_errors({}, params[0], params[2], params[3])
if isinstance(to_return, Response):
return to_return

Expand All @@ -104,7 +98,7 @@ def normal_rainfall() -> Response:
@app.route(f"{base_path}/rainfall/relative_distance_to_normal")
@swag_from(relative_distance_to_normal_specs.route_specs)
def rainfall_relative_distance_to_normal() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args,
param.time_mode,
param.normal_year,
Expand All @@ -114,9 +108,7 @@ def rainfall_relative_distance_to_normal() -> Response:
param.season,
)

to_return: Response | dict = manage_time_mode_errors(
{}, params[0], params[4], params[5]
)
to_return = manage_time_mode_errors({}, params[0], params[4], params[5])
if isinstance(to_return, Response):
return to_return

Expand All @@ -126,9 +118,7 @@ def rainfall_relative_distance_to_normal() -> Response:
"value": all_rainfall.get_relative_distance_from_normal(*params),
"normal_year": params[1],
"begin_year": params[2],
"end_year": params[3]
if params[3] is not None
else all_rainfall.get_last_year(),
"end_year": params[3] or all_rainfall.get_last_year(),
"time_mode": TimeMode[params[0]],
}
)
Expand All @@ -139,7 +129,7 @@ def rainfall_relative_distance_to_normal() -> Response:
@app.route(f"{base_path}/rainfall/standard_deviation")
@swag_from(standard_deviation_specs.route_specs)
def rainfall_standard_deviation() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args,
param.time_mode,
param.begin_year,
Expand All @@ -148,9 +138,7 @@ def rainfall_standard_deviation() -> Response:
param.season,
)

to_return: Response | dict = manage_time_mode_errors(
{}, params[0], params[3], params[4]
)
to_return = manage_time_mode_errors({}, params[0], params[3], params[4])
if isinstance(to_return, Response):
return to_return

Expand All @@ -159,9 +147,7 @@ def rainfall_standard_deviation() -> Response:
"name": "rainfall standard deviation (mm)",
"value": all_rainfall.get_rainfall_standard_deviation(*params),
"begin_year": params[1],
"end_year": params[2]
if params[2] is not None
else all_rainfall.get_last_year(),
"end_year": params[2] or all_rainfall.get_last_year(),
"time_mode": TimeMode[params[0]],
}
)
Expand All @@ -172,7 +158,7 @@ def rainfall_standard_deviation() -> Response:
@app.route(f"{base_path}/year/below_normal")
@swag_from(below_normal_specs.route_specs)
def years_below_normal() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args,
param.time_mode,
param.normal_year,
Expand All @@ -182,9 +168,7 @@ def years_below_normal() -> Response:
param.season,
)

to_return: Response | dict = manage_time_mode_errors(
{}, params[0], params[4], params[5]
)
to_return = manage_time_mode_errors({}, params[0], params[4], params[5])
if isinstance(to_return, Response):
return to_return

Expand All @@ -194,9 +178,7 @@ def years_below_normal() -> Response:
"value": all_rainfall.get_years_below_normal(*params),
"normal_year": params[1],
"begin_year": params[2],
"end_year": params[3]
if params[3] is not None
else all_rainfall.get_last_year(),
"end_year": params[3] or all_rainfall.get_last_year(),
"time_mode": TimeMode[params[0]],
}
)
Expand All @@ -207,7 +189,7 @@ def years_below_normal() -> Response:
@app.route(f"{base_path}/year/above_normal")
@swag_from(above_normal_specs.route_specs)
def years_above_normal() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args,
param.time_mode,
param.normal_year,
Expand All @@ -217,9 +199,7 @@ def years_above_normal() -> Response:
param.season,
)

to_return: Response | dict = manage_time_mode_errors(
{}, params[0], params[4], params[5]
)
to_return = manage_time_mode_errors({}, params[0], params[4], params[5])
if isinstance(to_return, Response):
return to_return

Expand All @@ -229,9 +209,7 @@ def years_above_normal() -> Response:
"value": all_rainfall.get_years_above_normal(*params),
"normal_year": params[1],
"begin_year": params[2],
"end_year": params[3]
if params[3] is not None
else all_rainfall.get_last_year(),
"end_year": params[3] or all_rainfall.get_last_year(),
"time_mode": TimeMode[params[0]],
}
)
Expand All @@ -242,13 +220,11 @@ def years_above_normal() -> Response:
@app.route(f"{base_path}/csv/minimal_csv")
@swag_from(minimal_csv_specs.route_specs)
def minimal_csv() -> Response:
params: tuple = parse_args(
params = parse_args(
request.args, param.time_mode, param.month, param.season, param.file_name
)

error: Response | dict = manage_time_mode_errors(
{}, params[0], params[1], params[2]
)
error = manage_time_mode_errors({}, params[0], params[1], params[2])
if isinstance(error, Response):
return error

Expand All @@ -260,9 +236,7 @@ def minimal_csv() -> Response:
@app.route(f"{base_path}/graph/monthly_averages")
@swag_from(monthly_averages_specs.route_specs)
def monthly_averages() -> Response:
params: tuple = parse_args(
request.args, param.file_name, param.begin_year, param.end_year
)
params = parse_args(request.args, param.file_name, param.begin_year, param.end_year)

all_rainfall.bar_rainfall_averages(begin_year=params[1], end_year=params[2])
plt.savefig(params[0], format="svg")
Expand All @@ -274,9 +248,7 @@ def monthly_averages() -> Response:
@app.route(f"{base_path}/graph/seasonal_averages")
@swag_from(seasonal_averages_specs.route_specs)
def seasonal_averages() -> Response:
params: tuple = parse_args(
request.args, param.file_name, param.begin_year, param.end_year
)
params = parse_args(request.args, param.file_name, param.begin_year, param.end_year)

all_rainfall.bar_rainfall_averages(
monthly=False, begin_year=params[1], end_year=params[2]
Expand Down
4 changes: 3 additions & 1 deletion src/api/swagger/error_specs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Dict, Any

from src.api.schemas import ApiError

bad_request_specs: dict = {
bad_request_specs: Dict[str, Any] = {
"description": 'Bad Request: error specific to API; check "message" field for information.',
"schema": ApiError,
}
1 change: 1 addition & 0 deletions src/api/swagger/graph/monthly_averages_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
route_specs: dict = {
"operationId": "getMonthlyAverages",
"summary": "Retrieve monthly averages of data as an SVG.",
"description": "If no end year is set, will default to most recent data year (usually last year).",
"tags": ["Graph"],
"responses": {
"200": {
Expand Down
1 change: 1 addition & 0 deletions src/api/swagger/graph/seasonal_averages_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
route_specs: dict = {
"operationId": "getSeasonalAverages",
"summary": "Retrieve seasonal averages of data as an SVG.",
"description": "If no end year is set, will default to most recent data year (usually last year).",
"tags": ["Graph"],
"responses": {
"200": {
Expand Down
18 changes: 10 additions & 8 deletions src/api/swagger/parameters_specs.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
"""
Swagger specifications for route parameters.
"""
from typing import Dict, Any

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

normal_year: dict = {
normal_year: Dict[str, Any] = {
"default": 1971,
"required": True,
"type": "integer",
"name": "normal_year",
"in": "query",
}

begin_year: dict = {
begin_year: Dict[str, Any] = {
"default": 1991,
"required": True,
"type": "integer",
"name": "begin_year",
"in": "query",
}

end_year: dict = {
end_year: Dict[str, Any] = {
"default": None,
"required": False,
"type": "integer",
"name": "end_year",
"in": "query",
}

time_mode: dict = {
time_mode: Dict[str, Any] = {
"default": TimeMode.YEARLY.name,
"required": True,
"type": "string",
Expand All @@ -38,7 +40,7 @@
"in": "query",
}

month: dict = {
month: Dict[str, Any] = {
"default": None,
"required": False,
"type": "string",
Expand All @@ -47,7 +49,7 @@
"in": "query",
}

season: dict = {
season: Dict[str, Any] = {
"default": None,
"required": False,
"type": "string",
Expand All @@ -56,9 +58,9 @@
"in": "query",
}

time_params: tuple = (time_mode, month, season)
time_params = (time_mode, month, season)

file_name: dict = {
file_name: Dict[str, Any] = {
"default": None,
"required": True,
"type": "string",
Expand Down
18 changes: 8 additions & 10 deletions src/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ def parse_args(args: MultiDict[str, str], *params: dict) -> tuple:
:param params: Desired parameters to parse as a list of swagger-conformed dicts.
:return: Tuple containing retrieved values from query parameters.
"""
to_return: list = []
for param in params:
to_return.append(
args.get(
param["name"],
default=param["default"],
type=swagger_type_to_python_type(param["type"]),
)
)

return tuple(to_return)
return tuple(
args.get(
param["name"],
default=param["default"],
type=swagger_type_to_python_type(param["type"]),
)
for param in params
)


def swagger_type_to_python_type(swagger_type: str) -> type | None:
Expand Down
14 changes: 7 additions & 7 deletions src/core/models/all_rainfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,22 +322,22 @@ def get_last_year(self) -> int:

def bar_rainfall_averages(
self,
monthly: bool | None = True,
monthly=True,
begin_year: int | None = None,
end_year: int | None = None,
) -> list:
"""
Plots a bar graphic displaying average rainfall for each month or each season.
:param monthly: if True, plots monthly rainfall averages.
if False, plots seasonal rainfall averages.
:param monthly: If True, plots monthly rainfall averages.
If False, plots seasonal rainfall averages. Defaults to True (optional).
:param begin_year: An integer representing the year
to start getting our rainfall values.
to start getting our rainfall values. (optional).
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:return: A list of the Rainfall averages for each month or season.
"""
label: str = f"Average rainfall (mm) between {begin_year or self.starting_year} and {self.get_last_year()}"
label = f"Average rainfall (mm) between {begin_year or self.starting_year} and {self.get_last_year()}"
if monthly:
return plotting.bar_monthly_rainfall_averages(
self.monthly_rainfalls, label, begin_year, end_year
Expand All @@ -347,12 +347,12 @@ def bar_rainfall_averages(
self.seasonal_rainfalls, label, begin_year, end_year
)

def bar_rainfall_linreg_slopes(self, monthly: bool | None = True) -> list:
def bar_rainfall_linreg_slopes(self, monthly=True) -> list:
"""
Plots a bar graphic displaying linear regression slope for each month or each season.
:param monthly: if True, plots monthly rainfall LinReg slopes.
if False, plots seasonal rainfall LinReg slopes.
if False, plots seasonal rainfall LinReg slopes. Defaults to True (optional).
:return: A list of the Rainfall LinReg slopes for each month or season.
"""
if monthly:
Expand Down
Loading

0 comments on commit ff52a4c

Please sign in to comment.