aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-09-17 16:45:19 -0500
committerFrederick Muriuki Muriithi2024-09-17 16:45:19 -0500
commit354a3907c191cda0b725e81ac15ed2af7db7aa2f (patch)
tree934f1dd52a942b09c43109fbb76fcb6d4fc62cb9
parente039cb0b69d1645efe396361a719e2e08c3cc050 (diff)
downloadgn-uploader-354a3907c191cda0b725e81ac15ed2af7db7aa2f.tar.gz
Parse ints from strings safely.
-rw-r--r--uploader/datautils.py10
1 files changed, 10 insertions, 0 deletions
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