-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.py
349 lines (263 loc) · 7.38 KB
/
oop.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
/**
*
* █████▒█ ██ ▄████▄ ██ ▄█▀ ██████╗ ██╗ ██╗ ██████╗
* ▓██ ▒ ██ ▓██▒▒██▀ ▀█ ██▄█▒ ██╔══██╗██║ ██║██╔════╝
* ▒████ ░▓██ ▒██░▒▓█ ▄ ▓███▄░ ██████╔╝██║ ██║██║ ███╗
* ░▓█▒ ░▓▓█ ░██░▒▓▓▄ ▄██▒▓██ █▄ ██╔══██╗██║ ██║██║ ██║
* ░▒█░ ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄ ██████╔╝╚██████╔╝╚██████╔╝
* ▒ ░ ░▒▓▒ ▒ ▒ ░ ░▒ ▒ ░▒ ▒▒ ▓▒ ╚═════╝ ╚═════╝ ╚═════╝
* ░ ░░▒░ ░ ░ ░ ▒ ░ ░▒ ▒░
* ░ ░ ░░░ ░ ░ ░ ░ ░░ ░
* ░ ░ ░ ░ ░
*/
'''
from types import MethodType
from enum import Enum, unique
from hello import hello
print('------模块------')
print('------oop------')
class Student(object):
def __init__(self, name, score):
self.__name = name
self.__score = score
def print_score(self):
print("%s: %s" % (self.__name, self.__score))
def get_grade(self):
if self.__score >= 90:
return 'A'
elif self.__score >= 60:
return 'B'
else:
return 'C'
def get_name(self):
return self.__name
def get_score(self):
return self.__score
def set_name(self, name):
self.__name = name
def set_score(self, score):
# if 0 <= score <= 100:
self.__score = score
# else:
# raise ValueError('bad score')
tom = Student('Math gg', 88)
jack = Student('enligsh', 55)
tom.print_score()
jack.print_score()
print(tom, '\n', jack, '\n', Student)
print(tom.get_grade(), '\n', jack.get_grade())
print(tom.get_score())
tom.set_score(101)
print(tom.get_score())
print(tom._Student__name)
print('------------')
class Animal(object):
name = 'ssss'
def run(self):
print('%s is running...' % self.__class__.__name__)
def run_twice(self, animal):
animal.run()
def fuck_xxx():
print('fuck xxx')
class Cat(Animal):
pass
class Dog(Animal):
pass
dog = Dog()
dog.run()
cat = Cat()
cat.run()
print(issubclass(Cat, Animal))
cat.run_twice(Cat())
print(type(cat))
print(dir(Animal()))
cat.age = 20
print(cat.age)
print(dir(Cat()))
del cat.age
print(cat)
print('------__slots__------')
class Student(object):
__slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称
s = Student()
s.name = 'Tom'
print(s.name)
def set_age(self, age):
self.age = age
# s.set_age = MethodType(set_age, s) # 给实例绑定一个方法
# s.set_age(25)
# print(s.age)
# 给class绑定方法
Student.set_age = MethodType(set_age, Student)
class Students(object):
@property
def score(self):
return self.__score
@score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self.__score = value
s1 = Students()
s1.score = 60 # OK,实际转化为s.set_score(60)
print(s1.score) # OK,实际转化为s.get_score()
print()
print('-------多重继承-------')
class CarnivorousMixIn(object):
pass
class HerbivoresMixIn(object):
pass
class RunnableMixIn(object):
def run(self):
print('running...')
class FlyableMixIn(object):
def fly(self):
print('flying...')
class Animal(object):
pass
class Mammal(object):
pass
class Bird(Animal):
pass
class Dog(Mammal, RunnableMixIn, CarnivorousMixIn):
pass
class Bat(Mammal, FlyableMixIn, HerbivoresMixIn):
pass
class Parrot(Bird):
pass
class Ostrich(Bird):
pass
print()
print('-------定制类-------')
class Student(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
__repr__ = __str__
print(Student('Toms'))
print(Student.__repr__.__name__)
class Chain(object):
def __init__(self, path=''):
self._path = path
def __getattr__(self, path):
return Chain('%s/%s' % (self._path, path))
def __str__(self):
return self._path
def __call__(self):
print('call me')
__repr__ = __str__
print(Chain().status.user.timeline.list)
s = Chain()
s()
print(callable(s))
print()
print('-------使用枚举类-------')
Month = Enum('Month', (
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
))
print(Month)
print(dir(Month))
print([name for name, member in Month.__members__.items()])
for name, member in Month.__members__.items():
print(name, '=>', member, ',', member.value)
print('------------')
@unique
class Weekday(Enum):
Sun = 0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
for name, member in Weekday.__members__.items():
print(name, '=>', member)
print()
print('------使用元类------')
class App(object):
def run(self):
print('app is running...')
def fn(self, name='world'):
print('Hello, %s.' % name)
Hello = type('Hello', (App, ), dict(hello=fn, age=20))
print(Hello)
h = Hello()
h.run()
h.hello()
print(h.age)
print(dir(Hello))
print(dir(type))
print(dir(object))
print('-------------------')
class ListMetaclass(type):
def __new__(cls, name, bases, attrs):
attrs['add'] = lambda self, value: self.append(value)
return type.__new__(cls, name, bases, attrs)
class MyList(list, metaclass = ListMetaclass):
pass
l = MyList()
l.add(10)
print(l)
print('------ORM------')
class Field(object):
def __init__(self, name, column_type):
self.name = name
self.column_type = column_type
def __str__(self):
return '<%s:%s>' % (self.__class__.__name__, self.name)
class StringField(Field):
def __init__(self, name):
super(StringField, self).__init__(name, 'varchar(100)')
class IntegerField(Field):
def __init__(self, name):
super(IntegerField, self).__init__(name, 'bigint')
class ModelMetaclass(type):
def __new__(cls, name, bases, attrs):
if name == 'Model':
return type.__new__(cls, name, bases, attrs)
print('Found model: %s' % name)
mappings = dict()
for k, v in attrs.items():
if isinstance(v, Field):
print('Found mapping: %s => %s' % (k, v))
mappings[k] = v
for k in mappings.keys():
attrs.pop(k)
attrs['__mappings__'] = mappings # 保存属性和列的映射关系
attrs['__table__'] = name # 假设表名和类名一致
return type.__new__(cls, name, bases, attrs)
class Model(dict, metaclass=ModelMetaclass):
def __init__(self, **kw):
super(Model, self).__init__(**kw)
def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(r"'Model' object has no attribute '%s'" % key)
def __setattr__(self, key, value):
self[key] = value
def save(self):
fields = []
params = []
args = []
for k, v in self.__mappings__.items():
fields.append(v.name)
params.append('?')
args.append(getattr(self, k, None))
sql = 'insert into %s (%s) values (%s)' % (self.__table__, ','.join(fields), ','.join(params))
print('SQL: %s' % sql)
print('ARGS: %s' % str(args))
class User(Model):
id = IntegerField('id')
name = StringField("username")
email = StringField('email')
password = StringField('password')
u = User(id=12345, name='Michael', email='[email protected]', password='my-pwd')
u.save()