Commit 141ca8fc authored by Niklas Selke's avatar Niklas Selke
Browse files

The 'check_seasons' function now raises a 'ValueError' if there is any problem...

The 'check_seasons' function now raises a 'ValueError' if there is any problem with the given 'seasons' argument.
parent cb55e3aa
...@@ -242,28 +242,41 @@ def check_metadata(metadata_in, station_lat_in, station_lon_in, ...@@ -242,28 +242,41 @@ def check_metadata(metadata_in, station_lat_in, station_lon_in,
return namedtuple("Metadata", metadata_dict)(**metadata_dict) return namedtuple("Metadata", metadata_dict)(**metadata_dict)
def check_seasons(seasons_in): def check_seasons(seasons_in, seasons_req):
"""Check the given seasons argument. """Check the given seasons argument.
:param seasons_in: the given seasons argument :param seasons_in: the given seasons argument
:param seasons_req: boolean denoting whether the seasons argument is
:return: The processed seasons argument or None if there was a :raises ValueError: raised if the seasons argument is required and
problem while processing the given seasons argument either missing or is not a type of list or a
string or if no season is given or if any given
season is not recognized
:return: A list of seasons or None if the seasons argument is not
""" """
if not isinstance(seasons_in, (list, str)): if not seasons_req:
return None return None
seasons_in_list = ( if not isinstance(seasons_in, (list, set, tuple, str)):
[seasons_in] if isinstance(seasons_in, str) else seasons_in raise ValueError("The seasons argument must be a list, set, tuple or"
) " string")
if not seasons_in:
raise ValueError("No seasons given")
seasons_in_list = ([seasons_in] if isinstance(seasons_in, str)
else list(seasons_in))
seasons_out = [] seasons_out = []
for season in seasons_in_list: for season in seasons_in_list:
if not isinstance(season, str): if not isinstance(season, str):
continue raise ValueError("Seasons must be given as strings")
season = season.strip() season = season.strip()
if season in SEASON_DICT and season not in seasons_out: if season in seasons_out:
if season in SEASON_DICT:
seasons_out.append(season) seasons_out.append(season)
if not seasons_out: else:
return None raise ValueError(f"{season} is not a recognized season")
return seasons_out return seasons_out
...@@ -110,6 +110,6 @@ def calculate_statistics( ...@@ -110,6 +110,6 @@ def calculate_statistics(
metadata, station_lat, station_lon, station_climatic_zone, metadata, station_lat, station_lon, station_climatic_zone,
station_lat_req, station_lon_req, station_climatic_zone_req station_lat_req, station_lon_req, station_climatic_zone_req
) )
seasons_list = check_seasons(seasons) seasons_list = check_seasons(seasons, seasons_req)
crops_list = check_crops(crops) crops_list = check_crops(crops)
data_capture_value = check_data_capture(data_capture) data_capture_value = check_data_capture(data_capture)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment