-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from m3dev/local_cache
[draft] add local cache
- Loading branch information
Showing
12 changed files
with
187 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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,28 @@ | ||
import unittest | ||
import os | ||
from pathlib import Path | ||
|
||
from thunderbolt.client.local_cache import LocalCache | ||
|
||
|
||
class TestLocalCache(unittest.TestCase): | ||
def setUp(self): | ||
self.base_path = './resources' | ||
self.local_cache = LocalCache(self.base_path, True) | ||
|
||
def test_init(self): | ||
self.assertTrue(os.path.exists('./thunderbolt')) | ||
|
||
def test_dump_and_get(self): | ||
target = {'foo': 'bar'} | ||
self.local_cache.dump('test.pkl', target) | ||
output = self.local_cache.get('test.pkl') | ||
self.assertDictEqual(target, output) | ||
|
||
def test_convert_file_path(self): | ||
output = self.local_cache._convert_file_path('test.pkl') | ||
target = Path(os.path.join(os.getcwd(), '.thunderbolt', self.base_path.split('/')[-1], 'test.pkl')) | ||
self.assertEqual(target, output) | ||
|
||
def tearDown(self): | ||
self.local_cache.clear() |
This file contains 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,31 @@ | ||
import unittest | ||
from os import path | ||
import pandas as pd | ||
import pickle | ||
|
||
from thunderbolt import Thunderbolt | ||
from thunderbolt.client.local_cache import LocalCache | ||
""" | ||
requires: | ||
python sample.py test.TestCaseTask --param=sample --number=1 --workspace-directory=./test_case --local-scheduler | ||
""" | ||
|
||
|
||
class LocalCacheTest(unittest.TestCase): | ||
def test_running(self): | ||
target = Thunderbolt(self._get_test_case_path(), use_cache=False) | ||
_ = Thunderbolt(self._get_test_case_path()) | ||
output = Thunderbolt(self._get_test_case_path()) | ||
|
||
for k, v in target.tasks.items(): | ||
if k == 'last_modified': # cache file | ||
continue | ||
self.assertEqual(v, output.tasks[k]) | ||
|
||
output.client.local_cache.clear() | ||
|
||
def _get_test_case_path(self, file_name: str = ''): | ||
p = path.abspath(path.join(path.dirname(__file__), 'test_case')) | ||
if file_name: | ||
return path.join(p, file_name) | ||
return p |
This file contains 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 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 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 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 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 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,37 @@ | ||
import os | ||
import pickle | ||
import shutil | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
|
||
class LocalCache: | ||
def __init__(self, workspace_directory: str, use_cache: bool): | ||
"""Log file cache. | ||
dump file: ./.thunderbolt/resources/{task_hash}.pkl | ||
""" | ||
self.cache_dir = Path(os.path.join(os.getcwd(), '.thunderbolt', workspace_directory.split('/')[-1])) | ||
if use_cache: | ||
self.cache_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
def get(self, file_name: str) -> Optional[dict]: | ||
cache_file_path = self._convert_file_path(file_name) | ||
if cache_file_path.exists(): | ||
with cache_file_path.open(mode='rb') as f: | ||
params = pickle.load(f) | ||
return params | ||
return None | ||
|
||
def dump(self, file_name: str, params: dict): | ||
cache_file_path = self._convert_file_path(file_name) | ||
with cache_file_path.open(mode='wb') as f: | ||
pickle.dump(params, f) | ||
|
||
def clear(self): | ||
shutil.rmtree(os.path.join(os.getcwd(), '.thunderbolt')) | ||
|
||
def _convert_file_path(self, file_name: str) -> Path: | ||
file_name = file_name.split('/')[-1] | ||
cache_file_path = self.cache_dir.joinpath(file_name) | ||
return cache_file_path.with_suffix('.pkl') |
This file contains 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 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 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