Skip to content

Commit b166bb6

Browse files
committed
CLN: use suppress instead of try/except/pass
1 parent 1448d49 commit b166bb6

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

pandas/io/pytables.py

+7-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
High level interface to PyTables for reading and writing pandas data structures
33
to disk
44
"""
5+
from contextlib import suppress
56
import copy
67
from datetime import date, tzinfo
78
import itertools
@@ -202,12 +203,10 @@ def _tables():
202203
# set the file open policy
203204
# return the file open policy; this changes as of pytables 3.1
204205
# depending on the HDF5 version
205-
try:
206+
with suppress(AttributeError):
206207
_table_file_open_policy_is_strict = (
207208
tables.file._FILE_OPEN_POLICY == "strict"
208209
)
209-
except AttributeError:
210-
pass
211210

212211
return _table_mod
213212

@@ -423,10 +422,8 @@ def read_hdf(
423422
except (ValueError, TypeError, KeyError):
424423
if not isinstance(path_or_buf, HDFStore):
425424
# if there is an error, close the store if we opened it.
426-
try:
425+
with suppress(AttributeError):
427426
store.close()
428-
except AttributeError:
429-
pass
430427

431428
raise
432429

@@ -763,10 +760,8 @@ def flush(self, fsync: bool = False):
763760
if self._handle is not None:
764761
self._handle.flush()
765762
if fsync:
766-
try:
763+
with suppress(OSError):
767764
os.fsync(self._handle.fileno())
768-
except OSError:
769-
pass
770765

771766
def get(self, key: str):
772767
"""
@@ -3025,11 +3020,9 @@ def write_array(self, key: str, value: ArrayLike, items: Optional[Index] = None)
30253020

30263021
atom = None
30273022
if self._filters is not None:
3028-
try:
3023+
with suppress(ValueError):
30293024
# get the atom for this datatype
30303025
atom = _tables().Atom.from_dtype(value.dtype)
3031-
except ValueError:
3032-
pass
30333026

30343027
if atom is not None:
30353028
# We only get here if self._filters is non-None and
@@ -5046,14 +5039,12 @@ def _maybe_adjust_name(name: str, version) -> str:
50465039
-------
50475040
str
50485041
"""
5049-
try:
5042+
with suppress(IndexError):
50505043
if version[0] == 0 and version[1] <= 10 and version[2] == 0:
50515044
m = re.search(r"values_block_(\d+)", name)
50525045
if m:
50535046
grp = m.groups()[0]
50545047
name = f"values_{grp}"
5055-
except IndexError:
5056-
pass
50575048
return name
50585049

50595050

@@ -5143,7 +5134,7 @@ def __init__(
51435134
if is_list_like(where):
51445135

51455136
# see if we have a passed coordinate like
5146-
try:
5137+
with suppress(ValueError):
51475138
inferred = lib.infer_dtype(where, skipna=False)
51485139
if inferred == "integer" or inferred == "boolean":
51495140
where = np.asarray(where)
@@ -5163,9 +5154,6 @@ def __init__(
51635154
)
51645155
self.coordinates = where
51655156

5166-
except ValueError:
5167-
pass
5168-
51695157
if self.coordinates is None:
51705158

51715159
self.terms = self.generate(where)

0 commit comments

Comments
 (0)