refactor(factors): 简化 add_factor API 并默认启用 metadata

- 合并 add_factor_by_name 到 add_factor,支持三种调用方式
- FactorManager 构造函数改为可选参数,使用默认路径
- FactorEngine 默认启用 metadata,无需手动配置路径
This commit is contained in:
2026-03-12 22:34:25 +08:00
parent 2bb7718dd1
commit ced7a929c3
7 changed files with 496 additions and 254 deletions

View File

@@ -53,23 +53,32 @@ class FactorManager:
_conn: DuckDB连接对象懒加载
Example:
>>> manager = FactorManager("data/factors.jsonl")
>>> manager = FactorManager() # 使用默认路径
>>> df = manager.get_factors_by_name("mom_5d")
>>> print(df["dsl"][0])
"""
def __init__(self, filepath: str) -> None:
_DEFAULT_FILENAME = "factors.jsonl"
def __init__(self, filepath: Optional[str] = None) -> None:
"""初始化因子管理器。
如果文件不存在会自动创建空的JSONL文件。
Args:
filepath: JSONL文件路径相对或绝对路径
filepath: JSONL文件路径相对或绝对路径为None时使用默认路径
Raises:
FileOperationError: 当文件创建失败时
"""
self.filepath = Path(filepath).resolve()
if filepath is None:
# 使用默认路径:从配置读取数据目录
from src.config.settings import settings
self.filepath = settings.data_path_resolved / self._DEFAULT_FILENAME
else:
self.filepath = Path(filepath).resolve()
self._conn: Optional[duckdb.DuckDBPyConnection] = None
# 确保文件存在