From f3aded5b17e1ce858bb5739f984179929c51f9fa Mon Sep 17 00:00:00 2001 From: Frh Date: Wed, 29 Apr 2020 13:52:58 -0700 Subject: [PATCH] Linting --- camelot/core.py | 33 +++++++++++------------ camelot/plotting.py | 10 +++---- camelot/utils.py | 63 ++++++++++++++++++++++---------------------- tests/test_common.py | 3 ++- 4 files changed, 54 insertions(+), 55 deletions(-) diff --git a/camelot/core.py b/camelot/core.py index ee9adcd..4e2e44d 100644 --- a/camelot/core.py +++ b/camelot/core.py @@ -171,19 +171,19 @@ class TextAlignments(): idx_insert = None if idx_closest is None: idx_insert = 0 - # Note: np.isclose is slow! - elif coord - 0.5 < \ - alignment_array[idx_closest].coord < \ - coord + 0.5: - self._update_alignment( - alignment_array[idx_closest], - coord, - textline - ) - elif alignment_array[idx_closest].coord < coord: - idx_insert = idx_closest + 1 else: - idx_insert = idx_closest + coord_closest = alignment_array[idx_closest].coord + # Note: np.isclose is slow! + if coord - 0.5 < coord_closest < coord + 0.5: + self._update_alignment( + alignment_array[idx_closest], + coord, + textline + ) + elif coord_closest < coord: + idx_insert = idx_closest + 1 + else: + idx_insert = idx_closest if idx_insert is not None: new_alignment = self._create_new_text_alignment( coord, textline, alignment_id @@ -212,8 +212,8 @@ class TextEdges(TextAlignments): te = self._create_new_text_alignment(coord, textline, align) self._text_alignments[align].append(te) - def _update_alignment(self, edge, coord, textline): - edge.update_coords(coord, textline, self.edge_tol) + def _update_alignment(self, alignment, coord, textline): + alignment.update_coords(coord, textline, self.edge_tol) def generate(self, textlines): """Generates the text edges dict based on horizontal text @@ -469,8 +469,7 @@ class Table(): if self.page == other.page: if self.order < other.order: return True - if self.page < other.page: - return True + return self.page < other.page @property def data(self): @@ -674,7 +673,7 @@ class Table(): bottom = cell.bottom if cell.bound == 4: continue - elif cell.bound == 3: + if cell.bound == 3: if not left and (right and top and bottom): cell.hspan = True elif not right and (left and top and bottom): diff --git a/camelot/plotting.py b/camelot/plotting.py index e5fec12..e390464 100644 --- a/camelot/plotting.py +++ b/camelot/plotting.py @@ -44,9 +44,9 @@ def draw_labeled_bbox( ) vlabel, hlabel = label_pos.split(",") - if (vlabel == "top"): + if vlabel == "top": y = max(bbox[1], bbox[3]) - elif (vlabel == "bottom"): + elif vlabel == "bottom": y = min(bbox[1], bbox[3]) else: y = 0.5 * (bbox[1] + bbox[3]) @@ -58,9 +58,9 @@ def draw_labeled_bbox( "center": "center" } vlabel_out_of_box = label_align_swap[vlabel] - if (hlabel == "right"): + if hlabel == "right": x = max(bbox[0], bbox[2]) - elif (hlabel == "left"): + elif hlabel == "left": x = min(bbox[0], bbox[2]) else: x = 0.5 * (bbox[0] + bbox[2]) @@ -203,7 +203,7 @@ class PlotMethods(): raise NotImplementedError( "Lattice flavor does not support kind='{}'".format(kind) ) - elif table.flavor in ["stream", "hybrid"] and kind in ["line"]: + if table.flavor in ["stream", "hybrid"] and kind in ["line"]: raise NotImplementedError( "Stream flavor does not support kind='{}'".format(kind) ) diff --git a/camelot/utils.py b/camelot/utils.py index 1432259..27d5b1e 100644 --- a/camelot/utils.py +++ b/camelot/utils.py @@ -157,12 +157,15 @@ def remove_extra(kwargs, flavor="lattice"): # https://stackoverflow.com/a/22726782 # and https://stackoverflow.com/questions/10965479 class TemporaryDirectory(): + def __init__(self): + self.dir_path = None + def __enter__(self): - self.name = tempfile.mkdtemp() + self.dir_path = tempfile.mkdtemp() # Only delete the temporary directory upon # program exit. - atexit.register(shutil.rmtree, self.name) - return self.name + atexit.register(shutil.rmtree, self.dir_path) + return self.dir_path def __exit__(self, exc_type, exc_value, traceback): pass @@ -343,8 +346,8 @@ def get_rotation(chars, horizontal_text, vertical_text): hlen = len([t for t in horizontal_text if t.get_text().strip()]) vlen = len([t for t in vertical_text if t.get_text().strip()]) if hlen < vlen: - clockwise = sum(t.matrix[1] < 0 and t.matrix[2] > 0 for t in chars) - anticlockwise = sum(t.matrix[1] > 0 and t.matrix[2] < 0 for t in chars) + clockwise = sum(t.matrix[1] < 0 < t.matrix[2] for t in chars) + anticlockwise = sum(t.matrix[1] > 0 > t.matrix[2] for t in chars) rotation = "anticlockwise" if clockwise < anticlockwise \ else "clockwise" return rotation @@ -753,7 +756,7 @@ def flag_font_size(textline, direction, strip_text=""): flist.append("".join(fchars)) fstring = "".join(flist) else: - fstring = "".join([t.get_text() for t in textline]) + fstring = "".join(t.get_text() for t in textline) return text_strip(fstring, strip_text) @@ -815,10 +818,9 @@ def split_textline(table, textline, direction, flag_size=False, strip_text=""): ): cut_text.append((r, cut[0], obj)) break - else: - # TODO: add test - if cut == x_cuts[-1]: - cut_text.append((r, cut[0] + 1, obj)) + # TODO: add test + if cut == x_cuts[-1]: + cut_text.append((r, cut[0] + 1, obj)) elif isinstance(obj, LTAnno): cut_text.append((r, cut[0], obj)) elif direction == "vertical" and not textline.is_empty(): @@ -848,10 +850,9 @@ def split_textline(table, textline, direction, flag_size=False, strip_text=""): and (obj.y0 + obj.y1) / 2 >= cut[1]: cut_text.append((cut[0], c, obj)) break - else: - # TODO: add test - if cut == y_cuts[-1]: - cut_text.append((cut[0] - 1, c, obj)) + # TODO: add test + if cut == y_cuts[-1]: + cut_text.append((cut[0] - 1, c, obj)) elif isinstance(obj, LTAnno): cut_text.append((cut[0], c, obj)) except IndexError: @@ -964,23 +965,21 @@ def get_table_index( ), error, ) - else: - if flag_size: - return ( - [ - ( - r_idx, - c_idx, - flag_font_size(t._objs, - direction, - strip_text=strip_text), - ) - ], - error, - ) - else: - return [(r_idx, c_idx, text_strip(t.get_text(), strip_text))], \ - error + if flag_size: + return ( + [ + ( + r_idx, + c_idx, + flag_font_size(t._objs, + direction, + strip_text=strip_text), + ) + ], + error, + ) + return [(r_idx, c_idx, text_strip(t.get_text(), strip_text))], \ + error def compute_accuracy(error_weights): @@ -1002,7 +1001,7 @@ def compute_accuracy(error_weights): SCORE_VAL = 100 try: score = 0 - if sum([ew[0] for ew in error_weights]) != SCORE_VAL: + if sum(ew[0] for ew in error_weights) != SCORE_VAL: raise ValueError("Sum of weights should be equal to 100.") for ew in error_weights: weight = ew[0] / len(ew[1]) diff --git a/tests/test_common.py b/tests/test_common.py index f0d95b5..934aa2a 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -7,8 +7,9 @@ from pandas.testing import assert_frame_equal import camelot from camelot.core import Table, TableList -from camelot.utils import compare_tables from camelot.__version__ import generate_version +# compare_tables used in console mode while debugging +from camelot.utils import compare_tables # noqa from .data import *