V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tankren
V2EX  ›  Python

哪位大佬给看看还能不能优化一下?

  •  
  •   tankren · 2022-07-30 19:13:40 +08:00 via Android · 1817 次点击
    这是一个创建于 607 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码地址:
    https://github.com/tankren/GoldenTax_Djh_Autofill/blob/main/Auto_mapping.py
    如题,非码农,帮财务部写的一个小工具,背景是金税系统导出 xml 单据号字段缺失。打帮助热线只会让你花几万上云系统...
    逻辑是从批量导入结果 txt 里面查找并填充。
    还有就是用 pyinstaller 打包失败,后来换的 niutka ,可以成功运行,体积高达 30m 是否正常?
    blankmiss
        1
    blankmiss  
       2022-07-30 20:35:16 +08:00
    python 的包好像都挺大的 没有示例文件(脱敏) 也不知道你是哪里出问题
    tankren
        2
    tankren  
    OP
       2022-07-30 21:45:58 +08:00
    @blankmiss #1 运行没啥问题,完全达到预期要求。就是我不知道这样写是不是符合规范,有没有什么需要改进的地方。这个是我一边 Google 一边写的。。。
    neptuno
        3
    neptuno  
       2022-07-30 22:37:01 +08:00
    我是写 java 的,python 忘得差不多了,提一点建议:1 、可以用多线程处理数据,2 、for 循环里面 import re 是不是不符合规范,应该在代码最前面引入吧 3 、for 循环里面是不是可以只写读取的逻辑,把需要写入的放入 list ,最后一次性写入
    tankren
        4
    tankren  
    OP
       2022-07-31 00:12:39 +08:00
    @neptuno #3 感谢建议
    ranleng
        5
    ranleng  
       2022-07-31 11:51:04 +08:00
    如果说规范的话,代码可以用 black 等工具格式化下。
    用了 logging 就不需要 print 了,况且 logging 可以控制输出等级 (INFO, DEBUG etc..)
    for 循环嵌套层数有点多,可以拆成不同的函数
    re.compile 可以放在循环外(因为只需要 compile 一次
    pattern.findall(line)[0] 可能会报错,比如匹配不到内容的时候
    if map == '': 可以直接写成 if not map:
    tankren
        6
    tankren  
    OP
       2022-07-31 16:21:40 +08:00
    @ranleng #5 感谢,回头我试试,特别是匹配为空的处理优化
    tankren
        7
    tankren  
    OP
       2022-08-01 19:52:15 +08:00
    @ranleng 大佬,今天有空撸了一个 GUI 版本,用的 PySide6 ,能麻烦瞅瞅吗?
    https://github.com/tankren/GoldenTax_Djh_Autofill/blob/main/Mapping_GUI.py
    另外有一个打包的问题,还是用 Nuitka 打包,可以正常运行,但是窗体图标没有了,调试的时候或者非单文件打包都能正常显示,但 onefile 就看不到我定义的图标,从 onefile 自己解包的 temp 目录运行也能显示图标,查不到相关的资料。
    ps: nuitka 我加了--include-data-files 参数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:15 · PVG 20:15 · LAX 05:15 · JFK 08:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.