1
2
3
5 """
6 Use this as a metaclass for types to set the magic attribute '__super' on
7 them. When this is accessed through self the result is an object
8 constructed as if super(ThisType, self) had been called. (Makes it a little
9 bit more like in Python 3, where super() will work like this attribute.)
10 """
11 - def __init__(cls, name, bases, members):
14
15
16
17
18
20 data = dict(zip(obj.__slots__, args))
21 data.update(kwargs)
22 for a in obj.__slots__:
23 setattr(obj, a, data.get(a, None))
24
26 """
27 Base class for making "struct" types with members defined by their
28 __slots__ value. Example:
29
30 >>> class Item(SlotStruct):
31 ... __slots__ = 'first', 'last'
32
33 >>> item1 = Item(first='a', last='b')
34 >>> item1.first, item1.last
35 ('a', 'b')
36
37 >>> item1.middle = '-'
38 Traceback (most recent call last):
39 ...
40 AttributeError: 'Item' object has no attribute 'middle'
41
42 >>> item2 = Item('a', 'b')
43 >>> assert (item1.first, item1.last) == (item2.first, item2.last)
44
45 >>> assert item1.make_dict() == {'first': 'a', 'last': 'b'}
46
47 """
48 __slots__ = ()
52 return "%s instance with %s" % (type(self).__name__, str(self.make_dict()))
54 return dict([(a, getattr(self, a)) for a in self.__slots__])
55
56
57
58
59
61 attr = '_%s' % fset.func_name
62 return property(lambda self: getattr(self, attr),
63 fset, doc=fset.__doc__)
64
65
66
67
68
69 -def call(caller, *args):
70 def decorator(decorated):
71 callArgs = [(arg, decorated)[arg is call] for arg in args]
72 caller(*callArgs)
73 return call
74 return decorator
75
76
77
78
79
81 """
82 A documentation helper. Usage:
83
84 >>> contract = BooleanDecorator('interface')
85 >>> class T:
86 ... @contract.interface
87 ... def action(self): raise NotImplementedError
88 ...
89 >>> assert T.action.interface == True
90 """
91
92 __slots__ = ('attributes')
93
96
98 if name not in self.attributes:
99 raise Exception("Unknown attribute: %s" % name)
100 def decorator(func):
101 setattr(func, name, True)
102 return func
103 return decorator
104
105 contract = BooleanDecorator('template_method', 'default_method', 'helper', 'state_change')
106
107
109 """
110 Generic decorator for setting attributes. Usage:
111
112 >>> @attrs(value="A value", thing=None, other=True)
113 ... def f(): pass
114 ...
115 >>> assert f.value == "A value"
116 >>> assert f.thing is None
117 >>> assert f.other is True
118 """
119 def decorator(func):
120
121 for key, value in kwargs.items():
122 setattr(func, key, value)
123 return func
124 return decorator
125