How to analyze a set of coding standards and style of the source code and to discuss improvements to optimize the code

  Engineering practice topics are related to data acquisition, analysis here to choose a micro-channel public number reptiles source code.

A source code directory structure

 

   The directory structure is relatively clear

  1.bin storing critical code

  

 

  2.docs storage of documentation, such as interface description, installation instructions, instructions for use, environment descriptions

  

 

  3.wechat Here is the reptile managed code, such as the number of control, link control

  

 

  4.wechatspider reptile store the code, url acquisition and analysis, etc.

  

 

  5. Some other configuration files and readme

  

 

II. Naming

  1. File Name

  You can see a small hump nomenclature, getNewIp.py the first letter of the first word, the second word of the beginning of each word capitalized

  

 

   2. The name of the class

  Use a large hump Nomenclature, the first letter of each word is capitalized.

  

 

  3. function name

  Snake Case nomenclature used, separated by a middle of a word _. This naming words in the first letter of the law are usually lowercase and the first letter of the first word of both uppercase and lowercase can be.

  

 

III. Evaluation of style

  First, the structure of the code is relatively clear hierarchical code points according to the following function, reasonable structure.

   Overall naming convention is very good, conditions of use three kinds of nomenclature in line with the usual naming convention.

  Number of comments just right, and very reasonable:  

    

  Beginning of the document indicate the author:

  

  But the disadvantage is that this code name appears in some places and some non-standard, such as the folder name no words separated by any means:

  

 

IV. General requirements and style code specifications

  Personally, more use of python, introduce here the usual Python code specifications.

  In fact the official Python gives a coding standard: PEP 8    

  This is the Chinese version of PEP8: https://blog.csdn.net/ratsniper/article/details/78954852

  Of course, this is just a standard only, not mandatory we all have to comply, but it seems most people use PEP 8 coding style, it has become the de facto standard coding style. This introduces PEP8.

  1. beginning of the document annotation

  Use pycharm can import predefined templates.

  打开一个新建的Python文件进行编辑(F4),这个文件中默认有两行代码:作者姓名和工程名称。之所以会出现这两行代码,是因为Python文件在创建时是基于文件模板进行创建的,因此会预定义这两个变量。在settings > file and code templates > python script 选中,然后写入模板语法。效果如下:  

    

 

   2.空格使用

  • 总是在二元运算符两边加一个空格:赋值(=),增量赋值(+=,-=),比较(==,<,>,!=,<>,<=,>=,in,not,in,is,is not),布尔(and, or, not)。
  • 如果使用具有不同优先级的运算符,请考虑在具有最低优先级的运算符周围添加空格。有时需要通过自己来判断;但是,不要使用一个以上的空格,并且在二元运算符的两边使用相同数量的空格。

 

  3.命名方式

  以下是常见的命名方式:

  • b(单个小写字母)
  • B(单个大写字母)
  • lowercase 小写字母
  • lower_case_with_underscores 使用下划线分隔的小写字母
  • UPPERCASE 大写字母
  • UPPER_CASE_WITH_UNDERSCORES 使用下划线分隔的大写字母
  • CapitalizedWords(或者叫 CapWords,或者叫CamelCase 驼峰命名法 —— 这么命名是因为字母看上去有起伏的外观)。有时候也被称为StudlyCaps。
    注意:当在首字母大写的风格中用到缩写时,所有缩写的字母用大写,因此,HTTPServerError 比 HttpServerError 好。
  • mixedCase(不同于首字母大写,第一个单词的首字母小写)

 

  命名约定:

  Names to Avoid 应避免的名字

  永远不要使用字母‘l’(小写的L),‘O’(大写的O),或者‘I’(大写的I)作为单字符变量名。
  在有些字体里,这些字符无法和数字0和1区分,如果想用‘l’,用‘L’代替。

  Package and Module Names 包名和模块名

  模块应该用简短全小写的名字,如果为了提升可读性,下划线也是可以用的。Python包名也应该使用简短全小写的名字,但不建议用下划线。
  当使用C或者C++编写了一个依赖于提供高级(更面向对象)接口的Python模块的扩展模块,这个C/C++模块需要一个下划线前缀(例如:_socket)

  Class Names 类名

  类名一般使用首字母大写的约定。
  在接口被文档化并且主要被用于调用的情况下,可以使用函数的命名风格代替。
  注意,对于内置的变量命名有一个单独的约定:大部分内置变量是单个单词(或者两个单词连接在一起),首字母大写的命名法只用于异常名或者内部的常量。

  Function Names 函数名

  函数名应该小写,如果想提高可读性可以用下划线分隔。
  大小写混合仅在为了兼容原来主要以大小写混合风格的情况下使用(比如 threading.py),保持向后兼容性。

 

   4.PEP8检查

  可以使用PEP8检查代码是否不符合规则

  故意写几行不符合Python编码风格的代码:

import sys, os
from subprocess import Popen, PIPE

def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)

  检查是否符合编码规范:

$ pep8 --first test.py
test.py:1:11: E401 multiple imports on one line
test.py:4:1: E302 expected 2 blank lines, found 1
test.py:6:5: E125 continuation line with same indent as next logical line

  可以看到1、4、6行代码不符合规范

  还可以输出不符合规范的代码和原因:

$ pep8 --show-source --show-pep8 test.py

 

Guess you like

Origin www.cnblogs.com/dwtenir/p/11615399.html