Python 如何修改 py 文件内函数的 docstring - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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

Python 如何修改 py 文件内函数的 docstring

  •  
  •   css3 Feb 18, 2021 2126 views
    This topic created in 1894 days ago, the information mentioned may be changed or developed.

    有这么个需求,需要用 py 脚本修改一些 py 文件内的函数 docstring, 并保存覆盖

    ├── test.py ├── modify.py 
    # test.py 待修改 docstring 的函数文件 def hello_world(): """ this function is a demo, 需要修改这里,并保存到文件 """ return 'hello world' """ # 这是一个干扰注释,不能修改这里 """ 
    # modify.py 修改 test.py 的 docstring 函数 from test import hello_world def modify_docstring(): print(hello_world.__doc__) # this function is a demo, 需要修改这里,并保存到文件 hello_world.__doc__ = 'I\' am new docstring' print(hello_world.__doc__) # I' am new docstring # 这样可以赋值,但只是在内存中,怎么修改写入 test.py 文件呢? 
    12 replies    2021-02-20 23:24:21 +08:00
    guyeu
        1
    guyeu  
       Feb 18, 2021
    难点在语法分析里。。不过只是识别 docstring 的话,感觉正则就行了。。
    nthhdy
        2
    nthhdy  
       Feb 18, 2021
    楼主 import 之后直接赋值 docstring 的方法,据我所知做不到改源文件的。
    可以考虑用 ast module,parse 出来之后改,然后再生成代码。
    xchaoinfo
        3
    xchaoinfo  
       Feb 18, 2021 via Android
    ast
    codists
        4
    codists  
       Feb 19, 2021
    个人认为这涉及到了文件读写。

    ```python
    # modify.py 修改 test.py 的 docstring 函数

    import re

    input_f = open('test.py', 'r')
    txt = re.sub(r'"""[\d\D]*?"""', r'""I\' am new docstring"""', input_f.read(), 1)

    # 写入内容
    output_f = open('test.py', 'w')
    output_f.write(txt)

    # 关闭文件
    input_f.close()
    output_f.close()

    ```
    julyclyde
        5
    julyclyde  
       Feb 19, 2021
    嘿,这是打算盗版谁家的软件产品啊
    css3
        6
    css3  
    OP
       Feb 19, 2021
    @guyeu 正则怕误伤"友军"
    css3
        7
    css3  
    OP
       Feb 19, 2021
    @nthhdy
    @xchaoinfo 感谢老哥们,尝试了一下 ast,貌似把原来的多行 doc 给用'\n'合并成了一行去了,还没找到解决办法
    @julyclyde 没有啊,老哥
    @codists 多谢老哥,不优先考虑用正则,因为大量文件里边除了 docstring 用了三引号,还有大量注释,怕误伤友军
    nthhdy
        8
    nthhdy  
       Feb 19, 2021
    > 貌似把原来的多行 doc 给用'\n'合并成了一行去了,还没找到解决办法

    感觉这个想办法解决一下就行了,就当是 IDE 的一个小功能呗,挺常见的,应不难搞。
    css3
        9
    css3  
    OP
       Feb 19, 2021
    @nthhdy 最新发现解析后生成的 docstring 的三个引号也变成单引号了,感觉要处理起来也挺麻烦的
    https://stackoverflow.com/questions/53564301/insert-docstring-attributes-in-a-python-file
    css3
        10
    css3  
    OP
       Feb 19, 2021 via iPhone
    @nthhdy
    @xchaoinfo
    老哥们,我通过 ast 模块读取 python 文件函数的 docstring,然后通过 unpack 解析出来,发现原来 docstring 的三引号多行内容变成了单引号单行,是通过"\n"连接成单行的,这跟我想要的还是有点差距,我只是想在原有的 docstring 中增减一些描述,不需要也不能改变其结构,使用的代码完全复制的我楼上中链接中 stackoverflow 的
    nthhdy
        11
    nthhdy  
       Feb 19, 2021
    我看了一下 ast 的文档,int float string 这些 literal 在解析树上都表示为 Constant 对象,也就是说,string 是单引号、双引号还是三个引号,这个信息在解析树上已经丢失了。所以你想做这件事,ast 的确做不到。不知道是否有边角的配置或者 hack 的办法可以。

    但这个思路是正解,先 parse 出来,做改动,再 dump 成代码。我没注意到 ast 的这个细节,所以只是工具不适合,可以再搜索一下同类型的工具。这么多做 code format 的工具呢,它们应该实现过类似的机制才对。

    这个思路是个比较通用的解法,再复杂一些的需求也可以用这个路子。如果要解决的问题是一次性的,变体又有限,可以尝试用 adhoc 的办法。比如用正则,甚至写一个自己代码都适用的规则。看问题的规模和复杂度而定吧。
    css3
        12
    css3  
    OP
       Feb 20, 2021 via iPhone
    @nthhdy 好的,多谢老哥指点了,还是采用了正则表达式
    About     Help     Advertise     Blog     API     FAQ     Solana     5738 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 102ms UTC 07:49 PVG 15:49 LAX 00:49 JFK 03:49
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86