Skip to content

Commit af2b598

Browse files
committed
feat: fix bugs with parsing
1 parent c1e9406 commit af2b598

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

hytek/hy3/converters.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
__all__ = ("date", "time")
55

66

7-
def date(data: str) -> datetime.date:
7+
def date(data: str) -> datetime.date | None:
8+
if not data:
9+
return None
810
month = int(data[:2])
911
day = int(data[2:4])
1012
year = int(data[-4:])
1113
return datetime.date(year, month, day)
1214

1315

14-
def time(data: str) -> datetime.time:
16+
def time(data: str) -> datetime.time | None:
17+
if not data:
18+
return None
1519
_time, ampm = data.split(" ")
1620
hours, minutes = _time.split(":")
1721
hours = int(hours)

hytek/hy3/records.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ def to_line(self) -> bytes:
108108

109109

110110
class Hy3Record:
111-
record_type: str
112-
fields: list[Field]
113-
data: dict[str, Any]
114-
raw: bytes
111+
record_type: str = ""
112+
fields: list[Field] = []
113+
data: dict[str, Any] = {}
114+
raw: bytes = b""
115115
children: list[Type[Hy3Record | Hy3RecordGroup]] = []
116116

117117
def __init__(self, raw: bytes = b"", /, **kwargs):
@@ -204,14 +204,16 @@ def parse_record(cls, line: bytes):
204204
return cls(line)
205205
given_checksum = line[-2:]
206206
if cls.calculate_checksum(line[:-2]) != given_checksum:
207-
print(line, type(line))
208207
raise Exception("Bad checksum")
209208
params = {}
210209
index = 2
211210
for field in cls.fields:
212211
data = line[index:index + field.length]
213212
index += field.length
214-
params[field.name] = field.type(data.decode("utf-8", errors="replace").strip())
213+
try:
214+
params[field.name] = field.type(data.decode("utf-8", errors="replace").strip())
215+
except Exception as e:
216+
raise Exception(f"Failed to parse field {field.name} as {field.type.__name__}: {e}") from e
215217
return cls(line, **params)
216218

217219
@staticmethod
@@ -297,6 +299,7 @@ class Hy3C3Record(Hy3Record):
297299
fields = []
298300
# here for completeness, contains contact info
299301

302+
# one of the files i have has a C8 record
300303

301304
# Swimmer data
302305
class Hy3D1Record(Hy3Record):

0 commit comments

Comments
 (0)