1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | Support Eiffel-style preconditions and postconditions for functions. |
---|
5 | n/a | |
---|
6 | n/a | An example for Python metaclasses. |
---|
7 | n/a | """ |
---|
8 | n/a | |
---|
9 | n/a | import unittest |
---|
10 | n/a | from types import FunctionType as function |
---|
11 | n/a | |
---|
12 | n/a | class EiffelBaseMetaClass(type): |
---|
13 | n/a | |
---|
14 | n/a | def __new__(meta, name, bases, dict): |
---|
15 | n/a | meta.convert_methods(dict) |
---|
16 | n/a | return super(EiffelBaseMetaClass, meta).__new__( |
---|
17 | n/a | meta, name, bases, dict) |
---|
18 | n/a | |
---|
19 | n/a | @classmethod |
---|
20 | n/a | def convert_methods(cls, dict): |
---|
21 | n/a | """Replace functions in dict with EiffelMethod wrappers. |
---|
22 | n/a | |
---|
23 | n/a | The dict is modified in place. |
---|
24 | n/a | |
---|
25 | n/a | If a method ends in _pre or _post, it is removed from the dict |
---|
26 | n/a | regardless of whether there is a corresponding method. |
---|
27 | n/a | """ |
---|
28 | n/a | # find methods with pre or post conditions |
---|
29 | n/a | methods = [] |
---|
30 | n/a | for k, v in dict.items(): |
---|
31 | n/a | if k.endswith('_pre') or k.endswith('_post'): |
---|
32 | n/a | assert isinstance(v, function) |
---|
33 | n/a | elif isinstance(v, function): |
---|
34 | n/a | methods.append(k) |
---|
35 | n/a | for m in methods: |
---|
36 | n/a | pre = dict.get("%s_pre" % m) |
---|
37 | n/a | post = dict.get("%s_post" % m) |
---|
38 | n/a | if pre or post: |
---|
39 | n/a | dict[m] = cls.make_eiffel_method(dict[m], pre, post) |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class EiffelMetaClass1(EiffelBaseMetaClass): |
---|
43 | n/a | # an implementation of the "eiffel" meta class that uses nested functions |
---|
44 | n/a | |
---|
45 | n/a | @staticmethod |
---|
46 | n/a | def make_eiffel_method(func, pre, post): |
---|
47 | n/a | def method(self, *args, **kwargs): |
---|
48 | n/a | if pre: |
---|
49 | n/a | pre(self, *args, **kwargs) |
---|
50 | n/a | rv = func(self, *args, **kwargs) |
---|
51 | n/a | if post: |
---|
52 | n/a | post(self, rv, *args, **kwargs) |
---|
53 | n/a | return rv |
---|
54 | n/a | |
---|
55 | n/a | if func.__doc__: |
---|
56 | n/a | method.__doc__ = func.__doc__ |
---|
57 | n/a | |
---|
58 | n/a | return method |
---|
59 | n/a | |
---|
60 | n/a | |
---|
61 | n/a | class EiffelMethodWrapper: |
---|
62 | n/a | |
---|
63 | n/a | def __init__(self, inst, descr): |
---|
64 | n/a | self._inst = inst |
---|
65 | n/a | self._descr = descr |
---|
66 | n/a | |
---|
67 | n/a | def __call__(self, *args, **kwargs): |
---|
68 | n/a | return self._descr.callmethod(self._inst, args, kwargs) |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class EiffelDescriptor: |
---|
72 | n/a | |
---|
73 | n/a | def __init__(self, func, pre, post): |
---|
74 | n/a | self._func = func |
---|
75 | n/a | self._pre = pre |
---|
76 | n/a | self._post = post |
---|
77 | n/a | |
---|
78 | n/a | self.__name__ = func.__name__ |
---|
79 | n/a | self.__doc__ = func.__doc__ |
---|
80 | n/a | |
---|
81 | n/a | def __get__(self, obj, cls): |
---|
82 | n/a | return EiffelMethodWrapper(obj, self) |
---|
83 | n/a | |
---|
84 | n/a | def callmethod(self, inst, args, kwargs): |
---|
85 | n/a | if self._pre: |
---|
86 | n/a | self._pre(inst, *args, **kwargs) |
---|
87 | n/a | x = self._func(inst, *args, **kwargs) |
---|
88 | n/a | if self._post: |
---|
89 | n/a | self._post(inst, x, *args, **kwargs) |
---|
90 | n/a | return x |
---|
91 | n/a | |
---|
92 | n/a | |
---|
93 | n/a | class EiffelMetaClass2(EiffelBaseMetaClass): |
---|
94 | n/a | # an implementation of the "eiffel" meta class that uses descriptors |
---|
95 | n/a | |
---|
96 | n/a | make_eiffel_method = EiffelDescriptor |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | class Tests(unittest.TestCase): |
---|
100 | n/a | |
---|
101 | n/a | def testEiffelMetaClass1(self): |
---|
102 | n/a | self._test(EiffelMetaClass1) |
---|
103 | n/a | |
---|
104 | n/a | def testEiffelMetaClass2(self): |
---|
105 | n/a | self._test(EiffelMetaClass2) |
---|
106 | n/a | |
---|
107 | n/a | def _test(self, metaclass): |
---|
108 | n/a | class Eiffel(metaclass=metaclass): |
---|
109 | n/a | pass |
---|
110 | n/a | |
---|
111 | n/a | class Test(Eiffel): |
---|
112 | n/a | def m(self, arg): |
---|
113 | n/a | """Make it a little larger""" |
---|
114 | n/a | return arg + 1 |
---|
115 | n/a | |
---|
116 | n/a | def m2(self, arg): |
---|
117 | n/a | """Make it a little larger""" |
---|
118 | n/a | return arg + 1 |
---|
119 | n/a | |
---|
120 | n/a | def m2_pre(self, arg): |
---|
121 | n/a | assert arg > 0 |
---|
122 | n/a | |
---|
123 | n/a | def m2_post(self, result, arg): |
---|
124 | n/a | assert result > arg |
---|
125 | n/a | |
---|
126 | n/a | class Sub(Test): |
---|
127 | n/a | def m2(self, arg): |
---|
128 | n/a | return arg**2 |
---|
129 | n/a | |
---|
130 | n/a | def m2_post(self, Result, arg): |
---|
131 | n/a | super(Sub, self).m2_post(Result, arg) |
---|
132 | n/a | assert Result < 100 |
---|
133 | n/a | |
---|
134 | n/a | t = Test() |
---|
135 | n/a | self.assertEqual(t.m(1), 2) |
---|
136 | n/a | self.assertEqual(t.m2(1), 2) |
---|
137 | n/a | self.assertRaises(AssertionError, t.m2, 0) |
---|
138 | n/a | |
---|
139 | n/a | s = Sub() |
---|
140 | n/a | self.assertRaises(AssertionError, s.m2, 1) |
---|
141 | n/a | self.assertRaises(AssertionError, s.m2, 10) |
---|
142 | n/a | self.assertEqual(s.m2(5), 25) |
---|
143 | n/a | |
---|
144 | n/a | |
---|
145 | n/a | if __name__ == "__main__": |
---|
146 | n/a | unittest.main() |
---|