def cat_stk_opt_record(input_str, json_file_url, tc): if opt_record_lock.acquire(): r = '未知错误' try: r = cat_stk_b_opt_sub(stk_name=input_str.split(' ')[1], json_file_url=json_file_url) except Exception as e: r = '读写opt_record.json文件失败!原因:\n' + str(e) finally: opt_record_lock.release() text_append_color(tc, r + '\n')
def add_opt(input_str, json_file_url, tc): if opt_record_lock.acquire(): r = ['未知错误'] try: r = add_opt_to_json_sub(input_str, json_file_url, tc) except Exception as e: r = ['读写opt_record.json文件失败!原因:\n' + str(e)] finally: opt_record_lock.release() for str_ in r: text_append_color(tc, str_ + '\n')
def save_json(self): """ 保存json文件 :return: """ if opt_record_lock.acquire(): try: with open(self.opt_record_file_url, 'w') as f: json.dump(self.opt_record, f) except Exception as e: print('函数OptRecord.save_json:出错! \n' + str(e)) finally: opt_record_lock.release()
def read_opt_json(stk_code, json_file_url_): if opt_record_lock.acquire(): try: if os.path.exists(json_file_url_): with open(json_file_url_, 'r') as f: json_p = json.load(f) if stk_code in json_p.keys(): return json_p[stk_code] else: return {} else: return {} except Exception as e: pass finally: opt_record_lock.release()
def read_json(self): """ 读取json文件 要不要加try catch? :return: """ # 已有文件,打开读取 if os.path.exists(self.opt_record_file_url): if opt_record_lock.acquire(): try: with open(self.opt_record_file_url, 'r') as f: return json.load(f) except Exception as _e: print('函数OptRecord.read_json:出错! \n' + str(_e)) finally: opt_record_lock.release() else: return {}
# coding=utf-8