6
6
- under `test/data/logfile.asc`
7
7
"""
8
8
9
- from typing import cast , Any , Generator , IO , List , Optional , Union , Dict
9
+ from typing import cast , Any , Generator , IO , List , Optional , Dict
10
10
11
11
from datetime import datetime
12
12
import time
@@ -73,8 +73,10 @@ def _extract_header(self):
73
73
elif lower_case .startswith ("base" ):
74
74
try :
75
75
_ , base , _ , timestamp_format = line .split ()
76
- except ValueError :
77
- raise Exception ("Unsupported header string format: {}" .format (line ))
76
+ except ValueError as exception :
77
+ raise Exception (
78
+ f"Unsupported header string format: { line } "
79
+ ) from exception
78
80
self .base = base
79
81
self ._converted_base = self ._check_base (self .base )
80
82
self .timestamps_format = timestamp_format
@@ -135,8 +137,8 @@ def _process_classic_can_frame(
135
137
# Error Frame
136
138
msg_kwargs ["is_error_frame" ] = True
137
139
else :
138
- abr_id_str , dir , rest_of_message = line .split (None , 2 )
139
- msg_kwargs ["is_rx" ] = dir == "Rx"
140
+ abr_id_str , direction , rest_of_message = line .split (None , 2 )
141
+ msg_kwargs ["is_rx" ] = direction == "Rx"
140
142
self ._extract_can_id (abr_id_str , msg_kwargs )
141
143
142
144
if rest_of_message [0 ].lower () == "r" :
@@ -164,10 +166,10 @@ def _process_classic_can_frame(
164
166
return Message (** msg_kwargs )
165
167
166
168
def _process_fd_can_frame (self , line : str , msg_kwargs : Dict [str , Any ]) -> Message :
167
- channel , dir , rest_of_message = line .split (None , 2 )
169
+ channel , direction , rest_of_message = line .split (None , 2 )
168
170
# See ASCWriter
169
171
msg_kwargs ["channel" ] = int (channel ) - 1
170
- msg_kwargs ["is_rx" ] = dir == "Rx"
172
+ msg_kwargs ["is_rx" ] = direction == "Rx"
171
173
172
174
# CAN FD error frame
173
175
if rest_of_message .strip ()[:10 ].lower () == "errorframe" :
@@ -291,7 +293,7 @@ def __init__(
291
293
292
294
# write start of file header
293
295
now = datetime .now ().strftime (self .FORMAT_START_OF_FILE_DATE )
294
- self .file .write ("date %s \n " % now )
296
+ self .file .write (f "date { now } \n " )
295
297
self .file .write ("base hex timestamps absolute\n " )
296
298
self .file .write ("internal events logged\n " )
297
299
@@ -327,7 +329,7 @@ def log_event(self, message: str, timestamp: Optional[float] = None) -> None:
327
329
formatted_date = time .strftime (
328
330
self .FORMAT_DATE .format (mlsec ), time .localtime (self .last_timestamp )
329
331
)
330
- self .file .write ("Begin Triggerblock %s \n " % formatted_date )
332
+ self .file .write (f "Begin Triggerblock { formatted_date } \n " )
331
333
self .header_written = True
332
334
self .log_event ("Start of measurement" ) # caution: this is a recursive call!
333
335
# Use last known timestamp if unknown
@@ -342,15 +344,15 @@ def log_event(self, message: str, timestamp: Optional[float] = None) -> None:
342
344
def on_message_received (self , msg : Message ) -> None :
343
345
344
346
if msg .is_error_frame :
345
- self .log_event ("{ } ErrorFrame". format ( self . channel ) , msg .timestamp )
347
+ self .log_event (f" { self . channel } ErrorFrame" , msg .timestamp )
346
348
return
347
349
if msg .is_remote_frame :
348
- dtype = "r {:x}" . format ( msg . dlc ) # New after v8.5
350
+ dtype = f "r { msg . dlc :x} " # New after v8.5
349
351
data : List [str ] = []
350
352
else :
351
- dtype = "d {:x}" . format ( msg . dlc )
352
- data = ["{ :02X}". format ( byte ) for byte in msg .data ]
353
- arb_id = "{ :X}". format ( msg . arbitration_id )
353
+ dtype = f "d { msg . dlc :x} "
354
+ data = [f" { byte :02X} " for byte in msg .data ]
355
+ arb_id = f" { msg . arbitration_id :X} "
354
356
if msg .is_extended_id :
355
357
arb_id += "x"
356
358
channel = channel2int (msg .channel )
0 commit comments