-
-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: new module distributed.collections
- Loading branch information
1 parent
9ef92e7
commit 9280162
Showing
5 changed files
with
50 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
from collections import OrderedDict, UserDict | ||
from typing import TypeVar, cast | ||
|
||
K = TypeVar("K") | ||
V = TypeVar("V") | ||
|
||
|
||
class LRU(UserDict[K, V]): | ||
"""Limited size mapping, evicting the least recently looked-up key when full""" | ||
|
||
def __init__(self, maxsize: float): | ||
super().__init__() | ||
self.data = OrderedDict() | ||
self.maxsize = maxsize | ||
|
||
def __getitem__(self, key): | ||
value = super().__getitem__(key) | ||
cast(OrderedDict, self.data).move_to_end(key) | ||
return value | ||
|
||
def __setitem__(self, key, value): | ||
if len(self) >= self.maxsize: | ||
cast(OrderedDict, self.data).popitem(last=False) | ||
super().__setitem__(key, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from distributed.collections import LRU | ||
|
||
|
||
def test_lru(): | ||
l = LRU(maxsize=3) | ||
l["a"] = 1 | ||
l["b"] = 2 | ||
l["c"] = 3 | ||
assert list(l.keys()) == ["a", "b", "c"] | ||
|
||
# Use "a" and ensure it becomes the most recently used item | ||
l["a"] | ||
assert list(l.keys()) == ["b", "c", "a"] | ||
|
||
# Ensure maxsize is respected | ||
l["d"] = 4 | ||
assert len(l) == 3 | ||
assert list(l.keys()) == ["c", "a", "d"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters