Reputation: 1163
I have been asking for a code review where I have got an answer that says:
We're not in JavaScript - objects don't have to be dictionaries. This could be well-represented by a class for product, and a class for product size.
I have been trying to contact but no luck yet however I did manage to go through chat and got the response:
look at this - this is a size
"[EU 40](https://testing/shgz?pid=16002&tkn=3381a6fdc2bf2675b42e756c6dc668e5&ipa=87783)': 1,
@dataclass
class ProductSize:
name: str
link: str
quantity: int
* have a list of class instances
* in your ProductSize dataclass, make a @classmethod constructor that accepts a dictionary and returns a class instance
However my problem is that my knowledge is not good enough to yet understand the issue and what the person means to be able to proceed my problem.
Currently how my code looks like is:
import json
import re
from json.decoder import JSONDecodeError
from typing import ClassVar, List, Match, Optional
import attr
import requests
from selectolax.parser import HTMLParser
from config import configuration
from lib.utils import get, normalize_input
@attr.dataclass
class ProductPage:
store: ClassVar[str] = "Shoezgallery"
link: str = None
name: Optional[str] = None
price: Optional[str] = None
image: Optional[str] = None
pid: Optional[str] = None
token: Optional[str] = None
sizes: Optional[dict] = attr.ib(factory=dict)
webhook: str = "mixed"
delay: int = 0
shortcut: List[str] = [
'[Login](https://www.shoezgallery.com/en/authentification?back=my-account)',
'[Cart](https://www.shoezgallery.com/en/commande)',
'[Checkout](https://www.shoezgallery.com/en/authentification?back=https%3A%2F%2Fwww.shoezgallery.com%2Fen%2Fcommande%3Fstep%3D1&display_guest_checkout=1)',
]
@staticmethod
def get_sizes(doc: Optional[Match[str]], pid: Optional[str], token: Optional[str]) -> dict:
try:
data = json.loads(doc.group(1))
return {
f"[EU {get_sizes}](https://testing/shgz?pid={pid.attrs['value']}&tkn={token.attrs['value']}&ipa={att})": get(values, 'quantity')
for att, values in data.items()
if get(values, 'quantity') > 0
for get_sizes in get(values, 'attributes_values').values()
}
except JSONDecodeError:
return {}
@classmethod
def from_page(cls, link: str) -> "ProductPage":
with requests.get(url=link) as response:
if not response.ok:
return cls(
link=link
)
doc = HTMLParser(response.text)
name = doc.css_first('h1[itemprop="name"]')
price = doc.css_first('span[itemprop="price"]')
image = doc.css_first('img[itemprop="image"]')
pid = doc.css_first('input[name="id_product"]')
token = doc.css_first('input[name="token"]')
sizes = re.search('var\s*combinationsFromController\s*=\s*(.*?);', response.text, re.M | re.S)
return cls(
link=link,
name=name and name.text().strip(),
price=price and price.text().strip().replace("'", ""),
image=image and image.attributes.get('src'),
sizes=sizes and cls.get_sizes(sizes, pid, token),
)
@property
def payload(self) -> dict:
return {
"store": self.store,
"link": self.link,
"name": self.name or self.link.split("/")[-1],
"price": self.price or "Not found",
"image": self.image or "Not found",
"sizes": self.sizes or {},
"shortcut": self.shortcut,
"webhook": self.webhook,
"delay": self.delay
}
if __name__ == '__main__':
payload = ProductPage.from_page(link="https://www.shoezgallery.com/en/p16002-air-pegasus-83-nike-dj6892-001")
print(payload)
and I wonder what can I do to achieve where I have a class for the product and a class for product size?
Upvotes: 0
Views: 64
Reputation: 106
You can define your own classes to represent custom data types. You can try reading this guide.
The generic area you want to read up on is called Object Oriented Programming. I'm sure you will find plenty to help you on the topic.
Upvotes: 1