Commit d3348312 authored by Niklas Selke's avatar Niklas Selke
Browse files

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

The 'check_crops' function now raises a 'ValueError' if there is any problem with the given 'crops' argument.
parent 6d25e6e5
...@@ -280,26 +280,39 @@ def check_seasons(seasons_in, seasons_req): ...@@ -280,26 +280,39 @@ def check_seasons(seasons_in, seasons_req):
return seasons_out return seasons_out
def check_crops(crops_in): def check_crops(crops_in, crops_req):
"""Check the given crops argument. """Check the given crops argument.
:param crops_in: the given crops argument :param crops_in: the given crops argument
:param crops_req: boolean denoting whether the crops argument is
required
:return: The processed crops argument or None if there was a problem :raises ValueError: raised if the crops argument is required and not
while processing the given crops argument None and either not a type of list or a string
or empty or any given crop is not recognized
:return: A list of crops or None if the crops argument is either
not needed or None
""" """
if not isinstance(crops_in, (list, str)): if not crops_req or crops_in is None:
return None return None
crops_in_list = [crops_in] if isinstance(crops_in, str) else crops_in if not isinstance(crops_in, (list, set, tuple, str)):
raise ValueError("The crops argument must be a list, set, tuple or"
" string")
if not crops_in:
raise ValueError("No crops given")
crops_in_list = [crops_in] if isinstance(crops_in, str) else list(crops_in)
crops_out = [] crops_out = []
for crop in crops_in_list: for crop in crops_in_list:
if not isinstance(crop, str): if not isinstance(crop, str):
continue raise ValueError("Crops must be given as strings")
crop = crop.strip() crop = crop.strip()
if crop in {"wheat", "rice"} and crop not in crops_out: if crop in crops_out:
continue
if crop in {"wheat", "rice"}:
crops_out.append(crop) crops_out.append(crop)
if not crops_out: else:
return None raise ValueError(f"{crop} is not a recognized crop")
return crops_out return crops_out
......
...@@ -111,5 +111,5 @@ def calculate_statistics( ...@@ -111,5 +111,5 @@ def calculate_statistics(
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_req) seasons_list = check_seasons(seasons, seasons_req)
crops_list = check_crops(crops) crops_list = check_crops(crops, crops_req)
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