Skip to content

Commit

Permalink
refactor: move perc_change computation outside of layout function f…
Browse files Browse the repository at this point in the history
…or easy tests
  • Loading branch information
loiccoyle committed Nov 15, 2024
1 parent 1e643f1 commit f7c862e
Show file tree
Hide file tree
Showing 31 changed files with 16 additions and 18 deletions.
Binary file modified tests/data/layouts/big_logo_False_False_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_False_False_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_False_True_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_False_True_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_True_False_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_True_False_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_True_True_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_logo_True_True_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_False_False_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_False_False_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_False_True_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_False_True_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_True_False_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_True_False_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_True_True_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/big_price_True_True_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_False_False_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_False_False_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_False_True_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_False_True_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_True_False_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_True_False_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_True_True_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/layouts/default_True_True_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
from tinyticker import utils

# monkey patch the now function so tests are time indenpendent
utils.__dict__["now"] = lambda: pd.Timestamp(
2021, 7, 22, 18, 00, 00, tzinfo=timezone.utc
)
utils.now = lambda: pd.Timestamp(2021, 7, 22, 18, 00, 00, tzinfo=timezone.utc)
2 changes: 1 addition & 1 deletion tests/unit/layouts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def layout_test(layout_func: LayoutFunc, dimensions, resp, data_dir):
config.layout.x_gaps = x_gap

ticker = TickerStock(config)
out = layout_func(dimensions, ticker, resp)
out = layout_func(dimensions, ticker, resp, 100)
filename = f"{layout_func.__name__}_{y_axis}_{x_gap}_{volume}.png"
assert out.size == dimensions, f"Wrong dimensions: {filename}"
if UPDATE_REF_PLOTS:
Expand Down
4 changes: 2 additions & 2 deletions tinyticker/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from .config import TinytickerConfig
from .layouts import LAYOUTS
from .layouts.utils import create_fig_ax, fig_to_image
from .layouts.utils import create_fig_ax, fig_to_image, perc_change
from .tickers._base import TickerBase, TickerResponse
from .waveshare_lib._base import EPDHighlight
from .waveshare_lib.models import MODELS, EPDModel
Expand Down Expand Up @@ -90,5 +90,5 @@ def show_image(self, image: Image.Image) -> None:

def show(self, ticker: TickerBase, resp: TickerResponse) -> None:
layout = LAYOUTS.get(ticker.config.layout.name, LAYOUTS["default"])
image = layout.func(self.epd.size, ticker, resp)
image = layout.func(self.epd.size, ticker, resp, perc_change(ticker, resp))
self.show_image(image)
5 changes: 2 additions & 3 deletions tinyticker/layouts/big_logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
fig_to_image,
fontsize_for_size,
historical_plot,
perc_change,
perc_change_abp,
resize_aspect,
)
Expand All @@ -27,7 +26,7 @@ def ttf_font_or_default(

@register
def big_logo(
size: Tuple[int, int], ticker: TickerBase, resp: TickerResponse
size: Tuple[int, int], ticker: TickerBase, resp: TickerResponse, perc_change: float
) -> Image.Image:
# some layout settings
padding = min(8, int(0.05 * size[0]))
Expand All @@ -44,7 +43,7 @@ def big_logo(
monospace_font = ttf_font_or_default(monospace_font_file, default_size)
regular_font = ttf_font_or_default(regular_font_file, default_size)

range_text = f"{len(resp.historical)}x{ticker.config.interval} {perc_change(ticker, resp):+.2f}%"
range_text = f"{len(resp.historical)}x{ticker.config.interval} {perc_change:+.2f}%"
if ticker.config.avg_buy_price:
range_text += f" ({perc_change_abp(ticker, resp):+.2f}%)"
range_text_bbox = monospace_font.getbbox(range_text)
Expand Down
5 changes: 2 additions & 3 deletions tinyticker/layouts/big_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
fig_to_image,
fontsize_for_size,
historical_plot,
perc_change,
perc_change_abp,
strip_ax,
)


@register
def big_price(
size: Tuple[int, int], ticker: TickerBase, resp: TickerResponse
size: Tuple[int, int], ticker: TickerBase, resp: TickerResponse, perc_change: float
) -> Image.Image:
"""Big price layout."""
show_logo = ticker.config.layout.show_logo and ticker.logo
Expand Down Expand Up @@ -47,7 +46,7 @@ def big_price(
)
)

sub_string = f"{len(resp.historical)}x{ticker.config.interval} {perc_change(ticker, resp):+.2f}%"
sub_string = f"{len(resp.historical)}x{ticker.config.interval} {perc_change:+.2f}%"
if ticker.config.avg_buy_price:
sub_string += f" ({perc_change_abp(ticker, resp):+.2f}%)"

Expand Down
5 changes: 2 additions & 3 deletions tinyticker/layouts/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
apply_layout_config,
fig_to_image,
historical_plot,
perc_change,
perc_change_abp,
strip_ax,
)


@register
def default(
size: Tuple[int, int], ticker: TickerBase, resp: TickerResponse
size: Tuple[int, int], ticker: TickerBase, resp: TickerResponse, perc_change: float
) -> Image.Image:
"""Default layout."""
show_logo = ticker.config.layout.show_logo and ticker.logo
Expand Down Expand Up @@ -48,7 +47,7 @@ def default(
sub_text = ax.text(
0,
1 - (top_text.get_window_extent().height + 1) / (pos.height * size[1]),
f"{len(resp.historical)}x{ticker.config.interval} {perc_change(ticker, resp):+.2f}%",
f"{len(resp.historical)}x{ticker.config.interval} {perc_change:+.2f}%",
transform=ax.transAxes,
fontsize=8,
weight="bold",
Expand Down
9 changes: 6 additions & 3 deletions tinyticker/layouts/register.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import dataclasses as dc
from typing import Callable, Optional, Tuple
from typing import Callable, Optional, Tuple, Dict


from PIL import Image

from ..tickers._base import TickerBase, TickerResponse

LayoutFunc = Callable[[Tuple[int, int], TickerBase, TickerResponse], Image.Image]
LAYOUTS = {}
LayoutFunc = Callable[[Tuple[int, int], TickerBase, TickerResponse, float], Image.Image]


@dc.dataclass
Expand All @@ -16,6 +16,9 @@ class LayoutData:
desc: Optional[str]


LAYOUTS: Dict[str, LayoutData] = {}


def register(func: LayoutFunc) -> LayoutFunc:
"""Register a layout function.
Expand Down

0 comments on commit f7c862e

Please sign in to comment.