Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
  • Loading branch information
thombashi committed Jan 1, 2025
1 parent a3d8cf7 commit d540948
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 53 deletions.
1 change: 0 additions & 1 deletion examples/ipynb/pytablewriter_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,6 @@
}
],
"source": [
"from datetime import datetime\n",
"from pytablewriter import PythonCodeTableWriter\n",
"\n",
"\n",
Expand Down
8 changes: 3 additions & 5 deletions examples/py/elasticsearch_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion pytablewriter/_logger/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[]"
4 changes: 2 additions & 2 deletions pytablewriter/_table_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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:
Expand Down
21 changes: 9 additions & 12 deletions pytablewriter/style/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,29 +318,29 @@ 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)
if align:
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)
Expand All @@ -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)}")
9 changes: 5 additions & 4 deletions pytablewriter/writer/_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
35 changes: 20 additions & 15 deletions pytablewriter/writer/_table_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
"""
Expand All @@ -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"]

Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions pytablewriter/writer/binary/_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pytablewriter/writer/binary/_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pytablewriter/writer/text/_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
5 changes: 3 additions & 2 deletions pytablewriter/writer/text/_text_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions pytablewriter/writer/text/sourcecode/_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d540948

Please sign in to comment.