Last active
August 29, 2015 14:25
-
-
Save clayote/e612e96322f9b287b140 to your computer and use it in GitHub Desktop.
Trying to make a decorator to make triggers lazily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.event import EventDispatcher | |
from kivy.clock import Clock | |
# this works fine | |
class Burger(EventDispatcher): | |
def flip(self, *args): | |
pass | |
@property | |
def _trigger_flip(self): | |
if not hasattr(self, '_real_trigger_flip'): | |
self._real_trigger_flip = Clock.create_trigger(self.flip) | |
return self._real_trigger_flip | |
# this keeps triggering the method on unexpected objects, | |
# including many that shouldn't have the method at all, eg. floats | |
def get_trigger(obj, fun): | |
n = '_real_trigger_' + fun.__name__ | |
if not hasattr(obj, n): | |
setattr(obj, n, Clock.create_trigger(fun)) | |
return getattr(obj, n) | |
trigger = lambda fun: property(fget=lambda self: get_trigger(self, fun)) | |
class Fries(EventDispatcher): | |
def fry(self, *args): | |
pass | |
_trigger_fry = trigger(fry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment