-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from matthewwardrop/instance_state
Store instance state separately for better invalidation logic during instantiation and thread-safety.
- Loading branch information
Showing
6 changed files
with
122 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import weakref | ||
from collections.abc import MutableMapping | ||
from typing import Any | ||
|
||
|
||
class WeakRef: | ||
def __init__(self, obj: Any): | ||
self._ref = weakref.ref(obj) | ||
|
||
def __hash__(self): | ||
return id(self._ref) | ||
|
||
def __eq__(self, other): | ||
return self._ref is other._ref | ||
|
||
|
||
class WeakRefCache(MutableMapping): | ||
def __init__(self): | ||
self.index = weakref.WeakValueDictionary() | ||
self.values = weakref.WeakKeyDictionary() | ||
|
||
def __getitem__(self, obj): | ||
return self.values[WeakRef(obj)] | ||
|
||
def __setitem__(self, obj, value): | ||
ref = WeakRef(obj) | ||
self.index[ref] = obj | ||
self.values[ref] = value | ||
|
||
def __delitem__(self, obj): | ||
ref = WeakRef(obj) | ||
del self.values[ref] | ||
del self.index[ref] | ||
|
||
def __iter__(self): | ||
return self.index.values() | ||
|
||
def __len__(self): | ||
return len(self.index) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from spec_classes.utils.weakref_cache import WeakRefCache | ||
|
||
|
||
class Object: | ||
pass | ||
|
||
|
||
def test_weak_ref_cache(): | ||
cache = WeakRefCache() | ||
a = Object() | ||
b = Object() | ||
c = Object() | ||
|
||
cache[a] = 1 | ||
cache[b] = 2 | ||
|
||
assert cache[a] == 1 | ||
assert cache[b] == 2 | ||
|
||
assert a in cache | ||
assert b in cache | ||
assert c not in cache | ||
assert len(cache) == 2 | ||
assert list(cache) == [a, b] | ||
|
||
del cache[b] | ||
assert len(cache) == 1 | ||
|
||
del a | ||
assert len(cache) == 0 |