From 0a8c1eab4292ed89da8e707088e86fbfac249635 Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Mon, 24 May 2021 14:21:23 +0200 Subject: [PATCH] fix parse hour --- libs/utils.py | 32 +++++++++++++++++--------------- test/test_unit.py | 4 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/libs/utils.py b/libs/utils.py index d6a851b..2ad60c9 100644 --- a/libs/utils.py +++ b/libs/utils.py @@ -1,9 +1,9 @@ -import re from functools import wraps from threading import Semaphore, Timer import socket import requests +from typing import List from mylogger import logger @@ -50,17 +50,19 @@ def is_port_in_use(ip, port): return s.connect_ex((ip, port)) == 0 -def parse_hour(hour_str): - reg = r"PT([0-9]{1,2})H([0-9]{1,2})?|PT([0-9]{1,2})S" - hour_minute = re.findall(reg, hour_str)[0] - second = 0 - if hour_minute[0] == '': - hour = 0 - second = hour_minute[2] - else: - hour = int(hour_minute[0]) - if hour_minute[1] == '': - minute = 0 - else: - minute = hour_minute[1] - return hour, minute, second +def parse_hour(s): + s = s[2:] + separators = ("H", "M", "S") + res: List[int] = [] + for sep in separators: + if sep in s: + n, s = s.split(sep) + else: + n = 0 + res.append(int(n)) + if s.isnumeric(): + res.append(int(s)) + break + if len(res) == 2: + res.append(0) + return res diff --git a/test/test_unit.py b/test/test_unit.py index afbf103..f9b2118 100644 --- a/test/test_unit.py +++ b/test/test_unit.py @@ -251,8 +251,8 @@ class TestUnit(unittest.TestCase): assert old_dummy_value != dummy_value def test_parse_hour(self): - expected_res = [(2, 0, 0), (3, '14', 0), (0, 0, '2')] - assert expected_res == [parse_hour(h) for h in ["PT2H", "PT3H14", "PT2S"]] + expected_res = [[2, 0, 0], [3, 14, 0], [0, 0, 2], [0, 30, 0]] + assert expected_res == [parse_hour(h) for h in ["PT2H", "PT3H14", "PT2S", "PT30M"]] if __name__ == '__main__':