Skip to content

Commit

Permalink
Merge pull request #3 from VorTECHsa/add-suffix-local-cache
Browse files Browse the repository at this point in the history
feat: add file extension in functional reader
  • Loading branch information
maxhipperson authored Feb 23, 2024
2 parents dece7d0 + bd0945a commit 677e8e2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
14 changes: 14 additions & 0 deletions tests/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from uhura.base import Readable, Writable
from uhura.caches import (
CachingService,
LocalCache,
)
from uhura.caching import (
cache_local_input,
Expand All @@ -20,6 +21,7 @@
)
from uhura.functional import uhura_reader, uhura_writer
from uhura.modes import fixture_builder_mode, task_test_mode
from uhura.serde import PickleSerde


class RandomReadable(Readable[float]):
Expand Down Expand Up @@ -293,3 +295,15 @@ def test_cache_key():
assert (
DatabaseReader("id1").read() != DatabaseReader("id2").read()
), "Cache key being ignored"


def test_local_cache_path_renders_path_with_file_extension_from_serde_if_not_present():
cache_path = "my/cache/dir"
serde = PickleSerde()
local_cache = LocalCache(cache_path, serde)

base_path = "base/path"
cache_key = "filename"
rendered_path = local_cache.render_path(base_path, cache_key, serde)

assert rendered_path.split(".")[-1] == "pkl"
21 changes: 18 additions & 3 deletions tests/test_serde.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from uhura.serde import PickleSerde
import os
from tempfile import TemporaryDirectory
from typing import Any

import pytest

from uhura.base import Readable
from uhura.modes import fixture_builder_mode, task_test_mode
from tempfile import TemporaryDirectory
import os
from uhura.serde import PickleSerde, Serde


class FakeSerde(PickleSerde):
Expand Down Expand Up @@ -44,3 +48,14 @@ def get_serde(self):
assert not test_serde.has_been_read
client.read()
assert test_serde.has_been_read


def test_serde_subclass_requires_file_extension():
with pytest.raises(AssertionError):

class Bad(Serde[Any]):
def read_from_file(self, file):
pass

def write_to_file(self, file, obj: Any):
pass
13 changes: 11 additions & 2 deletions uhura/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Callable, DefaultDict, Optional, Protocol, Type # ParamSpec

from uhura.composition import async_unit, compose
from uhura.serde import Serde, DEFAULT_SERDE
from uhura.serde import DEFAULT_SERDE, Serde

logger = logging.getLogger("uhura.caches")

Expand Down Expand Up @@ -57,9 +57,18 @@ def update(self, obj):
def get(self):
return self._serde.read_from_file(self._path)

@classmethod
def render_path(cls, base_path: str, cache_key: str, serde: Serde):
if not cache_key.endswith(serde.file_extension):
cache_key = cache_key + serde.file_extension
return os.path.join(base_path, cache_key)

@classmethod
def cache_for_instance(cls, cacheable: Cacheable, base_path: str):
return cls(os.path.join(base_path, cacheable.cache_key()), cacheable.get_serde())
cache_key = cacheable.cache_key()
serde = cacheable.get_serde()
path = cls.render_path(base_path, cache_key, serde)
return cls(path, serde)


async def _take(async_iterable, n=5):
Expand Down
8 changes: 7 additions & 1 deletion uhura/serde.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pickle
from typing import TypeVar, Generic, ClassVar, Any
from typing import Any, ClassVar, Generic, TypeVar

import pandas as pd

Expand All @@ -9,6 +9,12 @@
class Serde(Generic[SerdeType]):
file_extension: ClassVar[str]

def __init_subclass__(cls) -> None:
assert hasattr(
cls, "file_extension"
), "Serde implementations must have a valid file_extension"
return super().__init_subclass__()

def read_from_file(self, file) -> SerdeType:
raise NotImplementedError()

Expand Down

0 comments on commit 677e8e2

Please sign in to comment.