-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop13_item_class.py
More file actions
97 lines (75 loc) · 2.91 KB
/
oop13_item_class.py
File metadata and controls
97 lines (75 loc) · 2.91 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import csv
class Item:
pay_rate = 0.8 # The pay rate after 20% discount
all = [] # list to store all instances of Item class
# constructor
def __init__(self, name: str, price: float, quantity=0):
# Run validation to the received arguments
assert price >= 0, f"Price {price} is not greater than or equal to zero!"
assert quantity >=0, f"Quantity {quantity} is not greater than or equal to zero!"
# assigning to self object
self.__name = name # instance attributes
self.__price = price # instance attributes
self.quantity = quantity # instance attributes
# append the instance to the all list
Item.all.append(self)
@property
def price(self):
return self.__price
def apply_discount(self):
self.__price = self.__price * self.pay_rate # accessing class attribute via insta nce
def apply_increment(self, increment_value):
self.__price = self.__price + (self.__price * increment_value)
@property # cannot be changed, read-only attribute
def name(self):
print("You are trying to get name attribute")
return self.__name
@name.setter
def name(self, value):
print("You are trying to set name attribute")
if len(value) > 10:
raise Exception("The name is too long!")
self.__name = value
def calculate_total_price(self):
return self.__price * self.quantity
@classmethod # decorator to define a class method
def instantiate_from_csv(cls):
with open('oop07instances.csv', 'r') as f:
reader = csv.DictReader(f)
items = list(reader)
for item in items:
Item(
name = item.get('name'),
price = float(item.get('price')),
quantity = int(item.get('quantity')),
)
@staticmethod
def is_integer(num):
# checks if given parameter is instance of a float or int
if isinstance(num, float):
# count out the floats that are point zero
return num.is_integer()
elif isinstance(num, int):
return True
else:
return False
# __repr__ method to return a string representation of the object
def __repr__(self):
return f"{self.__class__.__name__}('{self.name}', {self.__price}, {self.quantity})"
def __connect(self, smpt_server):
pass
def __prepare_body(self):
return f"""Hello {self.name},
We have {self.quantity} items of {self.name} at price {self.price}.
Regards,
Store Owner
"""
def __send(self):
pass
def send_email(self):
self.__connect("smtp.server.com")
self.__prepare_body()
self.__send()
# @property # can't be changed, read-only attribute
# def read_only_name(self):
# return "AAA"