<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Python on My Universe</title><link>https://www.my-universe.com/tags/python/</link><description>Recent content in Python on My Universe</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Tue, 31 Oct 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://www.my-universe.com/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Elegant Python Exceptions</title><link>https://www.my-universe.com/2023/10/31/elegant-python-exceptions/</link><pubDate>Tue, 31 Oct 2023 00:00:00 +0000</pubDate><guid>https://www.my-universe.com/2023/10/31/elegant-python-exceptions/</guid><description>&lt;p>Particularly when creating library packages in &lt;a href="https://www.python.org/">Python&lt;/a>, raising exceptions is a great way to let the downstream developer know about problems occuring while executing code from within the library. Python&amp;rsquo;s &lt;a href="https://docs.python.org/3/library/exceptions.html">built-in exceptions&lt;/a> cover a whole host of cases. However, some problems might be library-specific and deserve a custom exception.&lt;/p>
&lt;h4 id="custom-exceptions">Custom Exceptions&lt;/h4>
&lt;p>Creating a &lt;a href="https://docs.python.org/3/tutorial/errors.html#user-defined-exceptions">custom exception&lt;/a> in Python isn&amp;rsquo;t particularly hard when observing a few rules:&lt;/p>
&lt;ul>
&lt;li>Custom exceptions shall be derived from Python&amp;rsquo;s &lt;a href="https://docs.python.org/3/library/exceptions.html#Exception">Exception&lt;/a> class&lt;/li>
&lt;li>By convention, exception names shall end with &amp;ldquo;Error&amp;rdquo;&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#ff79c6">class&lt;/span> &lt;span style="color:#50fa7b">MyLibError&lt;/span>(Exception):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> &lt;span style="color:#ff79c6">pass&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In many library packages, you will find exceptions similar to the one shown in the code example above. There&amp;rsquo;s nothing wrong with that; if chosen wisely, the exception name by itself will already tell what went wrong. However, the more details an exception provides about the circumstances it was raised in, the easier to diagnose and debug the problem, and the more appropriate the reaction to an exception.&lt;/p></description></item><item><title>Colours in Django Models</title><link>https://www.my-universe.com/2017/11/08/colours-in-django-models/</link><pubDate>Wed, 08 Nov 2017 00:00:00 +0000</pubDate><guid>https://www.my-universe.com/2017/11/08/colours-in-django-models/</guid><description>&lt;p>Colours are quite a common property to real world objects. So naturally when building web applications, sooner or later one encounters the need to assign a colour attribute to an object. For &lt;a href="https://djangoproject.com">Django&lt;/a> developers, this usually means adding a &lt;code>models.CharField&lt;/code> to their model, ready to capture the colour&amp;rsquo;s &lt;a href="https://en.wikipedia.org/wiki/Web_colors#Hex_triplet">hex code&lt;/a>.&lt;/p>
&lt;p>Technically this works pretty well, as those hex codes can directly be used in HTML style attributes, embedded SVG drawings, etc. However, setting colour values via text input widget is quite tedious. On the frontend side, various libraries offer quite elaborate solutions for integrating nice colour picking widgets. For the Django admin, there is however a quite simple solution: use HTML 5 color inputs!&lt;/p></description></item><item><title>Pythonic Distance Conversion</title><link>https://www.my-universe.com/2017/05/28/pythonic-distance-conversion/</link><pubDate>Sun, 28 May 2017 00:00:00 +0000</pubDate><guid>https://www.my-universe.com/2017/05/28/pythonic-distance-conversion/</guid><description>&lt;p>When dealing with distances or lengths, it&amp;rsquo;s a common problem to convert values between all the different units
available out there. Of course, converting itself is a less than complicated simple floating point division or
multiplication, depending on how values and conversion factors are stored internally. However, maintaining conversion
factors all across a project is a tedious task, and Python has some good means at hand to simplify our life.&lt;/p></description></item></channel></rss>