Repository navigation
Expand file tree
/
Copy pathclass.py
More file actions
126 lines (96 loc) · 3.48 KB
/
Copy pathclass.py
File metadata and controls
126 lines (96 loc) · 3.48 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from datetime import datetime
#ref: https://www.youtube.com/watch?v=rq8cL2XMM5M&list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc&index=3
class BaseEmployee:
def __init__(self, firstname : str, lastname : str):
self._firstname = firstname
self._lastname = lastname
@property
def fullname(self):
return f"{self._firstname} {self._lastname}"
@fullname.setter
def fullname(self, fullname: str):
self._firstname, self._lastname = fullname.split(" ")
@fullname.deleter
def fullname(self):
self._firstname = None
self._lastname = None
class PaidEmployee(BaseEmployee):
def __init__(self, firstname : str, lastname : str, initial_payment : int):
super().__init__(firstname, lastname)
self._initial_payment = initial_payment
self._raise_amount = 1.04
#__repr__ is for debugging
def __repr__(self):
return f"PaidEmployee('{self._firstname}','{self._lastname}',{self._initial_payment})"
def __str__(self):
return f"'{self._firstname}' '{self._lastname}',{self._initial_payment}"
def __add__(self, other):
if isinstance(other, PaidEmployee):
return self._initial_payment + other._initial_payment
return NotImplemented
def __len__(self):
return len(self.fullname())
def apply_raise(self):
self._initial_payment = int(self._initial_payment * self._raise_amount)
#classmethods are used to creae an instance of this class
@classmethod
def set_raise_payment_amount(cls, amount):
cls._raise_amount = amount
@classmethod
def from_string(cls, string : str):
firstname, lastname, payment = string.split("-")
return cls(firstname, lastname, payment)
@staticmethod
def is_workday(day : datetime):
if day.weekday() == 5 or day.weekday() == 6:
return False
return False
def main() -> None:
PaidEmployee.set_raise_payment_amount(1.05)
pe_1 = PaidEmployee("Max", "Power", 3000)
pe_1.fullname = "Hard Cora"
pe_2 = PaidEmployee("Victoria", "Major", 4000)
#this is working because of the @fullname.setter
firstname, lastname, payment = "John-Doe-6000".split("-")
pe_3 = PaidEmployee(firstname, lastname, payment)
#this is working because of the @fullname.deleter
del pe_3.fullname
pe_4 = PaidEmployee.from_string("Jane-Doe-8000")
print(":: Dump fullnames")
#this works because of the @property
print(pe_1.fullname)
print(pe_2.fullname)
print(pe_3.fullname)
print(pe_4.fullname)
print("")
print(":: Is workday")
current_datetime = datetime.now()
print(PaidEmployee.is_workday(current_datetime))
print("")
print(":: Add")
print(pe_1 + pe_2)
print("")
print(":: Check if this is an instance of")
print(f"{isinstance(pe_1, PaidEmployee)=}")
print("")
print(":: Check if this is the same class name")
if (type(pe_1).__name__ == PaidEmployee.__name__):
print(f"{type(pe_1).__name__=} is equal to {PaidEmployee.__name__=}")
else:
print(f"{type(pe_1).__name__=} is not equal to {PaidEmployee.__name__=}")
print("")
print(":: Is subclass")
print(issubclass(PaidEmployee, BaseEmployee))
print("")
print(":: rpr")
print(repr(pe_1))
print("")
print(":: str")
print(pe_1)
print("")
#uncomment if needed
#print(help(BaseEmployee))
#uncomment if needed
#print(help(PaidEmployee))
if __name__ == "__main__":
main()