Skip to content

Commit 2575c81

Browse files
committed
fix: improve error handling in DCM class and update version to 0.1.8
1 parent ba7194d commit 2575c81

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

dcm/dcm.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import os
55
import warnings
6-
from contextlib import contextmanager
6+
from contextlib import contextmanager, suppress
77
from dataclasses import dataclass, field
88
from io import (
99
BufferedReader,
@@ -13,12 +13,13 @@
1313
TextIOWrapper,
1414
)
1515
from math import isnan, nan
16-
from re import Match, Pattern, compile
16+
from re import I, Match, Pattern, compile
1717
from typing import (
1818
TYPE_CHECKING,
1919
Callable,
2020
Final,
2121
Iterable,
22+
Iterator,
2223
Optional,
2324
TypeAlias,
2425
TypeVar,
@@ -524,7 +525,7 @@ def from_bytestream(cls, stream: BytesReadable) -> list["Self"]:
524525
line
525526
)
526527
if function_match is None:
527-
raise ValueError(f"Invalid function: {line}")
528+
continue
528529
functions.append(
529530
cls(
530531
name=function_match.group(1).decode(errors="replace"),
@@ -1177,7 +1178,7 @@ def load_from_excel(
11771178

11781179
def load_maps(self, excel_path: PathOrReadable) -> "Self":
11791180
with _open_stream(excel_path) as excel_file:
1180-
if isinstance(excel_file, Exception):
1181+
if excel_file is None:
11811182
return self
11821183
excel = pd.ExcelFile(excel_file)
11831184
for sheet in excel.sheet_names:
@@ -1193,7 +1194,7 @@ def load_maps(self, excel_path: PathOrReadable) -> "Self":
11931194

11941195
def load_curves(self, excel_path: PathOrReadable) -> "Self":
11951196
with _open_stream(excel_path) as excel_file:
1196-
if isinstance(excel_file, Exception):
1197+
if excel_file is None:
11971198
return self
11981199
excel = pd.ExcelFile(excel_file)
11991200
for sheet in excel.sheet_names:
@@ -1214,7 +1215,7 @@ def load_lines(self) -> Callable[[PathOrReadable], "Self"]:
12141215

12151216
def load_parameter_blocks(self, excel_path: PathOrReadable) -> "Self":
12161217
with _open_stream(excel_path) as excel_file:
1217-
if isinstance(excel_file, Exception):
1218+
if excel_file is None:
12181219
return self
12191220
excel = pd.ExcelFile(excel_file)
12201221
for sheet in excel.sheet_names:
@@ -1230,7 +1231,7 @@ def load_parameter_blocks(self, excel_path: PathOrReadable) -> "Self":
12301231

12311232
def load_parameters(self, excel_path: PathOrReadable) -> "Self":
12321233
with _open_stream(excel_path) as excel_file:
1233-
if isinstance(excel_file, Exception):
1234+
if excel_file is None:
12341235
return self
12351236
df: pd.DataFrame = pd.read_excel(excel_file)
12361237
for _, row in df.iterrows():
@@ -1268,8 +1269,8 @@ def read(self, path_or_file: PathOrReadable) -> "Self":
12681269
is_file_header_finished: bool = False
12691270

12701271
with _open_stream(path_or_file) as stream:
1271-
if isinstance(stream, Exception):
1272-
return self
1272+
if stream is None:
1273+
raise FileNotFoundError(f"File not found: {path_or_file}")
12731274
for line in stream:
12741275
# Check if empty line
12751276
line = line.strip()
@@ -1483,18 +1484,20 @@ def wrapper(x: npt.ArrayLike) -> npt.NDArray[FloatDtype]:
14831484

14841485

14851486
@contextmanager
1486-
def _open_stream(path_or_file: PathOrReadable):
1487+
def _open_stream(
1488+
path_or_file: PathOrReadable,
1489+
) -> Iterator[Optional[BytesReadable]]:
14871490
stream: Optional[BytesReadable] = None
14881491
try:
1489-
if isinstance(path_or_file, BytesReadable):
1490-
stream = path_or_file
1491-
elif isinstance(path_or_file, StringReadable):
1492-
stream = BytesIO(path_or_file.read().encode("utf-8"))
1493-
else:
1494-
stream = open(path_or_file, "rb")
1492+
with suppress(BaseException):
1493+
1494+
if isinstance(path_or_file, BytesReadable):
1495+
stream = path_or_file
1496+
elif isinstance(path_or_file, StringReadable):
1497+
stream = BytesIO(path_or_file.read().encode("utf-8"))
1498+
else:
1499+
stream = open(path_or_file, "rb")
14951500
yield stream
1496-
except Exception as e:
1497-
yield e
14981501
finally:
14991502
if stream is not None:
15001503
stream.close()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-dcm"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "A high-performance Python package for handling ETAS DCM(Data Conversion Format) files used in engine calibration tools like INCA, MDA, EHANDBOOK, and CANape."
55
authors = ["c0sogi <dcas@naver.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)