13 | | |
14 | | 2. Use the directory structure to place your script appropriately into |
15 | | the source tree |
16 | | - scripts go into scripts/ |
17 | | |
18 | | Also add a Makefile and a <module>.html file into this directory. |
19 | | See existing Python scripts for examples. |
20 | | |
21 | | 3. Add a header section to the script you submit and make sure you |
22 | | include the copyright. The purpose section is meant to contain a |
23 | | general over view of the code in the file to assist other |
24 | | programmers that will need to make changes to your code. For this |
25 | | purpose use Python Docstring, see |
26 | | http://epydoc.sourceforge.net/docstrings.html |
27 | | |
28 | | Example (fictitious header for a script called g.myscript): |
| 13 | Use the directory structure to place your script appropriately into |
| 14 | the source tree: scripts go into `scripts` directory. |
| 15 | |
| 16 | Also add a Makefile and a <module>.html file into this directory. |
| 17 | See existing Python scripts for examples. |
| 18 | |
| 19 | Add a header section to the script you submit and make sure you |
| 20 | include the copyright. The purpose section is meant to contain a |
| 21 | general over view of the code in the file to assist other |
| 22 | programmers that will need to make changes to your code. For this |
| 23 | purpose use Python Docstring, see |
| 24 | http://epydoc.sourceforge.net/docstrings.html |
| 25 | |
| 26 | Example (fictitious header for a script called g.myscript): |
59 | | Follow PEP8 and use pep8 tool. |
60 | | |
61 | | As Python determines nesting based upon indentation, it isn't just |
62 | | a stylistic issue. |
63 | | |
64 | | Use 4-space indentation (GNU Emacs python-mode default). |
65 | | |
66 | | Make sure a new line is at the end of each file. |
| 63 | Follow PEP8 standard and use `pep8` tool to check compliance of your code to this standard. |
| 64 | Note that not all code is currently compliant to complete PEP8, so we are using a custom configuration stored in `tools/pep8config.txt`, so use: |
| 65 | |
| 66 | {{{ |
| 67 | pep8 --config=tools/pep8config.txt directory_to_check |
| 68 | }}} |
| 69 | |
| 70 | Alternatively, you can use `pep8` with `--diff` option to check just the parts of the code you have changed: |
| 71 | |
| 72 | {{{ |
| 73 | svn diff | pep8 --diff --config=tools/pep8config.txt |
| 74 | }}} |
| 75 | |
| 76 | The best practice is to use pep8 with default configuration (i.e., without custom configuration file) for new files and new code in old files. |
| 77 | |
| 78 | Use 4-space indentation (GNU Emacs python-mode default). Do not use tabs (tabulators) at all. |
| 79 | Note that Python determines nesting based upon indentation, so it is quite crucial to be consistent, i.e. use given rules. |
| 80 | |
| 81 | Make sure a new line is at the end of each file and there are no trailing spaces. |
| 82 | |
| 83 | Do not fix (intentionally or unintentionally) existing style issues in code (at lines) you are not changing. |
| 84 | If you are fixing style issues, do it in a separate commit. |
107 | | # test for input raster map |
108 | | result = grass.find_file(name = map_name, element = 'cell', quiet = True) |
109 | | if not result['file'] |
110 | | grass.fatal("Raster map <%s> not found" % map_name) |
111 | | |
112 | | # test for input vector map |
113 | | result = grass.find_file(name = map_name, element = 'vector', quiet = True) |
114 | | if not result['file'] |
115 | | grass.fatal("Vector map <%s> not found" % map_name) |
116 | | }}} |
117 | | |
118 | | ... and so forth. See 'g.manual g.findfile' for details. |
| 130 | # test for input raster map |
| 131 | result = grass.find_file(name = map_name, element = 'cell', quiet = True) |
| 132 | if not result['file'] |
| 133 | grass.fatal("Raster map <%s> not found" % map_name) |
| 134 | |
| 135 | # test for input vector map |
| 136 | result = grass.find_file(name = map_name, element = 'vector', quiet = True) |
| 137 | if not result['file'] |
| 138 | grass.fatal("Vector map <%s> not found" % map_name) |
| 139 | }}} |
| 140 | |
| 141 | ... and so forth. See 'g.manual g.findfile' for details. |
130 | | #normal message: |
131 | | grass.message("Done") |
132 | | |
133 | | # warning: |
134 | | grass.warning("No input values found, using default values") |
135 | | |
136 | | # error: |
137 | | grass.error("No map found") |
138 | | |
139 | | # fatal error: |
140 | | grass.fatal_error("No map found, exiting") |
141 | | |
142 | | # debug output (use g.gisenv to enable/disable) |
143 | | grass.debug("Our calculated value is: %d" % value) |
144 | | }}} |
145 | | |
146 | | Try to omit any usage of the 'print' command for informational output. |
| 153 | #normal message: |
| 154 | grass.message("Done") |
| 155 | |
| 156 | # warning: |
| 157 | grass.warning("No input values found, using default values") |
| 158 | |
| 159 | # error: |
| 160 | grass.error("No map found") |
| 161 | |
| 162 | # fatal error: |
| 163 | grass.fatal_error("No map found, exiting") |
| 164 | |
| 165 | # debug output (use g.gisenv to enable/disable) |
| 166 | grass.debug("Our calculated value is: %d" % value) |
| 167 | }}} |
| 168 | |
| 169 | Do not use the `print` statement (print function in Python 3) for informational output. This is reserved for standard module output if it has one. |
174 | | |
175 | | |
176 | | 6. Comment your classes and functions with docstrings. Use Sphinx syntax. |
177 | | |
178 | | Comment also the code itself such as the meaning of variables, |
179 | | conditions etc. |
180 | | |
181 | | |
182 | | 8. PLEASE take the time to add comments throughout your code explaining what |
183 | | the code is doing. It will save a HUGE amount of time and frustration for |
184 | | other programmers that may have to change your code in the future. |
185 | | |
186 | | |
187 | | |
188 | | 9. Use tools such as pylint and pep8 to check your code (both style and |
189 | | correctness). Just note that default settings of these tools is not fully |
190 | | compatible with wxGUI/wxPython style and that some of the reported errors |
191 | | may not apply to your code. |
| 197 | Comment your classes and functions with docstrings. Use Sphinx (reStructuredText) syntax. |
| 198 | |
| 199 | Comment also the code itself such as the meaning of variables, conditions etc. |
| 200 | |
| 201 | Take the time to add comments throughout your code explaining what |
| 202 | the code is doing. It will save a huge amount of time and frustration for |
| 203 | other programmers that may have to change your code in the future. |
| 204 | |
| 205 | == Checking the code == |
| 206 | |
| 207 | Use tools such as pylint and pep8 to check your code (both style and |
| 208 | correctness). Just note that default settings of these tools is not fully |
| 209 | compatible with wxGUI/wxPython style and that some of the reported errors |
| 210 | may not apply to your code. |