Skip to content

Commit 10632b0

Browse files
committed
Add missing cache_selfie_json and cache_selfie_binary methods.
1 parent 278c6a3 commit 10632b0

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

python/selfie-lib/selfie_lib/CacheSelfie.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ def cache_selfie(
3333
else:
3434
raise TypeError("Invalid arguments provided to cache_selfie")
3535

36+
def cache_selfie_json(to_cache: Callable[..., T]) -> "CacheSelfie[T]":
37+
return cache_selfie(to_cache, Roundtrip.json())
38+
39+
@overload
40+
def cache_selfie_binary(to_cache: Callable[..., bytes]) -> "CacheSelfieBinary[bytes]": ...
41+
42+
@overload
43+
def cache_selfie_binary(
44+
to_cache: Callable[..., T], roundtrip: Roundtrip[T, bytes]
45+
) -> "CacheSelfieBinary[T]": ...
46+
47+
def cache_selfie_binary(
48+
to_cache: Union[Callable[..., bytes], Callable[..., T]],
49+
roundtrip: Optional[Roundtrip[T, bytes]] = None,
50+
) -> Union["CacheSelfieBinary[bytes]", "CacheSelfieBinary[T]"]:
51+
if roundtrip is None:
52+
# the cacheable better be a bytes!
53+
return cache_selfie_binary(to_cache, Roundtrip.identity()) # type: ignore
54+
elif isinstance(roundtrip, Roundtrip) and to_cache is not None:
55+
deferred_disk_storage = _selfieSystem().disk_thread_local()
56+
return CacheSelfieBinary(deferred_disk_storage, roundtrip, to_cache) # type: ignore
57+
else:
58+
raise TypeError("Invalid arguments provided to cache_selfie")
3659

3760
class CacheSelfie(Generic[T]):
3861
def __init__(

python/selfie-lib/selfie_lib/Roundtrip.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ def parse(self, serialized: Any) -> Any:
2626
return serialized
2727

2828
return Identity()
29+
30+
@classmethod
31+
def json(cls) -> "Roundtrip[T, str]":
32+
"""Return a Roundtrip that serializes to/from JSON strings."""
33+
import json
34+
35+
class JsonRoundtrip(Roundtrip[Any, str]):
36+
def serialize(self, value: Any) -> str:
37+
return json.dumps(value, indent=4)
38+
39+
def parse(self, serialized: str) -> Any:
40+
return json.loads(serialized)
41+
42+
return JsonRoundtrip()

python/selfie-lib/selfie_lib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .ArrayMap import ArraySet as ArraySet
44
from .Atomic import AtomicReference as AtomicReference
55
from .CacheSelfie import cache_selfie as cache_selfie
6+
from .CacheSelfie import cache_selfie_binary as cache_selfie_binary
7+
from .CacheSelfie import cache_selfie_json as cache_selfie_json
68
from .CommentTracker import CommentTracker as CommentTracker
79
from .FS import FS as FS
810
from .Lens import Camera as Camera

0 commit comments

Comments
 (0)