Package grizzled :: Package collections :: Module dict :: Class LRUDict
[hide private]
[frames] | no frames]

Class LRUDict

source code

object --+    
         |    
      dict --+
             |
            LRUDict

LRUDict is a dictionary of a fixed maximum size that enforces a least recently used discard policy. When the dictionary is full (i.e., contains the maximum number of entries), any attempt to insert a new entry causes one of the least recently used entries to be discarded.

Note:

An LRUDict also supports the concept of removal listeners. Removal listeners are functions that are notified when objects are removed from the dictionary. Removal listeners can be:

This implementation is based on a Java LRUMap class in the org.clapper.util library. See http://software.clapper.org/java/util/ for details.

Instance Methods [hide private]
new empty dictionary

__init__(self, *args, **kw)
Initialize an LRUDict that will hold, at most, max_capacity items.
source code
 
__del__(self) source code
int
get_max_capacity(self)
Get the maximum capacity of the dictionary.
source code
 
set_max_capacity(self, new_capacity)
Set or change the maximum capacity of the dictionary.
source code
 
add_ejection_listener(self, listener, *args)
Add an ejection listener to the dictionary.
source code
 
add_removal_listener(self, listener, *args)
Add a removal listener to the dictionary.
source code
bool
remove_listener(self, listener)
Remove the specified removal or ejection listener from the list of listeners.
source code
 
clear_listeners(self)
Clear all removal and ejection listeners from the list of listeners.
source code
 
__setitem__(self, key, value)
x[i]=y
source code
 
__getitem__(self, key)
x[y]
source code
 
__delitem__(self, key)
del x[y]
source code
 
__str__(self)
str(x)
source code
 
__iter__(self)
iter(x)
source code
None
clear(self)
Remove all items from D.
source code
D[k] if k in D, else d
get(self, key, default=None)
d defaults to None.
source code
list of D's keys
keys(self) source code
list of D's (key, value) pairs, as 2-tuples
items(self) source code
list of D's values
values(self) source code
an iterator over the (key, value) items of D
iteritems(self) source code
an iterator over the keys of D
iterkeys(self) source code
an iterator over the values of D
itervalues(self) source code
None
update(self, d)
Update D from dict/iterable E and F.
source code
v, remove specified key and return the corresponding value
pop(self, key, default=None)
If key is not found, d is returned if given, otherwise KeyError is raised
source code
tuple
popitem(self)
Pops the least recently used recent key/value pair from the dictionary.
source code
 
__put(self, key, value) source code
 
__clear_to(self, size) source code
 
__notify_listeners(self, ejecting, key_value_pairs) source code

Inherited from dict: __cmp__, __contains__, __eq__, __ge__, __getattribute__, __gt__, __le__, __len__, __lt__, __ne__, __new__, __repr__, __sizeof__, copy, fromkeys, has_key, setdefault, viewitems, viewkeys, viewvalues

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __subclasshook__

Class Variables [hide private]

Inherited from dict: __hash__

Properties [hide private]
  max_capacity
The maximum capacity.

Inherited from object: __class__

Method Details [hide private]

__init__(self, *args, **kw)
(Constructor)

source code 
Initialize an LRUDict that will hold, at most, max_capacity items. Attempts to insert more than max_capacity items in the dictionary will cause the least-recently used entries to drop out of the dictionary.
Parameters:
  • max_capacity (int) - The maximum size of the dictionary
Returns:
new empty dictionary

Overrides: object.__init__

get_max_capacity(self)

source code 
Get the maximum capacity of the dictionary.
Returns: int
the maximum capacity

set_max_capacity(self, new_capacity)

source code 
Set or change the maximum capacity of the dictionary. Reducing the size of a dictionary with items already in it might result in items being evicted.
Parameters:
  • new_capacity (int) - the new maximum capacity

add_ejection_listener(self, listener, *args)

source code 

Add an ejection listener to the dictionary. The listener function should take at least two parameters: the key and value being removed. It can also take additional parameters, which are passed through unmodified.

An ejection listener is only notified when objects are ejected from the cache to make room for new objects; more to the point, an ejection listener is never notified when an object is removed from the cache manually, via use of the del operator.

Parameters:
  • listener (function) - Function to invoke
  • args (iterable) - Any additional parameters to pass to the function

add_removal_listener(self, listener, *args)

source code 

Add a removal listener to the dictionary. The listener function should take at least two parameters: the key and value being removed. It can also take additional parameters, which are passed through unmodified.

A removal listener is notified when objects are ejected from the cache to make room for new objects and when objects are manually deleted from the cache.

Parameters:
  • listener (function) - Function to invoke
  • args (iterable) - Any additional parameters to pass to the function

remove_listener(self, listener)

source code 
Remove the specified removal or ejection listener from the list of listeners.
Parameters:
  • listener (function) - Function object to remove
Returns: bool
True if the listener was found and removed; False otherwise

__setitem__(self, key, value)
(Index assignment operator)

source code 

x[i]=y

Overrides: dict.__setitem__
(inherited documentation)

__getitem__(self, key)
(Indexing operator)

source code 

x[y]

Overrides: dict.__getitem__
(inherited documentation)

__delitem__(self, key)
(Index deletion operator)

source code 

del x[y]

Overrides: dict.__delitem__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

__iter__(self)

source code 

iter(x)

Overrides: dict.__iter__
(inherited documentation)

clear(self)

source code 

Remove all items from D.

Returns: None
Overrides: dict.clear
(inherited documentation)

get(self, key, default=None)

source code 

d defaults to None.

Returns: D[k] if k in D, else d
Overrides: dict.get
(inherited documentation)

keys(self)

source code 
Returns: list of D's keys
Overrides: dict.keys
(inherited documentation)

items(self)

source code 
Returns: list of D's (key, value) pairs, as 2-tuples
Overrides: dict.items
(inherited documentation)

values(self)

source code 
Returns: list of D's values
Overrides: dict.values
(inherited documentation)

iteritems(self)

source code 
Returns: an iterator over the (key, value) items of D
Overrides: dict.iteritems
(inherited documentation)

iterkeys(self)

source code 
Returns: an iterator over the keys of D
Overrides: dict.iterkeys
(inherited documentation)

itervalues(self)

source code 
Returns: an iterator over the values of D
Overrides: dict.itervalues
(inherited documentation)

update(self, d)

source code 

Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

Returns: None
Overrides: dict.update
(inherited documentation)

pop(self, key, default=None)

source code 

If key is not found, d is returned if given, otherwise KeyError is raised

Returns: v, remove specified key and return the corresponding value
Overrides: dict.pop
(inherited documentation)

popitem(self)

source code 
Pops the least recently used recent key/value pair from the dictionary.
Returns: tuple
the least recent key/value pair, as a tuple
Raises:
  • KeyError - empty dictionary
Overrides: dict.popitem

Property Details [hide private]

max_capacity

The maximum capacity. Can be reset at will.
Get Method:
get_max_capacity(self) - Get the maximum capacity of the dictionary.
Set Method:
set_max_capacity(self, new_capacity) - Set or change the maximum capacity of the dictionary.