~openerp-community/openobject-doc/ksa-openobject-doc-6.0

« back to all changes in this revision

Viewing changes to i18n/ru/source/developer/Appendices/code_convention.rst

  • Committer: Don Kirkby
  • Date: 2011-02-21 20:46:11 UTC
  • mfrom: (433.1.53 openobject-doc)
  • Revision ID: donkirkby+launpd@gmail.com-20110221204611-1ykt6dmg4k3gh5dh
[MERGE] revisions 477 to 486 from the 5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
.. i18n: Appendices A : Coding Conventions
 
3
.. i18n: =================================
 
4
..
 
5
 
 
6
Appendices A : Coding Conventions
 
7
=================================
 
8
 
 
9
.. i18n: Python coding
 
10
.. i18n: -------------
 
11
..
 
12
 
 
13
Python coding
 
14
-------------
 
15
 
 
16
.. i18n: Use tabs: will be replaced by spaces soon...
 
17
..
 
18
 
 
19
Use tabs: will be replaced by spaces soon...
 
20
 
 
21
.. i18n: 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:
 
22
..
 
23
 
 
24
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:
 
25
 
 
26
.. i18n: .. code-block:: python
 
27
.. i18n: 
 
28
.. i18n:     def foo(a=None):
 
29
.. i18n: 
 
30
.. i18n:         if a is None:
 
31
.. i18n: 
 
32
.. i18n:             a=[] 
 
33
.. i18n: 
 
34
.. i18n:         # ... 
 
35
..
 
36
 
 
37
.. code-block:: python
 
38
 
 
39
    def foo(a=None):
 
40
 
 
41
        if a is None:
 
42
 
 
43
            a=[] 
 
44
 
 
45
        # ... 
 
46
 
 
47
.. i18n: 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
 
48
..
 
49
 
 
50
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
 
51
 
 
52
.. i18n:         copy = original[:] 
 
53
..
 
54
 
 
55
        copy = original[:] 
 
56
 
 
57
.. i18n: A lot of other objects, such as dict, define a copy method.
 
58
..
 
59
 
 
60
A lot of other objects, such as dict, define a copy method.
 
61
 
 
62
.. i18n: File names
 
63
.. i18n: -----------
 
64
..
 
65
 
 
66
File names
 
67
-----------
 
68
 
 
69
.. i18n: The structure of a module should be like this::
 
70
.. i18n: 
 
71
.. i18n:     /module/
 
72
.. i18n: 
 
73
.. i18n:         /__init__.py 
 
74
.. i18n:         /__terp__.py 
 
75
.. i18n:         /module.py 
 
76
.. i18n:         /module_other.py 
 
77
.. i18n:         /module_view.xml 
 
78
.. i18n:         /module_wizard.xml 
 
79
.. i18n:         /module_report.xml 
 
80
.. i18n:         /module_data.xml 
 
81
.. i18n:         /module_demo.xml 
 
82
.. i18n:         /wizard/ 
 
83
.. i18n:         /__init__.py 
 
84
.. i18n:         /wizard_name.py 
 
85
.. i18n: 
 
86
.. i18n:     /report/
 
87
.. i18n: 
 
88
.. i18n:         /__init__.py 
 
89
.. i18n:         /report_name.sxw 
 
90
.. i18n:         /report_name.rml 
 
91
.. i18n:         /report_name.py 
 
92
..
 
93
 
 
94
The structure of a module should be like this::
 
95
 
 
96
    /module/
 
97
 
 
98
        /__init__.py 
 
99
        /__terp__.py 
 
100
        /module.py 
 
101
        /module_other.py 
 
102
        /module_view.xml 
 
103
        /module_wizard.xml 
 
104
        /module_report.xml 
 
105
        /module_data.xml 
 
106
        /module_demo.xml 
 
107
        /wizard/ 
 
108
        /__init__.py 
 
109
        /wizard_name.py 
 
110
 
 
111
    /report/
 
112
 
 
113
        /__init__.py 
 
114
        /report_name.sxw 
 
115
        /report_name.rml 
 
116
        /report_name.py 
 
117
 
 
118
.. i18n: Naming conventions
 
119
.. i18n: ------------------
 
120
..
 
121
 
 
122
Naming conventions
 
123
------------------
 
124
 
 
125
.. i18n:     * modules: modules must be written in lower case, with underscores. The name of the module is the name of the directory in the addons path of the server. If the module depends on other modules, you can write several module names separated by underscores, starting by the most important name. Example:
 
126
.. i18n:           + sale
 
127
.. i18n:           + sale_commission 
 
128
.. i18n: 
 
129
.. i18n:     * objects: the name of an object must be of the form name_of_module.name1.name2.name3.... The namei part of the object must go from the most important name to the least important one, from left to right, in lower case. Try not to use plurals in object names and to avoid shortcuts in the names. Example:
 
130
.. i18n:           + sale.order
 
131
.. i18n:           + sale.order.line
 
132
.. i18n:           + sale.shop
 
133
.. i18n:           + sale_commission.commission.rate 
 
134
.. i18n: 
 
135
.. i18n:     * fields: field must be in lowercase, separated by underscores. Try to use commonly used names for fields: name, state, active, partner_id, eso. Conventions for the field name depends on the field type:
 
136
.. i18n:           + many2one: must end by '_id' (eg: partner_id, order_line_id)
 
137
.. i18n:           + many2many: must end by '_ids' (eg: category_ids)
 
138
.. i18n:           + one2many: must end by '_ids' (eg: line_ids
 
139
..
 
140
 
 
141
    * modules: modules must be written in lower case, with underscores. The name of the module is the name of the directory in the addons path of the server. If the module depends on other modules, you can write several module names separated by underscores, starting by the most important name. Example:
 
142
          + sale
 
143
          + sale_commission 
 
144
 
 
145
    * objects: the name of an object must be of the form name_of_module.name1.name2.name3.... The namei part of the object must go from the most important name to the least important one, from left to right, in lower case. Try not to use plurals in object names and to avoid shortcuts in the names. Example:
 
146
          + sale.order
 
147
          + sale.order.line
 
148
          + sale.shop
 
149
          + sale_commission.commission.rate 
 
150
 
 
151
    * fields: field must be in lowercase, separated by underscores. Try to use commonly used names for fields: name, state, active, partner_id, eso. Conventions for the field name depends on the field type:
 
152
          + many2one: must end by '_id' (eg: partner_id, order_line_id)
 
153
          + many2many: must end by '_ids' (eg: category_ids)
 
154
          + one2many: must end by '_ids' (eg: line_ids