Skip to content

Commit

Permalink
chore: update name from inmemory to in_memory
Browse files Browse the repository at this point in the history
chore: add type hints
style: remove `Protocol`
  • Loading branch information
utotsubasa committed Feb 12, 2025
1 parent b9dbb4d commit 769404a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
28 changes: 15 additions & 13 deletions docs/task_on_kart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,23 @@ If you want to dump csv file with other encodings, you can use `encoding` parame
Cache output in memory instead of dumping to files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can use :class:`~InMemoryTarget` to cache output in memory instead of dumping to files by calling :func:`~gokart.target.make_inmemory_target`.
You can use :class:`~InMemoryTarget` to cache output in memory instead of dumping to files by calling :func:`~gokart.target.make_in_memory_target`.

Please note that :class:`~InMemoryTarget` is an experimental feature.

.. code:: python
from gokart.in_memory.target import make_inmemory_target
from gokart.in_memory.target import make_in_memory_target
def output(self):
unique_id = self.make_unique_id() if use_unique_id else None
# TaskLock is not supported in InMemoryTarget, so it's dummy
task_lock_params = make_task_lock_params(
file_path='dummy_path',
unique_id=unique_id,
redis_host=None,
redis_port=None,
redis_timeout=self.redis_timeout,
raise_task_lock_exception_on_collision=False,
)
return make_inmemory_target('dummy_path', task_lock_params, unique_id)
unique_id = self.make_unique_id() if use_unique_id else None
# TaskLock is not supported in InMemoryTarget, so it's dummy
task_lock_params = make_task_lock_params(
file_path='dummy_path',
unique_id=unique_id,
redis_host=None,
redis_port=None,
redis_timeout=self.redis_timeout,
raise_task_lock_exception_on_collision=False,
)
return make_in_memory_target('dummy_path', task_lock_params, unique_id)
2 changes: 1 addition & 1 deletion gokart/in_memory/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .repository import InMemoryCacheRepository # noqa:F401
from .target import InMemoryTarget, make_inmemory_target # noqa:F401
from .target import InMemoryTarget, make_in_memory_target # noqa:F401
6 changes: 2 additions & 4 deletions gokart/in_memory/data.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Protocol
from typing import Any


class BaseData(Protocol): ...


@dataclass
class InMemoryData(BaseData):
class InMemoryData:
value: Any
last_modification_time: datetime

Expand Down
6 changes: 2 additions & 4 deletions gokart/in_memory/repository.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from typing import Any, Iterator, Protocol
from typing import Any, Iterator

from .data import InMemoryData


class BaseRepository(Protocol): ...


class InMemoryCacheRepository(BaseRepository):
class InMemoryCacheRepository:
_cache: dict[str, InMemoryData] = {}

def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion gokart/in_memory/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ def _path(self) -> str:
return self._data_key


def make_inmemory_target(target_key: str, task_lock_params: TaskLockParams):
def make_in_memory_target(target_key: str, task_lock_params: TaskLockParams) -> InMemoryTarget:
return InMemoryTarget(target_key, task_lock_params)
10 changes: 5 additions & 5 deletions test/in_memory/test_in_memory_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import pytest

from gokart.conflict_prevention_lock.task_lock import TaskLockParams
from gokart.in_memory import InMemoryCacheRepository, InMemoryTarget, make_inmemory_target
from gokart.in_memory import InMemoryCacheRepository, InMemoryTarget, make_in_memory_target


class TestInMemoryTarget:
@pytest.fixture
def task_lock_params(self):
def task_lock_params(self) -> TaskLockParams:
return TaskLockParams(
redis_host=None,
redis_port=None,
Expand All @@ -21,11 +21,11 @@ def task_lock_params(self):
)

@pytest.fixture
def target(self, task_lock_params: TaskLockParams):
return make_inmemory_target(target_key='dummy_key', task_lock_params=task_lock_params)
def target(self, task_lock_params: TaskLockParams) -> InMemoryTarget:
return make_in_memory_target(target_key='dummy_key', task_lock_params=task_lock_params)

@pytest.fixture(autouse=True)
def clear_repo(self):
def clear_repo(self) -> None:
InMemoryCacheRepository().clear()

def test_dump_and_load_data(self, target: InMemoryTarget):
Expand Down
2 changes: 1 addition & 1 deletion test/in_memory/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TestInMemoryCacheRepository:
@pytest.fixture
def repo(self):
def repo(self) -> Repo:
repo = Repo()
repo.clear()
return repo
Expand Down

0 comments on commit 769404a

Please sign in to comment.