Skip to content

Commit

Permalink
优化 python 部分 check 和 mylog 基础库
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Jan 30, 2022
1 parent 5eeb181 commit e34d223
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 122 deletions.
4 changes: 2 additions & 2 deletions hikyuu/fetcher/stock/zh_stock_a_sina_qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def parse_one_result_sina(resultstr):
result = {}
hku_check_ignore(resultstr, "Invalid input param!")
hku_check_ignore(len(resultstr) > 3 and resultstr[:3] == 'var', "Invalid input param! {}".format(resultstr))
hku_check_ignore(len(resultstr) > 3 and resultstr[:3] == 'var', "Invalid input param! {}", resultstr)
a = resultstr.split(',')
result['market'] = a[0][11:13]
Expand Down Expand Up @@ -61,7 +61,7 @@ def parse_one_result_sina(resultstr):
def parse_one_result_qq(resultstr):
result = {}
hku_check_ignore(resultstr, "Invalid input param!")
hku_check_ignore(len(resultstr) > 3 and resultstr[:2] == 'v_', "Invalid input param! {}".format(resultstr))
hku_check_ignore(len(resultstr) > 3 and resultstr[:2] == 'v_', "Invalid input param! {}", resultstr)
a = resultstr.split('~')
result['market'] = a[0][2:4]
Expand Down
25 changes: 11 additions & 14 deletions hikyuu/gui/HikyuuTDX.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,21 @@ def initLogger(self):
self.log_handler = logging.StreamHandler(self._stream)
FORMAT = logging.Formatter('%(asctime)-15s [%(levelname)s] - %(message)s [%(name)s::%(funcName)s]')
self.log_handler.setFormatter(FORMAT)
add_class_logger_handler(
[
MyMainWindow,
CollectSpotThread, #CollectToMySQLThread, CollectToMemThread,
UsePytdxImportToH5Thread,
UseTdxImportToH5Thread,
ImportTdxToH5Task,
SchedImportThread
],
logging.INFO
)
for name in logging.Logger.manager.loggerDict.keys():
logger = logging.getLogger(name)
logger.addHandler(self.log_handler)
logger.setLevel(logging.DEBUG)
# add_class_logger_handler(
# self.log_handler,
# [
# MyMainWindow,
# CollectSpotThread, #CollectToMySQLThread, CollectToMemThread,
# UsePytdxImportToH5Thread,
# UseTdxImportToH5Thread,
# ImportTdxToH5Task,
# SchedImportThread
# ],
# logging.INFO
# )
# get_default_logger().addHandler(self.log_handler)
# get_default_logger().setLevel(logging.INFO)
# 多进程日志队列
self.mp_log_q = multiprocessing.Queue()
Expand Down
18 changes: 7 additions & 11 deletions hikyuu/gui/data/CollectToMemThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ def __init__(self, config, hku_config_file, market='SH'):
self._phase1_start_time = Datetime(
datetime.datetime.combine(
datetime.date.today(),
datetime.time.fromisoformat(
(config.get('collect', 'phase1_start', fallback='09:05'))
)
datetime.time.fromisoformat((config.get('collect', 'phase1_start', fallback='09:05')))
)
)
self._phase1_end_time = Datetime(
datetime.datetime.combine(
datetime.date.today(),
datetime.time.fromisoformat(
(config.get('collect', 'phase1_end', fallback='09:05'))
)
datetime.time.fromisoformat((config.get('collect', 'phase1_end', fallback='09:05')))
)
)
self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False)
Expand All @@ -60,7 +56,7 @@ def run(self):
hikyuu_init(self.hku_config_file, ignore_preload=True)

stk_list = self.get_stock_list()
hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", self.logger)
hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", logger=self.logger)
self.mutex.tryLock()

end_delta = self._phase1_end_time - self._phase1_end_time.start_of_day()
Expand All @@ -77,14 +73,14 @@ def run(self):
delta = self._interval * ceil((start - self._phase1_start_time) / self._interval
) - (start - self._phase1_start_time)

