diff --git a/README.md b/README.md index e6d4e66..a402c9e 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ analyzer = chanlun.立体分析器("BTCUSD", [60, 60*5, 60*5*6], config) ```bash pip install maturin +# 推荐:一键清理缓存 + 构建 + 安装 +./clean_install.sh + +# 或手动: # 开发模式(直接安装到当前 venv) maturin develop @@ -48,6 +52,12 @@ maturin build --release pip install target/wheels/chanlun-*.whl ``` +> **注意**:若修改了 `chan.py`,安装前需清除 `__pycache__`,否则旧 `.pyc` 会被打包进 wheel 导致修改不生效: +> ```bash +> find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null +> find . -type f -name "*.pyc" -delete 2>/dev/null +> ``` + 也可使用项目内的 `build.sh`: ```bash diff --git a/chan.py b/chan.py index d7d0029..24f2d17 100644 --- a/chan.py +++ b/chan.py @@ -1114,10 +1114,13 @@ class 缠论配置: default = field_info["default"] try: - # 布尔类型验证 + # 布尔类型验证(与 Rust 绑定层 coerce_strings_to_numbers 对齐) if type_ is bool: if not isinstance(value, bool): - setattr(self, fname, bool(value)) + if isinstance(value, str) and value.lower() in ("true", "false"): + setattr(self, fname, value.lower() == "true") + else: + setattr(self, fname, default) # 整数类型验证 elif type_ is int: @@ -1164,7 +1167,22 @@ class 缠论配置: :return: 仅保留当前类已知字段的字典 """ valid_fields = cls.model_fields().keys() - cleaned = {k: v for k, v in values.items() if k in valid_fields} + cleaned = {} + for k, v in values.items(): + if k not in valid_fields: + continue + # 字符串值类型强制转换(与 Rust 绑定层 coerce_strings_to_numbers 对齐) + if isinstance(v, str): + if v.lower() in ("true", "false"): + v = v.lower() == "true" + elif v.lstrip("-").isdigit(): + v = int(v) + else: + try: + v = float(v) + except ValueError: + pass + cleaned[k] = v return cleaned def to_dict(self) -> dict: @@ -7144,7 +7162,9 @@ def 测试_指标挂载(配置: 缠论配置): 观察员.增加原始K线(k线) if i == 500: assert 观察员.普通K线序列[0].指标.macd_12_26_9 is not None, "指标挂载失败" - print(观察员.普通K线序列[0].指标["macd_12_26_9"]) + print(观察员.普通K线序列[-1].指标["macd_12_26_9"]) + print(观察员.普通K线序列[-1].macd) + break 消耗用时 = datetime.now() - 启动时间 diff --git a/chanlun-py/Cargo.toml b/chanlun-py/Cargo.toml index d2a9b88..abff485 100644 --- a/chanlun-py/Cargo.toml +++ b/chanlun-py/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chanlun-py" -version = "26.6.74" +version = "26.6.77" edition = "2024" description = "缠论技术分析库 — Rust 高性能 Python 绑定" authors = ["YuYuKunKun"] diff --git a/chanlun-py/chanlun/chan.py b/chanlun-py/chanlun/chan.py index 824ebc6..4ffc679 100644 --- a/chanlun-py/chanlun/chan.py +++ b/chanlun-py/chanlun/chan.py @@ -1114,10 +1114,13 @@ class 缠论配置: default = field_info["default"] try: - # 布尔类型验证 + # 布尔类型验证(与 Rust 绑定层 coerce_strings_to_numbers 对齐) if type_ is bool: if not isinstance(value, bool): - setattr(self, fname, bool(value)) + if isinstance(value, str) and value.lower() in ("true", "false"): + setattr(self, fname, value.lower() == "true") + else: + setattr(self, fname, default) # 整数类型验证 elif type_ is int: @@ -1164,7 +1167,22 @@ class 缠论配置: :return: 仅保留当前类已知字段的字典 """ valid_fields = cls.model_fields().keys() - cleaned = {k: v for k, v in values.items() if k in valid_fields} + cleaned = {} + for k, v in values.items(): + if k not in valid_fields: + continue + # 字符串值类型强制转换(与 Rust 绑定层 coerce_strings_to_numbers 对齐) + if isinstance(v, str): + if v.lower() in ("true", "false"): + v = v.lower() == "true" + elif v.lstrip("-").isdigit(): + v = int(v) + else: + try: + v = float(v) + except ValueError: + pass + cleaned[k] = v return cleaned def to_dict(self) -> dict: @@ -7144,7 +7162,9 @@ def 测试_指标挂载(配置: 缠论配置): 观察员.增加原始K线(k线) if i == 500: assert 观察员.普通K线序列[0].指标.macd_12_26_9 is not None, "指标挂载失败" - print(观察员.普通K线序列[0].指标["macd_12_26_9"]) + print(观察员.普通K线序列[-1].指标["macd_12_26_9"]) + print(观察员.普通K线序列[-1].macd) + break 消耗用时 = datetime.now() - 启动时间 diff --git a/chanlun-py/pyproject.toml b/chanlun-py/pyproject.toml index af6500a..b271448 100644 --- a/chanlun-py/pyproject.toml +++ b/chanlun-py/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "chanlun" -version = "2606.74" +version = "2606.77" description = "缠论技术分析库 — Rust 高性能实现" readme = { file = "README.md", content-type = "text/markdown" } license = { file = "LICENSE", content-type = "text/plain" } diff --git a/clean_install.sh b/clean_install.sh new file mode 100755 index 0000000..166db0b --- /dev/null +++ b/clean_install.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -e +cd "$(dirname "$0")" + +echo "=== 1/4 清除 Python 缓存 ===" +find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null +find . -type f -name "*.pyc" -delete 2>/dev/null +echo " Python 缓存已清除" + +echo "=== 2/4 清除 Cargo 编译缓存 ===" +rm -rf chanlun/target chanlun-py/target +echo " target/ 已清除" + +echo "=== 3/4 构建 Release ===" +cd chanlun-py +maturin build --release +echo " 构建完成" + +echo "=== 4/4 安装 ===" +pip install --break-system-packages --force-reinstall --no-deps \ + target/wheels/chanlun-*.whl +echo " 安装完成" + +echo +echo "✓ 清理 + 构建 + 安装完毕" +echo " pip show chanlun | grep Version" +pip show chanlun 2>/dev/null | grep Version