Share PyCharm five weekly tips (five)

Article first appeared in the public micro-channel number: Python programming time

PyCharm IDE of choice for most Python developers, every day we are all familiar with knocking in the above code, write one after another wonderful features.

A tool in use, if they can have some efficient tips and tricks will definitely give our development efficiency brought about a qualitative improvement every day. Today I come to write about me in the normal development has been in use, it can significantly improve the development efficiency of the use of skills, once learned, good enough for a lifetime.

This is the first effective use of techniques of this series PyCharm. By convention, this share was five.

Other articles in this series are as follows, if not see, you can click on to view





21. everywhere folded implementation code consisting of

PyCharm folding feature in the code block, rather conspicuous, the code in the left edit box, you can find +also -easily understood that +the representative block is folded can click here to expand, and -represent this block point can be expanded state this folding.

If you're like me a party keyboard, you can use the shortcut (Mac: hold down the Command key, then press +or -, Windows: hold down the Ctrl key, then press +or -) fast counter-folding / folding.

And folding the fold-back code blocks should be a basic function of the code editor. At this point, PyCharm as an IDE, is bound to do better and better at this point, it turns out, it did.

From the above, we know only the code block before scaling support, what kind of code PyCharm would think it is a block of code? Its strictly defined I did not find, from experience, a class, a function, a for loop, a while loop, a multi-line comments, etc. are blocks of code. It can be folded, fold-back.

Sometimes, we do not want piece of code folding, while others just want a large segment of our being useless code folding. You can do it?

The answer is yes.

只要你先选中你想折叠的代码,再按住 Command 紧接着按住 . 就可以了。效果如下:

(GIF动态只播放两次,重播请刷新页面)

22. 重构操作,一步到位

最近有一位同事走了,由我来接手他的全部工作。

可能由于我有代码洁癖,我花了一个星期对其代码进行了大量的重构。

重构代码,免不了要对变量进行重命名。

如果一个一个改,显然不太智能,要知道我们是在用IDE,你也许会说,用搜索全部替换不就行了?还真不行。

比如下面这段代码,我只想改myfun 里的的test_name,而对于全局下的同名变量是不应该修改的。如果你全局替换,就会有误伤。

这时候,我们如何做呢?

可以使用 PyCharm 的 Refactor 功能,它会自动匹配作用域,既做到批量更改,也做到不误伤。

操作方法很简单,先选中你的变量,然后使用快捷键 Shift+F6,就可以直接重命名了。

(GIF动态只播放两次,重播请刷新页面)

23. 复杂操作,录制成宏

如果你在使用PyCharm 的时候,遇到有一些操作是比较复杂(步骤多),且使用频率特别高。

那可以考虑一下,使用其自带的宏录制工具。

它会将你的一连串操作,录制下来。等你想用的时候,直接调用就行了。

这边,我以录制一个 删除函数 的宏为例:先按上面的方法折叠函数,再按 Command+y 删除该行,就删除了该函数。

做录制方法如下:

(GIF动态只播放两次,重播请刷新页面)

录制好后,你可以先定位到你要删除的函数处,点菜单栏 Edit - Macro 然后选择我们刚刚录制的宏,就可以播放宏了。

这样播放宏显得有点繁琐,个人建议你为这个宏定义一个快捷键,这样会更方便播放宏。

设置快捷键时,注意不要和已有的快捷键冲突。

设置好后,查看 Macro,发现PyCharm已经将这个快捷键绑定给这个宏。

之后你就可以使用这个快捷键删除一个函数(其实这只是删除一个代码块,但是这里只讨论设置方法)。

24. 多行标签页,一览无余

PyCharm 打开一个文件,就占用一个标签面。

你有没有发现,不知不觉地,打开的文件越来越多,多到一行标签都装不下,装不下的标签页 PyCharm 会将其隐藏起来,并以数字的形式告诉你隐藏了几个文件。

点击数字5,你才可以查看隐藏了哪些文件。

这时你肯定会说,一行装不下 PyCharm 为什么不能多行显示呢?

答案是,不是不能,而是需要你设置。

如下图,将单行显示取消勾选即可。

设置完后,有哪些文件就非常清晰了。

25. 应用搜索,阅读源码必备

你平时若有阅读框架源码的需求或习惯,那你一定要来掌握这个技能。

在阅读源码时,代码的入口、流程、走向特别重要。

假设你现在知道了一个特别关键的类,你特别想知道是哪里调用了这个类,你想知道源头,知道整个调用流程。

这个时候你使用全局搜索,就会有很多的干扰信息。

这个时候,你急需有一种方法,可以快速给你列出有哪些地方调用了这个类。

快捷键:Mac:Command+Option+F7 ,Windows:Ctrl+Alt+F7

如下图所示,按下快捷键后可以很轻松地看见调用列表。

如果你嫌这快捷键太长了,可以使用 鼠标中键 点击这个类,可以达到同样的效果。


关注公众号,获取最新干货!

Guess you like

Origin www.cnblogs.com/wongbingming/p/11122994.html
Recommended