Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions interactions/utils/dict_caches.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from collections import OrderedDict
from typing import Generic, TypeVar

from .missing import MISSING

__all__ = ("FIFODict", "LRUDict")

_KT = TypeVar("_KT")
Expand Down Expand Up @@ -45,3 +47,14 @@ def __setitem__(self, key: _KT, value: _VT):
# Prevent buildup over time
while len(self) > self._max_items:
del self[next(iter(self))]

__marker = object()

def pop(self, key: _KT, default: _VT = __marker) -> _VT:
if key in self:
result = self[key]
del self[key]
return result
if default is MISSING:
raise KeyError(key)
return default