-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jack Cherng <[email protected]>
- Loading branch information
Showing
8 changed files
with
191 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from functools import lru_cache | ||
from typing import TypeVar, Callable, Optional | ||
|
||
T = TypeVar("T", bound=Callable) | ||
|
||
__MAX_SIZE: Optional[int] = 2048 | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
def cache(function: T) -> T: | ||
return lru_cache(maxsize=get_cache_size())(function) # type: ignore | ||
|
||
|
||
def set_cache_size(size: Optional[int]) -> None: | ||
global __MAX_SIZE # pylint: disable=global-statement | ||
__MAX_SIZE = size | ||
|
||
|
||
def get_cache_size() -> Optional[int]: | ||
global __MAX_SIZE # pylint: disable=global-variable-not-assigned | ||
return __MAX_SIZE | ||
|
||
|
||
def clear_cache() -> None: | ||
cache.cache_clear() |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from typing import Dict, Any | ||
from typing import Mapping, Any | ||
|
||
Data = Dict[str, Any] | ||
Data = Mapping[str, Any] |
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,34 @@ | ||
from collections.abc import Mapping | ||
|
||
|
||
class FrozenDict(Mapping): | ||
dict_cls = dict | ||
|
||
def __init__(self, *args, **kwargs): | ||
self._dict = self.dict_cls(*args, **kwargs) | ||
self._hash = None | ||
|
||
def __getitem__(self, key): | ||
return self._dict[key] | ||
|
||
def __contains__(self, key): | ||
return key in self._dict | ||
|
||
def copy(self, **add_or_replace): | ||
return self.__class__(self, **add_or_replace) | ||
|
||
def __iter__(self): | ||
return iter(self._dict) | ||
|
||
def __len__(self): | ||
return len(self._dict) | ||
|
||
def __repr__(self): | ||
return f"<{self.__class__.__name__} {repr(self._dict)}>" | ||
|
||
def __hash__(self): | ||
if self._hash is None: | ||
self._hash = 0 | ||
for key, value in self._dict.items(): | ||
self._hash ^= hash((key, value)) | ||
return self._hash |
Oops, something went wrong.