33import logging
44import os
55import warnings
6- from contextlib import contextmanager
6+ from contextlib import contextmanager , suppress
77from dataclasses import dataclass , field
88from io import (
99 BufferedReader ,
1313 TextIOWrapper ,
1414)
1515from math import isnan , nan
16- from re import Match , Pattern , compile
16+ from re import I , Match , Pattern , compile
1717from 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 ()
0 commit comments