Home >Backend Development >Python Tutorial >Python IDE: Those useful functions in PyCharm

Python IDE: Those useful functions in PyCharm

高洛峰
高洛峰Original
2016-10-19 13:23:061170browse

I have been using Eclipse for some years so far, and most of the time I have spent writing Python (installing the Pydev plug-in) and C++ (installing the CDT plug-in).

Recently I heard about PyCharm and its new free community version logr from a friend. This friend of mine has been using Eclipse for a long time and praised PyCharm highly, so I decided to at least try to use PyCharm when working from home. So far, I have fallen in love with it and even use PyCharm in my daily work (the community version is licensed by Apache2).

  Below I will introduce some things that surprised me when using PyCharm.

Python IDE: Those useful functions in PyCharm

Disclaimer

I have no intention of criticizing Eclipse or PyDev, I have used them for many years and they are great! Please keep in mind that this is my personal experience and yours may be different.

 Real-time comparison

  PyCharm keeps real-time tracking of the changes you make in a file by displaying a blue mark in the left column of the editor.

Python IDE: Those useful functions in PyCharm

This is very convenient. I have been using the command "Compare against HEAD" in Eclipse to compare the changes before and after a file.

  In PyCharm, you can see your changes at a glance. At the same time, clicking on that marker will display the previous content and a toolbar:

Python IDE: Those useful functions in PyCharm

You can easily roll back these changes, view them in a detailed dialog box or paste the previous text into on the clipboard.

 Comparison preview during submission

 In Eclipse, when you submit changes, there will be a commit dialog box showing you the list of files to be submitted.

  You can double-click any of the items to view a comparison of the changes: At this time, if you see something in the code that you want to modify (such as a misspelled word, a

missing document, etc.), you can That's not fun: you have to close everything (including the commit box with your carefully written commit comments), find the offending bit of code, correct it, and then do it all over again.

 PyCharm has the same features, but your changes are editable. I can't stress enough how great it is to be able to fix those mistakes on the spot!

Python IDE: Those useful functions in PyCharm

I can easily fix typos like the ones above during the submission process.

An added benefit is that no matter what reason you close the commit dialog, when you try to commit again, it will retain those commit comments you just filled out.

CHECK ON COMMIT

During the commit process, we can also perform some other options before the actual commit, like "optimize imports (sort and remove unused imports)", check the pending items in the change set, etc. wait.

Python IDE: Those useful functions in PyCharm

An interesting point is the item "Perform Code Analysis". . .

 Code Review

 You can ask PyCharm to perform a "code review" operation on a file, directory or the entire project. It will identify existing problems and corresponding improvement methods without actually executing the program, such as type checking, static methods, violations of code specifications, etc.

 As explained in the previous section, this can also be done automatically in the changed file on commit. This is the result of one review of a file:

Python IDE: Those useful functions in PyCharm

 For some of these review results, you can also apply a fix suggestion. For example, for the suggestion "Function call can be replaced with set literal()", we can select the corresponding option to fix it.

  PyCharm will automatically replace this sentence:

extensions = set(['.avi', '.mp4', '.mpg', '.mkv'])

  with:

extensions = {'.avi', '.mp4', '.mpg', '.mkv'}

You can turn off any review you don’t want, including at the project level.

 Refactoring

  I have been trying to rename a module, but I can’t find that item in the menu item. . . F2 doesn't help either.

Suddenly I noticed the "Refactor/Rename" item under the file submenu and thought. . . Could it be it? Indeed it is!

When you rename a module, PyCharm will ask you if you want to automatically modify all projects associated with it. Blessed!

There are many other refactoring projects, but I haven’t had the chance to try them all so far.

Tips on outdated code and improvement suggestions

PyCharm can tell you the outdated structural blocks in the code and make some improvement suggestions. For example, I have a piece of code like the following:

with nested(open(filename1), open(filename2)) as (f1, f2):
 <代码块>

PyCharm will clearly mark nested:

Python IDE: Those useful functions in PyCharm

I am using Python 2.7. In fact, you can use nested context managers, But, alas, I still have to support some older versions of python, so I kept nested.

But PyCharm reminded me that this thing itself is still cool.

Task + Feature Branch

Like Eclipse (requires Mylin support), PyCharm also supports task-based workflows (including task-aware context).

However, PyCharm itself comes with many directly available connectors (GitHub, Mantis, Jira, Bugzilla, etc.).

 Moreover, when you start a task, it will ask you if you want to create a feature branch of that task. The name of the branch can be configured.

Python IDE: Those useful functions in PyCharm

This avoids having to manually create a new branch, which is really tedious (getting the task ID, choosing a name, etc.).

 This is a plus.

 Fast

 I have observed that PyCharm scans the code much faster than Eclipse when performing automatic code completion, and it does not interrupt your current work.

 How annoying is it when you want to save a file and Eclipse prevents you from doing so because it is executing a task in the background. . .

Quick View Documentation

When you move the cursor over a function, method, class or anything else, you will get an option to browse the "Quick Documentation". Take a look:

Python IDE: Those useful functions in PyCharm

As you can see, it displays the method documentation in a pleasing format, and also hints at the parameter types from the code usage instructions. . . Even if there is no corresponding documentation for this function, you will still get a description like the following:

Python IDE: Those useful functions in PyCharm

 Great!

 Docutils Support

  PyCharm also includes first-class support for doctils, making it easy to regenerate documentation directly from the integrated development environment.

On the other hand, it also includes a very good rich text (Translator’s Note: ReST, reStructuredTexteditor) editor (much easier to use than Eclipse’s IMHO):

Python IDE: Those useful functions in PyCharm

Complete plug-in system

According to my experience, installing plug-ins in Eclipse is quite painful:

It is difficult to find where you want to go (tip: Help/Install new software...);

You don’t have a single index, so you have to go online to find those plug-in sources;

The system sometimes crashes due to library dependencies, and as a result I don’t install the plug-ins I want (there are also some at work) Others also encountered this problem and had to give up in the end);

  PyCharm’s plug-in experience is much smoother.

 First, it’s in the place where you’d think it might be: just under “Settings”:

Python IDE: Those useful functions in PyCharm

You can easily browse for plugins:

Python IDE: Those useful functions in PyCharm

I quickly installed one Markup Editor (like ReST), a plug-in that temporarily stores code snippets, allows me to quickly cut and paste directly from the selected text, and preview the code, similar to SublimeText, but also with a beautiful code appearance.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn