Adapter编程风格规范
Adapter采用Python语言编写,因此,Adapter开发者应严格遵守 Python 编程规范 。
此外,为了增强 Adapter 编程的质量和可读性,Adapter 开发者还应遵守本节的约定。
命名规范
-
类名使用驼峰命名格式,例如:
class AdapterWidget
python -
类的成员变量和成员函数使用以下划线连接的小写单词,例如:
self.pick_count # Note that variables are generally nouns def start_viz(self): # Note that the function name is generally a verb + object pass
python -
类的成员变量和成员函数若只在类内部使用,可在名字前面加下划线,表示此名字不建议类外部使用,但类外部仍可使用,例如:
self._socket = socket() # Indicates that the _socket variable is only used inside the class def _init_widget(self): # Indicates that the _init_widget function is only used inside the class pass
python -
常量使用以下划线连接的大写单词,例如:
ADAPTER_DIR = "D:/adapter_for_communication"
python
注释规范
注释不宜过多,只需要在表达式复杂或所表达意思较为关键时添加。
-
单行注释
单行注释请以#开头。
-
多行注释
多行注释请包含在 """ 与 """ 之间。
-
函数、类或包的介绍注释
注释放在函数的下面、类的下面或包的最上面,例如:
def viz_is_running(self): """ Will be called when find viz is running during starting viz. """ class Adapter(QObject): """ Base class which encapsulates Viz/Vision/Hub/Robserver interfaces. """ """ Service base classes. """ import logging
python
日志规范
日志可帮助出现故障时候分析问题。日志信息最好在适当的时候输出,例如调用关键函数以及函数返回了可参考的数据。
Adapter 支持两种日志打印方式:
-
print:该方式只将日志输出到控制台上,该方式方便运行时实时观察,但程序故障时消息将会丢失,因此不建议在实际生产环境使用。
-
logging:该方式支持格式化日志显示格式,也支持将日志保存到日志文件中(可选功能),因此建议使用该方式。