Appendices A : Coding Conventions

Python coding

Use tabs: will be replaced by spaces soon…

Take care with default values for arguments: they are only evaluated once when the module is loaded and then used at each call. This means that if you use a mutable object as default value, and then modify that object, at the next call you will receive the modified object as default argument value. This applies to dict and list objects which are very often used as default values. If you want to use such objects as default value, you must either ensure that they won’t be modified or use another default value (such as None) and test it. For example:

  1. def foo(a=None):
  2. if a is None:
  3. a=[]
  4. # ...

This is what is [in the Python documentation]. In addition it is good practice to avoid modifying objects that you receive as arguments if it is not specified. If you want to do so, prefer to copy the object first. A list can easily be copied with the syntax

copy = original[:]

A lot of other objects, such as dict, define a copy method.

File names

The structure of a module should be like this:

  1. /module/
  2. /__init__.py
  3. /__terp__.py
  4. /module.py
  5. /module_other.py
  6. /module_view.xml
  7. /module_wizard.xml
  8. /module_report.xml
  9. /module_data.xml
  10. /module_demo.xml
  11. /wizard/
  12. /__init__.py
  13. /wizard_name.py
  14. /report/
  15. /__init__.py
  16. /report_name.sxw
  17. /report_name.rml
  18. /report_name.py

Naming conventions

  • 模块: 模块名称应该使用下划线(_),加上小写字母。模块的名称就是服务器上插件所在的路径名称。如果某个模块依赖其他的模块,你可以用最重要的模块作为名称的开始,把其他几个模块的名称用下划线分别串起来。例如:

    • sale

    • sale_commission

  • 物件: 物件的名称必须依照以下的形式 模块名称.物件名称1.物件名称2.物件名称3…. 物件名称i 的排列顺序必须是由最重要的到最不重要的,由左到右,而且要是小写字母。尽量不要在物件名称里使用复数形式,同时要避免在名称里使用捷径。例如:

    • sale.order

    • sale.order.line

    • sale.shop

    • sale_commission.commission.rate

  • 字段: 字段必须是小写,加上分隔的下划线。尽量使用一般常用的字段名称,例如:name, state, active, partner_id, 等等。字段名称是依据字段的属性来约定的:

    • 多对一: 必须在结尾加上 ‘_id’ (例如: partner_id, order_line_id)

    • 多对多: 必须在结尾加上’_ids’ (例如: category_ids)

    • 一对多: 必须在结尾加上’_ids’ (例如: line_ids)