Skip to content

Commit

Permalink
0.9.34 update data_client
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Nov 3, 2023
1 parent 120f892 commit 4326a66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 3 additions & 5 deletions czsc/utils/data_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
from functools import partial


logger.disable(__name__)


def set_url_token(token, url):
"""设置指定 URL 数据接口的凭证码,通常一台机器只需要设置一次即可
Expand Down Expand Up @@ -80,14 +77,15 @@ def post_request(self, api_name, fields='', **kwargs):
file_cache = self.cache_path / f"{hash_key}.pkl"
if file_cache.exists():
df = pd.read_pickle(file_cache)
logger.info(f"从缓存中获取数据,大小:{(file_cache.stat().st_size / 1048576):.2f}MB")
logger.info(f"缓存命中 | API:{api_name};参数:{kwargs};数据量:{df.shape}")
return df

res = requests.post(self.__http_url, json=req_params, timeout=self.__timeout)
if res:
result = res.json()
if result['code'] != 0:
raise Exception(result['msg'])
raise Exception(f"API: {api_name} - {kwargs} 数据获取失败: {result}")

df = pd.DataFrame(result['data']['items'], columns=result['data']['fields'])
df.to_pickle(file_cache)
else:
Expand Down
17 changes: 17 additions & 0 deletions examples/test_offline/test_data_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
sys.path.insert(0, r"D:\ZB\git_repo\waditu\czsc")
import czsc


def test_tushare_pro():
# czsc.set_url_token("******", url="http://api.tushare.pro")
dc = czsc.DataClient(url="http://api.tushare.pro", cache_path="tushare_data")
df = dc.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
try:
df = dc.stock_basic_1(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
except Exception as e:
print(e)


if __name__ == '__main__':
test_tushare_pro()

0 comments on commit 4326a66

Please sign in to comment.