From 73eca34e0dd9f77b25432303c5320ee1daa16e6b Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Thu, 9 Sep 2010 23:40:06 +0000 Subject: [PATCH] added PriorityCallbacks --- direct/src/showbase/PythonUtil.py | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index 42f3ea8ec6..6015bcad24 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -56,6 +56,7 @@ import ElementTree as ET from HTMLParser import HTMLParser import BpDb import unicodedata +import bisect __report_indent = 3 @@ -4344,6 +4345,76 @@ def encodedUtf8(s): # * -> 8-bit-encoded UTF-8 return unicodeUtf8(s).encode('utf-8') +class PriorityCallbacks: + """ manage a set of prioritized callbacks, and allow them to be invoked in order of priority """ + def __init__(self): + self._callbacks = [] + + def clear(self): + while self._callbacks: + self._callbacks.pop() + + def add(self, callback, priority=None): + if priority is None: + priority = 0 + item = (priority, callback) + bisect.insort(self._callbacks, item) + return item + + def remove(self, item): + self._callbacks.pop(bisect.bisect_left(self._callbacks, item)) + + def __call__(self): + for priority, callback in self._callbacks: + callback() + +if __debug__: + l = [] + def a(l=l): + l.append('a') + def b(l=l): + l.append('b') + def c(l=l): + l.append('c') + pc = PriorityCallbacks() + pc.add(a) + pc() + assert l == ['a'] + while len(l): + l.pop() + bItem = pc.add(b) + pc() + assert 'a' in l + assert 'b' in l + assert len(l) == 2 + while len(l): + l.pop() + pc.remove(bItem) + pc() + assert l == ['a'] + while len(l): + l.pop() + pc.add(c, 2) + bItem = pc.add(b, 10) + pc() + assert l == ['a', 'c', 'b'] + while len(l): + l.pop() + pc.remove(bItem) + pc() + assert l == ['a', 'c'] + while len(l): + l.pop() + pc.clear() + pc() + assert len(l) == 0 + del l + del a + del b + del c + del pc + del bItem + import __builtin__ __builtin__.Functor = Functor __builtin__.Stack = Stack