From c678b52c40ab1f4c99f0f2ae7a4001d284232796 Mon Sep 17 00:00:00 2001 From: Paulo Eduardo Neves Date: Tue, 7 Sep 2021 19:58:32 -0300 Subject: [PATCH] Fix flake8 warnings --- .vscode/settings.json | 5 +++-- charp.py | 14 +++++++------- charp_test.py | 13 +++---------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d55dd6c..16e1914 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -31,9 +31,10 @@ "." ], "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true + "python.testing.pytestEnabled": true, "python.testing.nosetestsEnabled": false, "python.linting.pylintEnabled": false, "python.linting.enabled": true, - "editor.formatOnPaste": true + "editor.formatOnPaste": true, + "files.trimTrailingWhitespace": true } \ No newline at end of file diff --git a/charp.py b/charp.py index 3facd09..ee6496a 100644 --- a/charp.py +++ b/charp.py @@ -2,28 +2,29 @@ CharP is Charts for Paulo — Paulos aux functions and preferences for beautiful and functional charts """ -import textwrap from typing import List, Iterable -import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.text import Text plt.style.use("./paulo.mplstyle") -color_highlight = plt.rcParams["lines.color"] # default is orange +# default is orange +color_highlight = plt.rcParams["lines.color"] # type: ignore color_highlight2 = (0, 153, 51) color_highlight3 = (30, 66, 139) def set_title(title, fontsize=16, ax=None): - "Title always aligned to the left axis label" + """Title always aligned to the left axis label. + + Call after changing labels""" # https://stackoverflow.com/questions/62997001/matplotlib-how-to-exact-align-the-title-to-the-y-label if ax is None: ax = plt.gca() plt.gcf().canvas.draw() # without this, it won't work x_min = min(lb.get_window_extent().x0 for lb in ax.get_yticklabels()) - x_min = min(mpl.text.Text.get_window_extent(lb).x0 for lb in ax.get_yticklabels()) + x_min = min(Text.get_window_extent(lb).x0 for lb in ax.get_yticklabels()) # type: ignore x, _ = ax.transAxes.inverted().transform([x_min, 0]) plt.gcf().canvas.draw() # without this, it won't work return ax.set_title(title, ha="left", x=x, fontsize=fontsize) @@ -79,8 +80,7 @@ def break_labels(tick_labels: Iterable[Text]) -> List[str]: line1, line2 = _break2lines(label_biggest) acceptable_size = max(len(line1), len(line2)) labels_breaked = [ - "\n".join(_break2lines(label.get_text(), acceptable_size)) - for label in tick_labels + "\n".join(_break2lines(label.get_text(), acceptable_size)) for label in tick_labels ] return labels_breaked diff --git a/charp_test.py b/charp_test.py index b10fc8e..782f8ad 100644 --- a/charp_test.py +++ b/charp_test.py @@ -6,23 +6,17 @@ def test_break2lines(): s = "1234 1234 1234 1234" - assert ["1234 1234", "1234 1234"] == charp._break2lines( - s - ), "Must break in 2 lines. Always." + assert ["1234 1234", "1234 1234"] == charp._break2lines(s), "Must break in 2 lines. Always." def test_break2lines_middle(): s = "123 1234 123" - assert ["123 1234", "123"] == charp._break2lines( - s - ), "Must break in 2 lines. Always." + assert ["123 1234", "123"] == charp._break2lines(s), "Must break in 2 lines. Always." def test_break2lines_second_bigger(): s = "123 123 12 12345" - assert ["123 123", "12 12345"] == charp._break2lines( - s - ), "Must break in 2 lines. Always." + assert ["123 123", "12 12345"] == charp._break2lines(s), "Must break in 2 lines. Always." def test_break2lines_acceptable_size(): @@ -38,4 +32,3 @@ def test_break_labels(): labels = build_ticklabels(["abc cde efg", "abc cde"]) labels_breaked = charp.break_labels(labels) assert ["abc cde\nefg", "abc cde"] == labels_breaked -