-
Notifications
You must be signed in to change notification settings - Fork 3
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 #43 from alan-turing-institute/linting
Add additional linting checks
- Loading branch information
Showing
28 changed files
with
600 additions
and
427 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Self | ||
|
||
from .uid_cache import UidCache | ||
|
||
|
||
class LocalCache(UidCache): | ||
def __init__(self) -> None: | ||
"""Implementation of UidCache using an in-memory dictionary.""" | ||
|
||
def __init__(self: Self) -> None: | ||
"""Initialise a RedisCache.""" | ||
self.cache: dict[str, int] = {} | ||
|
||
def get(self, identifier: str) -> int | None: | ||
def get(self: Self, identifier: str) -> int | None: | ||
return self.cache.get(identifier, None) | ||
|
||
def keys(self) -> list[str]: | ||
return [str(k) for k in self.cache.keys()] | ||
def keys(self: Self) -> list[str]: | ||
return [str(k) for k in self.cache] | ||
|
||
def set(self, identifier: str, uid_value: int) -> None: | ||
def set(self: Self, identifier: str, uid_value: int) -> None: | ||
self.cache[identifier] = uid_value | ||
|
||
def values(self, keys: list[str]) -> list[int]: | ||
def values(self: Self, keys: list[str]) -> list[int]: | ||
return [v for k, v in self.cache.items() if k in keys] |
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,36 +1,45 @@ | ||
from typing import cast | ||
from __future__ import annotations | ||
|
||
from typing import Self, cast | ||
|
||
import redis | ||
|
||
from .uid_cache import UidCache | ||
|
||
|
||
class RedisCache(UidCache): | ||
def __init__(self, redis_host: str, redis_port: int) -> None: | ||
"""Implementation of UidCache using a Redis backend.""" | ||
|
||
def __init__(self: Self, redis_host: str, redis_port: int) -> None: | ||
"""Initialise a RedisCache. | ||
@param redis_host: Host for the Redis cache | ||
@param redis_port: Port for the Redis cache | ||
""" | ||
self.redis_host = redis_host | ||
self.redis_port = redis_port | ||
self.cache_: "redis.Redis[str]" | None = None # noqa: UP037 | ||
self.cache_: redis.Redis[str] | None = None | ||
|
||
@property | ||
def cache(self) -> "redis.Redis[str]": | ||
""" | ||
Lazy-load the cache on request | ||
""" | ||
def cache(self: Self) -> redis.Redis[str]: | ||
"""Lazy-load the cache on request.""" | ||
if not self.cache_: | ||
self.cache_ = redis.Redis( | ||
host=self.redis_host, port=self.redis_port, decode_responses=True | ||
host=self.redis_host, | ||
port=self.redis_port, | ||
decode_responses=True, | ||
) | ||
return self.cache_ | ||
|
||
def get(self, identifier: str) -> int | None: | ||
def get(self: Self, identifier: str) -> int | None: | ||
value = self.cache.get(identifier) | ||
return None if value is None else int(value) | ||
|
||
def keys(self) -> list[str]: | ||
return [str(k) for k in self.cache.keys()] | ||
def keys(self: Self) -> list[str]: | ||
return [str(k) for k in self.cache.keys()] # noqa: SIM118 | ||
|
||
def set(self, identifier: str, uid_value: int) -> None: | ||
def set(self: Self, identifier: str, uid_value: int) -> None: | ||
self.cache.set(identifier, uid_value) | ||
|
||
def values(self, keys: list[str]) -> list[int]: | ||
def values(self: Self, keys: list[str]) -> list[int]: | ||
return [int(cast(str, v)) for v in self.cache.mget(keys)] |
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
Oops, something went wrong.