Skip to content

Instantly share code, notes, and snippets.

@clayote
Last active August 29, 2015 14:25
Show Gist options
  • Save clayote/e612e96322f9b287b140 to your computer and use it in GitHub Desktop.
Save clayote/e612e96322f9b287b140 to your computer and use it in GitHub Desktop.
Trying to make a decorator to make triggers lazily
from kivy.event import EventDispatcher
from kivy.clock import Clock
from functools import partial
class trigger(object):
"""Make a trigger from a method outside of your class's __init__."""
def __init__(self, func):
self.func = func
def __get__(self, instance, owner=None):
if instance is None:
# EventDispatcher iterates over its attributes before it instantiates.
# Don't try making any trigger in that case.
return
if not hasattr(self, '_trig'):
self._trig = Clock.create_trigger(
partial(self.func, instance), 0
)
return self._trig
class Salad(EventDispatcher):
def toss(self, *args):
pass
_trigger_toss = trigger(toss)
@trigger
def _trigger_serve(self, *args):
# for when a method should only be usable via trigger
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment