mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-21 08:25:08 +00:00
refactor w/ black
This commit is contained in:
parent
cc3cde89c8
commit
b474492637
8 changed files with 187 additions and 165 deletions
|
@ -1,4 +1,4 @@
|
|||
#== cache.py ==#
|
||||
# == cache.py ==#
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
@ -6,9 +6,7 @@ from collections import deque
|
|||
from collections.abc import MutableMapping
|
||||
from typing import Any, Deque, Tuple, TypeVar
|
||||
|
||||
__all__: Tuple[str, ...] = (
|
||||
'ObjectCache',
|
||||
)
|
||||
__all__: Tuple[str, ...] = ('ObjectCache',)
|
||||
|
||||
|
||||
K = TypeVar('K')
|
||||
|
@ -17,26 +15,26 @@ V = TypeVar('V')
|
|||
|
||||
class _BaseCache(MutableMapping[K, V]):
|
||||
"""This is a rough implementation of an LRU Cache using a deque and a dict."""
|
||||
|
||||
|
||||
__slots__: Tuple[str, ...] = ('_max_size', '_lru_keys')
|
||||
|
||||
def __init__(self, max_size: int, *args: Any) -> None:
|
||||
self._max_size: int = max(min(max_size, 15), 0) # bounding max_size to 15 for now
|
||||
self._max_size: int = max(min(max_size, 15), 0) # bounding max_size to 15 for now
|
||||
self._lru_keys: Deque[K] = deque[K](maxlen=self._max_size)
|
||||
super().__init__(*args)
|
||||
|
||||
|
||||
def __getitem__(self, __k: K) -> V:
|
||||
index = self._lru_keys.index(__k)
|
||||
target = self._lru_keys[index]
|
||||
del self._lru_keys[index]
|
||||
|
||||
|
||||
self._lru_keys.appendleft(target)
|
||||
return super().__getitem__(__k)
|
||||
|
||||
def __setitem__(self, __k: K, __v: V) -> None:
|
||||
if len(self) == self._max_size:
|
||||
self.__delitem__(self._lru_keys.pop())
|
||||
|
||||
|
||||
self._lru_keys.appendleft(__k)
|
||||
return super().__setitem__(__k, __v)
|
||||
|
||||
|
@ -44,12 +42,13 @@ class _BaseCache(MutableMapping[K, V]):
|
|||
for key, value in dict(**kwargs).items():
|
||||
key: K
|
||||
value: V
|
||||
|
||||
|
||||
self.__setitem__(key, value)
|
||||
|
||||
|
||||
class ObjectCache(_BaseCache[K, V]):
|
||||
"""This adjusts the typehints to reflect Github objects."""
|
||||
|
||||
def __getitem__(self, __k: K) -> V:
|
||||
index = self._lru_keys.index(__k)
|
||||
target = self._lru_keys[index]
|
||||
|
@ -59,7 +58,7 @@ class ObjectCache(_BaseCache[K, V]):
|
|||
def __setitem__(self, __k: K, __v: V) -> None:
|
||||
if self.__len__() == self._max_size:
|
||||
self.__delitem__(self._lru_keys.pop())
|
||||
|
||||
|
||||
self._lru_keys.appendleft(__k)
|
||||
return super().__setitem__(__k, __v)
|
||||
|
||||
|
@ -67,5 +66,5 @@ class ObjectCache(_BaseCache[K, V]):
|
|||
for key, value in dict(**kwargs).items():
|
||||
key: K
|
||||
value: V
|
||||
|
||||
self.__setitem__(key, value)
|
||||
|
||||
self.__setitem__(key, value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue