diff options
author | Jay Hutchinson <jlhutch@gmail.com> | 2018-05-06 00:04:02 -0500 |
---|---|---|
committer | Jay Hutchinson <jlhutch@gmail.com> | 2018-05-06 13:23:59 -0500 |
commit | d5b8b87e709b529d803a85408e1ca739613b8334 (patch) | |
tree | 88cadc4f248b15888d2bb05c8848c9e976fbe58c | |
parent | 7ec62efb9da433b679a924b26902b72e46371a24 (diff) | |
download | pylru-d5b8b87e709b529d803a85408e1ca739613b8334.tar.gz |
Improved update() method of lrucache.
-rw-r--r-- | pylru.py | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -31,6 +31,12 @@ # hash table under their associated key. The hash table allows efficient # lookup of values by key. +import sys +if sys.version_info < (3, 3): + from collections import Mapping +else: + from collections.abc import Mapping + # Class for the node objects. class _dlnode(object): def __init__(self): @@ -147,12 +153,6 @@ class lrucache(object): # need to adjust the 'head' variable. self.head = node - def update(self, items): - - # Add multiple items to the cache. - for n, v in items.items(): - self[n] = v - def __delitem__(self, key): # Lookup the node, remove it from the hash table, and mark it as empty. node = self.table[key] @@ -172,6 +172,22 @@ class lrucache(object): self.mtf(node) self.head = node.next + def update(self, *args, **kwargs): + if len(args) > 0: + other = args[0] + if isinstance(other, Mapping): + for key in other: + self[key] = other[key] + elif hasattr(other, "keys"): + for key in other.keys(): + self[key] = other[key] + else: + for key, value in other: + self[key] = value + + for key, value in kwargs.items(): + self[key] = value + def __iter__(self): # Return an iterator that returns the keys in the cache in order from # the most recently to least recently used. Does not modify the cache's |