Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagicMtdAndDescript.py
More file actions
446 lines (419 loc) · 9.61 KB
/
Copy pathmagicMtdAndDescript.py
File metadata and controls
446 lines (419 loc) · 9.61 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# #可调用对象
# def foo():
# print('hello')
# foo()#这个和下面的意思是一样的
# foo.__call__()
#
# #__call__类中的第一个方法,实例就可以向函数一样调用
# class Point:
# def __init__(self,x,y):
# self.x = x
# self.y = y
# def __call__(self,*args,**kwargs):
# return 'Point({},{})'.format(self.x,self.y)
#
# p = Point(4,5)
# print(p.__dict__)
# print(p())
#
# class Adder:
# def __call__(self,*args):
# ret = 0
# for x in args:
# ret += x
# self.ret = ret
# return ret
#
# adder = Adder()
# print(adder(2,3,4,5,8))
# print(adder.ret)
#
# #定义一个斐波那契数列,方便调用。计算第n项目
# class Fib:
# def __init__(self):
# self.items = [0,1,1]
# def __call__(self,index):
# return self[index]
# def __iter__(self):
# return iter(self.items)
# def __len__(self):
# return len(self.items)
# def __getitem__(self,index):
# if index < 0:
# raise IndexError('Wrong Index')
# if index < len(self.items):
# return self.items[index]
# for i in range(3,index+1):
# itm = self.items[i-1] + self.items[i-2]
# if itm not in self.items:
# self.items.append(self.items[i-1] + self.items[i-2])
# return self.items[index]
# def __str__(self):
# return str(self.items)
# __repr__ = __str__
# fib = Fib()
# print(fib(5),len(fib))
# print(fib(10),len(fib))
# for x in fib:
# print(x)
# #上下文管理
# import sys
# class Point:
# def __init__(self):
# print('init')
# def __enter__(self):
# print('enter')
# def __exit__(self,exc_type,exc_val,exc_tb):
# print('异常类型',exc_type)
# print('异常的值',exc_val)
# print('异常信息追踪',exc_tb)
# print('exit')
#
# with Point() as f:
# raise Exception('New error')
# print('do sth')
# print(f)
#练习 位加法函数计时
# #运用装饰器
# import datetime
# import time
# from functools import wraps
#
# def timeit(fn):
# @wraps(fn)
# def wrapper(*args,**kwargs):
# start = datetime.datetime.now()
# ret = fn(*args,**kwargs)
# delta = (datetime.datetime.now() - start).total_seconds()
# print('{} took {}s'.format(fn.__name__,delta))
# return ret
# return wrapper
# @timeit
# def add(x,y):
# time.sleep(2)
# return x + y
# print(add(4,5))
#运用上下文管理
# import time
# import datetime
# from functools import wraps
#
# def add(x,y):
# time.sleep(2)
# return x + y
#
#
# class TimeIt:
# def __init__(self,fn):
# self.fn = fn
#
# def __enter__(self):
# self.start = datetime.datetime.now()
# return self
# def __exit__(self,exc_type,exc_val,exc_tb):
# self.delta = (datetime.datetime.now() - self.start).total_seconds()
# print('{} took {}s'.format(self.fn.__name__,self.delta))
# pass
# def __call__(self,x,y):
# print(x,y)
# return self.fn(x,y)
#
# with TimeIt(add) as foo:
# foo(4,6)
# #运用类装饰器来实现
# import time
# import datetime
# from functools import wraps
#
# class TimeIt:
# '''this is a class'''
# def __init__(self,fn = None):
# if fn is not None:
# self.fn = fn
# #self.__doc__ = fn.__doc__
# wraps(fn)(self)
#
# def __enter__(self):
# self.start = datetime.datetime.now()
# return self
# def __exit__(self,exc_type,exc_val,exc_tb):
# self.delta = (datetime.datetime.now() - self.start).total_seconds()
# print('{} took {}s'.format(self.fn.__name__,self.delta))
# def __call__(self,*args,**kwargs):
# return self.fn(*args,**kwargs)
# @TimeIt
# def add(x,y):
# '''this is a add function'''
# return x + y
#
# print(add(4,5))
# print(add.__doc__)
# print(TimeIt.__doc__)
# #反射
# class Point:
# def __init__(self,x,y,z=None):
# self.x = x
# self.y = y
# def __str__(self):
# return 'Point({},{})'.format(self.x,self.y)
# def show(self):
# print(self.x,self.y)
#
# p1 = Point(4,5)
# p2 = Point(10,10)
# print(p1,p2)
# print(repr(p1),repr(p2),sep = '\n')
# print(p1.__dict__)
# setattr(p1,'y',20)
# setattr(p1,'z',9)
# print(getattr(p1,'__dict__'))
#
# if hasattr(p1,"show"):
# getattr(p1,'show')()
# if not hasattr(p1,'add'):
# setattr(Point,'__add__',lambda self,other:Point(self.x + other.x,self.y + other.y))
# print(Point.__add__)
# print(p2+p1)
# if not hasattr(p1,'sub'):
# setattr(p1,'sub',lambda self,other:Point(self.x - other.x,self.y - other.y))
# print(p1.sub(p1,p1))
#
# print(p1.__dict__)
# print(Point.__dict__)
# #用类来实现命令分发器
# class Dispatcher:
# def __init__(self):
# self._run()
# def cmd1(self):
# print("i'm cmd1")
#
# def cmd2(self):
# print("i'm cmd2")
#
# def _run(self):
# while True:
# cmd = input('Plz input a cmd')
# if cmd.strip() == 'quit':
# break
# getattr(self,cmd,lambda:print('Unknown Command{}'.format(cmd)))
# Dispatcher()
#反射相关的魔术方法
# class Base:
# n = 0
#
# class Point(Base):
# z = 6
# def __init__(self,x,y):
# self.x = x
# self.y = y
# def show(self):
# print(self.x, self.y)
# def __getattr__(self, item):
# return 'missing {}'.format(item)
#
# p1 = Point(4,5)
# print(p1.x)
# print(p1.z)
# print(p1.n)
# print(p1.t)#missing t
# class Base:
# n = 0
#
# class Point(Base):
# z = 6
# def __init__(self,x,y):
# print('init')
# self.x = x
# self.y = y
# def show(self):
# print(self.x, self.y)
# def __getattr__(self, item):
# return 'missing {}'.format(item)
# def __setattr__(self,key,value):
# print('setattr {} = {}'.format(key,value))
#
# p1 = Point(4,5)
# p1.t = 100
# print('1',p1.x)
# print('2',p1.y)
# print('3',p1.z)
# print('4',p1.n)
# print('5',p1.t)#missing t
# class Base:
# n = 0
#
# class Point(Base):
# z = 6
# def __init__(self,x,y):
# self.x = x
# self.y = y
# def show(self):
# print(self.x, self.y)
# def __getattr__(self, item):
# return 'missing {}'.format(item)
# def __getattribute__(self, item):
# return item
#
# p1 = Point(4,5)
# print(p1.__dict__)
# print(p1.x)
# print(p1.z)
# print(p1.n)
# print(p1.t)#missing t
# print(Point.__dict__)
# print(Point.z)
#描述器
# class A:
# def __init__(self):
# self.a1 = 'a1'
# print('A.init')
#
# class B:
# x = A()
# def __init__(self):
# print('B.init')
#
# print('-'*20)
# print(B.x.a1)
#
# print('='*20)
# b = B()
# print(b.x.a1)
#加入__get__函数
# class A:
# def __init__(self):
# self.a1 = 'a1'
# print('A.init')
#
# def __get__(self,instance,owner):
# print('A.__get__{} {} {}'.format(self,instance,owner))
# return self #解决返回值是None的问题
#
# class B:
# x = A()
# def __init__(self):
# print('B.init')
#
# print('-'*20)
# print(B.x.a1)
#
# print('='*20)
# b = B()
# print(b.x.a1)
#类B的实例属性可以吗
# class A:
# def __init__(self):
# self.a1 = 'a1'
# print('A.init')
#
# def __get__(self,instance,owner):
# print('A.__get__{} {} {}'.format(self,instance,owner))
# return self #解决返回值是None的问题
#
# class B:
# x = A()
# def __init__(self):
# self.b = A()
# print('B.init')
#
# print('-'*20)
# print(B.x)
# print(B.x.a1)
#
# print('='*20)
# b = B()
# print(b.x)
# print(b.x.a1)
#print(b.b)#访问实例的属性并不会触发get函数
# class A:
# def __init__(self):
# self.a1 = 'a1'
# print('A.init')
#
# def __get__(self,instance,owner):
# print('A.__get__{} {} {}'.format(self,instance,owner))
# return self #解决返回值是None的问题
#
# class B:
# x = A()
# def __init__(self):
# print('B.init')
# self.x = 'b.x'#增加实例属性
#
# print('-'*20)
# print(B.x)
# print(B.x.a1)
#
# print('='*20)
# b = B()
# print(b.x)
# #print(b.x.a1)
# class A:
# def __init__(self):
# self.a1 = 'a1'
# print('A.init')
#
# def __get__(self,instance,owner):
# print('A.__get__{} {} {}'.format(self,instance,owner))
# return self #解决返回值是None的问题
#
# def __set__(self,instance,value):
# print('A.__set__ {} {} {}'.format(self,instance,value))
# self.data = value
#
# class B:
# x = A()
# def __init__(self):
# print('B.init')
# self.x = 'b.x'
#
# print('-'*20)
# print(B.x)
# print(B.x.a1)
#
# print('='*20)
# b = B()
# print(b.x)
# print(b.x.a1)
# print(A.__dict__)
# print(B.__dict__)
# print(b.__dict__)
# b.x = 500
# print(A.__dict__)
# print(B.__dict__)
# print(b.__dict__)
# B.x =600
# print(A.__dict__)
# print(B.__dict__)
# print(b.__dict__)
#描述其的本质
# class A:
# def __init__(self):
# self.a1 = 'a1'
# print('A.init')
#
# def __get__(self,instance,owner):
# print('A.__get__{} {} {}'.format(self,instance,owner))
# return self #解决返回值是None的问题
#
# def __set__(self,instance,value):
# print('A.__set__ {} {} {}'.format(self,instance,value))
# self.data = value
#
# class B:
# x = A()
# def __init__(self):
# print('B.init')
# self.x = 'b.x'
# self.y = 'b.y'
#
# print('-'*20)
# print(B.x)
# print(B.x.a1)
#
# print('='*20)
# b = B()
# print(b.x)
# print(b.y)
# print(b.__dict__)
# print(B.__dict__)
You can’t perform that action at this time.