hku_info('{} 下次采集时间:{}'.format(self.market, start + delta), self.logger)
hku_info('{} 下次采集时间:{}', self.market, start + delta, logger=self.logger)
delta = int(delta.total_milliseconds())
while self.working and not self.cond.wait(self.mutex, int(delta)):
last_time = Datetime.today() + end_delta
start = Datetime.now()
if start >= last_time:
next_time = Datetime.today() + TimeDelta(1) + start_delta
hku_info('{} 明日采集时间:{}'.format(self.market, next_time), self.logger)
hku_info('{} 明日采集时间:{}', self.market, next_time, logger=self.logger)
delta = next_time - start
delta = int(delta.total_milliseconds())
continue
Expand All @@ -106,8 +102,8 @@ def get_stock_list(self):
sm = StockManager.instance()
return [
stk.market_code.lower() for stk in sm
if stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM)
and stk.valid and stk.market.lower() == self.market.lower()
if stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX,
constant.STOCKTYPE_GEM) and stk.valid and stk.market.lower() == self.market.lower()
]

def record_is_valid(self, record):
Expand Down
20 changes: 8 additions & 12 deletions hikyuu/gui/data/CollectToMySQLThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ def __init__(self, config, market='SH'):
self._phase1_start_time = Datetime(
datetime.datetime.combine(
datetime.date.today(),
datetime.time.fromisoformat(
(config.get('collect', 'phase1_start', fallback='09:05'))
)
datetime.time.fromisoformat((config.get('collect', 'phase1_start', fallback='09:05')))
)
)
self._phase1_end_time = Datetime(
datetime.datetime.combine(
datetime.date.today(),
datetime.time.fromisoformat(
(config.get('collect', 'phase1_end', fallback='09:05'))
)
datetime.time.fromisoformat((config.get('collect', 'phase1_end', fallback='09:05')))
)
)
self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False)
Expand All @@ -68,7 +64,7 @@ def stop(self):
self.cond.wakeAll()
self.wait()
if self._connect is not None:
hku_trace('关闭数据库连接', self.logger)
hku_trace('关闭数据库连接', logger=self.logger)
self._connect.close()

def __del__(self):
Expand All @@ -78,7 +74,7 @@ def __del__(self):
def run(self):
self.logger.info("{} 数据采集同步线程启动".format(self.market))
stk_list = self.get_stock_list()
hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", self.logger)
hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", logger=self.logger)
self.mutex.tryLock()

end_delta = self._phase1_end_time - self._phase1_end_time.start_of_day()
Expand All @@ -95,14 +91,14 @@ def run(self):
delta = self._interval * ceil((start - self._phase1_start_time) / self._interval
) - (start - self._phase1_start_time)

hku_info('{} 下次采集时间:{}'.format(self.market, start + delta), self.logger)
hku_info('{} 下次采集时间:{}', self.market, start + delta, logger=self.logger)
delta = int(delta.total_milliseconds())
while self.working and not self.cond.wait(self.mutex, int(delta)):
last_time = Datetime.today() + end_delta
start = Datetime.now()
if start >= last_time:
next_time = Datetime.today() + TimeDelta(1) + start_delta
hku_info('{} 明日采集时间:{}'.format(self.market, next_time), self.logger)
hku_info('{} 明日采集时间:{}', self.market, next_time, logger=self.logger)
delta = next_time - start
delta = int(delta.total_milliseconds())
continue
Expand Down Expand Up @@ -132,8 +128,8 @@ def process_one_record(self, record):
table = get_table(connect, self.market, record['code'], 'day')
sql = 'replace into {} (date, open, high, low, close, amount, count) \
values ({}, {}, {}, {}, {}, {}, {})'.format(
table, current_date, record['open'], record['high'], record['low'], record['close'],
record['amount'], record['volumn']
table, current_date, record['open'], record['high'], record['low'], record['close'], record['amount'],
record['volumn']
)
cur = connect.cursor()
cur.execute(sql)
Expand Down
2 changes: 1 addition & 1 deletion hikyuu/gui/data/ImportPytdxTimeToH5Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, log_queue, queue, sqlitefile, market, quotations, ip, port, d
self.max_days = int(max_days)

