Rewrite PreferenceScore.(from|to)_base64 to method chaining

Solves mypy issues while looking clean
master
Felix Stupp 2 years ago
parent a039aafff2
commit 760010e743
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -0,0 +1,26 @@
from __future__ import annotations
from typing import Callable, Generic, TypeVar
T = TypeVar("T")
S = TypeVar("S")
class Chain(Generic[T]):
__value: T
def __init__(self, start_value: T) -> None:
super().__init__()
self.__value = start_value
def use(self, converter: Callable[[T], S]) -> Chain[S]:
return Chain(converter(self.__value))
# infix notation
def __or__(self, converter: Callable[[T], S]) -> Chain[S]:
return self.use(converter=converter)
def get(self) -> T:
return self.__value

@ -30,6 +30,7 @@ from pony import orm
from pony.orm.core import Query as PonyQuery
from .common import trim
from .extras import Chain
db = orm.Database()
@ -284,26 +285,32 @@ class PreferenceScore:
@classmethod
def from_base64(cls, in_data: str, encoding: str = "utf-8") -> PreferenceScore:
data = in_data.encode(encoding)
data = base64.decodebytes(data)
data = gzip.decompress(data)
data = data.decode(encoding)
data = PreferenceScore.from_json(data)
return data
return (
Chain(in_data)
| (lambda d: d.encode(encoding=encoding))
| base64.decodebytes
| gzip.decompress
| (lambda d: d.decode(encoding=encoding))
| PreferenceScore.from_json
).get()
def to_json(self) -> str:
return json.dumps({tag.id: score for tag, score in self.points.items()})
def to_base64(self, encoding: str = "utf-8") -> str:
data = self.to_json()
data = data.encode(encoding)
data = gzip.compress(
data=data,
compresslevel=9,
)
data = base64.encodebytes(data)
data = data.decode(encoding)
return data
return (
Chain(self)
| PreferenceScore.to_json
| (lambda d: d.encode(encoding=encoding))
| (
lambda d: gzip.compress(
data=d,
compresslevel=9,
)
)
| base64.encodebytes
| (lambda d: d.decode(encoding=encoding))
).get()
class PreferenceScoreAppender:

Loading…
Cancel
Save