I am writing a small function that takes an integer value and a string such as:
param1: 1
param2: "1 1 1"
The function will split the string parameter and validate its len against the first parameter like so:
def prepare_integer_set(expected_len, integer_string):
prepared_integer_set = integer_string.split()
if len(prepared_integer_set) != expected_len:
raise ValueError('Number of expected integers does not match integer string')
return [int(x) for x in prepared_integer_set]
However, the exception is bothering me. I would rather return false as the input comes from the user so is not strictly an exception if they make an error.
How should this be handled? Split the method in 2, having a separate validation and preparation methods? Or is it pythonic as it currently is by throwing the exception?
Here would be the alternative which is split:
def validate_integer_set(expected_len, integer_set):
return expected_len == len(integer_set)
def prepare_integer_set(integer_string):
prepared_integer_set = integer_string.split()
return [int(x) for x in prepared_integer_set]