def two_step_init(func): """ This is meant to be used on the classes init method, it always call the no argument __init__, method. But if there are any arguments it also calls the 'init' method. This allow an object to either be used with two stage initialization or single. >>> class Obj(object): ... @two_step_init ... def __init__(self): ... print '__init__' ... def init(self, param): ... print 'init' ... self.val = param ... >>> a = Obj() __init__ >>> a.init(1) init >>> b = Obj(1) __init__ init >>> a.val == b.val True """ def new_init(func, self, *args, **kwargs): # Call Class.__init__(self) func(self) # If called with any arguments, call the create method if ((len(args) + len(kwargs)) > 0): self.init(*args, **kwargs) return decorator.decorate(func, new_init, decorator.make_weak_signature(func))
def wrapper(func): func._switch_state = False func._state = start_state def debounce_switch(func, self, key, down, mod_keys): # Button down, and switch not already down if down and not func._switch_state: func._switch_state = True func._state = not func._state func(self, func._state) # toggle off elif not down: func._switch_state = False decorated_func = decorator.decorate(func, debounce_switch, decorator.make_weak_signature(func)) return decorated_func
def wrapper(func): func._switch_state = False func._state = start_state def debounce_switch(func, self, key, down, mod_keys): # Button down, and switch not already down if down and not func._switch_state: func._switch_state = True func._state = not func._state func(self, func._state) # toggle off elif not down: func._switch_state = False decorated_func = decorator.decorate( func, debounce_switch, decorator.make_weak_signature(func)) return decorated_func
def test_signature(self): self.failUnless(decorator.make_weak_signature(bar)[1:3]==( "_decorator__varargs", "_decorator__kwargs")) self.failUnless(weakling(1,2)==2) self.failUnless(new_sig(1,2)==2)