Tips for Python beginners to use the Scikit-image example for themselves 6 Improve Python code

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.

** Improve your code with the Spyder integrated environment **

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)

** "Warning of unused modules and objects" **

You can get rid of unnecessary imports or delete the code that is calculating unused objects.

** "Remove trailing spaces at the trailing edge (source> remove trailing spaces)" **

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.

** Insert only one space before and after the assignment operator and comparison operator. ** **

** Insert only one space after ",". ** **

** Do not add unnecessary () to the conditional expression of the if statement. ** **

You can check to match the coding style.

** Write the documentation string **

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 -*-

** How to suppress the warning issued by pylint **

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.

** Use namespaces to identify sources **

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.

** C ++ conversion after cleaning the algorithm **

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.

Hint 7 How to make a module

Recommended Posts

Tips for Python beginners to use the Scikit-image example for themselves 6 Improve Python code
Tips for Python beginners to use the Scikit-image example for themselves
Tips for Python beginners to use the Scikit-image example for themselves 9 Use from C
Tips for Python beginners to use the Scikit-image example for themselves 2 Handle multiple files
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
Tips for Python beginners to use the Scikit-image example for themselves 8 Processing time measurement and profiler
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
Tips for Python beginners to use Scikit-image examples for themselves 5 Incorporate into network apps
~ Tips for beginners to Python ③ ~
The fastest way for beginners to master Python
[For beginners] How to use say command in python!
I didn't know how to use the [python] for statement
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
[Introduction to Python] How to use the in operator in a for statement?
[Python] Explains how to use the format function with an example
[Python] Organizing how to use for statements
How to use "deque" for Python data
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
[Python] Explains how to use the range function with a concrete example
[Example of Python improvement] What is the recommended learning site for Python beginners?
About Python code for simple moving average assuming the use of Numba
Memo # 4 for Python beginners to read "Detailed Python Grammar"
How to use the C library in Python
How to use MkDocs for the first time
Python for super beginners Python for super beginners # Easy to get angry
Tips for those who are wondering how to use is and == in Python
Python beginners tried to code some energy drinks
Specify the Python executable to use with virtualenv
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Use logger with Python for the time being
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
How to use data analysis tools for beginners
Tips for hitting the ATND API in Python
The easiest way to use OpenCV with python
Try to calculate RPN in Python (for beginners)
[Algorithm x Python] How to use the list
Use the Python framework "cocotb" to test Verilog.
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Code for checking the operation of Python Matplotlib
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Introduction to Programming (Python) TA Tendency for beginners
Memo # 6 for Python beginners to read "Detailed Python Grammar"
How to make Python faster for beginners [numpy]
~ Tips for Python beginners from Pythonista with love ② ~
Memo # 5 for Python beginners to read "Detailed Python Grammar"
"Book to train programming skills to fight in the world" Python code answer example --1.3 URLify
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
python beginners tried to predict the number of criminals
[BigQuery] How to use BigQuery API for Python -Table creation-
How to convert Python # type for Python super beginners: str
[For beginners] How to study Python3 data analysis exam
How to use the Raspberry Pi relay module Python
Tips for speeding up python code correctly with numba
I wanted to use the Python library from MATLAB
[Python] How to use the graph creation library Altair
Specify MinGW as the compiler to use with Python
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use the model learned in Lobe in Python