From 9aaec2fe6d99db78f527c0e9900941a3b25daf67 Mon Sep 17 00:00:00 2001 From: John Lane Date: Wed, 4 Dec 2019 00:27:05 -0600 Subject: [PATCH] Switch to using checks instead of try-catch flow in _to_int - Using conditionals for flow control rather than try/catch. - Switched to using list.index() instead of for loop matching. - Added documentation to function. --- pycron/__init__.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pycron/__init__.py b/pycron/__init__.py index f740967..2a0aaa8 100644 --- a/pycron/__init__.py +++ b/pycron/__init__.py @@ -12,18 +12,22 @@ def _to_int(value, allow_daynames=False): - try: + """ + Converts a value to an integer. If allow_daynames is True, it will convert day of week to an integer 0 through 6. + @input: + value = value to convert to integer + allow_daynames = True, to allow values like Mon or Monday + @output: value as an integer + """ + + if isinstance(value, int) or (isinstance(value, str) and value.isnumeric()): return int(value) - except ValueError: - if not allow_daynames: - raise - - for i, day_name in enumerate(DAY_NAMES): - if day_name == value: - return i - for i, day_abbr in enumerate(DAY_ABBRS): - if day_abbr == value: - return i + + elif isinstance(value, str) and allow_daynames and value in DAY_NAMES: + return DAY_NAMES.index(value) + + elif isinstance(value, str) and allow_daynames and value in DAY_ABBRS: + return DAY_ABBRS.index(value) raise ValueError('Failed to parse string to integer')