-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython.py
More file actions
255 lines (165 loc) · 6.58 KB
/
Copy pathpython.py
File metadata and controls
255 lines (165 loc) · 6.58 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
https://foobar.withgoogle.com/?eid=XCdAl
chr(number) : : This function converts number to its corresponding ASCII character.
ord('X') //will give ascii value
for idx, val in enumerate(ints):
print(idx, val)
# number of elements
n = int(input("Enter number of elements : "))
# Below line read inputs from user using map() function
a = list( map( int, input("\nEnter the numbers : ").strip().split() ) ) [:n]
lst1 = [int(item) for item in input("Enter the list items : ").split()]
list(map(int.__sub__, a, b))
for x, y in thisdict.items():
print(x, y)
for num1, num2 in zip(sorted(n, reverse = True), sorted(n)):
new_n += str(ord(num1) - ord(num2))
# sorting string -- sorted fucnction return sorted list
s = ''.join(sorted(str, key, reverse = True)) #key is custom comp function
dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object (say functions , modules, strings, lists, dictionaries etc.)
dir(object)
help(object)
global x
nonlocal x
// xrange, genreators
//////////////////////////////////////////////////////////
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@email.com'
self.pay = pay
Employee.num_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
@classmethod
def set_raise_amt(cls, amount):
cls.raise_amt = amount
@classmethod #method of class, takes cls as first argument
def from_string(cls, emp_str):
first, last, pay = emp_str.split('-')
return cls(first, last, pay)
@staticmethod #doesnt care about class
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True
def __repr__(self): #special method of representation, useful when we print(emp_1), otherwite it would print <__main__ Emloy object at 483902x832>
return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)
def __str__(self): #for ease of read for end user, same as repr
return '{} - {}'.format(self.fullname(), self.email)
def __add__(self, other): #arthmetic special operators, there are many like this
return self.pay + other.pay
def __len__(self):
return len(self.fullname())
emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Test', 'Employee', 60000)
Employee.set_raise_amt(1.05)
print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)
emp_str_1 = 'John-Doe-70000'
emp_str_2 = 'Steve-Smith-30000'
emp_str_3 = 'Jane-Doe-90000'
first, last, pay = emp_str_1.split('-')
#new_emp_1 = Employee(first, last, pay)
new_emp_1 = Employee.from_string(emp_str_1)
print(new_emp_1.email)
print(new_emp_1.pay)
import datetime
my_date = datetime.date(2016, 7, 11)
print(Employee.is_workday(my_date))
class Developer(Employee):
raise_amt = 1.10
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_emps(self):
for emp in self.employees:
print('-->', emp.fullname())
dev_1 = Developer('Corey', 'Schafer', 50000, 'Python')
dev_2 = Developer('Test', 'Employee', 60000, 'Java')
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1.email)
mgr_1.add_emp(dev_2)
mgr_1.remove_emp(dev_2)
mgr_1.print_emps()
#property decorators, Getters, Setters, and Deleters
class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
@property #now you can call email by print(emp.email) like an attribute, otherwith you would write like this print(emp.email()), which could've be a problem if otherpeople already using the class, then they have to change thier code for printing
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
@fullname.setter # so that you can write like emp.fullname = 'blah blah' , notice you are not passing, you are setting
def fullname(self, name):
first, last = name.split(' ')
self.first = first
self.last = last
@fullname.deleter
def fullname(self):
print('Delete Name!')
self.first = None
self.last = None
emp_1 = Employee('John', 'Smith')
emp_1.fullname = "Corey Schafer"
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)
del emp_1.fullname
///////////////////////////////////////////////////////////////////////////////////////////////////////////
shallow copy # only copy 1 level deep
l1 l2
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
after performin l2[1] = ['c', 'd']
l1 l2
0 --->['a', 'b']<--- 0
0 --->['a', 'b'] 0 --->['c', 'd']
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
but if we perform l2[1][1] (accesing 2 level deep) then it will follow the pointer and change both list
l1 l2
0 --->['a', 'b']<--- 0
0 --->['a', 'c']<--- 0
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
0 --->['a', 'b']<--- 0
print(l1[0] is l2[0]) #to check if both refer to same object
# importing copy module
import copy
# initializing list 1
li1 = [1, 2, [3,5], 4]
# using copy for shallow copy
li2 = copy.copy(li1) li2 = li1.copy() lst2 = lst1[:]
# using deepcopy for deepcopy
li3 = copy.deepcopy(li1)
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# -10,-9,-8,-7,-6,-5,-4,-3,-2,-1
# list[start:end:step] end is not inclusive
print(*my_list, sep = ", ", end = "") # for printing list elements