​量化交易之python篇 - hdp文件读写的封装

时间:2021-03-10 00:40:35


import pandas as pd

class TQZHPD5Operator:

@classmethod
def write_keyValue(cls, path: str, key: str, value, rewrite: bool = False):
mode = 'a'
if rewrite:
mode = 'w'

with pd.HDFStore(path=path, mode=mode) as hdf5_file:
hdf5_file.put(key=key, value=value)


@classmethod
def write_map(cls, path: str, content: dict):
with pd.HDFStore(path=path, mode='w') as hdf5_file:
for key, value in content.items():
hdf5_file.put(key=key, value=value)


@classmethod
def get_allkeys(cls, path: str):
with pd.HDFStore(path=path, mode='r') as hdf5_file:
return hdf5_file.keys()

@classmethod
def get_value(cls, path: str, key: str):
with pd.HDFStore(path=path, mode='r') as hdf5_file:
return hdf5_file.get(key)

@classmethod
def get_content(cls, path: str):
with pd.HDFStore(path=path, mode='r') as hdf5_file:
ret_map = {}
for key, value in hdf5_file.items():
ret_map[key] = hdf5_file.get(key)

return ret_map