Skip to content

Commit 44bf3de

Browse files
committed
Add format lookup map
1 parent 2bd4758 commit 44bf3de

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

can/io/trc.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,12 @@ class TRCWriter(TextIOMessageWriter):
278278
file: TextIO
279279
first_timestamp: Optional[float]
280280

281-
FORMAT_MESSAGE = (
282-
"{msgnr:>7} {time:13.3f} DT {channel:>2} {id:>8} {dir:>2} - {dlc:<4} {data}"
283-
)
284-
FORMAT_MESSAGE_V1_0 = "{msgnr:>6}) {time:7.0f} {id:>8} {dlc:<1} {data}"
281+
MESSAGE_FORMAT_MAP: Mapping[TRCFileVersion, str] = {
282+
TRCFileVersion.V1_0: "{msgnr:>6}) {time:7.0f} {id:>8} {dlc:<1} {data}",
283+
TRCFileVersion.V2_1: (
284+
"{msgnr:>7} {time:13.3f} DT {channel:>2} {id:>8} {dir:>2} - {dlc:<4} {data}"
285+
),
286+
}
285287

286288
def __init__(
287289
self,
@@ -309,7 +311,7 @@ def __init__(
309311
self.msgnr = 0
310312
self.first_timestamp = None
311313
self.file_version = TRCFileVersion.V2_1
312-
self._msg_fmt_string = self.FORMAT_MESSAGE_V1_0
314+
self._msg_fmt_string = self.MESSAGE_FORMAT_MAP[self.file_version]
313315
self._format_message = self._format_message_init
314316

315317
def _write_header_v1_0(self, start_time: datetime) -> None:
@@ -381,13 +383,12 @@ def _format_message_by_format(self, msg, channel):
381383
def _format_message_init(self, msg, channel):
382384
if self.file_version == TRCFileVersion.V1_0:
383385
self._format_message = self._format_message_by_format
384-
self._msg_fmt_string = self.FORMAT_MESSAGE_V1_0
385386
elif self.file_version == TRCFileVersion.V2_1:
386387
self._format_message = self._format_message_by_format
387-
self._msg_fmt_string = self.FORMAT_MESSAGE
388388
else:
389389
raise NotImplementedError("File format is not supported")
390390

391+
self._msg_fmt_string = self.MESSAGE_FORMAT_MAP[self.file_version]
391392
return self._format_message_by_format(msg, channel)
392393

393394
def write_header(self, timestamp: float) -> None:

0 commit comments

Comments
 (0)