量化交易之hft篇 - re dump log源文件


import os

from datetime import datetime

from vnpy.trader.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator
from vnpy.trader.tqz_extern.tools.pandas_operator.pandas_operator import pandas

from vnpy.app.tqz_hft_parser_app.tqz_constant import TQZLogTitles


class TQZHftParserApp:

    # --- api part ---
    @classmethod
    def tqz_reDump_format_log(cls, source_log_all_path):
        """
        Parse source_log_file with all_path to code_csv.
        """

        log_dataframe = cls.__get_source_log_dataframe(source_log_all_path=source_log_all_path)

        code_parser_fold = f'{cls.__parser_result_fold()}/{"hft_" + datetime.now().strftime("%Y%m%d")}'
        if os.path.exists(code_parser_fold) is False:
            os.mkdir(code_parser_fold)

        [pandas.DataFrame.to_csv(
            cls.__get_code_log_dataframe(source_log_all_path=source_log_all_path, code=code),
            path_or_buf=f'{code_parser_fold}/{code.replace(".", "_")}.csv',
            index=False
        ) for code in cls.__get_all_codes(log_dataframe)]


    # --- private part ---
    @classmethod
    def __get_source_log_all_path(cls):
        """
        Get all_path of source log file.
        """

        source_log_filename = "hft_" + datetime.now().strftime("%Y%m%d") + ".log"
        source_log_all_path = f'{cls.__source_log_fold()}/{source_log_filename}'

        assert os.path.exists(source_log_all_path) is True, f'{source_log_all_path} not exist.'
        return source_log_all_path


    @classmethod
    def __get_source_log_dataframe(cls, source_log_all_path: str):
        """
        Get dataframe based on source log with source_log_all_path.
        """

        line_log_dictionary_list = []
        for line in open(source_log_all_path).readlines():
            line_log_dictionary = {}
            for line_log_item in line.rstrip().split(" ")[-1].replace('[', '').replace(']', '').split(","):
                line_log_dictionary[line_log_item.split(':')[0]] = line_log_item.split(':')[1]
            line_log_dictionary_list.append(line_log_dictionary)

        return pandas.DataFrame(line_log_dictionary_list)

    @classmethod
    def __get_code_log_dataframe(cls, source_log_all_path: str, code: str):
        """
        Get code dataframe based on source log with source_log_all_path and code_name.
        """

        source_log_dataframe = cls.__get_source_log_dataframe(source_log_all_path=source_log_all_path)

        return source_log_dataframe.loc[source_log_dataframe[TQZLogTitles.CODE.value]==code]

    @staticmethod
    def __get_all_codes(source_log_dataframe: pandas.DataFrame) -> list:
        """
        Get codes list based on source log.
        """
        return list(set(list(source_log_dataframe[TQZLogTitles.CODE.value])))

    @staticmethod
    def __parser_result_fold():
        return f'{TQZFilePathOperator.father_path(source_path=__file__)}/parser_result_fold'

    @staticmethod
    def __source_log_fold():
        return f'{TQZFilePathOperator.father_path(source_path=__file__)}/source_log_fold'


if __name__ == '__main__':
    source_log_all_path_test = f'{TQZFilePathOperator.father_path(source_path=__file__)}/source_log_fold/{"hft_" + "20211123" + ".log"}'

    TQZHftParserApp.tqz_reDump_format_log(source_log_all_path=source_log_all_path_test)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值