-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmod_02_data_model.py
522 lines (407 loc) · 15.4 KB
/
mod_02_data_model.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#-*- coding: utf-8 -*-
###
# MODULE 02: Classes: new-style vs. old-style, data model & customization
###
# Let's create a class
class MyOldClass:
def print_instance_class(self):
print type(self)
print self.__class__
old_inst = MyOldClass()
old_inst.print_instance_class()
# It looks incoherent. Why?
# Let's create a new-style class
class MyNewClass(object):
def print_instance_class(self):
print type(self)
print self.__class__
new_inst = MyNewClass()
new_inst.print_instance_class()
#===============================================================================
# - New-style classes introduced in Python 2.2 to unify classes and types
# - Provide unified object model with a full meta-model
# - Other benefits: subclass most built-in types, descriptors (slots, properties, static and class methods)...
# - By default all classes are old-style until Python 3
# - In Python 2 you have to inherit from 'object' to use new-style
# - You must avoid old-style
#
# - Other changes introduced Python 2.2: __new__, new dir() behavior, metaclasses, new MRO (also in 2.3)
#
# - More info: http://www.python.org/doc/newstyle/
#===============================================================================
# Let's inherit from an old-style class
class MyNewOldClass(MyOldClass):
pass
new_old_inst = MyNewOldClass()
new_old_inst.print_instance_class()
# Let's inherit from an old-style class and 'object'
class MyGoodNewOldClass(MyOldClass, object):
pass
good_new_old_inst = MyGoodNewOldClass()
good_new_old_inst.print_instance_class()
#===============================================================================
# - You should inherit from both old-style classes and 'object' to have new-style classes
#===============================================================================
# Let's create a fractions class
class MyFraction(object):
def __init__(self, numerator, denominator):
self.num = int(numerator)
self.den = int(denominator)
# Let's instantiate a fraction
fract1 = MyFraction(5, 2)
print fract1
print repr(fract1)
# <__console__.MyFraction object at 0xWHATEVER> does not look useful, right?
# Let's customise our class representation
class MyFraction(object):
def __init__(self, numerator, denominator):
self.num = int(numerator)
self.den = int(denominator)
def __str__(self):
'''Called by the str() built-in function and by the 'print' statement
to compute the "informal" string representation of an object
'''
return "{0} / {1}".format(self.num, self.den)
def __repr__(self):
'''Called by the repr() built-in function and by string conversions
(reverse quotes) to compute the "official" string representation of an object
'''
return "{0}({1}, {2})".format(self.__class__.__name__, self.num, self.den)
# Let's instantiate again
fract1 = MyFraction(5, 2)
print fract1
print repr(fract1)
#===============================================================================
# - There are special method names to customise your classes behavior
# - Python invokes these methods (if present) when special syntax is executed
# - Instatiation and object creation
# - Representation
# - Rich comparison
# - Arithmetic operations
# - Attribute access
# - Container types emulation
# - Context managers emulation
# - Callable objects emulation
#
# - http://docs.python.org/2.7/reference/datamodel.html#basic-customization
#===============================================================================
# Let's customise our class rich comparison
class MyFraction(object):
def __init__(self, numerator, denominator):
self.num = int(numerator)
self.den = int(denominator)
def value(self):
return float(self.num) / self.den
def __lt__(self, other):
'''Called to implement evaluation of self < other
'''
try:
return self.value() < other.value()
except AttributeError:
return self.value() < other
def __le__(self, other):
'''Called to implement evaluation of self <= other
'''
try:
return self.value() <= other.value()
except AttributeError:
return self.value() <= other
def __eq__(self, other):
'''Called to implement evaluation of self == other
'''
try:
return self.value() == other.value()
except AttributeError:
return self.value() == other
def __ne__(self, other):
'''Called to implement evaluation of self != other
'''
try:
return self.value() != other.value()
except AttributeError:
return self.value() != other
fract1 = MyFraction(5, 2) # 2.5
fract2 = MyFraction(3, 2) # 1.5
fract3 = MyFraction(25, 10) # 2.5
print fract1 != fract3 # 2.5 != 2.5
print fract1 == fract3 # 2.5 == 2.5
print fract2 < fract3 # 1.5 < 2.5
# Let's try the other way
print fract1 >= fract2 # 2.5 >= 1.5
print fract2 >= fract3 # 1.5 >= 2.5
# Let's try with other types
print fract1 >= 2 # 2.5 >= 2
print fract2 != 1.5 # 1.5 != 1.5
# Let's try the other way with other types
print 2 <= fract1 # 2 <= 2.5
print 1.5 != fract2 # 1.5 != 1.5
#===============================================================================
# - You don't have to define all the possible methods, Python can take the opposite
# - Python will try the opposite when a comparison method of a type raises TypeError
# - Python 2.X could try to coerce, but this behaviour has been removed in Py3k
# - It's up to you to implement compatibility with other types
#===============================================================================
# Let's try again
print 10 > fract1 # 10 > 2.5
print 10 < fract1 # 10 < 2.5
print fract1 < 10 # 2.5 < 10
print fract1 > 10 # 2.5 > 10
# Let's define all comparison methods
class MyFraction(object):
def __init__(self, numerator, denominator):
self.num = int(numerator)
self.den = int(denominator)
def value(self):
return float(self.num) / self.den
def __lt__(self, other):
try:
return self.value() < other.value()
except AttributeError:
return self.value() < other
def __le__(self, other):
try:
return self.value() <= other.value()
except AttributeError:
return self.value() <= other
def __eq__(self, other):
try:
return self.value() == other.value()
except AttributeError:
return self.value() == other
def __ne__(self, other):
try:
return self.value() != other.value()
except AttributeError:
return self.value() != other
def __gt__(self, other):
try:
return self.value() > other.value()
except AttributeError:
return self.value() > other
def __ge__(self, other):
try:
return self.value() >= other.value()
except AttributeError:
return self.value() >= other
fract1 = MyFraction(5, 2) # 2.5
print 10 > fract1 # 10 > 2.5
print 10 < fract1 # 10 < 2.5
print fract1 < 10 # 2.5 < 10
print fract1 > 10 # 2.5 > 10
#===============================================================================
# - You don't have to define all the possible methods, Python can take the opposite
# - But you should do it to fully support other types!!
# - Python will try the opposite when a comparison method of a type raises TypeError
# - Python 2.X could try to coerce, but this behaviour has been removed in Py3k
# - It's up to you to implement compatibility with other types
#===============================================================================
# Let's emulate a container
class MyFraction(object):
def __init__(self, numerator, denominator):
self.num = int(numerator)
self.den = int(denominator)
def __str__(self):
return "{0} / {1}".format(self.num, self.den)
def __len__(self):
'''Called to implement the built-in function len()
'''
return 2
def __getitem__(self, key):
'''Called to implement evaluation of self[key]
'''
if key == 0 or key == 'num':
return self.num
elif key == 1 or key == 'den':
return self.den
else:
raise KeyError(key)
def __setitem__(self, key, value):
'''Called to implement assignment of self[key] = value
'''
if key == 0 or key == 'num':
self.num = value
elif key == 1 or key == 'den':
self.den = value
else:
raise KeyError(key)
f1 = MyFraction(7, 2)
print len(f1)
print f1['num'], "/", f1[1]
f1[0] = 5
f1['den'] = 3
print f1
#===============================================================================
# More info on emulating container types:
# - http://docs.python.org/2.7/reference/datamodel.html#emulating-container-types
#===============================================================================
# Let's emulate numeric types
class MyFraction(object):
def __init__(self, numerator, denominator):
self.num = int(numerator)
self.den = int(denominator)
def __str__(self):
return "{0} / {1}".format(self.num, self.den)
def __add__(self, other):
'''Called to implement the binary arithmetic operation self + other
'''
try:
if self.den == other.den:
return MyFraction(self.num + other.num, self.den)
else:
return MyFraction(self.num * other.den + other.num * self.den, self.den * other.den)
except AttributeError:
return MyFraction(self.num + other * self.den, self.den)
__radd__ = __add__ # Called to implement the binary arithmetic operation other + self
__iadd__ = __add__ # Called to implement the binary arithmetic operation self += other
fract1 = MyFraction(5, 3)
fract2 = MyFraction(2, 3)
print fract1 + fract2
print fract1 + 5
print 3 + fract1
fract2 += fract2
print fract2
#===============================================================================
# More info on emulating numeric types:
# - http://docs.python.org/2.7/reference/datamodel.html#emulating-numeric-types
#===============================================================================
# Let's customise dicts behaviour
class AttrDict(dict):
def __getattr__(self, name):
'''Called when an attribute lookup has NOT FOUND the attribute in the usual places
'''
try:
return self[name]
except KeyError, e:
raise AttributeError(e)
def __setattr__(self, name, value):
'''Called when an attribute assignment is attempted
'''
# self.__setitem__(key, value)
self[name] = value
def __delattr__(self, name):
'''Called when an attribute deletion is attempted
'''
del self[name]
d = dict(zip("abcde", range(1, 6)))
attr_d = AttrDict(d)
print attr_d
attr_d.f = 6
print attr_d.f
print attr_d
del attr_d.f
print attr_d
#===============================================================================
# - Thanks to new-style classes you can customise basic types
# - Be careful!
#
# More info on emulating container types:
# - http://docs.python.org/2.7/reference/datamodel.html#customizing-attribute-access
#===============================================================================
#===============================================================================
# - And yet more customisation methods:
#
# - Hashing (i.e. use as dict key) and truth value testing
# - __hash__ and __nonzero__
#
# - Objects copy and deepcopy
# - __copy__ and __deepcopy__
#
# - Slicing
# - __getslice__ (deprecated by __getitem__), __setslice__
#
# - Iterator protocol
# - __iter__, __reversed__, next
#
# - Context managers emulation
# - __enter__, __exit__
#
# - Callable objects
# - __call__
#
# - Descriptors and slots
# - __get__, __set__, __delete__, __slots__
#
# - Instance and subclass checking
# - __instancecheck__, __subclasscheck__
#===============================================================================
##===============================================================================
##===============================================================================
## TIME TO START WORKING!
##
## EXERCISE 2:
## - Solve old-style class inheritance issue in CustomOptionParser
## - Implement slicing and + and - operators in CustomOrderedDict
## - Modify AttrDict to access the dictionary only if key already exists (otherwise act as normal attributes)
## - http://docs.python.org/2.7/reference/datamodel.html
##
## INSTRUCTIONS:
## - Go to exercises/exercise_2 and edit exercise_2.py
## - Change the functions and class implementation to let tests_2.py pass
## - Check tests executing 'nosetests -sv'
##===============================================================================
##===============================================================================
# Wrong old-style class inheritance
from optparse import OptionParser
class CustomOptionParser(OptionParser):
def __str__(self):
return self.__class__.__name__
# Right old-style class inheritance
class CustomOptionParser(OptionParser, object):
def __str__(self):
return self.__class__.__name__
inst = CustomOptionParser()
print inst.__class__, type(inst)
# CustomOrderedDict with slicing and + and - operators
from collections import OrderedDict
class CustomOrderedDict(OrderedDict):
def __getitem__(self, key):
if isinstance(key, slice):
return self.__class__(self.items()[key])
return OrderedDict.__getitem__(self, key)
def __add__(self, other):
res = self.__class__(self)
res.update(other)
return res
def __sub__(self, other):
res = self.__class__(self)
[res.pop(k, None) for k in other]
return res
cod_inst = CustomOrderedDict(zip("abcde", range(1, 6)))
print cod_inst
print cod_inst[0:3] + cod_inst[4:10]
# AttrDict accessing the dict only if key already exists
class AttrDict(dict):
def __getattr__(self, name):
try:
return self[name]
except KeyError, e:
raise AttributeError(e)
def __setattr__(self, name, value):
if name in self:
self[name] = value
else:
# self.__dict__[name] = value # Perform action instead of delegating
dict.__setattr__(self, name, value) # Always call the method of an ancestor!
def __delattr__(self, name):
if name in self:
del self[name]
else:
# del self.__dict__[name] # Perform action instead of delegating
dict.__delattr__(self, name)
ad_inst = AttrDict(zip("abcde", range(1, 6)))
print ad_inst
ad_inst.f = 6
ad_inst.a = 0
del ad_inst.b
print ad_inst
print ad_inst.f
#===============================================================================
# WARNING!
# - As we will see in next examples, this implementation can be improved
#===============================================================================
#===============================================================================
# MORE INFO:
# - http://docs.python.org/2.7/reference/datamodel.html#special-method-names
# - http://www.python.org/doc/newstyle/
# - http://www.python.org/download/releases/2.2.3/descrintro/
#===============================================================================