From dcaef9506ee87358cf427fa235894c7e17089dbe Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Fri, 4 Jun 2021 14:48:28 +0200 Subject: [PATCH] avoid adding the append callback twice if the same source is added twice --- kabuki/cacher.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/kabuki/cacher.py b/kabuki/cacher.py index ff07a43..539b1eb 100644 --- a/kabuki/cacher.py +++ b/kabuki/cacher.py @@ -7,28 +7,26 @@ class Cacher: def __init__(self): self.caches = {} + def add_source(self, src, size=100): name = src.name if not src.is_connected: raise RuntimeError(f"source NOT connected: {name}") - cache = self.add_cache(name, size=size) - src.add_callback(cache.callback_append) - return cache - def add_cache(self, name, size=100): - # key contains size in case multiple caches per name with different sizes are needed - key = (name, size) caches = self.caches try: - return caches[key] + return caches[name] except KeyError: - caches[key] = cache = Cache(size=size) + caches[name] = cache = Cache(size=size) + src.add_callback(cache.callback_append) return cache + def __repr__(self): return repr(self.caches) + class Cache: def __init__(self, size=100):