The latest PyCharm basic use (3) ------ coding (Code), reconstruction (Refactor)

Contents
6. Code filling, supplementation, inspection, analysis, cleaning and other operations in PyCharm
7. Refactoring operations such as renaming, changing signatures, extraction, introduction, inline (restore), safe deletion, adjustment, conversion, etc. in PyCharm

Six, code filling, supplementation, inspection, analysis, cleaning and other operations in PyCharm

Code menu _

        The options of PyCharm's Code menu are shown in Figure 6-1, which are coding operations such as automatic code filling, supplementation, inspection, analysis, cleaning, expansion, folding, format specification, indentation, optimization import, and movement in PyCharm. The menu has Code manipulation options and their shortcut keys. Based on context awareness, PyCharm can automatically fill, supplement, check, analyze, etc., improving our programming speed and code quality.

Figure 6-1 Code menu of PyCharm

Override Methods

       override method. Place the mouse anywhere in the class, select this item, rewrite (overload) the method of the parent class, the method of the parent class is generated in the subclass, and then in the generated method, we can write code.

Implement Methods

       Implementation. Selecting this item can fill in the content of the method to realize the function of the method.

Generate

     generate. This option corresponds to a pop-up menu according to the position of the cursor, and the menu integrates options such as Override Methods, Implement Methods, Test, or Copyright.

Code Completion

       Code complete. The options in the pop-up menu after selecting this option can automatically supplement code-related content and speed up our writing code. For example: Basic in the menu can help us fill in the names of related classes, methods and keywords.

Inspect Code

       Check the code. Checks against the specified inspection range and displays problems with code within that range. The inspection code is inspected according to the relevant strategies provided in File→Settings→Editor→Inspections.

Code Cleanup

       Code cleanup. This option is based on the specified scope, where it finds potentially problematic code fragments and automatically fixes them immediately.

Analyze Code

      Analyze code. Select the option in the menu of this option to complete specific code analysis, such as: Run Inspection by Name, you can specify an inspection (noun, usually a file) based on the name, and use this inspection (noun) to inspect (verb) we specify The code within the range, that is, after selecting this option, first confirm a check (noun) in the pop-up window, and then specify the range in the new window after confirmation, and then you can conduct code review after confirmation.

Analyze Stack Trace or Thread Dump

       Analyze stack traces or thread dumps. Exceptions like deadlocks , freezes, etc., these exceptions do not have links to help you navigate to the corresponding location in the source code, using PyCharm, we can copy the stack trace produced by the exception , or copy the complete thread dump, paste it in Select this option to pop up the Stack trace Analyzer window, and navigate to the corresponding source code after confirmation.   

       Stack Trace (stack trace, stack trace), when an exception occurs, a stack trace will be generated. Among them, the stack (stack), also known as the stack, is a special linear table (a dynamic memory area in the memory) that can only be inserted and deleted at one end (the top of the stack). The stack generally stores the address of the function and the address of the object. Reference address, etc., stack trace (stack trace, stack trace) is a description of the activity record of the program running process.

      Thread Dump (thread dump) contains: thread, thread running status, identification and call stack, the stack contains the complete class name and executed method, the number of lines of source code , etc. Thread (English: thread) is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operating unit in the process. A thread refers to a single sequential flow of control in a process. Multiple threads can run concurrently in a process, and multiple threads can perform different tasks in parallel.

       Deadlock refers to a phenomenon in which two or more processes are blocked due to competition for resources or communication with each other during execution. If there is no external force, they will not be able to advance.

Insert Live Template

       Insert active template. Select this option to create a code construct (eg a fixed code statement) at the cursor position based on the active template. Selecting this option will pop up a menu, in which is the existing Live Template (active template), select a certain template, the cursor position will automatically generate the content represented by this template.

      The templates in the pop-up menu of this option are generally the default built-in templates. We can also create templates according to our own needs in File→Settings→Editor→Live Template, for example: the pop-up in File→Settings→Editor→Live Template Select the Python template group (template group) in the window, click the plus sign (+) on the right side of the window, there are Live Template and Template Group (create a template group, multiple templates can be placed under the template group name) two options, select Live Template , enter Abbreviation (the abbreviation of the template, that is, the template name), Description (the description of the template), and Template text (the template text, that is, the template content) at the bottom of the window, and you can customize and create a new Python template after confirming.

       Note that the Live Template option here is a different concept from File→Save File as Template.

