-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from Crinibus/create-folder-models
Move dataclasses to new folder "models"
- Loading branch information
Showing
6 changed files
with
87 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from scraper.models.product import Datapoint, Info, Product, MasterProduct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from dataclasses import dataclass, field | ||
import re | ||
|
||
|
||
@dataclass | ||
class Info: | ||
"""Scraped info about product""" | ||
|
||
name: str | ||
price: float | ||
currency: str | ||
id: str | ||
valid: bool = True | ||
|
||
|
||
@dataclass | ||
class Datapoint: | ||
date: str | ||
price: float | ||
|
||
|
||
@dataclass | ||
class Product: | ||
product_name: str | ||
category: str | ||
url: str | ||
id: str | ||
currency: str | ||
website: str | ||
datapoints: list[Datapoint] | ||
is_up_to_date: bool | ||
|
||
def get_all_dates(self) -> list[str]: | ||
return [datapoint.date for datapoint in self.datapoints] | ||
|
||
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: | ||
product_name: str | ||
category: str | ||
products: list[Product] = field(default_factory=list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters