Skip to content

Commit 2e064d6

Browse files
committed
ch25: simple enum metaclasses
1 parent 2f8bf06 commit 2e064d6

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

25-class-metaprog/evalsupport.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# tag::BEGINNING[]
2+
print('<[100]> evalsupport module start')
3+
4+
def deco_alpha(cls):
5+
print('<[200]> deco_alpha')
6+
7+
def inner_1(self):
8+
print('<[300]> deco_alpha:inner_1')
9+
10+
cls.method_y = inner_1
11+
return cls
12+
13+
# end::BEGINNING[]
14+
# tag::META_ALEPH[]
15+
class MetaAleph(type):
16+
print('<[400]> MetaAleph body')
17+
18+
def __init__(cls, name, bases, dic):
19+
print('<[500]> MetaAleph.__init__')
20+
21+
def inner_2(self):
22+
print('<[600]> MetaAleph.__init__:inner_2')
23+
24+
cls.method_z = inner_2
25+
26+
# end::META_ALEPH[]
27+
# tag::END[]
28+
print('<[700]> evalsupport module end')
29+
# end::END[]

25-class-metaprog/evaltime.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from evalsupport import deco_alpha
2+
3+
print('<[1]> evaltime module start')
4+
5+
6+
class ClassOne():
7+
print('<[2]> ClassOne body')
8+
9+
def __init__(self):
10+
print('<[3]> ClassOne.__init__')
11+
12+
def __del__(self):
13+
print('<[4]> ClassOne.__del__')
14+
15+
def method_x(self):
16+
print('<[5]> ClassOne.method_x')
17+
18+
class ClassTwo(object):
19+
print('<[6]> ClassTwo body')
20+
21+
22+
@deco_alpha
23+
class ClassThree():
24+
print('<[7]> ClassThree body')
25+
26+
def method_y(self):
27+
print('<[8]> ClassThree.method_y')
28+
29+
30+
class ClassFour(ClassThree):
31+
print('<[9]> ClassFour body')
32+
33+
def method_y(self):
34+
print('<[10]> ClassFour.method_y')
35+
36+
37+
if __name__ == '__main__':
38+
print('<[11]> ClassOne tests', 30 * '.')
39+
one = ClassOne()
40+
one.method_x()
41+
print('<[12]> ClassThree tests', 30 * '.')
42+
three = ClassThree()
43+
three.method_y()
44+
print('<[13]> ClassFour tests', 30 * '.')
45+
four = ClassFour()
46+
four.method_y()
47+
48+
49+
print('<[14]> evaltime module end')

25-class-metaprog/evaltime_meta.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from evalsupport import deco_alpha
2+
from evalsupport import MetaAleph
3+
4+
print('<[1]> evaltime_meta module start')
5+
6+
7+
@deco_alpha
8+
class ClassThree():
9+
print('<[2]> ClassThree body')
10+
11+
def method_y(self):
12+
print('<[3]> ClassThree.method_y')
13+
14+
15+
class ClassFour(ClassThree):
16+
print('<[4]> ClassFour body')
17+
18+
def method_y(self):
19+
print('<[5]> ClassFour.method_y')
20+
21+
22+
class ClassFive(metaclass=MetaAleph):
23+
print('<[6]> ClassFive body')
24+
25+
def __init__(self):
26+
print('<[7]> ClassFive.__init__')
27+
28+
def method_z(self):
29+
print('<[8]> ClassFive.method_z')
30+
31+
32+
class ClassSix(ClassFive):
33+
print('<[9]> ClassSix body')
34+
35+
def method_z(self):
36+
print('<[10]> ClassSix.method_z')
37+
38+
39+
if __name__ == '__main__':
40+
print('<[11]> ClassThree tests', 30 * '.')
41+
three = ClassThree()
42+
three.method_y()
43+
print('<[12]> ClassFour tests', 30 * '.')
44+
four = ClassFour()
45+
four.method_y()
46+
print('<[13]> ClassFive tests', 30 * '.')
47+
five = ClassFive()
48+
five.method_z()
49+
print('<[14]> ClassSix tests', 30 * '.')
50+
six = ClassSix()
51+
six.method_z()
52+
53+
print('<[15]> evaltime_meta module end')

25-class-metaprog/factories.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
record_factory: create simple classes just for holding data fields
3+
4+
# tag::RECORD_FACTORY_DEMO[]
5+
>>> Dog = record_factory('Dog', 'name weight owner') # <1>
6+
>>> rex = Dog('Rex', 30, 'Bob')
7+
>>> rex # <2>
8+
Dog(name='Rex', weight=30, owner='Bob')
9+
>>> name, weight, _ = rex # <3>
10+
>>> name, weight
11+
('Rex', 30)
12+
>>> "{2}'s dog weighs {1}kg".format(*rex) # <4>
13+
"Bob's dog weighs 30kg"
14+
>>> rex.weight = 32 # <5>
15+
>>> rex
16+
Dog(name='Rex', weight=32, owner='Bob')
17+
>>> Dog.__mro__ # <6>
18+
(<class 'factories.Dog'>, <class 'object'>)
19+
20+
# end::RECORD_FACTORY_DEMO[]
21+
22+
The factory also accepts a list or tuple of identifiers:
23+
24+
>>> Dog = record_factory('Dog', ['name', 'weight', 'owner'])
25+
>>> Dog.__slots__
26+
('name', 'weight', 'owner')
27+
28+
"""
29+
30+
# tag::RECORD_FACTORY[]
31+
def record_factory(cls_name, field_names):
32+
try:
33+
field_names = field_names.replace(',', ' ').split() # <1>
34+
except AttributeError: # no .replace or .split
35+
pass # assume it's already a sequence of strings
36+
field_names = tuple(field_names) # <2>
37+
if not all(s.isidentifier() for s in field_names):
38+
raise ValueError('field_names must all be valid identifiers')
39+
40+
def __init__(self, *args, **kwargs): # <3>
41+
attrs = dict(zip(self.__slots__, args))
42+
attrs.update(kwargs)
43+
for name, value in attrs.items():
44+
setattr(self, name, value)
45+
46+
def __iter__(self): # <4>
47+
for name in self.__slots__:
48+
yield getattr(self, name)
49+
50+
def __repr__(self): # <5>
51+
values = ', '.join('{}={!r}'.format(*i) for i
52+
in zip(self.__slots__, self))
53+
return '{}({})'.format(self.__class__.__name__, values)
54+
55+
cls_attrs = dict(__slots__ = field_names, # <6>
56+
__init__ = __init__,
57+
__iter__ = __iter__,
58+
__repr__ = __repr__)
59+
60+
return type(cls_name, (object,), cls_attrs) # <7>
61+
# end::RECORD_FACTORY[]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Testing ``AutoFillDict``::
3+
4+
>>> adict = AutoFillDict()
5+
>>> len(adict)
6+
0
7+
>>> adict['first']
8+
0
9+
>>> adict
10+
{'first': 0}
11+
>>> adict['second']
12+
1
13+
>>> adict['third']
14+
2
15+
>>> len(adict)
16+
3
17+
>>> adict
18+
{'first': 0, 'second': 1, 'third': 2}
19+
>>> adict['__magic__']
20+
Traceback (most recent call last):
21+
...
22+
KeyError: '__magic__'
23+
24+
Testing ``MicroEnum``::
25+
26+
>>> class Flavor(MicroEnum):
27+
... cocoa
28+
... coconut
29+
... vanilla
30+
>>> Flavor.cocoa, Flavor.vanilla
31+
(0, 2)
32+
>>> Flavor[1]
33+
'coconut'
34+
"""
35+
36+
37+
class AutoFillDict(dict):
38+
def __init__(self, *args, **kwargs):
39+
super().__init__(*args, **kwargs)
40+
self.__next_value = 0
41+
42+
def __missing__(self, key):
43+
if key.startswith('__') and key.endswith('__'):
44+
raise KeyError(key)
45+
self[key] = value = self.__next_value
46+
self.__next_value += 1
47+
return value
48+
49+
50+
class MicroEnumMeta(type):
51+
def __prepare__(name, bases, **kwargs):
52+
return AutoFillDict()
53+
54+
def __getitem__(cls, key):
55+
for k, v in cls.__dict__.items():
56+
if v == key:
57+
return k
58+
raise KeyError(key)
59+
60+
61+
class MicroEnum(metaclass=MicroEnumMeta):
62+
pass
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Testing ``Flavor``::
3+
4+
>>> Flavor.cocoa, Flavor.coconut, Flavor.vanilla
5+
(0, 1, 2)
6+
>>> Flavor[1]
7+
'coconut'
8+
9+
"""
10+
11+
from microenum import MicroEnum
12+
13+
14+
class Flavor(MicroEnum):
15+
cocoa
16+
coconut
17+
vanilla
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Testing ``KeyIsValueDict``::
3+
4+
>>> adict = KeyIsValueDict()
5+
>>> len(adict)
6+
0
7+
>>> adict['first']
8+
'first'
9+
>>> adict
10+
{'first': 'first'}
11+
>>> adict['second']
12+
'second'
13+
>>> len(adict)
14+
2
15+
>>> adict
16+
{'first': 'first', 'second': 'second'}
17+
>>> adict['__magic__']
18+
Traceback (most recent call last):
19+
...
20+
KeyError: '__magic__'
21+
"""
22+
23+
24+
class KeyIsValueDict(dict):
25+
26+
def __missing__(self, key):
27+
if key.startswith('__') and key.endswith('__'):
28+
raise KeyError(key)
29+
self[key] = key
30+
return key
31+
32+
33+
class NanoEnumMeta(type):
34+
def __prepare__(name, bases, **kwargs):
35+
return KeyIsValueDict()
36+
37+
38+
class NanoEnum(metaclass=NanoEnumMeta):
39+
pass
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Testing ``Flavor``::
3+
4+
>>> Flavor.coconut
5+
'coconut'
6+
>>> Flavor.cocoa, Flavor.vanilla
7+
('cocoa', 'vanilla')
8+
9+
"""
10+
11+
from nanoenum import NanoEnum
12+
13+
14+
class Flavor(NanoEnum):
15+
cocoa
16+
coconut
17+
vanilla

0 commit comments

Comments
 (0)