forked from bslatkin/effectivepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_48.py
More file actions
executable file
·407 lines (309 loc) · 9.88 KB
/
item_48.py
File metadata and controls
executable file
·407 lines (309 loc) · 9.88 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
#!/usr/bin/env PYTHONHASHSEED=1234 python3
# Copyright 2014-2019 Brett Slatkin, Pearson Education Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Reproduce book environment
import random
random.seed(1234)
import logging
from pprint import pprint
from sys import stdout as STDOUT
# Write all output to a temporary directory
import atexit
import gc
import io
import os
import tempfile
TEST_DIR = tempfile.TemporaryDirectory()
atexit.register(TEST_DIR.cleanup)
# Make sure Windows processes exit cleanly
OLD_CWD = os.getcwd()
atexit.register(lambda: os.chdir(OLD_CWD))
os.chdir(TEST_DIR.name)
def close_open_files():
everything = gc.get_objects()
for obj in everything:
if isinstance(obj, io.IOBase):
obj.close()
atexit.register(close_open_files)
# Example 1
class Meta(type):
def __new__(meta, name, bases, class_dict):
global print
orig_print = print
print(f'* Running {meta}.__new__ for {name}')
print('Bases:', bases)
print = pprint
print(class_dict)
print = orig_print
return type.__new__(meta, name, bases, class_dict) #=>see return type for args =># (method) def __new__(
# cls: Type[Self@__new__],
# __name: str,
# __bases: tuple[type, ...],
# __namespace: dict[str, Any],
# **kwds: Any
# ) -> Self@__new__
class MyClass(metaclass=Meta):#use generic type construct in class arg
stuff = 123
def foo(self):
pass
# * Running <class '__main__.Meta'>.__new__ for MyClass
# Bases: ()
# {'__module__': '__main__',
# '__qualname__': 'MyClass',
# 'foo': <function MyClass.foo at 0x0000019757FC0790>,
# 'stuff': 123}
class MySubclass(MyClass):#use extend object as class arg
other = 567
def bar(self):
pass
# * Running <class '__main__.Meta'>.__new__ for MySubclass
# Bases: (<class '__main__.MyClass'>,)
# {'__module__': '__main__',
# '__qualname__': 'MySubclass',
# 'bar': <function MySubclass.bar at 0x0000019757FC0820>,
# 'other': 567}
#from noconflict import makecls
# Example 2
class ValidatePolygon(type):
def __new__(meta, name, bases, class_dict):
# Only validate subclasses of the Polygon class
if bases:
if class_dict['sides'] < 3:
raise ValueError('Polygons need 3+ sides')
return type.__new__(meta, name, bases, class_dict)
class Polygon(metaclass=ValidatePolygon):
sides = None # Must be specified by subclasses
#@classmethod #TypeError: Polygon.interior_angles() missing 1 required positional argument: 'cls'
@classmethod
def interior_angles(cls):#(method) def interior_angles(cls: Type[Self@Polygon]) -> Any
return (cls.sides - 2) * 180
class Triangle(Polygon):#as first arg for class Polygon with method
sides = 3
class Rectangle(Polygon):
sides = 4
class Nonagon(Polygon):
sides = 9
assert Triangle.interior_angles() == 180
assert Rectangle.interior_angles() == 360
assert Nonagon.interior_angles() == 1260
# Example 3
try:
print('Before class')
class Line(Polygon):#test class with type change and method to deliver var with decorator
print('Before sides')
sides = 2
print('After sides')
print('After class')
except:
logging.exception('Expected')
else:
assert False
# Example 4
class BetterPolygon:
sides = None # Must be specified by subclasses
def __init_subclass__(cls):
super().__init_subclass__()#call parent class
if cls.sides < 3:
raise ValueError('Polygons need 3+ sides')
@classmethod#method to expose var to class
def interior_angles(cls):#get var from outer class as arg
return (cls.sides - 2) * 180
class Hexagon(BetterPolygon):
sides = 6
assert Hexagon.interior_angles() == 720 #->4 times 180
print(Hexagon.interior_angles())#720 ->call without creating instance of class see a= class
# Example 5
try:#ValueError: Polygons need 3+ sides
print('Before class')
class Point(BetterPolygon):#class call for parent class with var
sides = 1
print('After class')
except:
logging.exception('Expected')
else:
assert False
# Example 6
class ValidateFilled(type):
def __new__(meta, name, bases, class_dict):
# Only validate subclasses of the Filled class
if bases:
if class_dict['color'] not in ('red', 'green'):
raise ValueError('Fill color must be supported')
return type.__new__(meta, name, bases, class_dict)
class Filled(metaclass=ValidateFilled):
color = None # Must be specified by subclasses
##---------------------------------
#from six import with_metaclass
# class M_C(M_A, M_B):
# pass
# class C(with_metaclass(M_C, A, B)):
# implement your class here
# class M_A(type):
# pass
# class M_B(type):
# pass
# class A(object):
# __metaclass__=M_A
# class B(object):
# __metaclass__=M_B
# class C(A,B):
# pass
# print(C)
#-----------------------------------
# class polygon(type):
# pass
# class M_B(type):
# pass
# class A(object):
# __metaclass__=M_A
# class B(object):
# __metaclass__=M_B
# class C(A,B):
# pass
# print(C)
# class M_C(M_A, M_B):
# pass
# class C(with_metaclass(M_C, A, B)):
# implement your class here
##----------------------------------
from six import with_metaclass #solves the metaclass confict type/object
# Example 7
class RedPentagon_C(ValidateFilled, ValidatePolygon):
pass
try:
#class RedPentagon(Filled, Polygon):
class RedPentagon(with_metaclass(RedPentagon_C, Filled, Polygon)):
color = 'blue'
sides = 5
except:
logging.exception('Expected')
else:
assert False
try:
#class RedPentagon(Filled, Polygon):
class RedPentagon(ValidateFilled):#classic but part missed so use six module
color = 'blue'
sides = 5
except:
logging.exception('Expected')
else:
assert True
# Example 8
class ValidatePolygon(type):
def __new__(meta, name, bases, class_dict):
# Only validate non-root classes
if not class_dict.get('is_root'):
if class_dict['sides'] < 3:
raise ValueError('Polygons need 3+ sides')
return type.__new__(meta, name, bases, class_dict)
class Polygon(metaclass=ValidatePolygon):
is_root = True
sides = None # Must be specified by subclasses
try:
class RedPentagon(Polygon, Polygon):#obvious ->TypeError: duplicate base class Polygon
color = 'blue'
sides = 5
except:
logging.exception('Expected')
else:
assert False
class ValidateFilledPolygon(ValidatePolygon):
def __new__(meta, name, bases, class_dict):
# Only validate non-root classes
if not class_dict.get('is_root'):
if class_dict['color'] not in ('red', 'green'):
raise ValueError('Fill color must be supported')
return super().__new__(meta, name, bases, class_dict)
class FilledPolygon(Polygon, metaclass=ValidateFilledPolygon):
is_root = True
color = None # Must be specified by subclasses
# Example 9
class GreenPentagon(FilledPolygon):#if only one metaclass can use dunder "new" method
color = 'green'
sides = 5
greenie = GreenPentagon()
assert isinstance(greenie, Polygon)
# Example 10
try:
class OrangePentagon(FilledPolygon):
color = 'orange'#ValueError: Fill color must be supported
sides = 5
except:
logging.exception('Expected')
else:
assert False
# Example 11
try:
class RedLine(FilledPolygon):
color = 'red'#ValueError: Polygons need 3+ sides
sides = 2
except:
logging.exception('Expected')
else:
assert False
# Example 12
class Filled:#use subclass method to avoid metaclass conflicts
color = None # Must be specified by subclasses
def __init_subclass__(cls):
super().__init_subclass__()
if cls.color not in ('red', 'green', 'blue'):
raise ValueError('Fills need a valid color')
# Example 13
class RedTriangle(Filled, BetterPolygon):#multiclass then use dunder subclass method
color = 'red'
sides = 3
ruddy = RedTriangle()
assert isinstance(ruddy, Filled)
assert isinstance(ruddy, BetterPolygon)
# Example 14
try:
print('Before class')
class BlueLine(Filled, BetterPolygon):
color = 'blue'
sides = 2#ValueError: Polygons need 3+ sides
print('After class')
except:
logging.exception('Expected')
else:
assert False
# Example 15
try:
print('Before class')
class BeigeSquare(Filled, BetterPolygon):
color = 'beige'#ValueError: Fills need a valid color
sides = 4
print('After class')
except:
logging.exception('Expected')
else:
assert False
# Example 16
class Top:
def __init_subclass__(cls):
super().__init_subclass__()
print(f'Top for {cls}')
class Left(Top):
def __init_subclass__(cls):
super().__init_subclass__()
print(f'Left for {cls}')#Top for <class '__main__.Left'>
class Right(Top):
def __init_subclass__(cls):
super().__init_subclass__()
print(f'Right for {cls}')#Top for <class '__main__.Right'>
class Bottom(Left, Right):#Fill the stack first left -> top,right ->top, top->start printing order top-right-left
def __init_subclass__(cls):
super().__init_subclass__()
print(f'Bottom for {cls}')#Top for <class '__main__.Bottom'>
# Right for <class '__main__.Bottom'>
# Left for <class '__main__.Bottom'>