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



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016