-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycharm-the-good-parts-ii.html
46 lines (36 loc) · 17.2 KB
/
pycharm-the-good-parts-ii.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html> <head lang=en><meta charset=UTF-8><title>PyCharm: The Good Parts II | EF</title><link href=//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css rel=stylesheet><link href=http://fonts.googleapis.com/css?family=Inconsolata rel=stylesheet type=text/css><link rel=stylesheet href=http://nafiulis.me/theme/css/main.css><link rel=stylesheet href=http://nafiulis.me/theme/css/pygment.css><script src=http://nafiulis.me/theme/js/jquery.min.js></script><script src=http://nafiulis.me/theme/js/main.js></script></head> <body> <!--Heading at the top saying "Engineering Fantasy"--> <div id=header_top> <div class=title> <a href=http://nafiulis.me><span id=engineering>Engineering</span><span id=fantasy>Fantasy</span></a> </div> </div> <button type=button class="js-menu-trigger sliding-menu-button button-nav"> <img src=https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/menu-white.png alt="Menu Icon"> </button> <!--Navigation Bar--> <nav class="js-menu sliding-menu-content"> <span class=section-header>Pages</span> <ul> <li><a href=http://nafiulis.me>Home</a></li> <li><a href=http://nafiulis.me/tags.html>Tags</a></li> <li><a href=http://nafiulis.me/pages/about-me.html>About Me</a></li> </ul> <span class=section-header>Categories</span> <ul> <li><a href=http://nafiulis.me/category/anime.html>Anime</a></li> <li><a href=http://nafiulis.me/category/education.html>Education</a></li> <li><a href=http://nafiulis.me/category/productivity.html>Productivity</a></li> <li><a href=http://nafiulis.me/category/programming.html>programming</a></li> <li><a href=http://nafiulis.me/category/rants.html>rants</a></li> </ul> </nav> <div class="js-menu-screen menu-screen"></div> <!--Main Container--> <div class=container> <!--Top most menu--> <div id=menu> <div class=left> <a href=http://nafiulis.me/feeds/all.atom.xml><i class="fa fa-rss
icon"></i></a> <a href=https://twitter.com/gamesbrainiac><i class="fa fa-twitter icon"></i></a> </div> <div class=center> <h1 class=message>Nafiul Islam on casting spells with code</h1> </div> <div class=right> <a href=https://github.com/gamesbrainiac><i class="fa fa-github icon"></i></a> <a href=http://stackoverflow.com/users/1624921/games-brainiac><i class="fa fa-stack-overflow icon" style="padding-right: 30px"></i></a> </div> </div> <!--Main blog list container--> <div id=blogroll> <div class=article-container> <h1>PyCharm: The Good Parts II</h1> <p class=time>Sunday, 18 January 2015</p> <div class=article-content> <div class="contents topic" id=table-of-contents> <p class="topic-title first">Table of Contents</p> <ul class=simple> <li><a class="reference internal" href=#code-completion id=id6>Code Completion</a><ul> <li><a class="reference internal" href=#smart-completion id=id7>Smart Completion</a><ul> <li><a class="reference internal" href=#runtime-information id=id8>Runtime Information</a></li> <li><a class="reference internal" href=#docstrings id=id9>Docstrings</a></li> <li><a class="reference internal" href=#assert-statements id=id10>Assert Statements</a></li> </ul> </li> <li><a class="reference internal" href=#hippie-complete id=id11>Hippie Complete</a></li> <li><a class="reference internal" href=#spelling-correction id=id12>Spelling Correction</a></li> <li><a class="reference internal" href=#language-injection id=id13>Language Injection</a></li> </ul> </li> <li><a class="reference internal" href=#editor id=id14>Editor</a><ul> <li><a class="reference internal" href=#font id=id15>Font</a></li> <li><a class="reference internal" href=#braces-and-quotes id=id16>Braces and Quotes</a></li> <li><a class="reference internal" href=#case-sensitive-completion id=id17>Case Sensitive Completion</a></li> </ul> </li> <li><a class="reference internal" href=#up-next id=id18>Up Next</a></li> <li><a class="reference internal" href=#previously id=id19>Previously</a></li> <li><a class="reference internal" href=#update id=id20>Update</a></li> </ul> </div> <p>Before we begin, I must talk about what I mean by editing, and what I intend to cover. I believe that the most important thing about editing is code completion. In this section I pay special attention to python although PyCharm offers superb code completion for other languages like SQL and JavaScript.<a class=footnote-reference href=#webtools id=id1>[1]</a></p> <p>I start off with code completion, because if you're like me, you have little time and want to dive into the thick of things and code completion is what we want most out of an IDE.</p> <p>Then, I talk about a couple of editor settings that help keep me sane.</p> <div class=section id=code-completion> <h2><a class=toc-backref href=#id6>Code Completion</a></h2> <p>PyCharm's editor is very powerful and extensible. In this section we will go over some useful tools that I use to write less error prone code faster.</p> <div class=section id=smart-completion> <h3><a class=toc-backref href=#id7>Smart Completion</a></h3> <p>You get suggestions as you type. You can further enhance this by the following:</p> <ul class=simple> <li>Collecting runtime information</li> <li>Adding docstrings to functions and methods</li> <li>Invoking <code>assert</code> statements</li> </ul> <div class=section id=runtime-information> <h4><a class=toc-backref href=#id8>Runtime Information</a></h4> <p>Lets start with the easiest enhancement. All we need to do is check this box, and PyCharm will give you better code completion as you debug your program more:</p> <img alt="Runtime info" class=align-center src=/images/charm_good_parts_07.png> <p>If you ever get wrong suggestions for completion clear the caches. Running your tests in debug mode will also give PyCharm a better chance to get an understanding of the types that you're using.</p> </div> <div class=section id=docstrings> <h4><a class=toc-backref href=#id9>Docstrings</a></h4> <p>Writing docstrings helps PyCharm, but also helps developers gain a better understanding of the code that you have written. PyCharm <em>understands</em> docstrings and uses the information in them to give you code completion and give you warning signals if it thinks you're being inconsistent:</p> <div class=highlight><pre><span></span><span class=k>def</span> <span class=nf>foo</span><span class=p>(</span><span class=n>a</span><span class=p>,</span> <span class=n>b</span><span class=p>,</span> <span class=n>c</span><span class=p>):</span>
<span class=sd>"""</span>
<span class=sd> :param a: A for apple</span>
<span class=sd> :type a: str</span>
<span class=sd> :param b: B for ball</span>
<span class=sd> :type b: str</span>
<span class=sd> :param c: C for cat</span>
<span class=sd> :type c: str</span>
<span class=sd> :return: The answer to everything is 42. Remember ye well!</span>
<span class=sd> :rtype: int</span>
<span class=sd> """</span>
<span class=k>return</span> <span class=mi>42</span>
<span class=k>if</span> <span class=vm>__name__</span> <span class=o>==</span> <span class=s1>'__main__'</span><span class=p>:</span>
<span class=n>foo</span><span class=p>(</span><span class=mi>1</span><span class=p>,</span> <span class=mi>2</span><span class=p>,</span> <span class=mi>3</span><span class=p>)</span>
</pre></div> <p>In the example above, I've used PyCharm tells me that I'm being inconsistent:</p> <img alt="PyCharm yells at me" class=align-center src=/images/charm_good_parts_10.png> <p>But there's more! If you wrote docstrings, then PyCharm will give you a beautiful documentation popup about your variables (by using <em>quick documentation</em>):<a class=footnote-reference href=#keymap id=id2>[2]</a></p> <img alt="Beautiful docs" class=align-center src=/images/charm_good_parts_11.png> </div> <div class=section id=assert-statements> <h4><a class=toc-backref href=#id10>Assert Statements</a></h4> <p><code>assert</code> statements can help tell PyCharm what types you should expect. Please note that, <code>assert</code> statements are not overhead free, unlike docstrings. I would advise using them in tests, so that the <a class="reference internal" href=#runtime-information>debugger</a> can collect type information about your parameters. For example, the following will give code completions for <code>a</code> but not for <code>b</code>:</p> <div class=highlight><pre><span></span><span class=k>def</span> <span class=nf>foo</span><span class=p>(</span><span class=n>a</span><span class=p>,</span> <span class=n>b</span><span class=p>,</span> <span class=n>c</span><span class=p>):</span>
<span class=k>assert</span> <span class=nb>isinstance</span><span class=p>(</span><span class=n>a</span><span class=p>,</span> <span class=nb>str</span><span class=p>)</span>
<span class=k>return</span> <span class=mi>42</span>
</pre></div> <p>This is because PyCharm knows about <code>a</code>, and that its a string, but it doesn't know about <code>b</code>:</p> <img alt="Code completion for b" class=align-center src=/images/charm_good_parts_12.png> </div> </div> <div class=section id=hippie-complete> <h3><a class=toc-backref href=#id11>Hippie Complete</a></h3> <p>Hippie Complete or Cyclic completion is best explained with a video:</p> <div class="external youtube"><iframe width=640 height=360 src=//www.youtube.com/embed/jiaGFNnRL4o frameborder=0></iframe></div><p>Basically, it completes the word based on previous words you've typed <em>in the same document</em>.</p> </div> <div class=section id=spelling-correction> <h3><a class=toc-backref href=#id12>Spelling Correction</a></h3> <p>PyCharm also offers spelling correction as a quick fix. If you misspell "massachussets" like so:</p> <img alt=Spelling class=align-center src=/images/charm_good_parts_16.png> <p>Then you can try quick fixing it with <kbd class=light>⌥</kbd> + <kbd class=light>return</kbd>. After you hit <kbd class=light>return</kbd> on the first option above, you should see correction options:</p> <img alt="Correction options" class=align-center src=/images/charm_good_parts_17.png> <p><em>This was a request on reddit</em></p> </div> <div class=section id=language-injection> <h3><a class=toc-backref href=#id13>Language Injection</a></h3> <p>Language Inject basically means that you can <em>inject</em> other languages into Python strings, and have python <em>recognize</em> those languages. This comes in really handy when you're trying to syntax highlighting for regex or html, but it goes further than that often providing code completion. In order to get language injection, one must invoke the <em>quick fix</em> command inside string delimiters (<code>''</code>).<a class=footnote-reference href=#keymap id=id3>[2]</a></p> <p>Here is a video to demonstrate how language injection works:<a class=footnote-reference href=#emmet id=id4>[3]</a></p> <div class="external youtube"><iframe width=640 height=360 src=//www.youtube.com/embed/ZmBe0CCQZk0 frameborder=0></iframe></div></div> </div> <div class=section id=editor> <h2><a class=toc-backref href=#id14>Editor</a></h2> <p>This part covers the settings regarding the editor for better workflow. The options I set are based on my own preferences and hence not scientifically tested but they <em>do</em> make <em>my</em> editing a lot faster. Take the following advice with a healthy dose of skepticism since what might work for me, might not work for you.</p> <div class=section id=font> <h3><a class=toc-backref href=#id15>Font</a></h3> <img alt="Consolas FTW!" class=align-center src=/images/charm_good_parts_13.png> <p>Consolas is the only font that looks good on Windows, Mac and Linux. Consolas remains monospace regardless of whether it is bold or italic, and the only font that looks consistently good in PyCharm. I have tested this on Mac OSX Yosemite, Windows 8.1 and Ubuntu (14.04).<a class=footnote-reference href=#gettingconsonlinux id=id5>[4]</a></p> <p>If you've had success with other fonts (especially in Linux), please do mention them in the comments.</p> </div> <div class=section id=braces-and-quotes> <h3><a class=toc-backref href=#id16>Braces and Quotes</a></h3> <p>A lot of the time, I forget to put parentheses around things or quotes around strings. By enabling the following option, you can select text, and when you press <kbd class=light>"</kbd> or <kbd class=light>'</kbd> or <kbd class=light>(</kbd> or <kbd class=light>)</kbd>, instead of replacing the text with the character entered, it will surround the selection with quotes or parentheses:</p> <img alt=Parens! class=align-center src=/images/charm_good_parts_14.png> </div> <div class=section id=case-sensitive-completion> <h3><a class=toc-backref href=#id17>Case Sensitive Completion</a></h3> <p>By default, PyCharm comes with <em>first letter</em> case sensitivity, meaning that if you were to type <code>e</code>, <code>Exception</code> would not be offered as a viable completion. This is why I disable case sensitivity:</p> <img alt="Case Sensitivity" class=align-center src=/images/charm_good_parts_15.png> <p>Please note however, that on slower machines this does make code completion slower.</p> </div> </div> <div class=section id=up-next> <h2><a class=toc-backref href=#id18>Up Next</a></h2> <p>I will likely be covering how to deal with interpreters.</p> </div> <div class=section id=previously> <h2><a class=toc-backref href=#id19>Previously</a></h2> <p>I talked about a couple of things in <a class="reference external" href=http://nafiulis.me/pycharm-the-good-parts-i.html>part one</a>.</p> </div> <div class=section id=update> <h2><a class=toc-backref href=#id20>Update</a></h2> <p><em>2015-01-19 11:41</em> : Added a section on <a class="reference internal" href=#spelling-correction>Spelling Correction</a></p> <hr class=docutils> <table class="docutils footnote" frame=void id=webtools rules=none> <colgroup><col class=label><col></colgroup> <tbody valign=top> <tr><td class=label><a class=fn-backref href=#id1>[1]</a></td><td>I will cover javascript, html and other stuff in another section that deals with web tools. Don't worry I know that they are indispensable tools, and will make sure to give them the time they deserve.</td></tr> </tbody> </table> <table class="docutils footnote" frame=void id=keymap rules=none> <colgroup><col class=label><col></colgroup> <tbody valign=top> <tr><td class=label>[2]</td><td><em>(<a class=fn-backref href=#id2>1</a>, <a class=fn-backref href=#id3>2</a>)</em> Use the <a class="reference external" href=http://nafiulis.me/pycharm-the-good-parts-i.html#keymap>keymap</a> to find your corresponding keyboard shortcut discussed in the previous part.</td></tr> </tbody> </table> <table class="docutils footnote" frame=void id=emmet rules=none> <colgroup><col class=label><col></colgroup> <tbody valign=top> <tr><td class=label><a class=fn-backref href=#id4>[3]</a></td><td>Don't worry if you don't know what emmet does, I'll cover that when I cover web tools.</td></tr> </tbody> </table> <table class="docutils footnote" frame=void id=gettingconsonlinux rules=none> <colgroup><col class=label><col></colgroup> <tbody valign=top> <tr><td class=label><a class=fn-backref href=#id5>[4]</a></td><td><div class="first last line-block"> <div class=line>Take a look at this answer if you want to use consolas on Ubuntu:</div> <div class=line><a class="reference external" href=http://askubuntu.com/questions/269757/consolas-not-visible-in-intellij-idea-on-ubuntu-12-04>http://askubuntu.com/questions/269757/consolas-not-visible-in-intellij-idea-on-ubuntu-12-04</a></div> </div> </td></tr> </tbody> </table> </div> </div> </div> <div class=post-meta><span class=meta-type>Category: </span> <span><a href=http://nafiulis.me/category/programming.html>Programming</a></span> <span class=meta-type> Tags: </span> <span> <a href=http://nafiulis.me/tag/pycharm.html>pycharm</a>, <a href=http://nafiulis.me/tag/python.html>python</a>, <a href=http://nafiulis.me/tag/ide.html>ide</a>, <a href=http://nafiulis.me/tag/series.html>series</a>, <a href=http://nafiulis.me/tag/pycharm-the-good-parts.html>pycharm-the-good-parts</a> </span> </div> <div id=disqus_thread style="margin-top: 10px; margin-left: 20px; margin-right: 20px;"></div> <script type=text/javascript>
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'nafiulisme'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script> <noscript>Please enable JavaScript to view the <a href=http://disqus.com/?ref_noscript>comments powered by Disqus.</a></noscript> </div> <!--Footer--> <div id=footer> <footer> Code examples licenced under MIT License <br> Copyright <i class="fa fa-copyright"></i> 2018 Quazi Nafiul Islam </footer> </div> </div> <script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55554110-1', 'auto');
ga('send', 'pageview');
</script> </body> </html>