-
Notifications
You must be signed in to change notification settings - Fork 62
Feat: Create InMemoryTarget
from TaskOnKart
#441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
utotsubasa
wants to merge
19
commits into
m3dev:master
Choose a base branch
from
utotsubasa:feat/in_memory_parameter_on_task
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−6
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9068899
feat: add in-memory target
utotsubasa 58aa9a5
feat: add a data format to be stored in `Repository`
utotsubasa 4c311bb
fix: fix linting errors
utotsubasa da3ae69
fix: update type union shorthand to to make compatible with py39
utotsubasa c09040d
style: refactor some base classes to inherite from
utotsubasa 333986d
fix: remove unnessesary optional type
utotsubasa b0c6d5a
fix: fix format error
utotsubasa a53378a
chore: add an assertion error message
utotsubasa 4d9b033
style: update the variable name to for code consistency
utotsubasa b9dbb4d
docs: add a document of how to create InMemoryTarget
utotsubasa 769404a
chore: update name from `inmemory` to `in_memory`
utotsubasa 96d9b6e
fix: fix lint errors
utotsubasa 70daf49
Merge branch 'master' into feat/in_memory_single_thread
inakam 7adae09
chore: raise Error when using InMemoryTarget with Redis
utotsubasa 5796544
fix: fix a linting error
utotsubasa d777e92
feat: add the new `TargetOnKart` entrypoint `make_cache_target`
utotsubasa 76cb255
chore: delete unecessary comments
utotsubasa 1a349af
fix: fix typo
utotsubasa 2520067
resolve conflicts
utotsubasa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from typing import Optional, Type, Union | ||
|
||
import luigi | ||
import pytest | ||
|
||
import gokart | ||
from gokart.in_memory import InMemoryCacheRepository, InMemoryTarget | ||
from gokart.target import SingleFileTarget | ||
|
||
|
||
class DummyTask(gokart.TaskOnKart): | ||
task_namespace = __name__ | ||
param: str = luigi.Parameter() | ||
|
||
def run(self): | ||
self.dump(self.param) | ||
|
||
|
||
class DummyTaskWithDependencies(gokart.TaskOnKart): | ||
task_namespace = __name__ | ||
task: list[gokart.TaskOnKart[str]] = gokart.ListTaskInstanceParameter() | ||
|
||
def run(self): | ||
result = ','.join(self.load()) | ||
self.dump(result) | ||
|
||
|
||
class DumpIntTask(gokart.TaskOnKart[int]): | ||
task_namespace = __name__ | ||
value: int = luigi.IntParameter() | ||
|
||
def run(self): | ||
self.dump(self.value) | ||
|
||
|
||
class AddTask(gokart.TaskOnKart[Union[int, float]]): | ||
a: gokart.TaskOnKart[int] = gokart.TaskInstanceParameter() | ||
b: gokart.TaskOnKart[int] = gokart.TaskInstanceParameter() | ||
|
||
def requires(self): | ||
return dict(a=self.a, b=self.b) | ||
|
||
def run(self): | ||
a = self.load(self.a) | ||
b = self.load(self.b) | ||
self.dump(a + b) | ||
|
||
|
||
class TestTaskOnKartWithCache: | ||
@pytest.fixture(autouse=True) | ||
def clear_repository(self) -> None: | ||
InMemoryCacheRepository().clear() | ||
|
||
@pytest.mark.parametrize('data_key', ['sample_key', None]) | ||
@pytest.mark.parametrize('use_unique_id', [True, False]) | ||
def test_key_identity(self, data_key: Optional[str], use_unique_id: bool): | ||
task = DummyTask(param='param') | ||
ext = '.pkl' | ||
relative_file_path = data_key + ext if data_key else None | ||
target = task.make_target(relative_file_path=relative_file_path, use_unique_id=use_unique_id) | ||
cached_target = task.make_cache_target(data_key=data_key, use_unique_id=use_unique_id) | ||
|
||
target_path = target.path().removeprefix(task.workspace_directory).removesuffix(ext).strip('/') | ||
assert cached_target.path() == target_path | ||
|
||
def test_make_cached_target(self): | ||
task = DummyTask(param='param') | ||
target = task.make_cache_target() | ||
assert isinstance(target, InMemoryTarget) | ||
|
||
@pytest.mark.parametrize(['cache_in_memory_by_default', 'target_type'], [[True, InMemoryTarget], [False, SingleFileTarget]]) | ||
def test_make_default_target(self, cache_in_memory_by_default: bool, target_type: Type[gokart.TaskOnKart]): | ||
task = DummyTask(param='param', cache_in_memory_by_default=cache_in_memory_by_default) | ||
target = task.output() | ||
assert isinstance(target, target_type) | ||
|
||
def test_complete_with_cache_in_memory_flag(self, tmpdir): | ||
task = DummyTask(param='param', cache_in_memory_by_default=True, workspace_directory=tmpdir) | ||
assert not task.complete() | ||
file_target = task.make_target() | ||
file_target.dump('data') | ||
assert not task.complete() | ||
cache_target = task.make_cache_target() | ||
cache_target.dump('data') | ||
assert task.complete() | ||
|
||
def test_complete_without_cache_in_memory_flag(self, tmpdir): | ||
task = DummyTask(param='param', workspace_directory=tmpdir) | ||
assert not task.complete() | ||
cache_target = task.make_cache_target() | ||
cache_target.dump('data') | ||
assert not task.complete() | ||
file_target = task.make_target() | ||
file_target.dump('data') | ||
assert task.complete() | ||
|
||
def test_dump_with_cache_in_memory_flag(self, tmpdir): | ||
task = DummyTask(param='param', cache_in_memory_by_default=True, workspace_directory=tmpdir) | ||
file_target = task.make_target() | ||
cache_target = task.make_cache_target() | ||
task.dump('data') | ||
assert not file_target.exists() | ||
assert cache_target.exists() | ||
|
||
def test_dump_without_cache_in_memory_flag(self, tmpdir): | ||
task = DummyTask(param='param', workspace_directory=tmpdir) | ||
file_target = task.make_target() | ||
cache_target = task.make_cache_target() | ||
task.dump('data') | ||
assert file_target.exists() | ||
assert not cache_target.exists() | ||
|
||
def test_gokart_build(self): | ||
task = AddTask( | ||
a=DumpIntTask(value=2, cache_in_memory_by_default=True), b=DumpIntTask(value=3, cache_in_memory_by_default=True), cache_in_memory_by_default=True | ||
) | ||
output = gokart.build(task, reset_register=False) | ||
assert output == 5 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to change here instead of overwriting a method.