-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_decorators.py
79 lines (53 loc) · 2.96 KB
/
internal_decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#External imports
from rich.console import Console
from rich.table import Table
from prettytable import PrettyTable
#Typing imports
from typing import List, Tuple
def print_SQL_records(func):
"""A decorator that prints all the return records from a function"""
def wrapper(self, *args, **kwargs):
records_list = func(self, *args, **kwargs)
for record in records_list:
str_tuple = tuple(map(str, record))
print_string = " ".join(str_tuple)
print(print_string)
return wrapper
def print_prettified_products_for_user(func):
"""A decorator that prints all the returned product_records in a prettified format"""
def wrapper(self, *args, **kwargs):
products_list: List[Tuple[int, str, float, int, int, int]] = func(self, *args, **kwargs) #product_list variable is a list of tuples (product_id, name, price, bought, user_id, rating)
table = PrettyTable()
table.field_names = ["PRODUCT ID", "PRODUCT NAME", "PRICE ($CAD)", "BOUGHT-STATUS", "RATING (/10)"]
for product in products_list:
product = list(product)
product[2] = f"${product[2]:.2f}" #converting the price from a float to a string with a dollar sign in front of it.
if product[3] == 1: #converting the integer version of the 'bought' field to a string. It is more user-friendly
product[3] = 'Bought'
else:
product[3] = 'Unbought'
modified_list = list(map(str, product))
m_list_sliced = list(modified_list[:4] + [modified_list[-1]])
table.add_row(m_list_sliced)
print(table)
return products_list
return wrapper
def print_prettified_products_for_user2(func):
"""A decorator that prints all the returned product_records in a prettified format"""
def wrapper(self, *args, **kwargs):
products_list: List[Tuple[int, str, float, int, int, int]] = func(self, *args, **kwargs) #product_list variable is a list of tuples (product_id, name, price, bought, user_id, rating)
table = PrettyTable()
table.field_names = ["PRODUCT ID", "PRODUCT NAME", "PRICE ($CAD)", "BOUGHT-STATUS", "RATING (/10)"]
for product in products_list:
product = list(product)
product[2] = f"${product[2]:.2f}" #converting the price from a float to a string with a dollar sign in front of it.
if product[3] == 1: #converting the integer version of the 'bought' field to a string. It is more user-friendly
product[3] = 'Bought'
else:
product[3] = 'Unbought'
modified_list = list(map(str, product))
m_list_sliced = list(modified_list[:4] + [modified_list[-1]])
table.add_row(m_list_sliced)
print(table)
return products_list
return wrapper