From 769404a50133ba80ceab87199cc9f0fba43e1403 Mon Sep 17 00:00:00 2001 From: utotsubasa Date: Wed, 12 Feb 2025 21:28:08 +0900 Subject: [PATCH] chore: update name from `inmemory` to `in_memory` chore: add type hints style: remove `Protocol` --- docs/task_on_kart.rst | 28 +++++++++++++------------ gokart/in_memory/__init__.py | 2 +- gokart/in_memory/data.py | 6 ++---- gokart/in_memory/repository.py | 6 ++---- gokart/in_memory/target.py | 2 +- test/in_memory/test_in_memory_target.py | 10 ++++----- test/in_memory/test_repository.py | 2 +- 7 files changed, 27 insertions(+), 29 deletions(-) diff --git a/docs/task_on_kart.rst b/docs/task_on_kart.rst index 09e7a59f..0941996f 100644 --- a/docs/task_on_kart.rst +++ b/docs/task_on_kart.rst @@ -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) \ No newline at end of file + 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) \ No newline at end of file diff --git a/gokart/in_memory/__init__.py b/gokart/in_memory/__init__.py index 69e7e4c3..c418c139 100644 --- a/gokart/in_memory/__init__.py +++ b/gokart/in_memory/__init__.py @@ -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 diff --git a/gokart/in_memory/data.py b/gokart/in_memory/data.py index a01c3ad2..eb90a834 100644 --- a/gokart/in_memory/data.py +++ b/gokart/in_memory/data.py @@ -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 diff --git a/gokart/in_memory/repository.py b/gokart/in_memory/repository.py index 4d1920b6..d5073ab7 100644 --- a/gokart/in_memory/repository.py +++ b/gokart/in_memory/repository.py @@ -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): diff --git a/gokart/in_memory/target.py b/gokart/in_memory/target.py index b47e96a7..cb2122b5 100644 --- a/gokart/in_memory/target.py +++ b/gokart/in_memory/target.py @@ -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) diff --git a/test/in_memory/test_in_memory_target.py b/test/in_memory/test_in_memory_target.py index c20bfa3b..ae6ca11d 100644 --- a/test/in_memory/test_in_memory_target.py +++ b/test/in_memory/test_in_memory_target.py @@ -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, @@ -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): diff --git a/test/in_memory/test_repository.py b/test/in_memory/test_repository.py index 30c5b033..70d32151 100644 --- a/test/in_memory/test_repository.py +++ b/test/in_memory/test_repository.py @@ -9,7 +9,7 @@ class TestInMemoryCacheRepository: @pytest.fixture - def repo(self): + def repo(self) -> Repo: repo = Repo() repo.clear() return repo