Collection#

General purpose utilities

Indexable Dictionary#

class mhi.common.collection.IndexableDict#

An ordered dictionary, where values can be retrieved by index as well as by key, as long as the key is not an int:

>>> idict = IndexableDict([('foo', 10), ('bar', 30), ('baz', 20)])
>>> idict['foo'] == idict[0] == idict[-3] == 10
True
>>> idict['bar'] == idict[1] == idict[-2] == 30
True
>>> idict['baz'] == idict[2] == idict[-1] == 20
True

Lingering Cache#

class mhi.common.collection.LingeringCache(age_limit: float = 15)#

A cache of items that evaporates if references are not externally held.

The items must have a hashable key.

None can never be stored in the cache. Similarly, other immortal objects should never be cached.

  • cache[key] = obj will stores an object in the cache.

  • cache[key] will retrieve the object, or raise a KeyError if it has expired.

  • cache.get(key) returns None if the object has been forgotten.

  • del cache[key] will cause the object to be forgotten, but won’t raise an exception if it is already lost.

  • cache.clear() forgets everything.

No other dictionary methods are implemented.

get(key: KT) VT | None#

Fetch an item from the cache.

If it not longer exists, None is returned.

clear() None#

Forget all values in the cache