Skip to content

Commit

Permalink
Merge pull request #214 from Crinibus/add-to_string_format-to-product
Browse files Browse the repository at this point in the history
Add method to_string_format to Product and update function add_scatter_plot
  • Loading branch information
Crinibus authored May 28, 2023
2 parents 49c989b + 7676cbf commit 2dd80fc
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions scraper/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Iterable, Iterator
import plotly.graph_objs as go
from datetime import datetime
import re

from scraper import Filemanager
from scraper.constants import WEBSITE_COLORS
Expand Down Expand Up @@ -30,6 +31,42 @@ def get_all_dates(self) -> list[str]:
def get_all_prices(self) -> list[float]:
return [datapoint.price for datapoint in self.datapoints]

def to_string_format(self, format: str) -> str:
"""Return a string representing the product, controlled by an explicit format string.
>>> p = Product("ASUS RTX 4090", "GPU", "https://www.example.com/", "123", "USD", "example", [datepoints], True)
>>> p.to_string_format("Name: %name, Category: %category, URL: %url, ID: %id, Website: %website")
'Name: ASUS RTX 4090, Category: GPU, URL: https://www.example.com/, ID: 123, Website: example'
"""
# inspiration from https://docs.python.org/3/library/re.html#writing-a-tokenizer
token_specification = [
("NAME", r"(%name)"),
("CATEGORY", r"(%category)"),
("URL", r"(%url)"),
("ID", r"(%id)"),
("CURRENCY", r"(%currency)"),
("WEBSITE", r"(%website)"),
]
format_to = {
"NAME": self.product_name,
"CATEGORY": self.category,
"URL": self.url,
"ID": self.id,
"CURRENCY": self.currency,
"WEBSITE": self.website,
}

tok_regex = "|".join("(?P<%s>%s)" % pair for pair in token_specification)
new_string = format

for mo in re.finditer(tok_regex, format):
kind = mo.lastgroup
value = mo.group()

new_string = new_string.replace(value, format_to[kind], 1)

return new_string


@dataclass
class MasterProduct:
Expand Down Expand Up @@ -120,11 +157,8 @@ def show_products(products: list[Product], title: str) -> None:
for product in products:
add_scatter_plot(
fig,
product.website,
product.id,
product.currency,
product.get_all_dates(),
product.get_all_prices(),
product,
name_format="%website - %name - %id",
)
config_figure(fig, title)
fig.show()
Expand Down Expand Up @@ -204,24 +238,20 @@ def config_figure(figure: go.Figure, figure_title: str) -> None:

def add_scatter_plot(
figure: go.Figure,
website_name: str,
id: str,
currency: str,
dates: list[str],
prices: list[float],
name: str = None,
product: Product,
color: str = None,
hover_text: str = None,
name_format: str = None,
) -> None:
scatter_name = name if name else f"{website_name.capitalize()} - {id}"
scatter_color = color if color else WEBSITE_COLORS[website_name]
scatter_hover_text = hover_text if hover_text else "Price: %{y:.0f}" + f" {currency}"
scatter_name = product.to_string_format(name_format) if name_format else f"{product.website.capitalize()} - {product.id}"
scatter_color = color if color else WEBSITE_COLORS[product.website]
scatter_hover_text = hover_text if hover_text else "Price: %{y:.0f}" + f" {product.currency}"

figure.add_trace(
go.Scatter(
name=scatter_name,
x=dates,
y=prices,
x=product.get_all_dates(),
y=product.get_all_prices(),
line={"color": scatter_color, "width": 2},
hovertemplate=scatter_hover_text,
)
Expand Down

0 comments on commit 2dd80fc

Please sign in to comment.