Skip to content

Commit

Permalink
Improve the "ASCReader" read performance. (#1717)
Browse files Browse the repository at this point in the history
* Update asc.py

improve the "ASCReader" performance.

* Update asc.py

Fix: the test  error "error: Item "None" of "Optional[Match[str]]" has no attribute "group"  [union-attr]"

* Update asc.py

style: format code with "Black Code Formatter"

* improvement: make regular expression compile result as module constants.

* style:format import

---------

Co-authored-by: XXIN <black94xx@163.com>
  • Loading branch information
XXIN0 and XXIN committed Dec 30, 2023
1 parent 35eef6e commit 3f6e951
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions can/io/asc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
import time
from datetime import datetime
from typing import Any, Dict, Generator, List, Optional, TextIO, Union
from typing import Any, Dict, Final, Generator, List, Optional, TextIO, Union

from ..message import Message
from ..typechecking import StringPathLike
Expand All @@ -20,6 +20,14 @@
CAN_ID_MASK = 0x1FFFFFFF
BASE_HEX = 16
BASE_DEC = 10
ASC_TRIGGER_REGEX: Final = re.compile(
r"begin\s+triggerblock\s+\w+\s+(?P<datetime_string>.+)", re.IGNORECASE
)
ASC_MESSAGE_REGEX: Final = re.compile(
r"\d+\.\d+\s+(\d+\s+(\w+\s+(Tx|Rx)|ErrorFrame)|CANFD)",
re.ASCII | re.IGNORECASE,
)


logger = logging.getLogger("can.io.asc")

Expand Down Expand Up @@ -258,12 +266,7 @@ def __iter__(self) -> Generator[Message, None, None]:
for _line in self.file:
line = _line.strip()

trigger_match = re.match(
r"begin\s+triggerblock\s+\w+\s+(?P<datetime_string>.+)",
line,
re.IGNORECASE,
)
if trigger_match:
if trigger_match := ASC_TRIGGER_REGEX.match(line):
datetime_str = trigger_match.group("datetime_string")
self.start_time = (
0.0
Expand All @@ -272,11 +275,7 @@ def __iter__(self) -> Generator[Message, None, None]:
)
continue

if not re.match(
r"\d+\.\d+\s+(\d+\s+(\w+\s+(Tx|Rx)|ErrorFrame)|CANFD)",
line,
re.ASCII | re.IGNORECASE,
):
if not ASC_MESSAGE_REGEX.match(line):
# line might be a comment, chip status,
# J1939 message or some other unsupported event
continue
Expand Down

0 comments on commit 3f6e951

Please sign in to comment.