Skip to content

Commit

Permalink
Implement nasdaq api preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
tsugumi-sys committed Apr 29, 2024
1 parent d04f535 commit 549feaa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
22 changes: 17 additions & 5 deletions stocklake/nasdaqapi/preprocessor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
from typing import List

from stocklake.core.base_preprocessor import BasePreprocessor
from stocklake.nasdaqapi.entities import RawNasdaqApiSymbolData
from stocklake.nasdaqapi.entities import NasdaqApiSymbolData, RawNasdaqApiSymbolData


class NASDAQSymbolsPreprocessor(BasePreprocessor):
def process(
self, data: List[RawNasdaqApiSymbolData]
) -> List[RawNasdaqApiSymbolData]:
return data
def process(self, data: List[RawNasdaqApiSymbolData]) -> List[NasdaqApiSymbolData]:
processed_data: List[NasdaqApiSymbolData] = []
for data_dic in data:
_data: NasdaqApiSymbolData = {
"symbol": data_dic["symbol"],
"name": data_dic["name"],
"lastsale": float(
data_dic["lastsale"].replace("$", "").replace(",", "")
),
"netchange": float(data_dic["netchange"]),
"pctchange": float(data_dic["pctchange"].replace("%", "")),
"marketCap": float(data_dic["marketCap"].replace(",", "")),
"url": data_dic["url"],
}
processed_data.append(_data)
return processed_data
17 changes: 17 additions & 0 deletions tests/nasdaqapi/test_preprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import json

from stocklake.nasdaqapi.preprocessor import NASDAQSymbolsPreprocessor

with open("./tests/nasdaqapi/sample_response.json") as f:
mock_raw_data = json.load(f)["data"]["rows"]


def test_process():
preprocessor = NASDAQSymbolsPreprocessor()
data = preprocessor.process(mock_raw_data)
for data_dic in data:
for key, val in data_dic.items():
if key in ["lastsale", "netchange", "pctchange", "marketCap"]:
assert isinstance(val, float)
else:
assert isinstance(val, str)

0 comments on commit 549feaa

Please sign in to comment.