Skip to content

Commit

Permalink
REF: privatize/annotate in io.formats (#55438)
Browse files Browse the repository at this point in the history
* REF: privatize in formats.info

* privatize in formats.xml

* REF: privatize in formats.xml

* TYP: annotate in style_render

* lint fixup
  • Loading branch information
jbrockmendel authored Oct 9, 2023
1 parent a984fed commit 88af887
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 73 deletions.
52 changes: 26 additions & 26 deletions pandas/io/formats/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def _initialize_memory_usage(
return memory_usage


class BaseInfo(ABC):
class _BaseInfo(ABC):
"""
Base class for DataFrameInfo and SeriesInfo.
Expand Down Expand Up @@ -439,7 +439,7 @@ def render(
pass


class DataFrameInfo(BaseInfo):
class DataFrameInfo(_BaseInfo):
"""
Class storing dataframe-specific info.
"""
Expand Down Expand Up @@ -503,7 +503,7 @@ def render(
verbose: bool | None,
show_counts: bool | None,
) -> None:
printer = DataFrameInfoPrinter(
printer = _DataFrameInfoPrinter(
info=self,
max_cols=max_cols,
verbose=verbose,
Expand All @@ -512,7 +512,7 @@ def render(
printer.to_buffer(buf)


class SeriesInfo(BaseInfo):
class SeriesInfo(_BaseInfo):
"""
Class storing series-specific info.
"""
Expand All @@ -538,7 +538,7 @@ def render(
"Argument `max_cols` can only be passed "
"in DataFrame.info, not Series.info"
)
printer = SeriesInfoPrinter(
printer = _SeriesInfoPrinter(
info=self,
verbose=verbose,
show_counts=show_counts,
Expand Down Expand Up @@ -572,7 +572,7 @@ def memory_usage_bytes(self) -> int:
return self.data.memory_usage(index=True, deep=deep)


class InfoPrinterAbstract:
class _InfoPrinterAbstract:
"""
Class for printing dataframe or series info.
"""
Expand All @@ -586,11 +586,11 @@ def to_buffer(self, buf: WriteBuffer[str] | None = None) -> None:
fmt.buffer_put_lines(buf, lines)

@abstractmethod
def _create_table_builder(self) -> TableBuilderAbstract:
def _create_table_builder(self) -> _TableBuilderAbstract:
"""Create instance of table builder."""


class DataFrameInfoPrinter(InfoPrinterAbstract):
class _DataFrameInfoPrinter(_InfoPrinterAbstract):
"""
Class for printing dataframe info.
Expand Down Expand Up @@ -650,27 +650,27 @@ def _initialize_show_counts(self, show_counts: bool | None) -> bool:
else:
return show_counts

def _create_table_builder(self) -> DataFrameTableBuilder:
def _create_table_builder(self) -> _DataFrameTableBuilder:
"""
Create instance of table builder based on verbosity and display settings.
"""
if self.verbose:
return DataFrameTableBuilderVerbose(
return _DataFrameTableBuilderVerbose(
info=self.info,
with_counts=self.show_counts,
)
elif self.verbose is False: # specifically set to False, not necessarily None
return DataFrameTableBuilderNonVerbose(info=self.info)
return _DataFrameTableBuilderNonVerbose(info=self.info)
elif self.exceeds_info_cols:
return DataFrameTableBuilderNonVerbose(info=self.info)
return _DataFrameTableBuilderNonVerbose(info=self.info)
else:
return DataFrameTableBuilderVerbose(
return _DataFrameTableBuilderVerbose(
info=self.info,
with_counts=self.show_counts,
)


class SeriesInfoPrinter(InfoPrinterAbstract):
class _SeriesInfoPrinter(_InfoPrinterAbstract):
"""Class for printing series info.
Parameters
Expand All @@ -694,17 +694,17 @@ def __init__(
self.verbose = verbose
self.show_counts = self._initialize_show_counts(show_counts)

def _create_table_builder(self) -> SeriesTableBuilder:
def _create_table_builder(self) -> _SeriesTableBuilder:
"""
Create instance of table builder based on verbosity.
"""
if self.verbose or self.verbose is None:
return SeriesTableBuilderVerbose(
return _SeriesTableBuilderVerbose(
info=self.info,
with_counts=self.show_counts,
)
else:
return SeriesTableBuilderNonVerbose(info=self.info)
return _SeriesTableBuilderNonVerbose(info=self.info)

def _initialize_show_counts(self, show_counts: bool | None) -> bool:
if show_counts is None:
Expand All @@ -713,13 +713,13 @@ def _initialize_show_counts(self, show_counts: bool | None) -> bool:
return show_counts


class TableBuilderAbstract(ABC):
class _TableBuilderAbstract(ABC):
"""
Abstract builder for info table.
"""

_lines: list[str]
info: BaseInfo
info: _BaseInfo

@abstractmethod
def get_lines(self) -> list[str]:
Expand Down Expand Up @@ -769,7 +769,7 @@ def add_dtypes_line(self) -> None:
self._lines.append(f"dtypes: {', '.join(collected_dtypes)}")


class DataFrameTableBuilder(TableBuilderAbstract):
class _DataFrameTableBuilder(_TableBuilderAbstract):
"""
Abstract builder for dataframe info table.
Expand Down Expand Up @@ -820,7 +820,7 @@ def add_memory_usage_line(self) -> None:
self._lines.append(f"memory usage: {self.memory_usage_string}")


class DataFrameTableBuilderNonVerbose(DataFrameTableBuilder):
class _DataFrameTableBuilderNonVerbose(_DataFrameTableBuilder):
"""
Dataframe info table builder for non-verbose output.
"""
Expand All @@ -838,7 +838,7 @@ def add_columns_summary_line(self) -> None:
self._lines.append(self.ids._summary(name="Columns"))


class TableBuilderVerboseMixin(TableBuilderAbstract):
class _TableBuilderVerboseMixin(_TableBuilderAbstract):
"""
Mixin for verbose info output.
"""
Expand Down Expand Up @@ -931,7 +931,7 @@ def _gen_dtypes(self) -> Iterator[str]:
yield pprint_thing(dtype)


class DataFrameTableBuilderVerbose(DataFrameTableBuilder, TableBuilderVerboseMixin):
class _DataFrameTableBuilderVerbose(_DataFrameTableBuilder, _TableBuilderVerboseMixin):
"""
Dataframe info table builder for verbose output.
"""
Expand Down Expand Up @@ -997,7 +997,7 @@ def _gen_columns(self) -> Iterator[str]:
yield pprint_thing(col)


class SeriesTableBuilder(TableBuilderAbstract):
class _SeriesTableBuilder(_TableBuilderAbstract):
"""
Abstract builder for series info table.
Expand Down Expand Up @@ -1029,7 +1029,7 @@ def _fill_non_empty_info(self) -> None:
"""Add lines to the info table, pertaining to non-empty series."""


class SeriesTableBuilderNonVerbose(SeriesTableBuilder):
class _SeriesTableBuilderNonVerbose(_SeriesTableBuilder):
"""
Series info table builder for non-verbose output.
"""
Expand All @@ -1043,7 +1043,7 @@ def _fill_non_empty_info(self) -> None:
self.add_memory_usage_line()


class SeriesTableBuilderVerbose(SeriesTableBuilder, TableBuilderVerboseMixin):
class _SeriesTableBuilderVerbose(_SeriesTableBuilder, _TableBuilderVerboseMixin):
"""
Series info table builder for verbose output.
"""
Expand Down
8 changes: 4 additions & 4 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,7 @@ def color(value, user_arg, command, comm_arg):
return latex_styles


def _escape_latex(s):
def _escape_latex(s: str) -> str:
r"""
Replace the characters ``&``, ``%``, ``$``, ``#``, ``_``, ``{``, ``}``,
``~``, ``^``, and ``\`` in the string with LaTeX-safe sequences.
Expand Down Expand Up @@ -2392,7 +2392,7 @@ def _escape_latex(s):
)


def _math_mode_with_dollar(s):
def _math_mode_with_dollar(s: str) -> str:
r"""
All characters in LaTeX math mode are preserved.
Expand Down Expand Up @@ -2425,7 +2425,7 @@ def _math_mode_with_dollar(s):
return "".join(res).replace(r"rt8§=§7wz", r"\$")


def _math_mode_with_parentheses(s):
def _math_mode_with_parentheses(s: str) -> str:
r"""
All characters in LaTeX math mode are preserved.
Expand Down Expand Up @@ -2461,7 +2461,7 @@ def _math_mode_with_parentheses(s):
return "".join(res)


def _escape_latex_math(s):
def _escape_latex_math(s: str) -> str:
r"""
All characters in LaTeX math mode are preserved.
Expand Down
Loading

0 comments on commit 88af887

Please sign in to comment.