def __call__(self):
capture_multiprocess_all_logger(self.log_queue, get_default_logger().level)
capture_multiprocess_all_logger(self.log_queue)
count = 0
connect = sqlite3.connect(self.sqlitefile, timeout=1800)
try:
Expand Down
2 changes: 1 addition & 1 deletion hikyuu/gui/data/ImportPytdxToH5Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, log_queue, queue, config, market, ktype, quotations, ip, port
self.startDatetime = start_datetime

def __call__(self):
capture_multiprocess_all_logger(self.log_queue, get_default_logger().level)
capture_multiprocess_all_logger(self.log_queue)
if self.config.getboolean('hdf5', 'enable', fallback=True):
sqlite_file = "{}/stock.db".format(self.config['hdf5']['dir'])
connect = sqlite3.connect(sqlite_file, timeout=1800)
Expand Down
2 changes: 1 addition & 1 deletion hikyuu/gui/data/ImportPytdxTransToH5Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, log_queue, queue, sqlitefile, market, quotations, ip, port, d
self.max_days = int(max_days)

def __call__(self):
capture_multiprocess_all_logger(self.log_queue, get_default_logger().level)
capture_multiprocess_all_logger(self.log_queue)
count = 0
connect = sqlite3.connect(self.sqlitefile, timeout=1800)
try:
Expand Down
2 changes: 1 addition & 1 deletion hikyuu/gui/data/ImportTdxToH5Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __del__(self):
pass

def __call__(self):
capture_multiprocess_all_logger(self.log_queue, get_default_logger().level)
capture_multiprocess_all_logger(self.log_queue)
use_hdf = False
if self.config.getboolean('hdf5', 'enable', fallback=True):
sqlite_file = "{}/stock.db".format(self.config['hdf5']['dir'])
Expand Down
2 changes: 1 addition & 1 deletion hikyuu/gui/data/ImportWeightToSqliteTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, log_queue, queue, config, dest_dir):
self.msg_name = 'IMPORT_WEIGHT'

def __call__(self):
capture_multiprocess_all_logger(self.log_queue, get_default_logger().level)
capture_multiprocess_all_logger(self.log_queue)
total_count = 0
try:
if self.config.getboolean('hdf5', 'enable', fallback=True):
Expand Down
55 changes: 19 additions & 36 deletions hikyuu/gui/spot_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ def print_spot_list(buf):

@hku_catch()
def parse_phase(phase):
return [
TimeDelta(hours=int(a[0]), minutes=int(a[1]))
for a in [x.split(':') for x in phase.split('-')]
]
return [TimeDelta(hours=int(a[0]), minutes=int(a[1])) for a in [x.split(':') for x in phase.split('-')]]


def next_delta(start_time, interval, phase1_delta, phase2_delta, ignore_weekend):
Expand Down Expand Up @@ -165,33 +162,23 @@ def next_delta(start_time, interval, phase1_delta, phase2_delta, ignore_weekend)

def collect(use_proxy, source, seconds, phase1, phase2, ignore_weekend):
phase1_delta = parse_phase(phase1)
hku_error_if(
phase1_delta is None or len(phase1_delta) != 2,
"无效参数 phase1: {}".format(phase1),
callback=lambda: exit(1)
)
hku_error_if(
phase1_delta[0] > phase1_delta[1],
"无效参数 phase1: {}, 结束时间应大于等于起始时间".format(phase1),
callback=lambda: exit(1)
)
if phase1_delta is None or len(phase1_delta) != 2:
hku_error("无效参数 phase1: {}".format(phase1))
exit(1)
if phase1_delta[0] > phase1_delta[1]:
hku_error("无效参数 phase1: {}, 结束时间应大于等于起始时间".format(phase1))
exit(1)

