-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPep8.html
More file actions
428 lines (407 loc) · 29.6 KB
/
Copy pathPep8.html
File metadata and controls
428 lines (407 loc) · 29.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Coding Style and Linting — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Code Reviews" href="CodeReviews.html" />
<link rel="prev" title="19. Extra Topics" href="../topics/99-extras/index.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Coding Style and Linting</a></li>
<li class="toctree-l2"><a class="reference internal" href="CodeReviews.html">Code Reviews</a></li>
<li class="toctree-l2"><a class="reference internal" href="PersistanceAndSerialization.html">Persistence and Serialization</a></li>
<li class="toctree-l2"><a class="reference internal" href="Unicode.html">Unicode in Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="IteratorsAndGenerators.html">Iterators and Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="Decorators.html">Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-decorator.html">Mailroom – Decoratoring it</a></li>
<li class="toctree-l2"><a class="reference internal" href="ContextManagers.html">Context Managers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/context-managers-exercise.html">A Couple Handy Context Managers</a></li>
<li class="toctree-l2"><a class="reference internal" href="MetaProgramming.html">Metaprogramming</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-meta.html">Mailroom – metaprogramming it!</a></li>
<li class="toctree-l2"><a class="reference internal" href="Logging.html">Logging and the logging module</a></li>
<li class="toctree-l2"><a class="reference internal" href="Debugging.html">Debugging</a></li>
<li class="toctree-l2"><a class="reference internal" href="NoSQL.html">No SQL Databases</a></li>
<li class="toctree-l2"><a class="reference internal" href="GraphDatabases.html">Graph Databases</a></li>
<li class="toctree-l2"><a class="reference internal" href="Concurrency.html">Concurrent Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="Async.html">Asychronous Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="Coroutines.html">Notes on Coroutines</a></li>
<li class="toctree-l2"><a class="reference internal" href="ThreadingMultiprocessing.html">Threading and multiprocessing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/threaded_downloader.html">Threaded Web Scraper</a></li>
<li class="toctree-l2"><a class="reference internal" href="Profiling.html">Performance and Profiling</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/99-extras/index.html"><span class="section-number">19. </span>Extra Topics</a> »</li>
<li>Coding Style and Linting</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/Pep8.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="../topics/99-extras/index.html" class="btn btn-neutral float-left" title="19. Extra Topics" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="CodeReviews.html" class="btn btn-neutral float-right" title="Code Reviews" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="coding-style-and-linting">
<span id="pep8"></span><h1>Coding Style and Linting<a class="headerlink" href="#coding-style-and-linting" title="Permalink to this headline"></a></h1>
<p><strong>Readablity Counts</strong></p>
<p>It really is worth it to have consistently styled code!</p>
<div class="section" id="pep-8">
<h2>pep-8<a class="headerlink" href="#pep-8" title="Permalink to this headline"></a></h2>
<p>Style Guide for Python</p>
<p>You should all be familiar with this by now – but we’re going to hammer it home anyway!</p>
<p>Defines a good baseline for your own local style guide</p>
<p>The ‘rules’ are just suggestions. From the document:</p>
<blockquote>
<div><p><em>Most importantly: know when to be inconsistent – sometimes the
style guide just doesn’t apply</em></p>
</div></blockquote>
<p>and:</p>
<blockquote>
<div><p><em>A Foolish Consistency is the Hobgoblin of Little Minds</em></p>
</div></blockquote>
<div class="section" id="important-pep8-recommendations">
<h3>Important pep8 Recommendations<a class="headerlink" href="#important-pep8-recommendations" title="Permalink to this headline"></a></h3>
<div class="section" id="tabs-or-spaces">
<h4>Tabs or Spaces<a class="headerlink" href="#tabs-or-spaces" title="Permalink to this headline"></a></h4>
<p>This one is pretty much non-negotiable:</p>
<ul class="simple">
<li><p><a class="reference external" href="https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces">https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces</a></p></li>
<li><p>Use 4 space indents. The tabs vs. spaces wars will likely never
completely come to peaceful conclusion. But the Python community has
standardized on 4 space indents. Unless you have compelling reasons
not to, you should too.</p></li>
<li><p>Python 3 disallows mixing of tabs and spaces. With Python 2, you can
run with the -tt flag which will force errors on mixed use of tabs
and spaces.</p></li>
<li><p>Let your editor help you by having it insert 4 spaces for the tab
key, turning on visible whitespace markers, and/or integrating a
PEP-8 plugin</p></li>
<li><p>If your editor doesn’t do this right – <strong>Fix It</strong></p></li>
</ul>
</div>
<div class="section" id="line-length">
<h4>Line Length<a class="headerlink" href="#line-length" title="Permalink to this headline"></a></h4>
<ul class="simple">
<li><p><a class="reference external" href="https://www.python.org/dev/peps/pep-0008/#maximum-line-length">https://www.python.org/dev/peps/pep-0008/#maximum-line-length</a></p></li>
<li><p>Maximum line length of 79 characters</p></li>
<li><p>This is an easy one to let run away early. If 79 characters is too
short, find a line length your team can agree on and stick to it as
specified in PEP-8 – I like 95 characters or so.</p></li>
</ul>
</div>
<div class="section" id="encoding">
<h4>Encoding<a class="headerlink" href="#encoding" title="Permalink to this headline"></a></h4>
<ul>
<li><p><a class="reference external" href="https://www.python.org/dev/peps/pep-0008/#source-file-encoding">https://www.python.org/dev/peps/pep-0008/#source-file-encoding</a></p></li>
<li><p>The default encoding in Python 2 is ASCII, in Python 3 it is UTF-8.
UTF-8 has become ubiquitous enough that you probably haven’t noticed.
But do check your editor at some point – or if something weird happens.</p></li>
<li><p>If you insert a non encodable character in a string literal, the
interpreter will throw a SyntaxError when it reaches it.</p></li>
<li><p>For py2, if you want to use non-ascii encoding, UTF-8 for example, insert</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># coding: utf-8</span>
</pre></div>
</div>
<p>near the top of the file.</p>
</li>
</ul>
</div>
<div class="section" id="comparing-to-singletons">
<h4>Comparing to Singletons<a class="headerlink" href="#comparing-to-singletons" title="Permalink to this headline"></a></h4>
<ul class="simple">
<li><p>When comparing with singletons such as None, use <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">None</span></code>, not <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">None</span></code>. <a class="reference external" href="http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html">Why?</a> ( also <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">False</span></code>)</p></li>
</ul>
</div>
</div>
<div class="section" id="naming-conventions">
<h3>Naming Conventions<a class="headerlink" href="#naming-conventions" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>variables, attributes, and modules names should be lowercase, with
words separated by underscores</p></li>
<li><p>class names should be CamelCase, aka StudlyCaps</p></li>
<li><p>constants should be ALL CAPS</p></li>
</ul>
</div>
<div class="section" id="argument-conventions">
<h3>Argument Conventions<a class="headerlink" href="#argument-conventions" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>Instance methods should have a first argument called self</p></li>
<li><p>Class methods should have a first argument called cls</p></li>
<li><p>There’s nothing magic about these names. You don’t have to use this
convention, but not adopting it will likely confuse future readers</p></li>
</ul>
</div>
<div class="section" id="imports">
<h3>Imports<a class="headerlink" href="#imports" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://www.python.org/dev/peps/pep-0008/#imports">imports</a>
should be near the top of the file</p></li>
<li><p>imports should be grouped in the following order, separated by a
blank line:</p>
<ol class="arabic simple">
<li><p>standard library imports</p></li>
<li><p>related third party imports</p></li>
<li><p>local application/library specific imports</p></li>
</ol>
</li>
<li><p>avoid wildcard imports (<code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">*</span></code>) to keep the namespace clean for
both humans and automated tools</p></li>
</ul>
</div>
<div class="section" id="public-and-non-public-class-members">
<h3>Public and Non-public Class Members<a class="headerlink" href="#public-and-non-public-class-members" title="Permalink to this headline"></a></h3>
<p>Python does not have a mechanism for restricting access to a variable or method, but it does have culture and convention.</p>
<p>The default is public. This works for most things.</p>
<p>If you do not want people to change your attribute or method, and especially if you do not want people to depend on this attribute or method always remaining the same, make it private by using a single underscore in front.</p>
<p><code class="docutils literal notranslate"><span class="pre">_my_private_method</span></code></p>
<p>If you are particularly paranoid: A non-public attribute has two leading underscores and no trailing underscores and triggers Python’s name mangling.</p>
<p><code class="docutils literal notranslate"><span class="pre">__my_paranoid_method</span></code></p>
</div>
<div class="section" id="continuing-long-lines">
<h3>Continuing long lines<a class="headerlink" href="#continuing-long-lines" title="Permalink to this headline"></a></h3>
<p>PEP-8 is pretty flexible about how to continue long lines:</p>
<p><a class="reference external" href="https://www.python.org/dev/peps/pep-0008/#indentation">https://www.python.org/dev/peps/pep-0008/#indentation</a></p>
<p>But I’m kind of opinionated, so:</p>
<div class="section" id="lots-of-arguments-or-parameters">
<h4>Lots of Arguments or Parameters:<a class="headerlink" href="#lots-of-arguments-or-parameters" title="Permalink to this headline"></a></h4>
<p>If your function defintion or call can fit on one line, great:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="n">some_code</span>
</pre></div>
</div>
<p>but if not:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">,</span> <span class="n">arg4</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">kwargument</span><span class="o">=</span><span class="s2">"a_default"</span><span class="p">,</span> <span class="n">kwargument2</span><span class="o">=</span><span class="s2">"some other value"</span><span class="p">,</span> <span class="n">yet_another</span><span class="o">=</span><span class="n">something</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">some_code</span>
</pre></div>
</div>
<p>Then you need to break it. You can break it after a few arguments, when you run out of space, but I find that very hard to read. So – if they don’t all fit on one line, put them each on their own line:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span>
<span class="n">arg2</span><span class="p">,</span>
<span class="n">arg3</span><span class="p">,</span>
<span class="n">arg4</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">kwargument</span><span class="o">=</span><span class="s2">"a_default"</span><span class="p">,</span>
<span class="n">kwargument2</span><span class="o">=</span><span class="s2">"some other value"</span><span class="p">,</span>
<span class="n">yet_another</span><span class="o">=</span><span class="n">something</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span>
<span class="p">):</span>
<span class="n">some_code</span>
</pre></div>
</div>
<p>Isn’t that easier to read?</p>
</div>
</div>
<div class="section" id="tools-to-help">
<h3>Tools to help<a class="headerlink" href="#tools-to-help" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://pypi.python.org/pypi/pyflakes">pyflakes</a> - searches for
bugs, but without importing modules</p></li>
<li><p><a class="reference external" href="http://www.pylint.org/">Pylint</a> - style guide, searches for bugs</p></li>
<li><p><a class="reference external" href="https://pypi.python.org/pypi/pycodestyle">pycodestyle</a> - tests conformance to
PEP-8</p></li>
<li><p><a class="reference external" href="https://pypi.python.org/pypi/flake8">flake8</a> combines pyflakes,
pycodestyle, and mccabe, a code complexity analyzer</p></li>
</ul>
</div>
<div class="section" id="pylint">
<h3>pylint<a class="headerlink" href="#pylint" title="Permalink to this headline"></a></h3>
<p>Interesting options:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">-</span><span class="n">d</span> <span class="p">(</span><span class="n">msg</span> <span class="n">ids</span><span class="p">),</span> <span class="o">--</span><span class="n">disable</span><span class="o">=</span><span class="p">(</span><span class="n">msg</span> <span class="n">ids</span><span class="p">)</span> <span class="n">Disable</span> <span class="n">the</span> <span class="n">messages</span> <span class="n">identified</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">messages</span> <span class="n">table</span>
<span class="o">--</span><span class="n">generate</span><span class="o">-</span><span class="n">rcfile</span><span class="o">/--</span><span class="n">rcfile</span> <span class="n">Saves</span><span class="o">/</span><span class="n">restores</span> <span class="n">a</span> <span class="n">configuration</span>
</pre></div>
</div>
<p>Poor code example:</p>
<p><a class="reference download internal" download="" href="../_downloads/9eea9f00dc947ac0352b6f3ce01753df/listing1.py"><code class="xref download docutils literal notranslate"><span class="pre">listing1.py</span></code></a></p>
<p>was adapted from <a class="reference external" href="http://doughellmann.com/2008/03/01/static-code-analizers-for-python.html">Doug Hellman</a></p>
<p>What can you spot as an error, bad practice, or poor style?</p>
<p>Now see what pylint listing1.py has to say:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install pylint
$ pylint listing1.py
</pre></div>
</div>
</div>
<div class="section" id="pyflakes">
<h3>pyflakes<a class="headerlink" href="#pyflakes" title="Permalink to this headline"></a></h3>
<p>Doesn’t check style, just checks for functional errors, but does not run code.</p>
<p>Now see what pyflakes listing1.py has to say</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install pyflakes
$ pyflakes listing1.py
</pre></div>
</div>
<p>How much overlap with pylint?</p>
</div>
<div class="section" id="pycodestyle">
<h3>pycodestyle<a class="headerlink" href="#pycodestyle" title="Permalink to this headline"></a></h3>
<p>Used to be called “pep8” – but Guido didn’t like that it gave a tool apparent authority – so they changed it.</p>
<p>Only checks style</p>
<p>Interesting options:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">--</span><span class="n">statistics</span> <span class="n">count</span> <span class="n">errors</span> <span class="ow">and</span> <span class="n">warnings</span>
<span class="o">--</span><span class="n">count</span> <span class="nb">print</span> <span class="n">total</span> <span class="n">number</span> <span class="n">of</span> <span class="n">errors</span> <span class="ow">and</span> <span class="n">warnings</span> <span class="n">to</span> <span class="n">standard</span> <span class="n">error</span> <span class="ow">and</span> <span class="nb">set</span> <span class="n">exit</span> <span class="n">code</span> <span class="n">to</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">total</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">null</span>
</pre></div>
</div>
<p>Now see what pycodestyle listing1.py has to say</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install pycodestyle
$ pycodestyle listing1.py
</pre></div>
</div>
<p>What’s the overlap in pycodestyle’s output versus the other two tools?</p>
</div>
<div class="section" id="flake8">
<h3>flake8<a class="headerlink" href="#flake8" title="Permalink to this headline"></a></h3>
<p>A tool which wraps pycodestyle, pyflakes, and mccabe</p>
<p><a class="reference external" href="http://nedbatchelder.com/blog/200803/python_code_complexity_microtool.html">mccabe</a>
is a “microtool” written by Ned Batchelder (author of coverage) for
assessing
<a class="reference external" href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic Complexity</a></p>
<p>Interesting options:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">--</span><span class="nb">max</span><span class="o">-</span><span class="n">complexity</span><span class="o">=</span><span class="n">N</span> <span class="n">McCabe</span> <span class="n">complexity</span> <span class="n">threshold</span>
</pre></div>
</div>
<p>Now see what flake8 listing1.py has to say</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install pycodestyle
$ pycodestyle listing1.py
</pre></div>
</div>
<p>What’s the overlap in flake8 output versus the other tools?</p>
<p>Give them a try on your own code – mailroom?</p>
</div>
<div class="section" id="skipping-particular-lines">
<h3>skipping particular lines<a class="headerlink" href="#skipping-particular-lines" title="Permalink to this headline"></a></h3>
<p>Each of the tools has a way to mark particular lines to be ignored.</p>
<p>For instance, flake8 has the <code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">noqa</span></code> marker. It’s a comment as far as Python is concerned, but flake8 will skip that line if you mark it that way:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">functionName</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="n">local</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">+</span> <span class="mi">5</span> <span class="c1"># noqa</span>
<span class="n">module_variable</span> <span class="o">=</span> <span class="mi">5</span><span class="o">*</span><span class="mi">5</span>
<span class="k">return</span> <span class="n">module_variable</span>
</pre></div>
</div>
<p>This can be very nice to make the linter in your editor stop bugging you, and even nicer if you have an automated linter running – like on a CI system.</p>
</div>
<div class="section" id="analyzing-a-large-codebase-in-the-wild">
<h3>Analyzing a large codebase in the wild<a class="headerlink" href="#analyzing-a-large-codebase-in-the-wild" title="Permalink to this headline"></a></h3>
<p>It can be instructive to see what happens if you run these tools on a large established code base…</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install django
<span class="nb">cd</span> /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
flake8 django
pylint django
</pre></div>
</div>
</div>
<div class="section" id="code-analysis-tool-battle-royale">
<h3>Code Analysis Tool Battle Royale<a class="headerlink" href="#code-analysis-tool-battle-royale" title="Permalink to this headline"></a></h3>
<p>Try this!</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pylint flake8
$ flake8 pylint
</pre></div>
</div>
</div>
<div class="section" id="analysis-tool-summary">
<h3>Analysis Tool Summary<a class="headerlink" href="#analysis-tool-summary" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>There is no magic bullet that guarantees functional, beautiful code</p></li>
<li><p>Some classes of programming errors can be found before runtime</p></li>
<li><p>With the PEP-8 tools, it is easy to let rules such as line length
slip by</p></li>
<li><p>It’s up to you to determine your thresholds</p></li>
</ul>
</div>
<div class="section" id="conclusion">
<h3>Conclusion:<a class="headerlink" href="#conclusion" title="Permalink to this headline"></a></h3>
<p>Personally, I use flake8 – it gets most of it for me. Though a run with pylint isn’t a bad idea once in a while….</p>
<p>Also – if you set up your editor with a linter – you’ll be encouraged to fix it a bit at a time as you write – much better way to go.</p>
</div>
</div>
<div class="section" id="pythonic-style">
<h2>Pythonic Style<a class="headerlink" href="#pythonic-style" title="Permalink to this headline"></a></h2>
<p>Good “Pythonic” style goes beyond style guides and things linters can figure out for you.</p>
<p>The <a class="reference external" href="http://docs.python-guide.org/en/latest/writing/style/">Hitchhiker’s Guide to Python: Code Style</a> is a good read that gets into a nice level of detail.</p>
</div>
</div>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="../topics/99-extras/index.html" class="btn btn-neutral float-left" title="19. Extra Topics" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="CodeReviews.html" class="btn btn-neutral float-right" title="Code Reviews" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>