From d5409486e2461422e87f2ed91167ea117517e205 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Hombashi Date: Wed, 1 Jan 2025 21:52:42 +0900 Subject: [PATCH] Fix lint errors Signed-off-by: Tsuyoshi Hombashi --- examples/ipynb/pytablewriter_examples.ipynb | 1 - examples/py/elasticsearch_index.py | 8 ++--- pytablewriter/_logger/_logger.py | 2 +- pytablewriter/_table_format.py | 4 +-- pytablewriter/style/_style.py | 21 +++++------ pytablewriter/writer/_elasticsearch.py | 9 ++--- pytablewriter/writer/_table_writer.py | 35 +++++++++++-------- pytablewriter/writer/binary/_excel.py | 8 ++--- pytablewriter/writer/binary/_pandas.py | 2 +- pytablewriter/writer/text/_latex.py | 2 +- pytablewriter/writer/text/_text_writer.py | 5 +-- .../writer/text/sourcecode/_numpy.py | 8 ++--- requirements/requirements.txt | 2 +- 13 files changed, 54 insertions(+), 53 deletions(-) diff --git a/examples/ipynb/pytablewriter_examples.ipynb b/examples/ipynb/pytablewriter_examples.ipynb index e5afdc94..ee4e8908 100644 --- a/examples/ipynb/pytablewriter_examples.ipynb +++ b/examples/ipynb/pytablewriter_examples.ipynb @@ -1226,7 +1226,6 @@ } ], "source": [ - "from datetime import datetime\n", "from pytablewriter import PythonCodeTableWriter\n", "\n", "\n", diff --git a/examples/py/elasticsearch_index.py b/examples/py/elasticsearch_index.py index f6150877..8e9876d2 100755 --- a/examples/py/elasticsearch_index.py +++ b/examples/py/elasticsearch_index.py @@ -67,7 +67,7 @@ def main() -> None: ] # delete existing index --- - es.indices.delete(index=writer.index_name, ignore=404) + es.indices.delete(index=writer.index_name) # create an index and put data --- writer.write_table() @@ -76,13 +76,11 @@ def main() -> None: es.indices.refresh(index=writer.index_name) print("----- mappings -----") - response = es.indices.get_mapping(index=writer.index_name, doc_type="table") + response = es.indices.get_mapping(index=writer.index_name) print(f"{json.dumps(response, indent=4)}\n") print("----- documents -----") - response = es.search( - index=writer.index_name, doc_type="table", body={"query": {"match_all": {}}} - ) + response = es.search(index=writer.index_name, body={"query": {"match_all": {}}}) for hit in response["hits"]["hits"]: print(json.dumps(hit["_source"], indent=4)) diff --git a/pytablewriter/_logger/_logger.py b/pytablewriter/_logger/_logger.py index 81e62580..9dff075a 100644 --- a/pytablewriter/_logger/_logger.py +++ b/pytablewriter/_logger/_logger.py @@ -111,7 +111,7 @@ def __get_extra_log_entry_list(self) -> list[str]: def __get_typehint_message(self) -> str: try: return "type-hints={}".format( - [type_hint(None).typename for type_hint in self.__writer.type_hints] + [type_hint(None, 0).typename for type_hint in self.__writer.type_hints if type_hint] ) except (TypeError, AttributeError): return "type-hints=[]" diff --git a/pytablewriter/_table_format.py b/pytablewriter/_table_format.py index f1c5391a..fccd0475 100644 --- a/pytablewriter/_table_format.py +++ b/pytablewriter/_table_format.py @@ -261,7 +261,7 @@ def names(self) -> list[str]: return self.__names @property - def writer_class(self) -> AbstractTableWriter: + def writer_class(self) -> type[AbstractTableWriter]: """ Type[AbstractTableWriter]: Table writer class object associated with the table format. """ @@ -287,7 +287,7 @@ def file_extensions(self) -> list[str]: def __init__( self, names: Sequence[str], - writer_class: AbstractTableWriter, + writer_class: type[AbstractTableWriter], format_attribute: int, file_extensions: Sequence[str], ) -> None: diff --git a/pytablewriter/style/_style.py b/pytablewriter/style/_style.py index 96308a9d..7ca9ddf3 100644 --- a/pytablewriter/style/_style.py +++ b/pytablewriter/style/_style.py @@ -318,14 +318,14 @@ def __update_font(self, initialize: bool) -> None: self.__font_style = normalize_enum(font_style, FontStyle, default=FontStyle.NORMAL) elif initialize: self.__font_style = FontStyle.NORMAL - self.__validate_attr("font_style", FontStyle) + self.__validate_attr("font_style", (FontStyle,)) font_weight = self.__kwargs.pop("font_weight", None) if font_weight: self.__font_weight = normalize_enum(font_weight, FontWeight, default=FontWeight.NORMAL) elif initialize: self.__font_weight = FontWeight.NORMAL - self.__validate_attr("font_weight", FontWeight) + self.__validate_attr("font_weight", (FontWeight,)) def __update_align(self, initialize: bool) -> None: align = self.__kwargs.pop("align", None) @@ -333,14 +333,14 @@ def __update_align(self, initialize: bool) -> None: self.__align = normalize_enum(align, Align, default=Align.AUTO) elif initialize: self.__align = Align.AUTO - self.__validate_attr("align", Align) + self.__validate_attr("align", (Align,)) valign = self.__kwargs.pop("vertical_align", None) if valign: self.__valign = normalize_enum(valign, VerticalAlign, default=VerticalAlign.BASELINE) elif initialize: self.__valign = VerticalAlign.BASELINE - self.__validate_attr("vertical_align", VerticalAlign) + self.__validate_attr("vertical_align", (VerticalAlign,)) def __update_misc(self, initialize: bool) -> None: padding = self.__kwargs.pop("padding", None) @@ -356,21 +356,18 @@ def __update_misc(self, initialize: bool) -> None: ) elif initialize: self.__decoration_line = DecorationLine.NONE - self.__validate_attr("decoration_line", DecorationLine) + self.__validate_attr("decoration_line", (DecorationLine,)) thousand_separator = self.__kwargs.pop("thousand_separator", None) if thousand_separator: self.__thousand_separator = _normalize_thousand_separator(thousand_separator) elif initialize: self.__thousand_separator = ThousandSeparator.NONE - self.__validate_attr("thousand_separator", ThousandSeparator) + self.__validate_attr("thousand_separator", (ThousandSeparator,)) - def __validate_attr(self, attr_name: str, expected_type: Any) -> None: + def __validate_attr(self, attr_name: str, expected_types: tuple[type, ...]) -> None: value = getattr(self, attr_name) - if isinstance(expected_type, (list, tuple)): - expected = " or ".join(c.__name__ for c in expected_type) - else: - expected = expected_type.__name__ + expected = " or ".join(c.__name__ for c in expected_types) - if not isinstance(value, expected_type): + if not isinstance(value, expected_types): raise TypeError(f"{attr_name} must be instance of {expected}: actual={type(value)}") diff --git a/pytablewriter/writer/_elasticsearch.py b/pytablewriter/writer/_elasticsearch.py index 51a7011f..52a2c622 100644 --- a/pytablewriter/writer/_elasticsearch.py +++ b/pytablewriter/writer/_elasticsearch.py @@ -188,15 +188,16 @@ def _write_table(self, **kwargs: Any) -> None: result = self.stream.indices.create(index=self.index_name, body=mappings) self._logger.logger.debug(result) except es.TransportError as e: - if e.error == "index_already_exists_exception": - # ignore already existing index - self._logger.logger.debug(to_error_message(e)) + for error in e.errors: + if error == "index_already_exists_exception": + # ignore already existing index + self._logger.logger.debug(to_error_message(e)) else: raise for body in self._get_body(): try: - self.stream.index(index=self.index_name, body=body, doc_type=self.document_type) + self.stream.index(index=self.index_name, body=body) except es.exceptions.RequestError as e: self._logger.logger.error(f"{to_error_message(e)}, body={body}") diff --git a/pytablewriter/writer/_table_writer.py b/pytablewriter/writer/_table_writer.py index 62770722..1033ecf2 100644 --- a/pytablewriter/writer/_table_writer.py +++ b/pytablewriter/writer/_table_writer.py @@ -6,7 +6,7 @@ import copy import math import warnings -from collections.abc import Mapping, Sequence +from collections.abc import Sequence from typing import TYPE_CHECKING, Any, Optional, Union, cast import typepy @@ -140,6 +140,8 @@ def callback_example(iter_count: int, iter_length: int) -> None: - second argument: a total number of iteration """ + FORMAT_NAME = "abstract" + @property def margin(self) -> int: raise NotImplementedError() @@ -200,7 +202,7 @@ def __init__(self, **kwargs: Any) -> None: self._dp_extractor.min_column_width = 1 self._dp_extractor.strip_str_header = '"' self._dp_extractor.preprocessor = Preprocessor(dequote=kwargs.get("dequote", True)) - self._dp_extractor.type_value_map[Typecode.NONE] = "" + self._dp_extractor.set_type_value(Typecode.NONE, "") self._dp_extractor.matrix_formatting = MatrixFormatting.HEADER_ALIGNED self._dp_extractor.update_strict_level_map({Typecode.BOOL: 1}) @@ -312,7 +314,7 @@ def max_workers(self) -> int: return self._dp_extractor.max_workers @max_workers.setter - def max_workers(self, value: Optional[int]) -> None: + def max_workers(self, value: int) -> None: self._dp_extractor.max_workers = value @property @@ -466,7 +468,7 @@ def _quoting_flags(self) -> dict[Typecode, bool]: return self._dp_extractor.quoting_flags @_quoting_flags.setter - def _quoting_flags(self, value: Mapping[Typecode, bool]) -> None: + def _quoting_flags(self, value: dict[Typecode, bool]) -> None: self._dp_extractor.quoting_flags = value self.__clear_preprocess() @@ -627,7 +629,7 @@ def __is_skip_close(self) -> bool: except ImportError: try: # for pytest 5.4.1 or older versions - from _pytest.compat import CaptureIO + from _pytest.compat import CaptureIO # type: ignore if isinstance(self.stream, CaptureIO): # avoid closing streams for pytest @@ -781,26 +783,28 @@ def from_dataframe( :ref:`example-from-pandas-dataframe` """ - if typepy.String(dataframe).is_type(): + if isinstance(dataframe, str): import pandas as pd - dataframe = pd.read_pickle(dataframe) + df = pd.read_pickle(dataframe) + else: + df = dataframe - self.headers = list(dataframe.columns.values) + self.headers = list(df.columns.values) if not self.type_hints or overwrite_type_hints: - self.type_hints = [extract_typepy_from_dtype(dtype) for dtype in dataframe.dtypes] + self.type_hints = [extract_typepy_from_dtype(dtype) for dtype in df.dtypes] # type: ignore if add_index_column: - self.headers = [" "] + self.headers + index_header = str(df.index.name) if df.index.name else " " + self.headers = [index_header] + self.headers if self.type_hints: self.type_hints = [Integer] + self.type_hints self.value_matrix = [ - [index] + row - for index, row in zip(dataframe.index.tolist(), dataframe.values.tolist()) + [index] + list(row) for index, row in zip(df.index.tolist(), df.values.tolist()) ] else: - self.value_matrix = dataframe.values.tolist() + self.value_matrix = df.values.tolist() def from_series(self, series: "pandas.Series", add_index_column: bool = True) -> None: """ @@ -820,7 +824,7 @@ def from_series(self, series: "pandas.Series", add_index_column: bool = True) -> """ if series.name: - self.headers = [series.name] + self.headers = [str(series.name)] else: self.headers = ["value"] @@ -841,7 +845,8 @@ def from_tablib(self, tablib_dataset: "tablib.Dataset") -> None: Set tabular attributes to the writer from :py:class:`tablib.Dataset`. """ - self.headers = tablib_dataset.headers + if tablib_dataset.headers: + self.headers = tablib_dataset.headers self.value_matrix = [row for row in tablib_dataset] def from_writer( diff --git a/pytablewriter/writer/binary/_excel.py b/pytablewriter/writer/binary/_excel.py index 4c19293b..6712365b 100644 --- a/pytablewriter/writer/binary/_excel.py +++ b/pytablewriter/writer/binary/_excel.py @@ -293,12 +293,12 @@ def __get_cell_style(self, col: int) -> "XFStyle": raise if col in self.__col_style_table: - return self.__col_style_table.get(col) + return self.__col_style_table.get(col) # type: ignore try: col_dp = self._column_dp_list[col] except KeyError: - return {} + return {} # type: ignore if col_dp.typecode not in [typepy.Typecode.REAL_NUMBER]: raise ValueError() @@ -307,7 +307,7 @@ def __get_cell_style(self, col: int) -> "XFStyle": raise ValueError() float_digit = col_dp.minmax_decimal_places.max_value - if float_digit <= 0: + if float_digit is None or float_digit <= 0: raise ValueError() num_format_str = "#,{:s}0.{:s}".format("#" * int(float_digit), "0" * int(float_digit)) @@ -452,7 +452,7 @@ def __get_number_property(self, col: int) -> dict[str, str]: num_props = {} if Integer(col_dp.minmax_decimal_places.max_value).is_type(): float_digit = col_dp.minmax_decimal_places.max_value - if float_digit > 0: + if float_digit is not None and float_digit > 0: num_props = {"num_format": "0.{:s}".format("0" * int(float_digit))} self.__col_numprops_table[col] = num_props diff --git a/pytablewriter/writer/binary/_pandas.py b/pytablewriter/writer/binary/_pandas.py index 4363063f..42027b3e 100644 --- a/pytablewriter/writer/binary/_pandas.py +++ b/pytablewriter/writer/binary/_pandas.py @@ -71,7 +71,7 @@ def _verify_stream(self) -> None: pass def _write_table(self, **kwargs: Any) -> None: - if not self.is_opened(): + if self.__filepath is None or not self.is_opened(): self._logger.logger.error("required to open(file_path) first.") return diff --git a/pytablewriter/writer/text/_latex.py b/pytablewriter/writer/text/_latex.py index cce1c4a2..c9b7397c 100644 --- a/pytablewriter/writer/text/_latex.py +++ b/pytablewriter/writer/text/_latex.py @@ -183,7 +183,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__(**kwargs) self.char_right_side_row = r" \\ \hline" - self._dp_extractor.type_value_map[Typecode.INFINITY] = r"\infty" + self._dp_extractor.set_type_value(Typecode.INFINITY, r"\infty") def _get_opening_row_items(self) -> list[str]: return [ diff --git a/pytablewriter/writer/text/_text_writer.py b/pytablewriter/writer/text/_text_writer.py index 65bda5c5..2599002a 100644 --- a/pytablewriter/writer/text/_text_writer.py +++ b/pytablewriter/writer/text/_text_writer.py @@ -276,9 +276,10 @@ def dump(self, output: Union[str, IO], close_after_write: bool = True, **kwargs: except AttributeError: self.stream = open(output, "w", encoding="utf-8") # type: ignore + stash = self.enable_ansi_escape + self.enable_ansi_escape = False + try: - stash = self.enable_ansi_escape - self.enable_ansi_escape = False self.write_table(**kwargs) finally: if close_after_write: diff --git a/pytablewriter/writer/text/sourcecode/_numpy.py b/pytablewriter/writer/text/sourcecode/_numpy.py index 20c237b1..b9aeba85 100644 --- a/pytablewriter/writer/text/sourcecode/_numpy.py +++ b/pytablewriter/writer/text/sourcecode/_numpy.py @@ -47,11 +47,11 @@ def __init__(self, **kwargs: Any) -> None: super().__init__(**kwargs) self.import_numpy_as = "np" - self._dp_extractor.type_value_map[typepy.Typecode.INFINITY] = "{:s}.inf".format( - self.import_numpy_as + self._dp_extractor.set_type_value( + typepy.Typecode.INFINITY, "{:s}.inf".format(self.import_numpy_as) ) - self._dp_extractor.type_value_map[typepy.Typecode.NAN] = "{:s}.nan".format( - self.import_numpy_as + self._dp_extractor.set_type_value( + typepy.Typecode.NAN, "{:s}.nan".format(self.import_numpy_as) ) def _get_opening_row_items(self) -> list[str]: diff --git a/requirements/requirements.txt b/requirements/requirements.txt index da9aa794..017c1639 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,4 +1,4 @@ -DataProperty>=1.0.1,<2 +DataProperty>=1.1.0,<2 mbstrdecoder>=1.0.0,<2 pathvalidate>=2.3.0,<4 tabledata>=1.3.1,<2