<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Coding Standards on Wanzi&#39;s Blog – Notes on life, tech, and DevOps.</title>
    <link>https://wanzi.dev/tags/coding-standards/</link>
    <description>Recent content in Coding Standards on Wanzi&#39;s Blog – Notes on life, tech, and DevOps.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>wzdushu@gmail.com (wanzi)</managingEditor>
    <webMaster>wzdushu@gmail.com (wanzi)</webMaster>
    <copyright>丸子有记</copyright>
    <lastBuildDate>Sat, 20 Jan 2018 10:22:42 +0800</lastBuildDate>
    
        <atom:link href="https://wanzi.dev/tags/coding-standards/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Python Coding Standards</title>
      <link>https://wanzi.dev/post/devops-python-coding-standards/</link>
      <pubDate>Sat, 20 Jan 2018 10:22:42 +0800</pubDate>
      <author>wzdushu@gmail.com (wanzi)</author>
      <guid>https://wanzi.dev/post/devops-python-coding-standards/</guid>
      
        <description>&lt;h1 id=&#34;encoding-declaration&#34;&gt;Encoding Declaration&lt;/h1&gt;
&lt;p&gt;Default encoding is not ASCII. You can specify it using:&lt;/p&gt;
&lt;div class=&#34;highlight-container&#34;&gt;

  &lt;button class=&#34;copy-code-btn outline&#34;&gt;Copy&lt;/button&gt;

  
  &lt;div class=&#34;highlight&#34;&gt;&lt;div style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;
&lt;table style=&#34;border-spacing:0;padding:0;margin:0;border:0;&#34;&gt;&lt;tr&gt;&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-0-1&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-0-1&#34;&gt;1&lt;/a&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;;width:100%&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#60a0b0;font-style:italic&#34;&gt;# coding=&amp;lt;encoding name&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class=&#34;highlight-container&#34;&gt;

  &lt;button class=&#34;copy-code-btn outline&#34;&gt;Copy&lt;/button&gt;

  
  &lt;div class=&#34;highlight&#34;&gt;&lt;div style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;
&lt;table style=&#34;border-spacing:0;padding:0;margin:0;border:0;&#34;&gt;&lt;tr&gt;&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-1-1&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-1-1&#34;&gt;1&lt;/a&gt;
&lt;/span&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-1-2&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-1-2&#34;&gt;2&lt;/a&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;;width:100%&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#60a0b0;font-style:italic&#34;&gt;#!/usr/bin/python&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#60a0b0;font-style:italic&#34;&gt;# -*- coding: &amp;lt;encoding name&amp;gt; -*-&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class=&#34;highlight-container&#34;&gt;

  &lt;button class=&#34;copy-code-btn outline&#34;&gt;Copy&lt;/button&gt;

  
  &lt;div class=&#34;highlight&#34;&gt;&lt;div style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;