phase2_delta = parse_phase(phase2)
hku_error_if(
phase2_delta is None or len(phase2_delta) != 2,
"无效参数 phase2: {}".format(phase2),
callback=lambda: exit(1)
)
hku_error_if(
phase2_delta[0] > phase2_delta[1],
"无效参数 phase2: {}, 结束时间应大于等于起始时间".format(phase2),
callback=lambda: exit(1)
)
hku_error_if(
phase1_delta[1] > phase2_delta[0],
"无效参数 phase1: {}, phase2: {}, phase2 起始时间应大于等于 phase1 结束时间".format(phase1, phase2),
callback=lambda: exit(1)
)
if phase2_delta is None or len(phase2_delta) != 2:
hku_error("无效参数 phase2: {}".format(phase2))
exit(1)
if phase2_delta[0] > phase2_delta[1]:
hku_error("无效参数 phase2: {}, 结束时间应大于等于起始时间".format(phase2))
exit(1)
if phase1_delta[1] > phase2_delta[0]:
hku_error("无效参数 phase1: {}, phase2: {}, phase2 起始时间应大于等于 phase1 结束时间".format(phase1, phase2))
exit(1)

hku_logger.info("采集时间段1:{}".format(phase1))
hku_logger.info("采集时间段2:{}".format(phase2))
Expand All @@ -205,8 +192,8 @@ def collect(use_proxy, source, seconds, phase1, phase2, ignore_weekend):

sm = StockManager.instance()
stk_list = [
stk.market_code.lower() for stk in sm if stk.valid and stk.type in
(constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM)
stk.market_code.lower() for stk in sm
if stk.valid and stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM)
]

spot_topic = ':spot:'
Expand Down Expand Up @@ -234,11 +221,7 @@ def batch_func(records):
start_time = Datetime.now()
pub_sock.send("{}{}".format(spot_topic, '[start spot]').encode('utf-8'))
records = get_spot_parallel(stk_list, source, use_proxy, batch_func)
hku_info(
"{}:{}:{} 采集数量: {}".format(
start_time.hour, start_time.minute, start_time.second, len(records)
)
)
hku_info("{}:{}:{} 采集数量: {}".format(start_time.hour, start_time.minute, start_time.second, len(records)))
pub_sock.send('{}{}'.format(spot_topic, '[end spot]').encode('utf-8'))
delta = next_delta(start_time, seconds, phase1_delta, phase2_delta, ignore_weekend)
time.sleep(delta.total_seconds())
Expand Down
17 changes: 13 additions & 4 deletions hikyuu/util/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def checkif(expression, message, excepion=None, **kwargs):
raise excepion(message, **kwargs)


def hku_check(exp, msg):
def hku_check(exp, msg, *args, **kwargs):
if not exp:
st = traceback.extract_stack()[-2]
check_exp = st._line.split(',')[0]
errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg, st.name, st.filename, st.lineno)
errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg.format(*args, **kwargs), st.name, st.filename, st.lineno)
raise HKUCheckError(exp, errmsg)
Expand All @@ -70,12 +70,21 @@ def hku_check_throw(expression, message, excepion=None, **kwargs):
raise excepion(errmsg, **kwargs)
def hku_check_ignore(exp, msg=None):
def hku_check_ignore(exp, *args, **kwargs):
"""可忽略的检查"""
if not exp:
st = traceback.extract_stack()[-2]
check_exp = st._line.split(',')[0]
errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg, st.name, st.filename, st.lineno)
msg = kwargs.pop("msg") if "msg" in kwargs else ''
if msg:
errmsg = "{}) {} [{}] [{}:{}]".format(
check_exp, msg.format(*args, **kwargs), st.name, st.filename, st.lineno
)
elif args:
msg = args[0]
errmsg = "{}) {} [{}] [{}:{}]".format(
check_exp, msg.format(*args[1:], **kwargs), st.name, st.filename, st.lineno
)
raise HKUIngoreError(exp, errmsg)
Expand Down
Loading

0 comments on commit e34d223

Please sign in to comment.