-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_class.py
76 lines (66 loc) · 2.27 KB
/
python_class.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
from datetime import datetime
import csv
class write2csv:
def __init__(
self,
item: str,
purchase_date: datetime,
quantity: int,
total_price: float,
):
self.item = item
self.purchase_date = purchase_date
self.quantity = quantity
self.total_price = total_price
def get_tax_rate(self) -> float:
"""return different tax rate based on year of purchase"""
if self.purchase_date.year < 2023:
return 0.07
elif self.purchase_date.year >= 2022 and self.purchase_date.year < 2024:
return 0.08
elif self.purchase_date.year >= 2024:
return 0.09
def get_unit_price(self) -> float:
return self.total_price/self.quantity
def get_price_before_tax(self) -> float:
tax_rate = self.get_tax_rate()
return self.total_price/(1+tax_rate)
def get_unit_price_before_tax(self) -> float:
total_before_tax = self.get_price_before_tax()
return total_before_tax/self.quantity
def header(self) -> list[str]:
return [
"item",
"date of purchase",
"quantity",
"total price",
"total price before tax",
"unit price",
"unit price before tax"
]
def one_row(self) -> list:
return [
self.item,
self.purchase_date.date(),
self.quantity,
self.total_price,
self.get_price_before_tax(),
self.get_unit_price(),
self.get_unit_price_before_tax()
]
def save_csv(
self,
filename: str = "regular_python_class.csv",
newfile: bool = False,
):
if newfile:
# overwrite file and add header
with open(filename, "w") as fileObj:
writer = csv.writer(fileObj)
writer.writerow(self.header())
writer.writerow(self.one_row())
else:
# append row to existing file
with open(filename, "a") as fileObj:
writer = csv.writer(fileObj)
writer.writerow(self.one_row())