Tools for Python programming: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(pylint, pep8, cprofile, pyreverse, spyder)
 
(+PyCharm IDE)
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
To help with finding broken code, unused variables and other problems, there are several tools available. Described tools are free and open source and usually widely available in GNU/Linux distribution repositories or through pip. They can be connected to some code editor or run separately from command line. Some editors integrate some of these tools. Generally, all tools need to be run from a GRASS session.  
To help with finding broken code, unused variables and other problems, there are several tools available. Described tools are free and open source and usually widely available in GNU/Linux distribution repositories or through pip. They can be connected to some code editor or run separately from command line. Some editors integrate some of these tools. Generally, all tools need to be run from a GRASS session.  


== pep8 ==
== pep8 for checking of style ==


Checks the basic correctness of Python code according to [http://www.python.org/dev/peps/pep-0008/ PEP 8 -- Style Guide for Python Code].
Checks the basic correctness and style of Python code according to [http://www.python.org/dev/peps/pep-0008/ PEP 8 -- Style Guide for Python Code].


<source lang="bash">
<source lang="bash">
Line 9: Line 9:
</source>
</source>


== Pylint ==
== Pylint static checker ==


''Main article: [[Pylint rc file for GRASS]]''
''Main article: [[Pylint rc file for GRASS]]''
Line 15: Line 15:
Pylint is a tool for static source code analysis. It provides wide range of analysis and is highly customizable. It is integrated to some Python editors.
Pylint is a tool for static source code analysis. It provides wide range of analysis and is highly customizable. It is integrated to some Python editors.


== cProfile ==
== cProfile profiling tool ==


cProfile is a Python profiling tool. It can be used directly in your Python source code or from the command line. The output can be processed by the gprof2dot tool which generates call graph in dot (depends on Graphviz is necessary to render the graph).
cProfile is a Python profiling tool. It can be used directly in your Python source code or from the command line. The output can be processed by the gprof2dot tool which generates call graph in dot (depends on Graphviz is necessary to render the graph).
Line 44: Line 44:
</source>
</source>


== Pyreverse ==
== Pyreverse static checker ==


Needs to run in distribution directory.
Needs to run in distribution directory.


Note that it exploits only import and usage dependencies. Because of the Python dynamic typing it is not possible to find dependency on particular object interface when the object is not created but only passed to a function or object constructor.
Note that it shows only import and usage dependencies. Because of the Python dynamic typing it is not possible to find dependency on particular object interface when the object is not created but only passed to a function or object constructor.


<source lang="bash">
<source lang="bash">
Line 55: Line 55:
</source>
</source>


== Spyder ==
== PyCharm IDE ==


To get proper results, you should run it form the GRASS session. It has integrated pylint tool. Bugs and requests can be reported here: https://code.google.com/p/spyderlib/.
[https://www.jetbrains.com/pycharm/ PyCharm] is a editor, or better to say IDE, for Python with large number of tools integrated into it. To get better results, you should run PyCharm within a GRASS GIS session.
 
TODO: add setup
 
== Spyder editor ==
 
[https://www.spyder-ide.org/ Spyder] is a editor, or better to say IDE, for Python with large number of tools integrated into it. To get better results, you should run Spyder within a GRASS GIS session. It has an integrated pylint tool to verify the source code instantly for undefined or unused variables, wrong indentation and more.
 
* Linux: many distributions provide "spyder" as a package for easy installation.
* Windows: you may also install Spyder as part of [http://winpython.sourceforge.net/ WinPython]
* ...
 
'''Important''': Change the editor settings from 'tab' to ''''4 spaces'''' (according to the style guide at https://trac.osgeo.org/grass/wiki/Submitting/Python):
* Menu Tools > Preferences > Editor > Advanced Settings: Indentation characters:  4 spaces
 
An ideal way how to run Spyder within a GRASS session ('<tt>.</tt>' can by replaced by any directory):
spyder -w . 2>/dev/null &
 
== Eric Python IDE ==
 
"Eric is a full featured Python and Ruby editor and IDE, written in python":
 
* http://eric-ide.python-projects.org/eric-screenshots.html
 
== Simple Python Editor in GRASS GIS ==
 
[[File:Simple python editor v buffer.png|300px|thumb|right|Simple Python editor with the interactive Python shell (console) in the background]]
 
Since GRASS GIS 7.2, there is an embedded editor for Python in GRASS GIS with basic set of editing features. It is aimed towards beginners and contains examples and templates. User scripts and newly developed GRASS GIS modules can be executed in the (command) Console tab directly from the editor.


{{Python}}
{{Python}}

Revision as of 21:07, 12 June 2019

To help with finding broken code, unused variables and other problems, there are several tools available. Described tools are free and open source and usually widely available in GNU/Linux distribution repositories or through pip. They can be connected to some code editor or run separately from command line. Some editors integrate some of these tools. Generally, all tools need to be run from a GRASS session.

pep8 for checking of style

Checks the basic correctness and style of Python code according to PEP 8 -- Style Guide for Python Code.

pep8 lmgr/toolbars.py

Pylint static checker

Main article: Pylint rc file for GRASS

Pylint is a tool for static source code analysis. It provides wide range of analysis and is highly customizable. It is integrated to some Python editors.

cProfile profiling tool

cProfile is a Python profiling tool. It can be used directly in your Python source code or from the command line. The output can be processed by the gprof2dot tool which generates call graph in dot (depends on Graphviz is necessary to render the graph).

For Ubuntu, Graphviz and cProfile are available in repository (graphviz and python-profiler packages). gprof2dot tool needs to be installed through pip (sudo pip install gprof2dot).

# does the actual profiling
python -m cProfile -o output.pstats  my_python_script.py --various-my-script-parameters foo bar

# creates a callgraph with profiling info (filtered)
gprof2dot -f pstats output.pstats | dot -Tpng -o gprof2dot_output.png
#!/bin/bash

# Usage:
# ./python-profile.sh your_python_script.py and its parameters
# need to run in the directory which contains profiled script 

OUTFILE=$(basename $1 .py)_profile

python -m cProfile -o $OUTFILE.pstats $* \
&& gprof2dot -f pstats $OUTFILE.pstats > $OUTFILE.dot \
&& dot -Tpng -o $OUTFILE.png $OUTFILE.dot \
&& dot -Tpdf -o $OUTFILE.pdf $OUTFILE.dot

Pyreverse static checker

Needs to run in distribution directory.

Note that it shows only import and usage dependencies. Because of the Python dynamic typing it is not possible to find dependency on particular object interface when the object is not created but only passed to a function or object constructor.

cd dist.../etc/gui/wxpython
pyreverse -o pdf -p wxgui --only-classnames core/ gui_core/ lmgr/ mapdisp/

PyCharm IDE

PyCharm is a editor, or better to say IDE, for Python with large number of tools integrated into it. To get better results, you should run PyCharm within a GRASS GIS session.

TODO: add setup

Spyder editor

Spyder is a editor, or better to say IDE, for Python with large number of tools integrated into it. To get better results, you should run Spyder within a GRASS GIS session. It has an integrated pylint tool to verify the source code instantly for undefined or unused variables, wrong indentation and more.

  • Linux: many distributions provide "spyder" as a package for easy installation.
  • Windows: you may also install Spyder as part of WinPython
  • ...

Important: Change the editor settings from 'tab' to '4 spaces' (according to the style guide at https://trac.osgeo.org/grass/wiki/Submitting/Python):

  • Menu Tools > Preferences > Editor > Advanced Settings: Indentation characters: 4 spaces

An ideal way how to run Spyder within a GRASS session ('.' can by replaced by any directory):

spyder -w . 2>/dev/null &

Eric Python IDE

"Eric is a full featured Python and Ruby editor and IDE, written in python":

Simple Python Editor in GRASS GIS

Simple Python editor with the interactive Python shell (console) in the background

Since GRASS GIS 7.2, there is an embedded editor for Python in GRASS GIS with basic set of editing features. It is aimed towards beginners and contains examples and templates. User scripts and newly developed GRASS GIS modules can be executed in the (command) Console tab directly from the editor.