&lt;table style=&#34;border-spacing:0;padding:0;margin:0;border:0;&#34;&gt;&lt;tr&gt;&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-2-1&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-2-1&#34;&gt;1&lt;/a&gt;
&lt;/span&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-2-2&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-2-2&#34;&gt;2&lt;/a&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;;width:100%&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#60a0b0;font-style:italic&#34;&gt;#!/usr/bin/python&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#60a0b0;font-style:italic&#34;&gt;# coding: utf-8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h1 id=&#34;code-layout&#34;&gt;Code Layout&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Use four spaces for indentation; avoid using tabs, and never mix tabs with spaces.&lt;/li&gt;
&lt;li&gt;Limit line length to 79 characters. Use backslashes for line continuation, but prefer parentheses instead.&lt;/li&gt;
&lt;li&gt;Leave two blank lines between classes and top-level functions; leave one blank line between methods within a class.&lt;/li&gt;
&lt;li&gt;Module structure order: module description and docstring, &lt;code&gt;import&lt;/code&gt; statements, global variables, constants, others. Within &lt;code&gt;import&lt;/code&gt;, arrange in order: standard library, third-party, local modules. Avoid writing multiple imports on one line.&lt;/li&gt;
&lt;li&gt;Add one space on both sides of operators. Omit spaces around assignment operators in default function arguments. Do not add spaces before closing brackets (&lt;code&gt;]&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;, &lt;code&gt;)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Comments must be in English, preferably complete sentences. Start with a capital letter, end with a period (or other punctuation), followed by two spaces before the next sentence. For phrases, punctuation may be omitted.&lt;/li&gt;
&lt;li&gt;Always write docstrings for public modules, functions, classes, and methods. Non-public ones don&amp;rsquo;t require docstrings, but comments are acceptable (placed on the line immediately after &lt;code&gt;def&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;naming-conventions&#34;&gt;Naming Conventions&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Avoid using single lowercase letter &lt;code&gt;l&lt;/code&gt; or uppercase &lt;code&gt;O&lt;/code&gt; as they can easily be confused.&lt;/li&gt;
&lt;li&gt;Module names should be short and use all lowercase letters, optionally separated by underscores.&lt;/li&gt;
&lt;li&gt;Package names should be short and use all lowercase letters without underscores.&lt;/li&gt;
&lt;li&gt;Class names use &lt;code&gt;CapWords&lt;/code&gt;. Internal classes use &lt;code&gt;_CapWords&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Exception names use &lt;code&gt;CapWords&lt;/code&gt; with an &lt;code&gt;Error&lt;/code&gt; suffix.&lt;/li&gt;
&lt;li&gt;Global variables should ideally be limited to module scope (like &lt;code&gt;static&lt;/code&gt; in C). Two ways to achieve this: use &lt;code&gt;__all__&lt;/code&gt; mechanism or prefix with an underscore.&lt;/li&gt;
&lt;li&gt;Function names use all lowercase letters, optionally separated by underscores.&lt;/li&gt;
&lt;li&gt;Constant names use all uppercase letters, optionally separated by underscores.&lt;/li&gt;
&lt;li&gt;Class attributes (methods and variables) use all lowercase letters, optionally separated by underscores.&lt;/li&gt;
&lt;li&gt;Class attributes have three scopes: public, non-public, and subclass API—similar to &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt;, &lt;code&gt;protected&lt;/code&gt; in C++. Prefix non-public attributes with a single underscore.&lt;/li&gt;
&lt;li&gt;If a class attribute conflicts with a keyword, append an underscore. Avoid abbreviations or other workarounds.&lt;/li&gt;
&lt;li&gt;To prevent naming conflicts with subclasses, prefix certain class attributes with double underscores. For example, in class &lt;code&gt;Foo&lt;/code&gt;, define &lt;code&gt;__a&lt;/code&gt;; access via &lt;code&gt;Foo._Foo__a&lt;/code&gt; to avoid ambiguity. If a subclass also uses &lt;code&gt;Foo&lt;/code&gt;, this protection fails.&lt;/li&gt;
&lt;li&gt;The first parameter of instance methods must be &lt;code&gt;self&lt;/code&gt;. The first parameter of static methods must be &lt;code&gt;cls&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;code-checking-tools&#34;&gt;Code Checking Tools&lt;/h1&gt;
&lt;div class=&#34;highlight-container&#34;&gt;

  &lt;button class=&#34;copy-code-btn outline&#34;&gt;Copy&lt;/button&gt;

  
  &lt;div class=&#34;highlight&#34;&gt;&lt;div style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;
&lt;table style=&#34;border-spacing:0;padding:0;margin:0;border:0;&#34;&gt;&lt;tr&gt;&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-3-1&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-3-1&#34;&gt;1&lt;/a&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;;width:100%&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pip install flake8&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;After installing flake8 successfully, open VSCode, go to File → Preferences → Settings, and add &lt;code&gt;&amp;quot;python.linting.flake8Enabled&amp;quot;: true&lt;/code&gt; in &lt;code&gt;settings.json&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id=&#34;auto-formatting-tool&#34;&gt;Auto Formatting Tool&lt;/h1&gt;
&lt;div class=&#34;highlight-container&#34;&gt;

  &lt;button class=&#34;copy-code-btn outline&#34;&gt;Copy&lt;/button&gt;

  
  &lt;div class=&#34;highlight&#34;&gt;&lt;div style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;
&lt;table style=&#34;border-spacing:0;padding:0;margin:0;border:0;&#34;&gt;&lt;tr&gt;&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-4-1&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-4-1&#34;&gt;1&lt;/a&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;;width:100%&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pip install yapf&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;After installing yapf successfully, open VSCode, go to File → Preferences → Settings, and add &lt;code&gt;&amp;quot;python.formatting.provider&amp;quot;: &amp;quot;yapf&amp;quot;&lt;/code&gt; in &lt;code&gt;settings.json&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id=&#34;project-development-notes&#34;&gt;Project Development Notes:&lt;/h1&gt;
&lt;p&gt;When starting a new project, keep the following in mind:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;README.md&lt;/code&gt;: Write your project overview, quick start guide, etc. Although &lt;code&gt;distutils&lt;/code&gt; requires no extension, GitHub renders &lt;code&gt;.md&lt;/code&gt; files directly as HTML.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ChangeLog.txt&lt;/code&gt;: Store version change logs with a consistent format. Refer to &lt;code&gt;web.py&lt;/code&gt;&amp;rsquo;s &lt;code&gt;ChangeLog.txt&lt;/code&gt; for examples.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LICENSE.txt&lt;/code&gt;: Include the license used by your project. Do not create custom licenses.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;requirements.txt&lt;/code&gt;: List dependencies on separate lines. This allows &lt;code&gt;pip install&lt;/code&gt; to automatically install them.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setup.py&lt;/code&gt;: Installation script — detailed later.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;docs&lt;/code&gt;: Store project documentation such as high-level design, detailed design, maintenance documents, and auto-generated &lt;code&gt;pydoc&lt;/code&gt; content. Strongly recommend using Markdown format.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;src&lt;/code&gt;: Place main module code here. Avoid placing module directories directly in the root directory. Module paths can be specified in &lt;code&gt;setup.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tests&lt;/code&gt;: Store all unit tests and performance test scripts. Test files must start with &lt;code&gt;test_&lt;/code&gt; so that &lt;code&gt;distutils&lt;/code&gt; will automatically include them in packaging. Run tests using:&lt;br&gt;
&lt;code&gt;python -m unittest discover -s ./ -p &#39;test_*.py&#39; -v&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;References:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.python.org/dev/peps/pep-0008/&#34;&gt;https://www.python.org/dev/peps/pep-0008/&lt;/a&gt;
&lt;a href=&#34;https://wiki.woodpecker.org.cn/moin/PythonCodingRule&#34;&gt;https://wiki.woodpecker.org.cn/moin/PythonCodingRule&lt;/a&gt;&lt;/p&gt;
</description>
      
    </item>
    
  </channel>
</rss>
