Skip to content

CLN: pytables make lookups explicit instead of using globals #30343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 21 additions & 42 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,6 @@ class DuplicateWarning(Warning):
# formats
_FORMAT_MAP = {"f": "fixed", "fixed": "fixed", "t": "table", "table": "table"}

# storer class map
_STORER_MAP = {
"series": "SeriesFixed",
"frame": "FrameFixed",
}

# table class map
_TABLE_MAP = {
"generic_table": "GenericTable",
"appendable_series": "AppendableSeriesTable",
"appendable_multiseries": "AppendableMultiSeriesTable",
"appendable_frame": "AppendableFrameTable",
"appendable_multiframe": "AppendableMultiFrameTable",
"worm": "WORMTable",
}

# axes map
_AXES_MAP = {DataFrame: [0]}

Expand Down Expand Up @@ -1553,12 +1537,17 @@ def _create_storer(
self,
group,
format=None,
value=None,
value: Optional[FrameOrSeries] = None,
encoding: str = "UTF-8",
errors: str = "strict",
) -> Union["GenericFixed", "Table"]:
""" return a suitable class to operate """

cls: Union[Type["GenericFixed"], Type["Table"]]

if value is not None and not isinstance(value, (Series, DataFrame)):
raise TypeError("value must be None, Series, or DataFrame")

def error(t):
# return instead of raising so mypy can tell where we are raising
return TypeError(
Expand Down Expand Up @@ -1587,23 +1576,20 @@ def error(t):
)
else:
_TYPE_MAP = {Series: "series", DataFrame: "frame"}
try:
pt = _TYPE_MAP[type(value)]
except KeyError:
raise error("_TYPE_MAP")
pt = _TYPE_MAP[type(value)]

# we are actually a table
if format == "table":
pt += "_table"

# a storer node
if "table" not in pt:
_STORER_MAP = {"series": SeriesFixed, "frame": FrameFixed}
try:
return globals()[_STORER_MAP[pt]](
self, group, encoding=encoding, errors=errors
)
cls = _STORER_MAP[pt]
except KeyError:
raise error("_STORER_MAP")
return cls(self, group, encoding=encoding, errors=errors)

# existing node (and must be a table)
if tt is None:
Expand All @@ -1625,29 +1611,22 @@ def error(t):
tt = "appendable_frame"
elif index.nlevels > 1:
tt = "appendable_multiframe"
elif pt == "wide_table":
tt = "appendable_panel"
elif pt == "ndim_table":
tt = "appendable_ndim"

else:

# distinguish between a frame/table
tt = "legacy_panel"
try:
fields = group.table._v_attrs.fields
if len(fields) == 1 and fields[0] == "value":
tt = "legacy_frame"
except IndexError:
pass

_TABLE_MAP = {
"generic_table": GenericTable,
"appendable_series": AppendableSeriesTable,
"appendable_multiseries": AppendableMultiSeriesTable,
"appendable_frame": AppendableFrameTable,
"appendable_multiframe": AppendableMultiFrameTable,
"worm": WORMTable,
}
try:
return globals()[_TABLE_MAP[tt]](
self, group, encoding=encoding, errors=errors
)
cls = _TABLE_MAP[tt]
except KeyError:
raise error("_TABLE_MAP")

return cls(self, group, encoding=encoding, errors=errors)

def _write_to_group(
self,
key: str,
Expand Down