Package oort :: Package util :: Module code
[hide private]
[frames] | no frames]

Source Code for Module oort.util.code

  1  #======================================================================= 
  2   
  3   
4 -class autosuper(type):
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):
12 super(autosuper, cls).__init__(name, bases, members) 13 setattr(cls, "_%s__super" % name, super(cls))
14 15 16 #======================================================================= 17 18
19 -def init_slots(obj, *args, **kwargs):
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
25 -class SlotStruct(object):
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__ = ()
49 - def __init__(self, *args, **kwargs):
50 init_slots(self, *args, **kwargs)
51 - def __str__(self):
52 return "%s instance with %s" % (type(self).__name__, str(self.make_dict()))
53 - def make_dict(self):
54 return dict([(a, getattr(self, a)) for a in self.__slots__])
55 56 57 #======================================================================= 58 59
60 -def property_with_setter(fset):
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
80 -class BooleanDecorator(object):
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
94 - def __init__(self, *args):
95 self.attributes = args
96
97 - def __getattr__(self, name):
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
108 -def attrs(**kwargs):
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 # TODO: or just func.__dict__.update(kwargs) 121 for key, value in kwargs.items(): 122 setattr(func, key, value) 123 return func
124 return decorator 125