From 4570b749eb05152512a6dcdc2e88715d9902cb87 Mon Sep 17 00:00:00 2001 From: dobesv Date: Thu, 17 Apr 2014 14:16:32 -0700 Subject: Add "get" method to match dict API. --- pylru.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pylru.py b/pylru.py index 3de5cb0..13aba67 100644 --- a/pylru.py +++ b/pylru.py @@ -1,5 +1,4 @@ - # Cache implementaion with a Least Recently Used (LRU) replacement policy and # a basic dictionary interface. @@ -100,6 +99,10 @@ class lrucache(object): # Return the value. return node.value + def get(self, key, default=None): + """Get an item - return default (None) if not present""" + try: return self[key] + except KeyError: return default def __setitem__(self, key, value): # First, see if any value is stored under 'key' in the cache already. -- cgit v1.2.1