Repository navigation
feat(contributor): 新增 t0 与 Excel 按列拆分的修正版实现 - #187
Open
wenxiaotan wants to merge 1 commit into
Open
wenxiaotan wants to merge 1 commit into
wenxiaotan wants to merge 1 commit into
Conversation
在 contributors/wenxiaotan/ 下新增两个可独立调用的修正版实现,
规避 python-office 当前版本中已复现的缺陷。按仓库贡献约定,
本提交只新增本目录文件,未改动 office/ 等任何其他目录。
## 缺陷一:t0 的 min_rate 未做 Decimal 规范化
office.api.finance.t0 把 w_rate/buy_price/sale_price/stamp_tax 都转成了
Decimal,唯独漏了 min_rate。当买卖金额低于 RATE_LINE 时手续费直接取
min_rate(仍是 int/float),随后与 Decimal 相减即抛 TypeError。
CLI 把 min_rate 声明为 type: float,因此以下命令必然失败:
$ python -m office finance t0 11.99 12.26 700 --min-rate 5
po: error: TypeError: unsupported operand type(s) for -:
'decimal.Decimal' and 'float'
另:函数签名与文档标注 -> float,实际返回 Decimal,影响调用方的
isinstance 判断与 json 序列化。
## 缺陷二:Excel 按列拆分的 4 个问题
office.api.excel.split_excel_by_column 在 poexcel 未导出该函数时会回退到
office/lib/excel/SplitExcel.py。经核实当前 poexcel 确实未导出(hasattr
返回 False),因此回退实现就是实际执行的那份代码,问题有 4 个:
1. filepath.replace('.xls', ...) 会命中路径中任意位置的 .xls(含目录名),
例如 .../archive.xls.v2/demo.xls 会输出到不存在的目录并抛
FileNotFoundError。.xlsx 分支同样写法、同样问题。
2. .xls 分支的 sheet 查找未纳入异常保护,表名错误时直接把 XLRDError 抛给
调用者,而 .xlsx 分支返回提示字符串,两格式行为不一致。
3. .xls 分支缺少列号越界检查,column 越界抛 IndexError。
4. column=0 会被 Python 负索引解释为最后一列,产生「不报错但拆错列」的结果;
CLI 帮助却写的是 column index (0-based)。
## 交付内容
- finance_t0_fix.py :修正版 t0,附带 t0_safe 容错包装
- split_excel_fix.py :修正版拆分,列号从 1 开始,输出路径按扩展名精确重建
- test_contribution.py:26 个用例,覆盖正常路径与上述全部缺陷场景
- README.md :缺陷复现记录、修正版用法、给上游的修复建议
## 测试
$ cd contributors/wenxiaotan && python -m unittest test_contribution
Ran 26 tests
OK
## 说明
contributors/ 目录不随 pip 包发布(该目录无 __init__.py,setup.cfg 的
packages = find: 不会收录),已实测确认。因此本贡献定位为「可立即使用的
修正实现 + 给维护者的修复参考」;若希望修复直接惠及 pip 用户,需在
office/ 内改动(参考已合并的 CoderWanFeng#161、CoderWanFeng#180)。
仅使用 openpyxl/xlrd/xlwt/tqdm,均为现有依赖链中的包,未引入新依赖。
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
说明
按仓库贡献约定,在
contributors/wenxiaotan/下新增两个修正版实现,未改动office/、skills/等任何其他目录。本 PR 涉及两个我已在本机复现的缺陷(基准:
developHEAD549bdc9,v1.1.2,Python 3.12)。缺陷一:
t0的min_rate未做Decimal规范化office/api/finance.py:39-49把w_rate、buy_price、sale_price、stamp_tax都转成了Decimal,唯独漏了min_rate。当买卖金额低于RATE_LINE时手续费直接取min_rate(此时仍是int/float),与Decimal相减即抛异常。CLI(
office/cli/commands.py)把min_rate声明为type: float,因此该命令必然失败:另:签名与文档均标注
-> float,实际返回Decimal:建议修复:
缺陷二:Excel 按列拆分的 4 个问题
office.api.excel.split_excel_by_column在poexcel未导出该函数时回退到office/lib/excel/SplitExcel.py。经核实当前发布的 poexcel 确实未导出,因此回退实现就是实际执行的那份代码:1. 输出路径被字符串替换破坏(
filepath.replace('.xls', ...)命中路径中任意位置的.xls,含目录名):.xlsx分支同样写法、同样问题。2. 错误的工作表名抛异常(
.xls的 sheet 查找未纳入try,而.xlsx分支返回提示字符串):3.
.xls分支缺少列号越界检查:4.
column=0静默拆错列(实现是 1-based,但 CLI 帮助写column index (0-based);0 被负索引解释为最后一列):column=1column=3column=0这是「不报错但结果错误」,排查成本最高。
本 PR 交付内容
finance_t0_fix.pyt0,附带t0_safe容错包装split_excel_fix.pytest_contribution.pyREADME.md仅使用
openpyxl/xlrd/xlwt/tqdm,均为现有依赖链中的包,未引入新依赖。一点需要维护者确认的事
contributors/目录不随 pip 包发布(该目录无__init__.py,setup.cfg的packages = find:不收录),已实测确认:因此本贡献定位为「可立即使用的修正实现 + 修复参考」。如果维护者希望这两个修复直接惠及
pip install python-office的用户,我可以改为直接修改office/api/finance.py与office/lib/excel/SplitExcel.py(参考已合并的 #161、#180 的做法),并补充对应的回归测试。请告知倾向哪种方式。行为变更提示
column=0从「静默按最后一列拆分」变为返回错误提示,这是有意的修正。现有测试与文档示例均使用column >= 1,合法列号的输出结果完全不变,输出文件命名规则也保持不变。