summaryrefslogtreecommitdiff
path: root/src/pyparsing.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2016-07-27 07:07:08 +0000
committerPaul McGuire <ptmcg@austin.rr.com>2016-07-27 07:07:08 +0000
commitc9d8d1d9268d5decd109757c575439dc95273563 (patch)
tree155611e33bbd1470be8f981dbc8e7ee53ef9414a /src/pyparsing.py
parent014b91b1ffd48039427e457de7a8afadd320eadf (diff)
downloadpyparsing-git-c9d8d1d9268d5decd109757c575439dc95273563.tar.gz
_TTLCache logic cleanup, add get() method similar to dict.get
Diffstat (limited to 'src/pyparsing.py')
-rw-r--r--src/pyparsing.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/pyparsing.py b/src/pyparsing.py
index e68e3b0..c8d3f68 100644
--- a/src/pyparsing.py
+++ b/src/pyparsing.py
@@ -58,7 +58,7 @@ The pyparsing module handles some of the problems that are typically vexing when
"""
__version__ = "2.1.6"
-__versionTime__ = "26 Jul 2016 23:50 UTC"
+__versionTime__ = "27 Jul 2016 06:06 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -892,7 +892,10 @@ class _TTLCache(object):
def __len__(self):
return len(self.lookup)
- def __getitem__(self, key, default=None):
+ def __getitem__(self, key):
+ return self.lookup.get(key)
+
+ def get(self, key, default=None):
return self.lookup.get(key, default)
def __setitem__(self, key, value):
@@ -900,12 +903,12 @@ class _TTLCache(object):
if not self.isfull:
self.isfull = len(self) >= self.maxsize
+ if self.isfull:
+ self.lookup.pop(self.key_ttl_queue.popleft(), None)
+
self.lookup[key] = value
self.key_ttl_queue.append(key)
- if self.isfull:
- self.lookup.pop(self.key_ttl_queue.popleft())
-
class _UnboundedTTLCache(_TTLCache):
def __setitem__(self, key, value):
# unbounded cache, just add new item
@@ -1203,9 +1206,9 @@ class ParserElement(object):
SIZE, HIT, MISS = 0, 1, 2
lookup = (self, instring, loc, callPreParse, doActions)
with ParserElement.packrat_cache_lock:
- if lookup in ParserElement.packrat_cache:
+ value = ParserElement.packrat_cache.get(lookup, None)
+ if value is not None:
ParserElement.packrat_cache_stats[HIT] += 1
- value = ParserElement.packrat_cache[lookup]
if isinstance(value, Exception):
raise value
return (value[0], value[1].copy())