[pitivi: 3/6] new module: thumbnailcache, with unit test
- From: Edward Hervey <edwardrv src gnome org>
- To: svn-commits-list gnome org
- Subject: [pitivi: 3/6] new module: thumbnailcache, with unit test
- Date: Sun, 8 Mar 2009 08:02:22 -0400 (EDT)
commit 10aea80b5784141d7e0b2d5f06fc8806d4073682
Author: Brandon Lewis <brandon_lewis berkeley edu>
Date: Fri Mar 6 18:11:32 2009 -0800
new module: thumbnailcache, with unit test
---
pitivi/thumbnailcache.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++
tests/test_cache.py | 29 +++++++++++++++++++++
2 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/pitivi/thumbnailcache.py b/pitivi/thumbnailcache.py
new file mode 100644
index 0000000..a7e3fcc
--- /dev/null
+++ b/pitivi/thumbnailcache.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python
+# PiTiVi , Non-linear video editor
+#
+# thumbnailcache.py
+#
+# Copyright (c) 2009, Brandon Lewis (brandon_lewis berkeley edu)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+"""
+Dictionary-Like object for caching of thumbnails.
+"""
+
+import collections
+
+class ThumbnailCache(object):
+
+ """Caches thumbnails by key using LRU policy, implemented with heapq"""
+
+ def __init__(self, size=100):
+ object.__init__(self)
+ self.queue = collections.deque()
+ self.cache = {}
+ self.hits = 0
+ self.misses = 0
+ self.size = size
+
+ def __contains__(self, key):
+ if key in self.cache:
+ self.hits += 1
+ return True
+ self.misses += 1
+ return False
+
+ def __getitem__(self, key):
+ if key in self.cache:
+ # I guess this is why LRU is considered expensive
+ self.queue.remove(key)
+ self.queue.append(key)
+ return self.cache[key]
+ raise KeyError(key)
+
+ def __setitem__(self, key, value):
+ self.cache[key] = value
+ self.queue.append(key)
+ if len(self.cache) > self.size:
+ self.ejectLRU()
+
+ def ejectLRU(self):
+ key = self.queue.popleft()
+ del self.cache[key]
diff --git a/tests/test_cache.py b/tests/test_cache.py
new file mode 100644
index 0000000..bf18606
--- /dev/null
+++ b/tests/test_cache.py
@@ -0,0 +1,29 @@
+import unittest
+import pitivi
+from common import TestCase
+from pitivi.thumbnailcache import ThumbnailCache
+
+class CacheTest(TestCase):
+ """
+ Basic test to create the proper creation of the Pitivi object
+ """
+
+ def testCache(self):
+ c = ThumbnailCache(size=32)
+ for i in xrange(0, 64):
+ c[i] = i
+ assert len(c.cache) == 32
+ assert not 31 in c
+ assert 32 in c
+
+ # touch the LRU item, and then add something to the queue
+ # the item should still remain in the queue
+
+ c[32]
+ c[65] = 65
+
+ assert 32 in c
+ assert not 33 in c
+
+if __name__ == "__main__":
+ unittest.main()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]