Save as Live Template

       Save as active template. First select a certain content of the code, and then select this option to create a Live Template (active template). In fact, the default template is created under the user template group in File→Settings→Editor→Live Template.

Surround With

       Wrap around to create code constructs. Put the cursor on a line of code, select the option in the pop-up menu, and you can generate corresponding code statements around the line of code, such as: if statement, while statement, comments, etc. The options in the option pop-up menu are determined by Pycharm according to the context of the line of code. When the line of code cannot generate surround, it prompts Couldn't find Surround With variants applicable to the current context (cannot find Surround that is applicable to the current context Variants).

Unwrap/Remove

       Expand or Remove, quickly expand or extract expressions from enclosing statements. For example: the cursor is placed at a certain position in the if body, after selecting this item, the code statement at the same level above and below will be automatically selected, and a prompt window for confirming Unwrap/Remove will pop up (the options in the window will be generated according to the context of the selected statement), confirm After that, these selected statements will be automatically extracted, and the if condition will be removed. This option operation is more suitable for statements of control structures such as if, while, for, etc.

Folding

       fold. Put the cursor at a certain position or select a certain area, and select the option in the menu of this option to expand and collapse the corresponding code. For example: Select Collapse in the menu to collapse and hide related codes, select Expand in the menu, or expand related codes.

Comment with Line Comment

        Line comment, after selecting this option, a comment symbol will be automatically added at the beginning of the line where the cursor is located, and this line will become a comment line.

Comment with Block Comment

        Block comments, multi-line comments, these multi-lines become comment lines.

Reformat Code

       Re-format the code (code format specification), automatically adjust, align, and standardize the code, making the code space interval more standardized and enhancing readability.

Reformat File

       The code for the entire file can be formatted. It can automatically adjust, align, standardize, clean up, etc. the code of the entire file, making the space interval of the entire file code more standardized and enhancing readability. In the menu of this option, some options of formatting can also be set. For example, if you check the Optimize Imports in the menu of this option, you can remove the unused import statement. Same.

Auto-Indent Lines

       Automatically indent. If this item is selected, the line where the cursor is located or the selected code will be indented to the left or to the right automatically according to the context.

Optimize Imports

       Optimize imports. It is possible to remove unused or duplicate imports in a file, and it is also possible to organize (plan) import statements.

Rearrange Code

       Rearrange the code, you can rearrange the code according to the arrangement rules. Python does not support this operation.

Move Statement Down

       Move the statement down. The line or code block corresponding to the mouse position will move down. For example: in the class, the cursor is at the position of the defined method name. After selecting this item, the method will move down as a whole, and exchange positions with the methods below in the class.

Move Statement Up

       Move statement up, opposite to Move Statement Down above.

Move Element Left,Move Element Right

       Moves the element at the cursor position to the left, and moves the element at the cursor position to the right.

Move Line Down,Move Line Up

       Move down one line, move up one line, similar to Move Statement Down, Move Statement Up, but only one line can be moved.

Update Copyright

       Update copyright.

7. Refactoring operations such as renaming, changing signatures, extracting, introducing, inlining (restoring), safely deleting, adjusting, converting, etc. in PyCharm

Refactor menu _

       The options of PyCharm's Refactor menu are shown in Figure 7-1. The options of the Refactor menu are determined by the current context. They are refactoring operations such as renaming, changing signatures, extracting, introducing, inlining, safe deletion, adjustment, and conversion in PyCharm. , there are refactoring operation options and their shortcut keys in the menu. Refactor, re-alter, improve. Code refactoring is generally about improving source code without creating new functionality. Refactoring helps keep your code stable and easy to maintain.

 Figure 7-1 Refactor menu of PyCharm

