Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Make your Python code look good with Black on Fedora

#1
Make your Python code look good with Black on Fedora

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora.png" width="72" height="72" title="" alt="" /></div><div><p>The Python programing language is often praised for its simple syntax. In fact the language recognizes that code is read much more often than it is written. <a href="http://github.com/psf/black">Black</a> is a tool that automatically formats your Python source code making it uniform and compliant to the <a href="https://www.python.org/dev/peps/pep-0008/">PEP-8 style guide</a>.</p>
<p> <span id="more-29445"></span> </p>
<h2>How to install Black on Fedora</h2>
<p>Installing Black on Fedora is quite simple. Black is maintained in the official repositories.</p>
<pre class="wp-block-preformatted">$ sudo dnf install python3-black</pre>
<p>Black is a command line tool and therefore it is run from the terminal.</p>
<pre class="wp-block-preformatted">$ black --help</pre>
<h2>Format your Python code with Black</h2>
<p>Using Black to format a Python code base is straight forward.</p>
<pre class="wp-block-preformatted">$ black myfile.py
All done! <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora.png" alt="✨" class="wp-smiley" style="height: 1em;max-height: 1em" /> <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora-1.png" alt="?" class="wp-smiley" style="height: 1em;max-height: 1em" /> <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora.png" alt="✨" class="wp-smiley" style="height: 1em;max-height: 1em" /> 1 file left unchanged.
$ black path_to_my_python_project/
All done! <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora.png" alt="✨" class="wp-smiley" style="height: 1em;max-height: 1em" /> <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora-1.png" alt="?" class="wp-smiley" style="height: 1em;max-height: 1em" /> <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora.png" alt="✨" class="wp-smiley" style="height: 1em;max-height: 1em" />
165 files reformatted, 24 files left unchanged.</pre>
<p>By default Black allows 88 characters per line, meaning that the code will be reformatted to fit within 88 characters per line. It is possible to change this to a custom value, for example :</p>
<pre class="wp-block-preformatted">$ black --line-length 100 my_python_file.py</pre>
<p>This will set the line length to allow 100 characters.</p>
<h2>Run Black as part of a CI pipeline</h2>
<p>Black really shines when it is integrated with other tools, like a continuous integration pipeline.</p>
<p>The <em>–check</em> option allows to verify if any files need to be reformatted. This is useful to run as a CI test to ensure all your code is formatted in consistent manner.</p>
<pre class="wp-block-preformatted">$ black --check myfile.py
would reformat myfile.py
All done! <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora-2.png" alt="?" class="wp-smiley" style="height: 1em;max-height: 1em" /> <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora-3.png" alt="?" class="wp-smiley" style="height: 1em;max-height: 1em" /> <img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/10/make-your-python-code-look-good-with-black-on-fedora-2.png" alt="?" class="wp-smiley" style="height: 1em;max-height: 1em" />
1 file would be reformatted.</pre>
<h2>Integrate Black with your code editor</h2>
<p>Running Black during the continuous integration tests is a great way to keep the code base correctly formatted. But developers really wants to forget about formatting and have the tool managing it for them. </p>
<p>Most of the popular code editors support Black. It allows developers to run the format tool every time a file is saved. The <a href="https://black.readthedocs.io/en/stable/editor_integration.html">official documentation</a> details the configuration needed for each editor. </p>
<p>Black is a must-have tool in the Python developer toolbox and is easily available on Fedora.</p>
</div>


https://www.sickgaming.net/blog/2019/10/...on-fedora/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Contribute to Fedora 39 Upgrade, Virtualization, and Cloud Test Day xSicKxBot 0 2,031 09-30-2023, 03:56 AM
Last Post: xSicKxBot
  Fedora - Share your game achievements with Gamerzilla xSicKxBot 0 1,002 09-27-2023, 09:59 AM
Last Post: xSicKxBot
  Fedora - Using Cockpit to graphically manage systems, without installing Cockpit on xSicKxBot 0 895 09-26-2023, 06:41 AM
Last Post: xSicKxBot
  Fedora - Announcing Fedora Linux 39 Beta xSicKxBot 0 917 09-20-2023, 09:48 AM
Last Post: xSicKxBot
  Fedora - Contribute at Passkey Auth, Fedora CoreOS and IoT Test Week xSicKxBot 0 890 09-19-2023, 12:23 PM
Last Post: xSicKxBot
  Fedora - Quick Fedora shirt update and sale of last stock with the old logo xSicKxBot 0 942 09-16-2023, 12:28 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora Linux Test Week for Kernel 6.5 and Toolbx Test Day xSicKxBot 0 950 09-11-2023, 02:47 PM
Last Post: xSicKxBot
  Fedora - Fedora Linux Flatpak cool apps to try for September xSicKxBot 0 931 09-10-2023, 04:59 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Test Week for the Anaconda WebUI Installer for Fedora Wor xSicKxBot 0 923 09-09-2023, 11:54 PM
Last Post: xSicKxBot
  Fedora - Docs workshop: Virtually writing together xSicKxBot 0 967 09-09-2023, 05:08 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016