So far, I've modified the Python code for scikit-image. Moreover, I have introduced it at the level that I should move for the time being. That's not enough forever, so I'll show you how to improve your Python code while using the Spyder integrated environment. This article is especially for beginners. It doesn't contain any useful information for those who are already familiar with Python.
You can read more about how to improve your code with the Spyder integrated environment in the following blog post: From there, I will write down the parts that I often check.
Blog article [[Series] "CV programming using python and OpenCV" Part 5: GUI operation of Spyder](http://gyao.yahoo.co.jp/player/00220/v12056/v1000000000000001123/?auto=1&rep = 2)
You can get rid of unnecessary imports or delete the code that is calculating unused objects.
By doing this, you can prevent the occurrence of meaningless diffs when managing versions with SVN. "code analysis by pylint (soucer> run pylint code analysis)" You can make it easier to read by checking the coding style that comes out in Pylint by executing.
You can check to match the coding style.
Python Tutorial Documentation String The documentation string is
def square(x):
"""return square value"""
return x**2
Write like this.
You can use " ""
or'''
triple quotes to work with strings that span multiple lines.
If you write the documentation string in that way, you can run help to see how to use it as follows:
>>>help(square) square(x) return square value
>>> And so on, you can use help just like any other library.
If you are using unicode u "" "Documentation string" "" " Use the letter u, such as. Python standard library [sequence type — str, unicode, list, tuple, bytearray, buffer, xrange](http://docs.python.jp/2.7/library/stdtypes.html#str-unicode-list-tuple-bytearray- buffer-xrange)
Unicode strings are almost the same as strings, but are specified by prefixing them with the character'u', such as u'abc', u "def".
If the file contains a unicode string, add the character code specification at the beginning of the script. (Without the addition, the python interpreter may fail to process the string and give an error.)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Add the following line after specifying the character code of the Python source code. #pylint:disable=C0103
The character code is also used when saving the script to a text file.
from pylab import * It is not recommended to write all modules like this without namespace [Note 1]. import pylab Please use the method of specifying the module name as in.
You will often have to implement it in C ++. When you have to try and error the algorithm, the data structure is messed up and the variable names are img2 or tmp, which I can't recommend. Since it plays a different role than when it first came up, it is easy for variables and function names to deviate from what they actually do, regardless of the type of language. It's easy to leave unused variables, unnecessary imports, or unnecessary #inculde. If you do the same thing, Python should be easier to refactor because it has fewer lines and a better view of the process. The Spyder integrated environment, pylint, etc. will make refactoring easier in Python. Then write code that is clear enough. Then test and profile to see how to reduce execution time without compromising accuracy. I find python to be several times easier to do that than C ++. Then, the C ++ implementation time should be shortened if the algorithm is sufficiently sophisticated.
Note 1: from pylab import * There is an advantage that you can use it like MATLAB, but from cv2 import * If you write with imshow() It is dangerous because you cannot tell which imshow () is.
Recommended Posts