Refactor This

       Refactor, re-alter, improve. Code refactoring is the improvement of source code without creating new functionality, refactoring helps to keep the code solid and easy to maintain. Select (or hover over) the symbol or code fragment to be refactored with the cursor, select this option, and the options of the refactoring menu will pop up (the menu options are determined by the context where the cursor is located), such as: renaming the class or method (Rename, The usage is the same as the Rename mentioned below), Change Signature (change the signature, the usage is the same as below), introduce variables (Introduce Variable), introduce constants (Introduce Constant), etc. If it cannot be refactored, it will prompt Cannot perform refactoring using the selected element (cannot perform refactoring using the selected element (option)).

        There are many options in the option pop-up menu that will be described below.

Rename

       To rename a variable, function, class, or method, select this option to pop up a window for modifying the name of the cursor position, and some pay attention to select the scope of modification (Scope), for example: when modifying the class name, you can target the entire project, or you can choose to target current document.

Change Signature

       To change the signature, you can change the function name or method name in the file in the current directory of the project, or add, delete, reorder, and rename its parameters, and the same call (including inheritance) levels in all files in this directory also happen the same Change, for example: the method func in class AA in the file in the current directory of the project, select Change Signature, change the name of the func method in the pop-up menu, the func method in the current file will be renamed, and all files in the current directory of the project, as long as they are Func in AA.func or inherited calls will also be renamed.

       Therefore, this option is special. When using this option, you should pay attention to the impact on other files. When this option is set to refactor, you can preview (preview) first to check the changes to other files.

Extract/Introduce

       Extract (excerpt) or import, this option determines the extraction (extract) or import according to the operation of the cursor and the context. If the cursor is not selected or a certain name is selected, it will be imported by default. If the cursor selects an expression, it will be extracted. For example: select an expression with the cursor, and then select the Variable option in the option pop-up menu, then the expression The expression is assigned to a new variable, and this new variable is used to refer to (represent) the expression. If it cannot be extracted (excerpted) or introduced, it will prompt Cannot perform refactoring using the selected element (cannot perform refactoring using the selected element (option)).

       The pop-up menu of this option mainly includes Variable (variable), Constant (constant), Field (field, provided for computer identification, similar to the reference function of variable, commonly used in databases), Parameter (parameter, such as: function parameter ), Method (method), Superclass (superclass, parent class) and other options.

Inline

      Inline, restore. Inline refactoring is equivalent to the inverse operation of Extract above, which is the inverse operation of method or variable extraction (Extract). For example: put the mouse on a variable or select a variable, and then select this option to restore the extraction, that is, to restore the original expression before extraction. Inline reconstruction of the method, there are two options Inline all invocations and remove the declaration and Inline all invocations and keep the declaration in the pop-up menu, the former is to inline all invocations and delete the declaration (defined method), the latter is to inline all calls and preserve declarations (defined for this method).

Move File

        Move the current file to a folder.

Copy File

         Copy the current file to a folder.

Safe Delete

       Safe to delete. Safe Delete refactoring can safely delete files. Select the file in the Project display bar on the left side of the PyCharm window, and then select this option to prompt whether it is safe to delete the file. This operation will check whether there is an external use when deleting. If there is a reference, it will open "Detected usage ” dialog box.

Pull Members Up

       Pull up the members, this refactoring can move the members in the class to the superclass (parent class). To move a variable, you need the cursor to select the defined variable, and to move the method, you only need to place the cursor at the position of the defined method.

Pull Members Down

         Pull down members, this refactoring is the opposite of Pull Members Up, which can move members from the parent class to the subclass.

Invert Boolean

       Invert Boolean values, this refactoring can convert the value of a Boolean variable and all its usages from True to False and vice versa. Put the cursor on the variable position, and select this option to realize it. For example: original:

def func():
    c1 = True
    return c1

After selecting this option:

def func():
    c1 = False
    return not c1

Convert to Python Package

        Convert the module (module) into a package (also called a library), select the module (for example: .py file) in the Project item display bar on the left side of the PyCharm window, and select this option to generate the package.

Convert to Python Module

       Converting a package to a module is the reverse operation of Convert to Python Package. Select the package in the Project display bar on the left side of the PyCharm window, and select this option to generate a module.

Finally, you are welcome to like, bookmark, and follow!

Guess you like

Origin blog.csdn.net/thefg/article/details/129385113