<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Code Formatting |</title><link>https://www.isarvin.com/tags/code-formatting/</link><atom:link href="https://www.isarvin.com/tags/code-formatting/index.xml" rel="self" type="application/rss+xml"/><description>Code Formatting</description><generator>HugoBlox Kit (https://hugoblox.com)</generator><language>en-gb</language><lastBuildDate>Sun, 09 Feb 2025 00:00:00 +0000</lastBuildDate><image><url>https://www.isarvin.com/media/sharing.png</url><title>Code Formatting</title><link>https://www.isarvin.com/tags/code-formatting/</link></image><item><title>A Starter for Python 2 Code Formatting with Black</title><link>https://www.isarvin.com/blog/black-for-py2.7/</link><pubDate>Sun, 09 Feb 2025 00:00:00 +0000</pubDate><guid>https://www.isarvin.com/blog/black-for-py2.7/</guid><description>
&lt;div class="callout flex px-4 py-3 mb-6 rounded-md border-l-4 bg-red-100 dark:bg-red-900 border-red-500"
data-callout="caution"
data-callout-metadata=""&gt;
&lt;span class="callout-icon pr-3 pt-1 text-red-600 dark:text-red-300"&gt;
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0zM12 15.75h.007v.008H12z"/&gt;&lt;/svg&gt;
&lt;/span&gt;
&lt;div class="callout-content dark:text-neutral-300"&gt;
&lt;div class="callout-title font-semibold mb-1"&gt;Caution&lt;/div&gt;
&lt;div class="callout-body"&gt;&lt;p&gt;Python 2.7 is the last major version in the 2.x series and
. Please upgrade your Python if possible.&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Some of the projects I work on still use Python 2.7. Installing Black for that case can take a little fiddling, so I&amp;rsquo;d like to show the steps that worked for me.&lt;/p&gt;
&lt;div class="callout flex px-4 py-3 mb-6 rounded-md border-l-4 bg-blue-100 dark:bg-blue-900 border-blue-500"
data-callout="note"
data-callout-metadata=""&gt;
&lt;span class="callout-icon pr-3 pt-1 text-blue-600 dark:text-blue-300"&gt;
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m16.862 4.487l1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8l.8-2.685a4.5 4.5 0 0 1 1.13-1.897zm0 0L19.5 7.125"/&gt;&lt;/svg&gt;
&lt;/span&gt;
&lt;div class="callout-content dark:text-neutral-300"&gt;
&lt;div class="callout-title font-semibold mb-1"&gt;Note&lt;/div&gt;
&lt;div class="callout-body"&gt;&lt;ol&gt;
&lt;li&gt;
, even for formatting Python 2 code.&lt;/li&gt;
&lt;li&gt;The following assumes you have admin/root privileges and use &lt;em&gt;pip&lt;/em&gt; as your package installer. Adjustments may be required to match your environment.&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;details class="print:hidden xl:hidden" open&gt;
&lt;summary&gt;Table of Contents&lt;/summary&gt;
&lt;div class="text-sm"&gt;
&lt;nav id="TableOfContents"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#prerequisites-python-36-with-a-package-installer"&gt;Prerequisites: Python 3.6+ with a package installer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#install-a-specific-black-version"&gt;Install a specific Black version&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#verify-installation"&gt;Verify installation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;/details&gt;
&lt;div class="hb-steps"&gt;
&lt;h3 id="prerequisites-python-36-with-a-package-installer"&gt;Prerequisites: Python 3.6+ with a package installer&lt;/h3&gt;
&lt;p&gt;I am not going to cover how to install Python 3 or set up a package installer such as &lt;em&gt;pip&lt;/em&gt;. You may need to search the web if your environment does not meet the prerequisites.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;command&lt;/span&gt; -v python3
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;python3 -m pip -V
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="install-a-specific-black-version"&gt;Install a specific Black version&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;python3 -m pip install &lt;span class="s1"&gt;&amp;#39;black[python2]==21.12b0&amp;#39;&lt;/span&gt; &lt;span class="nv"&gt;click&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;8.0.4
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;V21.12b0 is the last Black version with support for formatting Python 2 code. As defined in
, &lt;code&gt;click &amp;gt;= 7.1.2&lt;/code&gt; is a must.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;click &amp;lt;= 8.0.4&lt;/code&gt; is also required. Otherwise, you may encounter an error saying &lt;code&gt;ImportError: cannot import name '_unicodefun' from 'click'&lt;/code&gt; when running Black.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;python2&lt;/code&gt; extra for Black installs the optional dependency &lt;em&gt;typed-ast&lt;/em&gt;, which
. If there is no matching pre-built binary package for your environment, building it can fail with a Clang or GCC error. My suggestion is to remove &lt;code&gt;[python2]&lt;/code&gt; as shown below. It generally works fine.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;python3 -m pip install &lt;span class="nv"&gt;black&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;21.12b0 &lt;span class="nv"&gt;click&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;8.0.4
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="verify-installation"&gt;Verify installation&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;python3 -m black --version
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description></item></channel></rss>