Skip to content

Commit 8cbfd06

Browse files
jbrockmendeljreback
authored andcommitted
CLN: make lookups explicit instead of using globals (#30343)
1 parent f8b9ce7 commit 8cbfd06

File tree

1 file changed

+21
-42
lines changed

1 file changed

+21
-42
lines changed

pandas/io/pytables.py

+21-42
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,6 @@ class DuplicateWarning(Warning):
176176
# formats
177177
_FORMAT_MAP = {"f": "fixed", "fixed": "fixed", "t": "table", "table": "table"}
178178

179-
# storer class map
180-
_STORER_MAP = {
181-
"series": "SeriesFixed",
182-
"frame": "FrameFixed",
183-
}
184-
185-
# table class map
186-
_TABLE_MAP = {
187-
"generic_table": "GenericTable",
188-
"appendable_series": "AppendableSeriesTable",
189-
"appendable_multiseries": "AppendableMultiSeriesTable",
190-
"appendable_frame": "AppendableFrameTable",
191-
"appendable_multiframe": "AppendableMultiFrameTable",
192-
"worm": "WORMTable",
193-
}
194-
195179
# axes map
196180
_AXES_MAP = {DataFrame: [0]}
197181

@@ -1553,12 +1537,17 @@ def _create_storer(
15531537
self,
15541538
group,
15551539
format=None,
1556-
value=None,
1540+
value: Optional[FrameOrSeries] = None,
15571541
encoding: str = "UTF-8",
15581542
errors: str = "strict",
15591543
) -> Union["GenericFixed", "Table"]:
15601544
""" return a suitable class to operate """
15611545

1546+
cls: Union[Type["GenericFixed"], Type["Table"]]
1547+
1548+
if value is not None and not isinstance(value, (Series, DataFrame)):
1549+
raise TypeError("value must be None, Series, or DataFrame")
1550+
15621551
def error(t):
15631552
# return instead of raising so mypy can tell where we are raising
15641553
return TypeError(
@@ -1587,23 +1576,20 @@ def error(t):
15871576
)
15881577
else:
15891578
_TYPE_MAP = {Series: "series", DataFrame: "frame"}
1590-
try:
1591-
pt = _TYPE_MAP[type(value)]
1592-
except KeyError:
1593-
raise error("_TYPE_MAP")
1579+
pt = _TYPE_MAP[type(value)]
15941580

15951581
# we are actually a table
15961582
if format == "table":
15971583
pt += "_table"
15981584

15991585
# a storer node
16001586
if "table" not in pt:
1587+
_STORER_MAP = {"series": SeriesFixed, "frame": FrameFixed}
16011588
try:
1602-
return globals()[_STORER_MAP[pt]](
1603-
self, group, encoding=encoding, errors=errors
1604-
)
1589+
cls = _STORER_MAP[pt]
16051590
except KeyError:
16061591
raise error("_STORER_MAP")
1592+
return cls(self, group, encoding=encoding, errors=errors)
16071593

16081594
# existing node (and must be a table)
16091595
if tt is None:
@@ -1625,29 +1611,22 @@ def error(t):
16251611
tt = "appendable_frame"
16261612
elif index.nlevels > 1:
16271613
tt = "appendable_multiframe"
1628-
elif pt == "wide_table":
1629-
tt = "appendable_panel"
1630-
elif pt == "ndim_table":
1631-
tt = "appendable_ndim"
1632-
1633-
else:
1634-
1635-
# distinguish between a frame/table
1636-
tt = "legacy_panel"
1637-
try:
1638-
fields = group.table._v_attrs.fields
1639-
if len(fields) == 1 and fields[0] == "value":
1640-
tt = "legacy_frame"
1641-
except IndexError:
1642-
pass
16431614

1615+
_TABLE_MAP = {
1616+
"generic_table": GenericTable,
1617+
"appendable_series": AppendableSeriesTable,
1618+
"appendable_multiseries": AppendableMultiSeriesTable,
1619+
"appendable_frame": AppendableFrameTable,
1620+
"appendable_multiframe": AppendableMultiFrameTable,
1621+
"worm": WORMTable,
1622+
}
16441623
try:
1645-
return globals()[_TABLE_MAP[tt]](
1646-
self, group, encoding=encoding, errors=errors
1647-
)
1624+
cls = _TABLE_MAP[tt]
16481625
except KeyError:
16491626
raise error("_TABLE_MAP")
16501627

1628+
return cls(self, group, encoding=encoding, errors=errors)
1629+
16511630
def _write_to_group(
16521631
self,
16531632
key: str,

0 commit comments

Comments
 (0)