backtrader/utils.py

34 lines
926 B
Python
Raw Normal View History

2025-04-04 15:24:02 +08:00
import os
import yaml
# 模块级单例
_config = None
_engine = None
2025-04-04 15:24:02 +08:00
def load_config():
global _config
if _config is not None:
return _config
2025-04-04 15:24:02 +08:00
config_path = 'config.yaml'
template_path = 'config_template.yaml'
2025-04-04 15:24:02 +08:00
if not os.path.exists(config_path):
if os.path.exists(template_path):
# 如果配置文件不存在但模板存在,则复制模板
import shutil
shutil.copy(template_path, config_path)
print(f"已从 {template_path} 创建新的配置文件 {config_path},请完善其中的配置信息后再运行。")
exit(1)
else:
print(f"错误:配置文件 {config_path} 和模板 {template_path} 均不存在。")
print(f"请创建 {template_path} 文件后再运行。")
exit(1)
2025-04-04 15:24:02 +08:00
with open(config_path, 'r') as f:
_config = yaml.safe_load(f)
return _config
2025-04-04 15:24:02 +08:00