mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-22 01:16:14 +00:00
fix parse hour
This commit is contained in:
+17
-15
@@ -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
|
||||
|
||||
+2
-2
@@ -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__':
|
||||
|
||||
Reference in New Issue
Block a user