-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass_methods.py
More file actions
90 lines (67 loc) · 2.19 KB
/
class_methods.py
File metadata and controls
90 lines (67 loc) · 2.19 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
import datetime
import pprint
class Employee:
raise_amount = 1.05
def __init__(self, first_name: str, last_name: str, age: int):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.salary = 1000
def increase_salary(self):
self.salary *= Employee.raise_amount
def full_name(self) -> str:
return f'{self.first_name} {self.last_name}'
# decorator
@classmethod
def set_raise_amount(cls, amount: float):
cls.raise_amount = amount
@classmethod
def from_string(cls, string: str):
first_name, last_name, age = string.split('-')
return Employee(first_name, last_name, age)
@staticmethod
def is_work_day(day):
if day.weekday() in (5, 6):
return False
return True
@property
def fullname(self):
return f'{self.first_name} {self.last_name}'
@fullname.setter
def fullname(self, value: str):
self.first_name, self.last_name = value.split()
def __repr__(self):
return f'Employee(first={self.first_name}, last={self.last_name}, age={self.age}, salary={self.salary})'
def __add__(self, other):
print('hey i am adding employees together')
return 'hello'
class Developer(Employee):
def __init__(self, first_name, last_name, age, language):
super().__init__(first_name, last_name, age)
self.language = language
# for developers
def __repr__(self):
return 'hello'
# for customers
def __str__(self):
return 'hey'
john = Employee(first_name='john', last_name='doe', age=30)
print(john)
Employee.set_raise_amount(1.10)
print(john.raise_amount)
print(Employee.raise_amount)
john.increase_salary()
print(john)
# Factory pattern object creation
ada = Employee.from_string('Ada-Lovelace-22')
print(ada)
ada.increase_salary()
ada.increase_salary()
print(ada)
some_day = datetime.datetime(2021, 1, 15)
print(Employee.is_work_day(some_day))
print(john + ada)
# anish = Developer(first_name='Anish', last_name='Sachdeva', age=22, language='Java')
# ada = Developer(first_name='Ada', last_name='Lovelace', age=30, language='Python')
# print(ada)
# pprint.pprint(ada)