From 354a3907c191cda0b725e81ac15ed2af7db7aa2f Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 17 Sep 2024 16:45:19 -0500 Subject: Parse ints from strings safely. --- uploader/datautils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'uploader/datautils.py') diff --git a/uploader/datautils.py b/uploader/datautils.py index e593937..2ee079d 100644 --- a/uploader/datautils.py +++ b/uploader/datautils.py @@ -26,3 +26,13 @@ def order_by_family(items: tuple[dict, ...], return sorted(tuple(reduce(__order__, items, {}).items()), key=lambda item: item[0][0]) + + +def safe_int(val: str) -> int: + """ + Convert val into an integer: if val cannot be converted, return a zero. + """ + try: + return int(val) + except ValueError: + return 0 -- cgit v1.2.3