{"id":9633,"date":"2022-09-20T19:05:00","date_gmt":"2022-09-20T17:05:00","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=9633"},"modified":"2022-09-20T09:08:06","modified_gmt":"2022-09-20T07:08:06","slug":"9633","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2022\/09\/20\/9633\/","title":{"rendered":""},"content":{"rendered":"\n<p>Another interesting article to &#8220;convert to Eiffel&#8221;&#8230;. <a href=\"https:\/\/martinheinz.dev\/blog\/77\">It&#8217;s Time to Say Goodbye to These Obsolete Python Libraries<\/a><\/p>\n\n\n<blockquote><header>\n<h1 class=\"posttitle\">It&#8217;s Time to Say Goodbye to These Obsolete Python Libraries<\/h1>\n<div class=\"meta\"><span class=\"author\">Martin<\/span>\n<div class=\"postdate\"><time datetime=\"2022-07-20T14:30:00Z\">Jul 20, 2022<\/time><\/div>\n<div class=\"article-tag\"><i class=\"icon-tag\"><\/i><a class=\"tag-link\" href=\"https:\/\/martinheinz.dev\/tag\/python\/\">Python<\/a><\/div>\n<\/div>\n<\/header>\n<div>\n<article>\n<div class=\"content\">\n<div>\n<p>With every Python release, there are new modules being added and new and better ways of doing things get introduced. We all get used to using the good old Python libraries and to certain way of doing things, but it&#8217;s time upgrade and make use of the new and improved modules and their features.<\/p>\n<h2>Pathlib<\/h2>\n<p><code class=\"\" data-line=\"\">pathlib<\/code> is definitely one of the bigger, recent additions to Python&#8217;s standard library. It&#8217;s been part of standard library since Python 3.4, yet a lot of people still use <code class=\"\" data-line=\"\">os<\/code> module for filesystem operations.<\/p>\n<p><code class=\"\" data-line=\"\">pathlib<\/code> has however many advantages over old <code class=\"\" data-line=\"\">os.path<\/code> &#8211; while <code class=\"\" data-line=\"\">os<\/code> module represents paths in raw string format, <code class=\"\" data-line=\"\">pathlib<\/code> uses object-oriented style, which makes it more readable and natural to write:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; pathlib &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; Path\n&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;path\n\n&lt;span class=&quot;token comment&quot;&gt;# Old, Unreadable&lt;\/span&gt;\ntwo_dirs_up &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;dirname&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;dirname&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;abspath&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;__file__&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# New, Readable&lt;\/span&gt;\ntwo_dirs_up &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; Path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;__file__&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;resolve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;parent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;parent<\/code><\/pre>\n<p>The fact that paths are treated as objects rather than strings also makes it possible to create the object once and then lookup its attributes or make operations on it:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">readme &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; Path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;README.md&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;resolve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Absolute path: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;readme&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;absolute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Absolute path: \/home\/martin\/some\/path\/README.md&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;File name: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;readme&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# File name: README.md&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Path root: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;readme&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Path root: \/&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Parent directory: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;readme&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;parent&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Parent directory: \/home\/martin\/some\/path&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;File extension: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;readme&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;suffix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# File extension: .md&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Is it absolute: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;readme&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;is_absolute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Is it absolute: True&lt;\/span&gt;<\/code><\/pre>\n<p>The one feature that I love the most about <code class=\"\" data-line=\"\">pathlib<\/code> though, is possibility to use the <code class=\"\" data-line=\"\">\/<\/code> (<i>&#8220;division&#8221;<\/i>) operator to join paths:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token comment&quot;&gt;# Operators:&lt;\/span&gt;\netc &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; Path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;\/etc&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\njoined &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; etc &lt;span class=&quot;token operator&quot;&gt;\/&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cron.d&quot;&lt;\/span&gt; &lt;span class=&quot;token operator&quot;&gt;\/&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;anacron&quot;&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Exists? - &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;joined&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;exists&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Exists? - True&lt;\/span&gt;<\/code><\/pre>\n<p>This makes handling of paths so easy and really is a <i>chef\u2019s kiss<\/i> \ud83d\udc4c.<\/p>\n<p>With that said, it&#8217;s important to note that <code class=\"\" data-line=\"\">pathlib<\/code> is only replacement for <code class=\"\" data-line=\"\">os.path<\/code> and not a whole <code class=\"\" data-line=\"\">os<\/code> module. It however includes also functionality from <code class=\"\" data-line=\"\">glob<\/code> module, so if you&#8217;re used to using <code class=\"\" data-line=\"\">os.path<\/code> in combination with <code class=\"\" data-line=\"\">glob.glob<\/code>, then you can just forget that those 2 exist.<\/p>\n<p>In the above snippets we presented some handy path manipulations and object attributes, but <code class=\"\" data-line=\"\">pathlib<\/code> also includes all the methods that you&#8217;re used to from <code class=\"\" data-line=\"\">os.path<\/code>, such as:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Working directory: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;Path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;cwd&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# same as os.getcwd()&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Working directory: \/home\/martin\/some\/path&lt;\/span&gt;\nPath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;mkdir&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;Path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;cwd&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt; &lt;span class=&quot;token operator&quot;&gt;\/&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;new_dir&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; exist_ok&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# same as os.makedirs()&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;Path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;README.md&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;resolve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# same as os.path.abspath()&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# \/home\/martin\/some\/path\/README.md&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;Path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;home&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# same as os.path.expanduser()&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# \/home\/martin&lt;\/span&gt;<\/code><\/pre>\n<p>For full mapping of <code class=\"\" data-line=\"\">os.path<\/code> functions to new ones in <code class=\"\" data-line=\"\">pathlib<\/code> see <a href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html#correspondence-to-tools-in-the-os-module\">docs<\/a> .<\/p>\n<p>For more examples of how great <code class=\"\" data-line=\"\">pathlib<\/code> is, check out nice <a href=\"https:\/\/treyhunner.com\/2018\/12\/why-you-should-be-using-pathlib\/\">write-up by Trey Hunner<\/a>.<\/p>\n<h2>Secrets<\/h2>\n<p>Speaking of <code class=\"\" data-line=\"\">os<\/code> module, another part of it that you should stop using is <code class=\"\" data-line=\"\">os.urandom<\/code>. Instead, you should use new <code class=\"\" data-line=\"\">secrets<\/code> module available since Python 3.6:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token comment&quot;&gt;# Old:&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; os\n\nlength &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;\/span&gt;\n\nvalue &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;urandom&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Bytes: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Bytes: b&#039;\\xfa\\xf3...\\xf2\\x1b\\xf5\\xb6&#039;&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Hex: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;token builtin&quot;&gt;hex&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Hex: faf3cc656370e31a938e7...33d9b023c3c24f1bf5&lt;\/span&gt;\n\n&lt;span class=&quot;token comment&quot;&gt;# New:&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; secrets\n\nvalue &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; secrets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;token_bytes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Bytes: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Bytes: b&#039;U\\xe9n\\x87...\\x85&gt;\\x04j:\\xb0&#039;&lt;\/span&gt;\nvalue &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; secrets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;token_hex&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Hex: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Hex: fb5dd85e7d73f7a08b8e3...4fd9f95beb08d77391&lt;\/span&gt;<\/code><\/pre>\n<p>Using <code class=\"\" data-line=\"\">os.urandom<\/code> isn&#8217;t actually the problem here though, the <a href=\"https:\/\/peps.python.org\/pep-0506\/#rationale\">reason<\/a> <code class=\"\" data-line=\"\">secrets<\/code> module got introduced is because people were using <code class=\"\" data-line=\"\">random<\/code> module for generating passwords and such, even though <code class=\"\" data-line=\"\">random<\/code> module doesn&#8217;t produce cryptographically safe tokens.<\/p>\n<p>As per docs, <code class=\"\" data-line=\"\">random<\/code> module should not be used for security purposes. You should use either <code class=\"\" data-line=\"\">secrets<\/code> or <code class=\"\" data-line=\"\">os.urandom<\/code>, but the <code class=\"\" data-line=\"\">secrets<\/code> module is definitely preferable, considering that it&#8217;s newer and includes some utility\/convenience methods for hexadecimal tokens as well as URL safe tokens.<\/p>\n<h2>Zoneinfo<\/h2>\n<p>Until Python 3.9, there wasn&#8217;t builtin library for timezone manipulation, so everyone was using <code class=\"\" data-line=\"\">pytz<\/code>, but now we have <code class=\"\" data-line=\"\">zoneinfo<\/code> in standard library, so it&#8217;s time to switch!<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; datetime &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; datetime\n&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; pytz  &lt;span class=&quot;token comment&quot;&gt;# pip install pytz&lt;\/span&gt;\n\ndt &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; datetime&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token number&quot;&gt;2022&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\nnyc &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; pytz&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;timezone&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;America\/New_York&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\nlocalized &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; nyc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;localize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;dt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Datetime: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;localized&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;, Timezone: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;localized&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;tzname&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;, TZ Info: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;localized&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;tzinfo&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\n&lt;span class=&quot;token comment&quot;&gt;# New:&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; zoneinfo &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; ZoneInfo\n\nnyc &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; ZoneInfo&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;America\/New_York&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\nlocalized &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; datetime&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token number&quot;&gt;2022&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; tzinfo&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;nyc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Datetime: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;localized&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;, Timezone: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;localized&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;tzname&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;, TZ Info: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;localized&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;tzinfo&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Datetime: 2022-06-04 00:00:00-04:00, Timezone: EDT, TZ Info: America\/New_York&lt;\/span&gt;<\/code><\/pre>\n<p>The <code class=\"\" data-line=\"\">datetime<\/code> module delegates all timezone manipulation to abstract base class <code class=\"\" data-line=\"\">datetime.tzinfo<\/code>. This abstract base class needs a concrete implementation &#8211; before introducing this module that would most likely come from <code class=\"\" data-line=\"\">pytz<\/code>. Now that we have <code class=\"\" data-line=\"\">zoneinfo<\/code> in standard library we can use that instead.<\/p>\n<p>Using <code class=\"\" data-line=\"\">zoneinfo<\/code> however has one caveat &#8211; it assumes that there&#8217;s time zone data available on the system, which is the case on UNIX systems. If your system doesn&#8217;t have timezone data though, then you should use <a href=\"https:\/\/pypi.org\/project\/tzdata\/\">tzdata<\/a> package which is a first-party library maintained by the CPython core developers, which contains IANA time zone database.<\/p>\n<h2>Dataclasses<\/h2>\n<p>An important addition to Python 3.7 was <code class=\"\" data-line=\"\">dataclasses<\/code> package which is a replacement for <code class=\"\" data-line=\"\">namedtuple<\/code>.<\/p>\n<p>You might be wondering why would you need to replace <code class=\"\" data-line=\"\">namedtuple<\/code>? So, these are some reasons why you should consider switching to <code class=\"\" data-line=\"\">dataclasses<\/code>:<\/p>\n<ul>\n<li>Can be mutable,<\/li>\n<li>By default provides <code class=\"\" data-line=\"\">__repr__<\/code>, <code class=\"\" data-line=\"\">__eq__<\/code>, <code class=\"\" data-line=\"\">__init__<\/code>, <code class=\"\" data-line=\"\">__hash__<\/code> magic methods,<\/li>\n<li>Allows to specify default values,<\/li>\n<li>Supports inheritance.<\/li>\n<\/ul>\n<p>Additionally, dataclasses also support <code class=\"\" data-line=\"\">__frozen__<\/code> and <code class=\"\" data-line=\"\">__slots__<\/code> (from 3.10) attributes to give feature parity with named tuples.<\/p>\n<p>And switching really shouldn&#8217;t be too difficult, as you only need to change the definitions:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token comment&quot;&gt;# Old:&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# from collections import namedtuple&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; NamedTuple\n&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; sys\n\nUser &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; NamedTuple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;User&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;surname&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;password&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\nu &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Doe&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#039;tfeL+uD...\\xd2&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Size: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;getsizeof&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Size: 64&lt;\/span&gt;\n\n&lt;span class=&quot;token comment&quot;&gt;# New:&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; dataclasses &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; dataclass\n\n&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;class&lt;\/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt;\n    name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;\n    surname&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;\n    password&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;\/span&gt;\n\nu &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Doe&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;b&#039;tfeL+uD...\\xd2&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# User(name=&#039;John&#039;, surname=&#039;Doe&#039;, password=b&#039;tfeL+uD...\\xd2&#039;)&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Size: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;getsizeof&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;getsizeof&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;\/span&gt; sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;getsizeof&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;u&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Size: 48, 152&lt;\/span&gt;<\/code><\/pre>\n<p>In the above code we also included a size comparison, as that&#8217;s one of the bigger differences between the <code class=\"\" data-line=\"\">namedtuple<\/code> and <code class=\"\" data-line=\"\">dataclasses<\/code>. As you can see, named tuples have significantly smaller size, which is due to dataclasses using <code class=\"\" data-line=\"\">dict<\/code> to represent attributes.<\/p>\n<p>As for the speed comparison, the access time for attributes should be mostly the same, or not significant enough to matter unless you plan to create millions of instances:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; timeit\n\nsetup &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&#039;&#039;&#039;\nfrom typing import NamedTuple\nUser = NamedTuple(&quot;User&quot;, [(&quot;name&quot;, str), (&quot;surname&quot;, str), (&quot;password&quot;, bytes)])\nu = User(&quot;John&quot;, &quot;Doe&quot;, b&#039;&#039;)\n&#039;&#039;&#039;&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Access speed: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;&lt;span class=&quot;token builtin&quot;&gt;min&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;timeit&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;repeat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;u.name&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; setup&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;setup&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; number&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;token number&quot;&gt;10000000&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Access speed: 0.16838401100540068&lt;\/span&gt;\n\nsetup &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&#039;&#039;&#039;\nfrom dataclasses import dataclass\n\n@dataclass(slots=True)\nclass User:\n    name: str\n    surname: str\n    password: bytes\n\nu = User(&quot;John&quot;, &quot;Doe&quot;, b&#039;&#039;)\n&#039;&#039;&#039;&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Access speed: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;&lt;span class=&quot;token builtin&quot;&gt;min&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;timeit&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;repeat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;u.name&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; setup&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;setup&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; number&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;token number&quot;&gt;10000000&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# Access speed: 0.17728697300481144&lt;\/span&gt;<\/code><\/pre>\n<p>If the above persuaded you switch to dataclasses, but you&#8217;re stuck in 3.6 or earlier you can grab a backport from <a href=\"https:\/\/pypi.org\/project\/dataclasses\/\">https:\/\/pypi.org\/project\/dataclasses\/<\/a>.<\/p>\n<p>Conversely, if you don&#8217;t want to switch and really want to use named tuples for some reason, then you should at very least <code class=\"\" data-line=\"\">NamedTuple<\/code> from <code class=\"\" data-line=\"\">typing<\/code> module instead of the one from <code class=\"\" data-line=\"\">collections<\/code>:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token comment&quot;&gt;# Bad:&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; collections &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; namedtuple\nPoint &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; namedtuple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Point&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;x&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;y&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\n&lt;span class=&quot;token comment&quot;&gt;# Better:&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;from&lt;\/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; NamedTuple\n&lt;span class=&quot;token keyword&quot;&gt;class&lt;\/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Point&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;NamedTuple&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt;\n    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;\/span&gt;\n    y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;\/span&gt;<\/code><\/pre>\n<p>Finally, if you don&#8217;t use either <code class=\"\" data-line=\"\">namedtuple<\/code> nor <code class=\"\" data-line=\"\">dataclasses<\/code> you might want to consider going straight to <a href=\"https:\/\/pydantic-docs.helpmanual.io\/\">Pydantic<\/a>.<\/p>\n<h2>Proper Logging<\/h2>\n<p>This isn&#8217;t a recent addition to standard library, but it bears repeating &#8211; you should use proper logging instead of <code class=\"\" data-line=\"\">print<\/code> statements. It&#8217;s fine to use print if you&#8217;re debugging an issue locally, but for any production-ready program that will run without user intervention, proper logging is a must.<\/p>\n<p>Especially considering that setting up Python <code class=\"\" data-line=\"\">logging<\/code> is as easy as:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; logging\nlogging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;basicConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;\n    filename&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;application.log&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt;\n    level&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;logging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;WARNING&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt;\n    &lt;span class=&quot;token builtin&quot;&gt;format&lt;\/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt;\n    datefmt&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;%H:%M:%S&#039;&lt;\/span&gt;\n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\nlogging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Some serious error occurred.&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# [12:52:35] {&lt;stdin&gt;:1} ERROR - Some serious error occurred.&lt;\/span&gt;\nlogging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;warning&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&#039;Some warning.&#039;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# [12:52:35] {&lt;stdin&gt;:1} WARNING - Some warning.&lt;\/span&gt;<\/code><\/pre>\n<p>Just the simple configuration above will give you superior debugging experience in comparison to <code class=\"\" data-line=\"\">print<\/code> statements. On top of that you can further customize the <code class=\"\" data-line=\"\">logging<\/code> library to log to different places, change log levels, automatically rotate logs, etc. For examples on how to set up all of that see my previous article <i><a href=\"https:\/\/martinheinz.dev\/blog\/24\">Ultimate Guide to Python Debugging<\/a><\/i>.<\/p>\n<h2>f-strings<\/h2>\n<p>Python includes quite a few ways to format strings including C-style formatting, f-strings, template strings or <code class=\"\" data-line=\"\">.format<\/code> function. One of them &#8211; f-strings &#8211; the formatted string literals &#8211; are just superior, though. They&#8217;re more natural to write, more readable, and <i>the fastest<\/i> of the previously mentioned options.<\/p>\n<p>Therefore, I think there&#8217;s no point arguing or explaining why you should use them. There are however a couple cases where f-strings cannot be used:<\/p>\n<p>Only reason to ever use <code class=\"\" data-line=\"\">%<\/code> formatting is for logging:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; logging\n\nthings &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;something happened...&quot;&lt;\/span&gt;\n\nlogger &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; logging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;getLogger&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;__name__&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\nlogger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Message: %s&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; things&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Evaluated inside logger method&lt;\/span&gt;\nlogger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Message: &lt;\/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;\/span&gt;things&lt;span class=&quot;token punctuation&quot;&gt;}&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;\/span&gt;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Evaluated immediately&lt;\/span&gt;<\/code><\/pre>\n<p>In the example above if you use f-strings the expression would be evaluated immediately, while with C-style formatting, substitution will be deferred until it&#8217;s actually needed. This is important for grouping of messages, where all messages with the same template can be recorded as one. That doesn&#8217;t work with f-strings, because the template is populated with data before it&#8217;s passed to logger.<\/p>\n<p>Also, there are things that f-strings simply cannot do. For example populating template at runtime &#8211; that is, dynamic formatting &#8211; that&#8217;s the reason f-strings are referred to as <i>literal<\/i> string formatting:<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token comment&quot;&gt;# Dynamically set both the template and its parameters&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;def&lt;\/span&gt; &lt;span class=&quot;token function&quot;&gt;func&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;tpl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; param1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; param2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;\/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt;\n    &lt;span class=&quot;token keyword&quot;&gt;return&lt;\/span&gt; tpl&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;token builtin&quot;&gt;format&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;param&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;param1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; param2&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;param2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\nsome_template &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;First template: {param1}, {param2}&quot;&lt;\/span&gt;\nanother_template &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Other template: {param1} and {param2}&quot;&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;func&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;some_template&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;func&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;another_template&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Python&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n\n&lt;span class=&quot;token comment&quot;&gt;# Dynamically reuse same template with different parameters.&lt;\/span&gt;\ninputs &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;!&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;\/span&gt;\ntemplate &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Here&#039;s some dynamic value: {value}&quot;&lt;\/span&gt;\n\n&lt;span class=&quot;token keyword&quot;&gt;for&lt;\/span&gt; value &lt;span class=&quot;token keyword&quot;&gt;in&lt;\/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt;\n    &lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;template&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;token builtin&quot;&gt;format&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;value&lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;<\/code><\/pre>\n<p>Bottom line is though, use f-strings wherever possible because they&#8217;re more readable and more performant, be aware though that there are cases where other formatting style are still preferred and\/or necessary.<\/p>\n<h2>Tomllib<\/h2>\n<p>TOML is widely used configuration format and is especially important to Python&#8217;s tooling and ecosystem, because if its usage for <code class=\"\" data-line=\"\">pyproject.toml<\/code> configuration files. Until now, you&#8217;d have to use external libraries to manage TOML files, but <a href=\"https:\/\/peps.python.org\/pep-0680\/\">starting with Python 3.11<\/a>, there will be builtin library named <code class=\"\" data-line=\"\">tomllib<\/code> which is based on <code class=\"\" data-line=\"\">tomli<\/code> package.<\/p>\n<p>So, as soon as you switch to Python 3.11, you should get into habit of using <code class=\"\" data-line=\"\">import tomllib<\/code> instead of <code class=\"\" data-line=\"\">import tomli<\/code>. It&#8217;s one less dependency to worry about!<\/p>\n<pre class=\"language-python\" tabindex=\"0\"><code class=\"language-python\" data-line=\"\">&lt;span class=&quot;token comment&quot;&gt;# import tomli as tomllib&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;import&lt;\/span&gt; tomllib\n\n&lt;span class=&quot;token keyword&quot;&gt;with&lt;\/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pyproject.toml&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;\/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;rb&quot;&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;\/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;\/span&gt;\n    config &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; tomllib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;load&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n    &lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n    &lt;span class=&quot;token comment&quot;&gt;# {&#039;project&#039;: {&#039;authors&#039;: [{&#039;email&#039;: &#039;contact@martinheinz.dev&#039;,&lt;\/span&gt;\n    &lt;span class=&quot;token comment&quot;&gt;#                           &#039;name&#039;: &#039;Martin Heinz&#039;}],&lt;\/span&gt;\n    &lt;span class=&quot;token comment&quot;&gt;#              &#039;dependencies&#039;: [&#039;flask&#039;, &#039;requests&#039;],&lt;\/span&gt;\n    &lt;span class=&quot;token comment&quot;&gt;#              &#039;description&#039;: &#039;Example Package&#039;,&lt;\/span&gt;\n    &lt;span class=&quot;token comment&quot;&gt;#              &#039;name&#039;: &#039;some-app&#039;,&lt;\/span&gt;\n    &lt;span class=&quot;token comment&quot;&gt;#              &#039;version&#039;: &#039;0.1.0&#039;}}&lt;\/span&gt;\n\ntoml_string &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;\n[project]\nname = &quot;another-app&quot;\ndescription = &quot;Example Package&quot;\nversion = &quot;0.1.1&quot;\n&quot;&quot;&quot;&lt;\/span&gt;\n\nconfig &lt;span class=&quot;token operator&quot;&gt;=&lt;\/span&gt; tomllib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;\/span&gt;loads&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;toml_string&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token keyword&quot;&gt;print&lt;\/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;\/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;)&lt;\/span&gt;\n&lt;span class=&quot;token comment&quot;&gt;# {&#039;project&#039;: {&#039;name&#039;: &#039;another-app&#039;, &#039;description&#039;: &#039;Example Package&#039;, &#039;version&#039;: &#039;0.1.1&#039;}}&lt;\/span&gt;<\/code><\/pre>\n<h2>Setuptools<\/h2>\n<p>Last one is more of a deprecation notice:<\/p>\n<p>&gt; As Distutils is deprecated, any usage of functions or objects from distutils is similarly discouraged, and Setuptools aims to replace or deprecate all such uses.<\/p>\n<p>It&#8217;s time to say goodbye to <code class=\"\" data-line=\"\">distutils<\/code> package and switch to <code class=\"\" data-line=\"\">setuptools<\/code>. <a href=\"https:\/\/setuptools.pypa.io\/en\/latest\/deprecated\/distutils-legacy.html\"><code class=\"\" data-line=\"\">setuptools<\/code> docs<\/a> provide guidance on how you should replace usages of <code class=\"\" data-line=\"\">distutils<\/code>. Apart from that, also the PEP 632 provides <a href=\"https:\/\/peps.python.org\/pep-0632\/#migration-advice\">migration advice<\/a> for parts of <code class=\"\" data-line=\"\">distutils<\/code> not covered by <code class=\"\" data-line=\"\">setuptools<\/code>.<\/p>\n<h2>Conclusion<\/h2>\n<p>Every new Python release brings new features, so I&#8217;d recommend checking <i>&#8220;New Modules&#8221;<\/i>, <i>&#8220;Deprecated Modules&#8221;<\/i> and <i>&#8220;Removed Modules&#8221;<\/i> sections in Python <a href=\"https:\/\/docs.python.org\/3\/whatsnew\/3.9.html#new-modules\">release notes<\/a>, which is a great way to stay up to date with major changes to Python&#8217;s standard library. This way you can continuously incorporate new features and best practices into your projects.<\/p>\n<p>Now you might think that making all these changes and upgrades would require a lot of effort. In reality, you might be able to run <a href=\"https:\/\/github.com\/asottile\/pyupgrade\"><code class=\"\" data-line=\"\">pyupgrade<\/code><\/a> on your project and upgrade the syntax to the latest Python version automatically where possible.<\/p>\n<\/div>\n<\/div>\n<\/article>\n<\/div>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Another interesting article to &#8220;convert to Eiffel&#8221;&#8230;. It&#8217;s Time to Say Goodbye to These Obsolete Python Libraries It&#8217;s Time to Say Goodbye to These Obsolete Python Libraries Martin Jul 20, 2022 Python With every Python release, there are new modules being added and new and better ways of doing things get introduced. We all get&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2022\/09\/20\/9633\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[238,98,113],"tags":[],"class_list":["post-9633","post","type-post","status-publish","format-standard","hentry","category-agenda","category-liberty-eiffel","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s6daft-9633","jetpack-related-posts":[{"id":9245,"url":"https:\/\/monodes.com\/predaelli\/2022\/04\/05\/so-many-things-to-fix\/","url_meta":{"origin":9633,"position":0},"title":"So many things to fix!","author":"Paolo Redaelli","date":"2022-04-05","format":false,"excerpt":"Well, instead of reading Write Your Own C-extension to Speed Up Python by 100x | by Mike Huls | Towards Data Science How to write, compile, package and import your own, superfast C-module into\u00a0Python We should have read Write Your Own Eiffel-extension to Speed Up Python by 100x How to\u2026","rel":"","context":"In &quot;Mood&quot;","block_context":{"text":"Mood","link":"https:\/\/monodes.com\/predaelli\/category\/mood\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11033,"url":"https:\/\/monodes.com\/predaelli\/2023\/12\/17\/textual\/","url_meta":{"origin":9633,"position":1},"title":"Textual","author":"Paolo Redaelli","date":"2023-12-17","format":false,"excerpt":"Textual is a TUI framework for Python, inspired by modern web development. Textual is a Rapid Application Development framework for Python, built by Textualize.io. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal or a web browser! Well, I just wish I could have\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6318,"url":"https:\/\/monodes.com\/predaelli\/2019\/12\/15\/eiffels-conditional-expressions\/","url_meta":{"origin":9633,"position":2},"title":"Eiffel&#8217;s conditional expressions","author":"Paolo Redaelli","date":"2019-12-15","format":false,"excerpt":"The ternary conditional operator is a short-hand method for writing an if\/else statement. Does Python Have a Ternary Conditional Operator? Yes, it does. This remids me that Eiffel, at least \"ISE Eiffel\" does actually have conditional expressions that are the same: answer := if time < noon then \"Good morning\"\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"Jonathan Hsu","src":"https:\/\/i0.wp.com\/miro.medium.com\/fit\/c\/58\/58\/2%2A_KGzadiy9s83D4vzhsCyyg.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":9725,"url":"https:\/\/monodes.com\/predaelli\/2022\/10\/15\/python-is-inefficient\/","url_meta":{"origin":9633,"position":3},"title":"Python is inefficient","author":"Paolo Redaelli","date":"2022-10-15","format":false,"excerpt":"The most significant damage caused by Python Python is Destroying the Planet.writes Mohammed Ayar of course, I shall say as it is much less efficient, i.e. it's a lot slower than C. It's quite simple, and it's clearly showed by The\u00a0Computer Language Benchmarks Game, once known as \"The great language\u2026","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9039,"url":"https:\/\/monodes.com\/predaelli\/2022\/01\/01\/deciphering-glyph-the-one-python-library-everyone-needs\/","url_meta":{"origin":9633,"position":4},"title":"Deciphering Glyph :: The One Python Library Everyone Needs","author":"Paolo Redaelli","date":"2022-01-01","format":"link","excerpt":"Use attrs. Use it. Use it for everything. From: Deciphering Glyph :: The One Python Library Everyone Needs Looks a little like magic. But that's what happen with loosely typed languages, \"interpreted\" languages.","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/monodes.com\/predaelli\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9674,"url":"https:\/\/monodes.com\/predaelli\/2022\/09\/28\/10-powerful-python-one-liners\/","url_meta":{"origin":9633,"position":5},"title":"10 Powerful Python One-Liners.","author":"Paolo Redaelli","date":"2022-09-28","format":false,"excerpt":"I shall Eiffellize those, one day or another: 10 Powerful Python One-Liners. Python one-liners can be just as\u2026 | by Ishaan Gupta | Sep, 2022 | Python in Plain English 10 Powerful Python One-Liners Python one-liners can be just as powerful as a long and tedious program written in another\u2026","rel":"","context":"In &quot;Agenda&quot;","block_context":{"text":"Agenda","link":"https:\/\/monodes.com\/predaelli\/category\/agenda\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2022\/09\/1jLSxNQvNqsYr02VB_wr96A.jpeg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2022\/09\/1jLSxNQvNqsYr02VB_wr96A.jpeg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2022\/09\/1jLSxNQvNqsYr02VB_wr96A.jpeg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/9633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=9633"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/9633\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=9633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=9633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=9633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}