The Python programming language is required to follow the PEP8 specification, but beginners often fail to remember the specification and the code is ugly. This article recommends a few magic tricks to save python code from being ugly.

A Jupyter Notebook

For a regular Jupyter notebook, the Code prettify plugin is recommended.

Installing a plug-in

  • First, the plug-in Nbextensions\

Execute the following command \

pip install jupyter_contrib_nbextensions
Copy the code

No error before execution: \

jupyter contrib nbextension install --user
Copy the code
  • Select the Code Prettify module

Note: The YAPF module must be installed

pip install yapf
Copy the code

  • Use as follows:

Original non-standard code:

Use the button on the far right of the toolbar:

The code becomes normalized immediately after use:

Second, Pycharm article

2.1 PyCharm Integrates with PyLint

Pylint is a code checking tool and cannot automatically modify code

  • Pylint installation
pip install pylint
Copy the code
  • PyCharm set

File – Settings – External-tools – Add, where:

The program:

Is the python installation path Scripts, mine is (recommended search pylint.exe to find path)

C:\ProgramData\Anaconda3\Scripts\pylint.exe

Arguments:

–reports=n –disable=C0103 $FilePath$  

(Must end with $FilePath$)

Working directory:

$FileDir$

(It has to be this one)

The output filters:

$FILE_PATH$:$LINE$:

The specific configuration is shown as follows:

\

Once configured, select a Python program and right-click it. The shortcut menu will show Extensions Tools -> Pylint. Click Run. The output is in the window where the program results are executed (the lower part of the IDE).

If you see a return value of 0, the program is fine. \

2.2 autopep8

Autopep8 is a tool for automatically typesetting Python code in the PEP8 style

  • Autopep8 installation
pip install autopep8
Copy the code
  • Autopep8 configuration \

File – Settings – External-tools – Add, where:

Name:

autopep8

(Just a name, whatever)

Program: autopep8\

Arguments:\

–in-place –aggressive –aggressive $FilePath$

Working directory:\

$ProjectFileDir$

Output filters:\

$FILE_PATH$\:$LINE$\:$COLUMN$\:.*

The specific configuration is shown as follows:

\

Place the mouse in the editor of the file → right click →External Tools→ Autopep8. So your code is pep8 style. \

  • The use effect is as follows:

Original non-standard code:

After using the tool:

Variable naming

In the usual work, a lot of programmers for the variable naming intertwined, call the fear of the later don’t understand, want to have a good name but my English level is not good, may need to spend a lot of time on this name, might delay the development of efficiency, recommends an artifact, today this artifact can get rid of the variable naming struggle!

  • Tool url \

unbug.github.io/codelf/\

Making links:

Github.com/unbug/codel…

\

Enter the variable to be named, either in Chinese or English.

Click search, the site will give the translation of the variable name, the following variable naming suggestions, you can copy the variable name, you can also look at the source code using the variable, and you can choose the type of development language.

  • Click “Search” to Search for other related names based on the current name.
  • Clicking “Repo” links to the repository of the code that uses that name.
  • Click “Copy” to Copy the name.
  • Click on “Codes” to see sample code using names.

conclusion

This article recommends a few magic tricks to save python code from being ugly. The above recommended tools will help you write beautiful code, make sure to bookmark and forward!

This article can be viewed “Read the original article”. \

Please follow and share ↓↓↓\

Machine learning beginners \

QQ group: 727137612

(Note: there are 7 QQ groups on this site, those who have joined any of them do not need to add more)

ID: 92416895

Currently, it ranks no.1 in the knowledge planet of machine learning

Past wonderful review \

  • Conscience Recommendation: Introduction to machine learning and learning recommendations (2018 edition) \

  • Github Image download by Dr. Hoi Kwong (Machine learning and Deep Learning resources)

  • Printable version of Machine learning and Deep learning course notes \

  • Machine Learning Cheat Sheet – understand Machine Learning like reciting TOEFL Vocabulary

  • Introduction to Deep Learning – Python Deep Learning, annotated version of the original code in Chinese and ebook

  • Zotero paper Management tool

  • The mathematical foundations of machine learning

  • Machine learning essential treasure book – “statistical learning methods” python code implementation, ebook and courseware

  • Blood vomiting recommended collection of dissertation typesetting tutorial (complete version)

  • The encyclopaedia of Machine learning introduction – A collection of articles from the “Beginner machine Learning” public account in 2018

  • Installation of Python (Anaconda+Jupyter Notebook +Pycharm)