-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX13_class_exemple.py
148 lines (107 loc) · 3.17 KB
/
EX13_class_exemple.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
"""Class Exemple.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1JT4iRV2I3dQLegcJzLBA0fCVSfBl1apJ
"""
from ast import If
class Employee:
num_empl=0
raise_amount= 1.25
def __init__(self, first,last, pay):
self.first= first
self.last= last
self.pay= pay
self.email= first + '.' + last + '@company.com'
Employee.num_empl+=1
#regular methodes: pass the instances as first arguments
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
@fullname.setter
def fullname(self, name):
first, last= name.split(' ')
self.first= first
self.last= last
def apply_raise(self):
self.pay= int(self.pay + self.raise_amount)
#pass the class as first argument
@classmethod
def set_raise_amt(cls, amount):
cls.raise_amount= amount
@classmethod
def from_string(cls, emp_str):
first, last, pay= emp_str.split('-')
return cls(first, last, pay)
@staticmethod
def is_workday(day):
if day.weekday()==5 or day.weekday()==6:
return False
return True
def __repr__(self):
return "Employee('{}', '{}','{}')".format(self.first, self.last, self.pay)
def __str__(self):
return "Employee('{}' - '{}'.format(self.fullname(), self.employee))"
def __add__(self, other):
return self.pay + other.pay
def __len__(self):
return len(self.fullname())
emp_1= Employee('Ahmed','loussci',30000)
emp_2= Employee('Ahmed','loui',60000)
print(emp_1 + emp_2)
print(len(emp_1))
emp_1.fullname= 'salim mass'
print(emp_1.first)
print(emp_1.pay)
print(emp_1.email)
print(emp_1.__dict__)
emp_1.fullname()
print(emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
print(Employee.num_empl)
print(Employee.raise_amount)
print(emp_1.raise_amount)
emp_str_1= 'Salim-soud-10000'
new_emp= Employee.from_string(emp_str_1)
print('Gmail: {} and Pay: {}'.format(new_emp.email, new_emp.pay))
import datetime
my_date= datetime.date(2016, 7,11)
print(Employee.is_workday(my_date))
#inheretane & subclass
class Developer(Employee):
#pass
def __init__(self, first, last, pay, prog_lang):
super().__init__(first, last, pay)
self.prog_lang= prog_lang
class Manager(Employee):
def __init__(self, first, last, pay, employees= None):
super().__init__(first, last, pay)
if employees is None:
self.employees=[]
else:
self.employees= employees
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emp(self):
for emp in self.employees:
print('-->', emp.fullname())
dev_1= Developer("dima", 'lin', 13000, 'Python')
dev_2= Developer('sami', 'lonm' ,33000,'c#')
print(dev_1.email, dev_1.prog_lang)
print(dev_2.email, dev_2.prog_lang)
mang_1= Manager('sahim','ham',333333,[dev_1])
print(mang_1.email)
mang_1.add_emp(dev_2)
mang_1.print_emp()
print(isinstance(mang_1, Developer))
print(issubclass(Manager, Developer))
print(issubclass(Developer, Employee))
print(help(Developer))