-
Notifications
You must be signed in to change notification settings - Fork 0
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 #169 from tsugumi-sys/feature/impl-stores-local-ar…
…tifact Store: Implement local artifact store for wiki sp500
- Loading branch information
Showing
8 changed files
with
72 additions
and
26 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
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,36 @@ | ||
import os | ||
import tempfile | ||
from typing import List, Optional | ||
|
||
from stocklake.core.base_store import BaseStore | ||
from stocklake.core.constants import DATA_DIR | ||
from stocklake.stores.artifact.local_artifact_repo import LocalArtifactRepository | ||
from stocklake.stores.constants import StoreType | ||
from stocklake.stores.db.database import ( | ||
DATABASE_SESSION_TYPE, | ||
local_session, | ||
) | ||
from stocklake.utils.file_utils import save_data_to_csv | ||
from stocklake.wiki_sp500.entities import PreprocessedWikiSp500Data | ||
|
||
SAVE_ARTIFACTS_DIR = os.path.join(DATA_DIR, "wiki_sp500") | ||
|
||
|
||
class WikiSP500Stores(BaseStore): | ||
def __init__(self, sqlalchemy_session: Optional[DATABASE_SESSION_TYPE] = None): | ||
if sqlalchemy_session is None: | ||
sqlalchemy_session = local_session() | ||
self.sqlalchemy_session = sqlalchemy_session | ||
|
||
def save(self, store_type: StoreType, data: List[PreprocessedWikiSp500Data]) -> str: | ||
if store_type == StoreType.LOCAL_ARTIFACT: | ||
repository = LocalArtifactRepository(SAVE_ARTIFACTS_DIR) | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
csv_file_path = os.path.join(tmpdir, "wiki_sp500.csv") | ||
save_data_to_csv([d.model_dump() for d in data], csv_file_path) | ||
repository.save_artifact(csv_file_path) | ||
return repository.list_artifacts()[0].path | ||
elif store_type == StoreType.POSTGRESQL: | ||
raise NotImplementedError() | ||
else: | ||
raise NotImplementedError() |
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,21 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from stocklake.stores.constants import StoreType | ||
from stocklake.wiki_sp500.data_loader import WikiSP500DataLoader | ||
from stocklake.wiki_sp500.preprocessor import WikiSP500Preprocessor | ||
from stocklake.wiki_sp500.stores import WikiSP500Stores | ||
|
||
|
||
@pytest.fixture | ||
def wiki_sp500_data(): | ||
data_loader = WikiSP500DataLoader() | ||
preprocessor = WikiSP500Preprocessor() | ||
yield preprocessor.process(data_loader.download()) | ||
|
||
|
||
def test_save_local_artifact_repo(wiki_sp500_data): | ||
store = WikiSP500Stores() | ||
saved_path = store.save(StoreType.LOCAL_ARTIFACT, wiki_sp500_data) | ||
assert os.path.exists(saved_path) |