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
unity0703
V2EX  ›  Python

整理了一本书《Python 代码、单元测试和项目规范》,供大家参考

  •  
  •   unity0703 ·
    ee0703 · 2015-12-05 18:27:55 +08:00 · 6388 次点击
    这是一个创建于 3036 天前的主题,其中的信息可能已经有所发展或是发生改变。

    关于 python 项目,尤其是 web 项目,并没有一个比较完整(包含代码、测试、项目)的开源规范,于是我整理(chaoxi)了一本《 Python 代码、单元测试和项目规范》,以供大家参考,代码规范部分主要基于 PEP8

    目前书还没有写完,可能存在很多错误和不合理的地方,欢迎大家参与编辑和指正

    下载和预览(在线阅读、 pdf 、 epub 、 mobi ):
    https://www.gitbook.com/book/python-guide/python-style-guide/details

    项目地址(欢迎 PR 和 issu):
    https://github.com/ee0703/python-style-guide

    特别感谢 @sinux 提供第二章内容:单元测试规范

    23 条回复    2015-12-15 22:06:25 +08:00
    holyzhou
        1
    holyzhou  
       2015-12-05 18:33:15 +08:00
    不错 对我这种新手很有帮助
    congeec
        2
    congeec  
       2015-12-05 18:36:34 +08:00   ❤️ 1
    很好,不过不够详细。比如说程序设计规范里 filter 和 map 的例子就不合适,这种情况下用 listcomp 最好。当然,你要是在前面说明编程范式统一用函数式,这个例子还是不错的
    shyling
        3
    shyling  
       2015-12-05 19:12:29 +08:00   ❤️ 1
    我觉得有些写法说它“错误”不太好吧。。。不推荐的写法感觉会好点
    xufang
        4
    xufang  
       2015-12-05 19:15:35 +08:00
    打开扫了一篇,非常好。单元测试这一块说得不错,堪称亮点。

    希望能加入更多内容。好顶赞。
    unity0703
        5
    unity0703  
    OP
       2015-12-05 19:17:31 +08:00
    @shyling 嗯,多谢指正,用 '不推荐的写法' 比较好
    unity0703
        6
    unity0703  
    OP
       2015-12-05 19:21:02 +08:00
    @congeec 多谢指正,这里确实列表生成式好一点,性能上会快很多
    XianZaiZhuCe
        7
    XianZaiZhuCe  
       2015-12-05 19:22:05 +08:00 via Android
    求 php 版本(逃
    neoblackcap
        8
    neoblackcap  
       2015-12-05 19:33:31 +08:00   ❤️ 1
    @unity0703 filter 函数在 Python 3 与生成器表达式是一个样,所以我觉得首要说明用什么版本的 Python

    其二,正则表达式那边,我觉得尽可能不如说, 2 次以上的调用则要编译,否则就可以略去编译。
    mahone3297
        9
    mahone3297  
       2015-12-05 19:36:18 +08:00   ❤️ 1
    @XianZaiZhuCe 最近不是有本 moder php 么
    还要 php the right way
    Yc1992
        10
    Yc1992  
       2015-12-05 19:46:06 +08:00
    很好,收藏了,感谢楼主
    KyleMeow
        11
    KyleMeow  
       2015-12-05 20:04:36 +08:00
    GitBook PDF 的中文字体太奇怪了吧,楷体宋体混合,大小不一
    unity0703
        12
    unity0703  
    OP
       2015-12-05 20:27:52 +08:00 via Android
    @KyleMeow 这个好像是 gitbook 的问题,我找找看有没有解决办法
    deepurple
        13
    deepurple  
       2015-12-05 20:55:17 +08:00   ❤️ 1
    @KyleMeow GitBook 的中文字体一贯就是那个丑陋的样子
    staticor
        14
    staticor  
       2015-12-05 21:17:35 +08:00
    收藏+感谢. 恰好响应了我前阵子对测试, UNITTEST, 甚至是 TTD 开发流程上的一些困惑.
    unity0703
        15
    unity0703  
    OP
       2015-12-05 21:53:10 +08:00
    @neoblackcap 多谢指正
    KyleMeow
        16
    KyleMeow  
       2015-12-05 22:07:13 +08:00 via iPhone
    @deepurple 嗯,其他教程的也是,只能下 EPUB 和 MOBI 后转为 PDF 。强迫症实在忍不了默认 PDF 的字体。
    dydhyh
        17
    dydhyh  
       2015-12-06 09:25:44 +08:00
    mark , 好评啊, 新手表示很通俗易懂, 给楼主赞一个
    guyskk
        18
    guyskk  
       2015-12-06 12:43:04 +08:00   ❤️ 1
    模块命名我不赞同
    ```
    # 正确的模块名
    import decoder
    import htmlParser

    # 错误的模块名
    import Decoder
    import html_parser
    ```
    我认为
    htmlparser > html_parser > htmlParser
    模块名不应该用大写字母
    https://pythonhosted.org/six/#module-six.moves
    unity0703
        19
    unity0703  
    OP
       2015-12-06 15:38:53 +08:00
    @guyskk 多谢指正,只有一个单词时一般采用全小写这点没有问题

    关键就在于多个单词的时候,这个标准并不统一,比如 python2 中既有 SimpleHTTPServer, StingIO ,又有 argparse,unittest ,而 google 代码规范中的建议是 lower_with_underline ( Python 之父 guido 也建议这种方式)

    就可读性来讲,首字母小写的驼峰命名会好一点,这也是大部分 java 规范采用的命名方法,其次是 lower_with_underline ,全小写的话可读性可能不是那么好,比如说 simplefilecache, commoninterpretergenerator, 你感受一下,改成 simpleFileCache, common_interpreter_generator 会好很多

    综上,个人认为,首字母小写的驼峰命名,或者 lower_with_underline 会好很多
    guyskk
        20
    guyskk  
       2015-12-06 22:28:50 +08:00
    python2 中的命名比较混乱, python3 就好多了
    commoninterpretergenerator 这样肯定很难看的,用 common_interpreter_generator 更好一点,
    python3 标准库中的名字比较长的模块也是用下划线命名,比如 zip_longest 而不是 zipLongest 。
    但是这么长的模块名也是不好的,比如 web_server_gateway_interface 可以缩写为 wsgi , common_interpreter_generator 也可以缩写为 cig 。
    enlau0912
        21
    enlau0912  
       2015-12-07 01:45:26 +08:00
    最近剛從 C 轉過來學習 python ,這本書很有幫助,感謝。
    hjkl0001
        22
    hjkl0001  
       2015-12-07 13:04:27 +08:00
    收藏了,谢谢!!!
    mingyun
        23
    mingyun  
       2015-12-15 22:06:25 +08:00
    mark
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1061 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:05 · PVG 03:05 · LAX 12:05 · JFK 15:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.