In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you what are the practical skills of PyCharm. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Today, I mainly introduce the efficient use of PyCharm skills!
The following code demonstrates that in the Mac environment, Windows is similar, and the Command key is equivalent to the Control key on Windows.
1. Code typesetting, automatic PEP8
Pep8 is a coding specification of the Python language. If you are a novice and want to master the basics quickly at the moment, without paying too much attention to the writing style of the code (although this is important), you can try this tool-autopep8
First install the tool in a global environment (not in a virtual environment).
Sudo pip install autopep8
Then import the tool in Pycharm.
Name: AutoPep8 Description: autopep8 your code Program: autopep8 Arguments:-- in-place-- aggressive-- aggressive $FilePath$ Working directory: $ProjectFileDir$ Output filters: $FILE_PATH$\: $LINE$\: $COLUMN$\:. *
I wrote a random piece of code that didn't conform to the pep8 specification.
Right click and select External Tools-> AutoPep8
Take a look at the effect, it is still quite obvious.
You might say, Pycharm itself has this function, ah, the shortcut key Command+Option+L, you can achieve one-click pep8. You can compare that Pycharm's native code pep8 function is not as thorough as this autopep8. I'm sure your final choice must be the latter.
two。 Delete the file by mistake and retrieve it in a second
Last week, due to my own misoperation, I deleted a project that I had written for two weeks without any backup. When I look back, I can't even remember when the deletion was done.
As an old driver, of course, he opened the Recycle Bin in the face of danger and carried out a search. the Recycle Bin, which had not been cleaned for months, was really a mess, with all kinds of jpg,avi.
I skimmed through it in a minute and didn't find the py files I was looking for. I thought to myself, there must be too many files. Look at the forks. Since the project was recently written, I still remember the file name clearly. Since there is a file name, I still use the search function that comes with windows, but the results are still not there. It was only then that I began to realize the seriousness of the situation, and the file may really be "gone".
I haven't cleaned the Recycle Bin for a long time. Why are there no files for me in the Recycle Bin?
I think this may be an unusual delete, could it be a delete operation initiated in Pycharm that will not be thrown into the Recycle Bin? After some testing, I really can't find it in the Recycle Bin, but this attempt also inadvertently found a hidden function of Pycharm, Local History, which keeps records of all your operations on the file.
Take the file I just tested, for example, I first created a new file, and then added a few lines of code to the file. Finally, I deleted the file.
At this point you can right-click in your project directory, there is an option for Local History, and then click the sub-option Show History, you can see that there is a record board here. If you want to restore deleted files, right-click on the deleted entry and select Revert to restore.
3. Hug Vim and stay away from the mouse
In most scenarios, the efficiency and accuracy of using the mouse is far less than keyboard shortcuts (provided you have mastered keyboard shortcuts quite skillfully), which you have to admit.
Vi can meet all your needs for text manipulation, which is more efficient and geek than the visual interface. If you are a loyal vim fan like me. After installing Pycharm, we will definitely install the ideaVim plug-in as soon as possible, which allows us to use vim to edit the code in Pycharm.
The installation method is as follows. After installation, you need to restart Pycharm to take effect.
4. Code template, efficiency coding
This code template provided by Pycharm can be said to be a quite practical function. When you create a new file, it can generate a piece of content for you according to your preset template, such as interpreter path, encoding method, author details, etc.
According to the template above, the effect is as follows.
In addition to initializing files when creating new files, you can also use the practical code templates that come with Pycharm when developing and writing code to improve your coding efficiency.
When you type Command + J in the keyboard, you can bring up a panel, and you can see that there are many preset templates in the image below.
If we want to choose the last main, we can continue typing main, and then we can directly generate the following code that we would normally type manually.
Here's another example. The for loop can be written like this.
5. Use bookmarks to locate quickly
When I look at the source code of the framework, I most often use Command + B (that is, command + left mouse button) to go deep into it layer by layer, but when there is more source code, maybe a dozen files are involved in the whole event process, and the function calls are complex. For a huge project, there may be only a few key functions that are useful. Every time you want to find these functions, you have to start from the source function layer by layer. This is so troublesome that I often confuse myself with it.
It wasn't until I discovered the bookmark function of Pycharm.
Using the bookmark function, I can bookmark in key locations. When I want to see it, I can call the bookmark and locate it quickly.
To use it, you need to remember the following shortcuts
Command + F11 bookmark Shift + F11 to show all bookmarks F11 delete bookmarks (tested to be valid only on Windows)
In the position where you want to bookmark, press Command + F11, you can add a serial number to this position, it can be a number or a letter, if you add the serial number 1 in the position below, next time you can use Control + 1 to jump to this position directly.
Of course, you don't have to add it, or it's an anonymous bookmark. You can use Shift + F11 to display all the bookmarks and then jump.
6. Divine debugging skills
If we were in a crawler project, we would use regular expressions to match what we want to grab. How many people can achieve this kind of thing in one step, it usually needs to be debugged many times before it can match as expected. After we changed the rule once, after running it, we need to crawl the request to the website again to find that there is no match, and then we change another version, and we also need to initiate a request to run again, but we still find that there is still no match, and we often repeat it. Students with bad rules may have to make dozens of attempts.
(the above example may not be appropriate. After all, there are many ways to avoid reissuing the request. It just enumerates a very clumsy and inefficient debugging process. Just take a look at it.)
And in these dozens of debugging, launching a request to the same website is a meaningless repetitive work. If you can be like IPython Shell and Jupyter Notebook in Pycharm, you can remember all the variable information after running, and you can do regular debugging without rerunning the project or script, just slightly adjusting our code.
Of course the answer is yes.
Suppose I am debugging the following simple lines of code. A breakpoint was made at line 3. Then click the Show Python Prompt button on the location shown in the icon.
Enter the interface of Python Shell, this Shell environment is connected to the program environment we are running, using the same namespace, so you can easily debug.
We made a breakpoint above to facilitate the illustration of this effect. It doesn't mean it has to be broken. If you don't break the point, you can still view and manipulate all variables in this interface after the script is executed.
Now we can meet our debugging needs, but every time we run the script, we have to click Show Python Prompt manually, which is a bit troublesome. Yeah? In fact, there is a place to open it by default. The beginning is rather secret, and most people really can't find it.
You need to click on the location Edit Configurations.
And then check it here.
After setting it up, every time you run the post-script, it will store the values of all variables for you by default and open the console command line debugging interface for you.
In addition to the above method, in fact, there is another way to execute command expressions during debugging, which you may be familiar with. I will also mention it here as a summary. But functionally, it is not as easy to use as the above method. Because of this approach, you must be required to run the project in debug mode and break points.
The way to use it is, after you hit the breakpoint, right-click on the location shown in the diagram and use Evaluate Expression
An Evaluate Expression window pops up, where you can run command expressions and manipulate variables directly.
7. Specify parameters to execute the script
How do you usually execute your project when you run it in Pycharm? What I do is, right-click and click Run, or use the shortcut key Shift + F10.
Sometimes, when running / debugging a script, we need to specify some parameters, which can be specified directly on the command line.
Suppose that on the command line, the command that runs the script looks like this
Python main.py init-local
For students who have just used Pycharm, they may not know that Pycharm can also specify parameters. Click the location of the image below
Enter the settings panel and fill in the parameters in Script parameters.
At the same time, at the bottom of the image above, you can see that it is very convenient to switch interpreters here, which is much easier than if you come here.
8. Filter test files when searching
Next, I would like to introduce a little trick when I look at the source code of the framework, which may only be applicable to a small number of people.
The framework I usually look at is OpenStack. I don't know what other frameworks look like, but there are a lot (really a lot) of unit test files in OpenStack. This brings me a lot of trouble when using Find in Path. You can feel it from the search results in the figure below. Search for a function, and the results in test files are much more than normal files.
The search results of these test files are not only not helpful for us to look at the source code, but also disturb the line of sight. So I studied it, starting with the file name, just fill in the File mask! test* can filter out these test files. The search results are much clearer at once.
9. Turn off the annoying light bulb prompt
I didn't want to write this, but there was a friend in Zhihu who had this need, so I studied it.
Let's start with what this light bulb hint is and what's the use?
When we have syntax errors in the code, or the code does not conform to the pep8 code specification, the mouse selects the code in question, it will automatically pop up a small light bulb, this light bulb is divided into colors, if it is a red light bulb, it is generally a syntax problem, if it is not dealt with, it will affect the code to run. If it is a yellow light bulb, it is just a hint that your code is not standard, and it will not affect the operation of the program.
Although this light bulb is an act of kindness, I think it is a bit superfluous (maybe I am not in the habit of using it personally), and there will be a red wavy line if there is a grammatical error. You might say that the light bulb not only serves as a hint, it can also correct the code automatically. I personally feel that it is not as efficient and accurate as manual correction.
Based on sometimes as Zhihu this friend said, will block our code, will often be late, this is indeed a worry.
I have studied that there is a switch button in Pycharm (version 2018). Uncheck this option (Show intention bulb) in the image below and you can turn this function off.
10. Close the wavy line that is a nuisance.
Let me give you a short code example to think about why name,my_name doesn't have wavy lines, while myname and wangbm have wavy lines.
Pycharm itself checks the variable name in real time. If the variable name is not an existing English word, a wavy line appears. When there are more than one word in a variable, Python recommends that it be separated by an underscore (other languages may be used to using hump naming, but Python uses an underscore), so in Pycharm's view, my_name is standard, while myname is treated as a word. Because it doesn't have it in the thesaurus, myname is irregular.
Everyone's variable naming habits are different, how you use a lot of myname-style variable naming methods in the project, like the following (randomly find a piece of cloudinit code), it is very uncomfortable, there is always a code has the illusion of bug.
So how do you turn off this non-grammatical wavy line? It's very simple. The switch is on the head-like button in the lower right corner of you.
Then select the Syntax level. The same piece of code, the effect is as follows, much cleaner.
11. One-click code performance analysis
There are many modules in Python that can help you analyze and find out where there are performance problems in your project.
For example, the commonly used modules are cProfile, and in some frameworks, middleware is also built-in to help you with performance analysis, such as Django and WSGI.
As the first IDE of Python, PyCharm itself supports this feature. And it's very easy to use, rookie.
Suppose you now want to analyze the performance loss of the following code to find out which function takes the most time
Import time def fun1 (): time.sleep (1) def fun2 (): time.sleep (1) def fun3 (): time.sleep (2) def fun4 (): time.sleep (1) def fun5 (): time.sleep (1) fun4 () fun1 () fun3 () fun5 ()
Click Run-> Profile 'Program' to perform performance analysis.
After running, a performance statistics interface will pop up automatically.
The performance statistics interface consists of four columns: Name, Call Count, Time (ms) and Own Time (ms), as shown in the following figure.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The header Name shows the module or function called; Call Count shows the number of times called; and Time (ms) shows the elapsed time and the percentage of time in milliseconds (ms).
Click the small triangle on the header to sort the table in ascending or descending order.
Double-click a row in the Name column to jump to the corresponding code.
Take the fun4 line as an example: fun4 was called once, and the run time was 1000ms, accounting for 16.7% of the total run time.
Click the Call Graph (call Diagram) interface to visually show the direct calling relationship, running time and percentage of time of each function, as shown in the following figure.
The four buttons in the upper left corner indicate zoom in, zoom out, true size, and appropriate size.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The arrow indicates the calling relationship, pointing from the caller to the callee.
The upper left corner of the rectangle shows the name of the module or function, and the upper right corner shows the number of calls.
The middle of the rectangle shows the elapsed time and percentage of time
The color of the rectangle indicates the trend of running time or percentage of time: red > yellowish green > green. From the figure, you can see that the rectangle of fun3 is yellowish green, fun1 is green, and all fun3 runs longer than fun1.
You can see from the figure that Test.py calls fun3, fun1, fun2 and fun5 functions directly; fun5 function calls fun4 functions directly; fun1, fun2, fun3, fun4 and fun5 all call print and sleep functions directly; the total running time of the whole test code is 6006ms, where fun3 runs for 1999ms, accounting for 33.3% of the time, that is, 1999ms / 6006ms = 33.3%.
twelve。 Using Git for version control
Follow the prompts below to click the Git warehouse configuration
Then enter the warehouse address
Click Test to test connectivity and will ask for a password
If all goes well, you will see the following interface
After the test connection is successful, click Clone to clone it.
For those who used to use the Git command to manage, they can now be operated directly using the menu bar of PyCharm. These functions can already meet the daily needs of most people and should be sufficient.
13. Tab easily turns spaces
In teamwork, you will inevitably touch other people's edited files, some people like to do tab indentation, some people like to indent with four spaces. (it is recommended to use tab to change four spaces.)
But in the same Python file module, tab and four space indentation styles cannot coexist. This requires you to encode according to the original indentation style of the file. In Pycharm, you can set the indentation style of the original file to automatically detect the indentation style of the original file to determine whether it is TAB or four spaces when you use tab indentation.
Automatic detection can be turned on by ticking the position shown in the diagram.
The above is how to determine the indentation of the current editor when making changes to an old Python module.
For new modules, how is the default indentation determined?
As shown below, if you tick Use tab character, after you create a new Python, you will use TAB for indentation, and vice versa, use four spaces for indentation.
14. Source documents, quick preview
Ctrl + left mouse button (Mac is: Command + left mouse button), you can achieve the function jump to view the source code, which is almost every PyCharmer will be a common skill.
Here are two similar tips for quickly viewing the function documentation and previewing the source code.
At the beginning of the function, use three quotation marks to contain what is called the function document (DocString).
It is a good coding habit to write the interface documentation of the function when writing the code. It introduces the function parameter type and description, return value type and example, better written will also write a few simple Example Usage to help understand and use. Here is a random example of Werkzeug.
If we forget this usage when using this class, we can press and hold Ctrl + Q (the shortcut key for Mac has not been found yet), and you can quickly preview the LocalStack interface document on the current page.
Similarly, if you are interested in the code logic of this class or function, you can also use a quick preview to display the source code on the current page. The shortcut key is: Ctrl + shift + I (Mac:Command + shift + I). The effect is as follows
If PyCharm detects more than one function with the same name, you can click here to switch and select to view.
These two shortcuts are more convenient than using Ctrl + the left mouse button to jump into the source code. Like WeChat Mini Programs, they burn immediately after use, without creating a new label page or jumping back and forth.
These are the practical skills of PyCharm shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.