<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: João M.C. Teixeira</title>
    <description>The latest articles on DEV Community by João M.C. Teixeira (@joaomcteixeira).</description>
    <link>https://dev.to/joaomcteixeira</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F382220%2F48103cdd-300e-4159-9658-68b4fc110a69.jpg</url>
      <title>DEV Community: João M.C. Teixeira</title>
      <link>https://dev.to/joaomcteixeira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joaomcteixeira"/>
    <language>en</language>
    <item>
      <title>Separating business and administrative logic - example 1</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Tue, 28 Nov 2023 23:06:31 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/separating-business-and-administrative-logic-1o8b</link>
      <guid>https://dev.to/joaomcteixeira/separating-business-and-administrative-logic-1o8b</guid>
      <description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(I am re-posting here a post I made in 2019 on my blog)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A quick view on business and administrative logic separation. &lt;/p&gt;

&lt;p&gt;A dummy example where we use decorators to check if the input path exists before assigning it to the class attribute. Context managers could accomplish the same, but with the decorator, checking takes place before even executing the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_folder_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Decorator for class property setters
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;FileNotFoundError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Path not found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kwargs&lt;/span&gt;

    &lt;span class="nd"&gt;@property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;db_folder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_db_folder&lt;/span&gt;

    &lt;span class="nd"&gt;@db_folder.setter&lt;/span&gt;
    &lt;span class="nd"&gt;@check_folder_exists&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;db_folder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_db_folder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is another snippet from my code where I use a combination of three contexts to handle exceptions hierarchically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;config_reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;GCTXT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort_if_exception&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; \
            &lt;span class="n"&gt;GCTXT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query_upon_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;XCPTNS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConfigReadingException&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; \
            &lt;span class="n"&gt;GCTXT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort_if_critical_exceptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CONFREADER&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;critical_errors&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you find this useful for your projects? Let me know.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

</description>
      <category>python</category>
      <category>architecture</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Type hints in Python: to be or not to be</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Tue, 04 Apr 2023 09:50:25 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/type-hints-in-python-to-be-or-not-to-be-301c</link>
      <guid>https://dev.to/joaomcteixeira/type-hints-in-python-to-be-or-not-to-be-301c</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;Using type hints in Python is an active controversy nowadays. Are they good, or are they not? Should we use them, or should we not?&lt;/p&gt;

&lt;p&gt;In one of the projects I was lead developer, someone asked today about adopting type hints. I will share with you my opinion. It may help you to decide whether to use type hints or not. GitHub discussion below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/haddocking/haddock3/discussions/637"&gt;https://github.com/haddocking/haddock3/discussions/637&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Simple password generator in Python</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Thu, 03 Mar 2022 16:39:51 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/simple-password-generator-in-python-2c1n</link>
      <guid>https://dev.to/joaomcteixeira/simple-password-generator-in-python-2c1n</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;I published today a very simple implementation of a password generator using Python through the command-line. Yet, there are some interesting options.&lt;/p&gt;

&lt;p&gt;We can discuss about the implementation details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/joaomcteixeira/passgen"&gt;https://github.com/joaomcteixeira/passgen&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>security</category>
    </item>
    <item>
      <title>Modular, extensible, maintainable, and testable functional strategy pattern - part 1</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Fri, 10 Sep 2021 15:09:54 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/modular-extensible-maintainable-and-testable-functional-strategy-pattern-part-1-c98</link>
      <guid>https://dev.to/joaomcteixeira/modular-extensible-maintainable-and-testable-functional-strategy-pattern-part-1-c98</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Strategy Pattern&lt;/strong&gt; is likely the most used design pattern. &lt;em&gt;Who said it? Well, I said it.&lt;/em&gt; The strategy pattern concept is simple. At a given moment, the execution workflow must take one from different paths depending on the evaluation of the current state, being &lt;em&gt;state&lt;/em&gt; a variable or a group of variables.&lt;/p&gt;

&lt;p&gt;There are countless situations to apply the strategy pattern; however, we will focus on analysing a &lt;strong&gt;single variable&lt;/strong&gt; to control the execution flow.&lt;/p&gt;

&lt;p&gt;We have variable &lt;code&gt;var1&lt;/code&gt; and, depending on the boolean result of different evaluations, &lt;code&gt;var1&lt;/code&gt; is processed differently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var1 is int -&amp;gt; process it as an int
var1 is long string -&amp;gt; process the long string
var1 is short string -&amp;gt; process the short string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What could be the most &lt;strong&gt;extensible&lt;/strong&gt;, &lt;strong&gt;maintainable&lt;/strong&gt;, &lt;strong&gt;modular&lt;/strong&gt;, and &lt;strong&gt;testable&lt;/strong&gt; way to implement a strategy pattern here? Let's see different options:&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;if-statements&lt;/code&gt; are the most easily grasping strategy, likely used by all novice programmers but also advanced ones (when coding for very straightforward cases 😉).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="c1"&gt;# do some logic here
&lt;/span&gt;&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="c1"&gt;# do other logic here
&lt;/span&gt;&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="c1"&gt;# do yet another logic here
&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="c1"&gt;# everything before was false
&lt;/span&gt;     &lt;span class="c1"&gt;# do this other thing
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The case presented above might be a feasible quick implementation for some simple cases. But here, we imagine all operations are long and complex because we are developing an extensive program ☺️. So, what are the problems of a direct &lt;code&gt;if/elif/else&lt;/code&gt; implementation as the one presented above? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;if-statements&lt;/code&gt; will become too lengthy and maybe nested as the number of options grows.&lt;/li&gt;
&lt;li&gt;Also, having the business logic directly under the &lt;code&gt;if clauses&lt;/code&gt; render that code challenging to test. Surely, impossible to test on its own.&lt;/li&gt;
&lt;li&gt;Adding more options will just make the code less readable, especially if implementations are lengthy.&lt;/li&gt;
&lt;li&gt;And, the code is not modular because you cannot use those logic blocks anywhere else as they are hardcoded directly under the &lt;code&gt;if-clauses&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How can we improve from this situation?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first step you can do to improve the code's modularity and testability is to encapsulate each logical process in its separate function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="n"&gt;process_is_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="n"&gt;process_is_long_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="n"&gt;process_is_short_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="n"&gt;do_else_logic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this, you will be able to &lt;strong&gt;test&lt;/strong&gt; the different functions in their dedicated unit-tests. Also, your code becomes more modular because you can re-use those functions anywhere else; even have them as a public, well-documented library API.&lt;/p&gt;

&lt;p&gt;The second step towards improving the quality of your code is to transform your asserting statements into, also, individual functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;is_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="n"&gt;process_is_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;is_long_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="n"&gt;process_is_long_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;is_short_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="n"&gt;process_is_short_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="n"&gt;do_else_logic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how you can now use the evaluation functions (&lt;code&gt;is_int&lt;/code&gt;, &lt;code&gt;is_long_string&lt;/code&gt;, and &lt;code&gt;is_short_string&lt;/code&gt;) anywhere else in the code and test them properly. Also, suppose you want to add additional features to those functions in a later stage, for example, new assertions or logging. In that case, you can do it easily without the need to modify the &lt;code&gt;if/elif&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(side comment)&lt;/em&gt; Once you reach this point of understanding, your brain will likely spark the light of using a dictionary to control the flow. Probably you have read about that strategy somewhere. I have used it also. Using dictionaries to control flow is productive in some cases, but its use won't excel in this particular case. So I will discuss using dictionaries to control flow in other posts of my series (keep an eye on them) and won't talk about them here. &lt;em&gt;(end of side comment)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finally, &lt;strong&gt;there is a way that out-stands the previous two&lt;/strong&gt;, both &lt;code&gt;if-else&lt;/code&gt; blocks and that crazy dictionary strategy I told you about but didn't explain 😉.&lt;/p&gt;

&lt;p&gt;The implementation design that best suits our example (in my experience and opinion)  is using a &lt;code&gt;for&lt;/code&gt; loop. Yes, a for-loop. &lt;strong&gt;How?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, we create a &lt;strong&gt;list of tuples&lt;/strong&gt; where each tuple has two values: 1) the evaluation function (boolean logic) and 2) the business logic. The &lt;code&gt;for&lt;/code&gt; loop will loop through the list and operate on the first function that evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# note how our logic is getting defined in separate functions
# as we evolve from if-statements to this pipeline-like method
&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;# (if_this_is_true, do_this).
&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_is_int&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_long_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_is_long_string&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_short_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_is_short_string&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_the_else_logic&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;evaluation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;evaluation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# the loop does not need to continue
&lt;/span&gt;        &lt;span class="c1"&gt;# after a function is executed
&lt;/span&gt;        &lt;span class="k"&gt;break&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case the &lt;code&gt;else&lt;/code&gt; logic does not receive &lt;code&gt;var1&lt;/code&gt; as an argument, you can move it to the &lt;code&gt;else&lt;/code&gt; method of the for-loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_is_int&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_long_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_is_long_string&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_short_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;process_is_short_string&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;evaluation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;evaluation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;process_the_else_logic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Benefits of this last approach?&lt;/strong&gt; Several:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The for-loop executes the boolean logic only once and only if the previous evaluated to &lt;code&gt;False&lt;/code&gt;, so there are no losses against the &lt;code&gt;if-else&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;You can easily change the order of the process by changing the order of the tuples in the list without the need to be moving large pieces of code around.&lt;/li&gt;
&lt;li&gt;You can add/remove new functionalities by adding/removing tuples in the list without changing the engine.&lt;/li&gt;
&lt;li&gt;It is effortless to read and understand &lt;strong&gt;what&lt;/strong&gt; the code will do. Understanding &lt;strong&gt;how&lt;/strong&gt; the code will do it is another story. For that you would need to read into each function. Congratulations, you have separated the &lt;em&gt;what&lt;/em&gt; from the &lt;em&gt;how&lt;/em&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Honestly, I see no flaws in this method. I think it really is &lt;strong&gt;extensible&lt;/strong&gt;, &lt;strong&gt;maintainable&lt;/strong&gt;, &lt;strong&gt;modular&lt;/strong&gt;, and &lt;strong&gt;testable&lt;/strong&gt;. If you think differently or know other possible examples, let me know in the comments. One can apply the &lt;em&gt;strategy pattern&lt;/em&gt; to countless situations. This is one of them. Likely, you have heard of the strategy pattern when implementing OOP, but life is not always about OOP despite what &lt;em&gt;they&lt;/em&gt; told you 😛&lt;/p&gt;

&lt;p&gt;But this is not the whole story. For example, many times, we use &lt;code&gt;if-statements&lt;/code&gt; to evaluate the state of multiple variables. And, of course, that is also possible using the for loop strategy presented here. But asserting the state of multiple variables is a more complex case that would require the use of &lt;code&gt;functools.partial&lt;/code&gt;, so we leave that discussion for another post.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this quick yet profound discussion, which I am sure will help you design larger and more maintainable software packages. Despite not being skilled in other languages besides Python, I trust you can apply this same (or similar) idea in other realms of programming.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

</description>
      <category>python</category>
      <category>architecture</category>
      <category>functional</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Express your intentions in your coding - attributes that are not</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Sat, 28 Aug 2021 10:13:58 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/express-your-intentions-in-your-coding-attributes-that-are-not-33jg</link>
      <guid>https://dev.to/joaomcteixeira/express-your-intentions-in-your-coding-attributes-that-are-not-33jg</guid>
      <description>&lt;p&gt;In the previous post of this series, I suggested prefixing the underscore &lt;code&gt;_&lt;/code&gt; to variables intended to be short-lived. Here I will discuss another example where variable names lie on their intentions.&lt;/p&gt;

&lt;p&gt;Pay attention: people are addicted to the &lt;code&gt;self.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you are coding a class, you have some logic in a method, and you are defining a variable within that method, do &lt;strong&gt;not&lt;/strong&gt; use &lt;code&gt;self.&lt;/code&gt; if that variable has no usage outside that method! 😌&lt;/p&gt;

&lt;p&gt;When you assign a variable to &lt;code&gt;self.&lt;/code&gt; you are telling the readers of your code that variable (better said, that attribute) has another purpose somewhere else in the execution workflow. Maybe it is even part of the public API. Therefore, someone editing your method will automatically trigger the alarms and search the codebase for a place where that attribute appears before refactoring or recoding your method. The maintainer of your code will get a bit stressed if she/he finds no place anywhere else where that attribute is used. What is happening here?&lt;/p&gt;

&lt;p&gt;So, be mindful and express your intentions in your coding. That is, if you define a variable inside a class method, don't use &lt;code&gt;self.&lt;/code&gt; if the scope of that variable does not go beyond that method. Instead, use &lt;code&gt;self.&lt;/code&gt; indeed if that variable is an actual attribute part of the private or public API of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MegaClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;method_1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="s"&gt;"""Write a docstring ;-)"""&lt;/span&gt;
        &lt;span class="n"&gt;var1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;# some logic with arg1. `var1` dies after the method
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;var2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;# some logic with var1 and arg2
&lt;/span&gt;        &lt;span class="c1"&gt;# self.var2 is an actual attribute of MegaClass part of its API.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this example is clear and you can use it to better express your intentions in your coding 😄&lt;/p&gt;

</description>
      <category>python</category>
      <category>codequality</category>
      <category>architecture</category>
      <category>refactorit</category>
    </item>
    <item>
      <title>Express your intentions in your coding - short lived variables</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Fri, 27 Aug 2021 16:51:20 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/express-your-intentions-in-your-coding-short-lived-variables-38hn</link>
      <guid>https://dev.to/joaomcteixeira/express-your-intentions-in-your-coding-short-lived-variables-38hn</guid>
      <description>&lt;p&gt;Short Friday post. Let's talk a bit about &lt;strong&gt;implementation intentions&lt;/strong&gt; and how you reflect those in your code.&lt;/p&gt;

&lt;p&gt;The more I code, and the more I review others code, some little things start to "stress" me out a bit 😄 but it's positive stress, I've to say.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Have you considered how others read and understand the scope of your variables?&lt;/strong&gt; More and more, I try to express my intentions when naming variables. And no, I am not talking about &lt;em&gt;long names&lt;/em&gt; or &lt;em&gt;short cryptic names&lt;/em&gt;. Here, I am talking about the &lt;strong&gt;time&lt;/strong&gt; and the &lt;strong&gt;length&lt;/strong&gt; (scope) a variable will have in the execution/implementation. Let's see. Imagine the following block of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;var1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;# some logic
&lt;/span&gt;
&lt;span class="c1"&gt;# some logic defined in 2 or 3 lines 
# using var1
&lt;/span&gt;
&lt;span class="c1"&gt;# the code continues on and var1 is never used again.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another more rigorous example could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;var1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;# some long-line logic about 80 chars
# one shorter line using var1
&lt;/span&gt;
&lt;span class="c1"&gt;# the code continues on and var1 is never used again.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How do you know that &lt;code&gt;var1&lt;/code&gt; is not used anywhere else in the implementation when you read these lines? You don't (until you search for it and find it's not present anywhere else). So here comes the question: How can we inform the reader that &lt;code&gt;var1&lt;/code&gt; is used only in the immediate scope of the following lines?&lt;/p&gt;

&lt;p&gt;Here's what I do: prefix the variable name with &lt;code&gt;_&lt;/code&gt;. Like this, when reading &lt;code&gt;_var1&lt;/code&gt;, we know its scope will end in the subsequent short implementation. Of course, you can argue we could use &lt;code&gt;_&lt;/code&gt; only instead of &lt;code&gt;_var1&lt;/code&gt;. That is true, and you are right. But sometimes, it is nice to name variables to give them a purpose. Also because the pure underscore &lt;code&gt;_&lt;/code&gt; is most often used for &lt;em&gt;not used variables&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;_var1&lt;/code&gt; to temporarily store the result of some logic to facilitate the reading in the following lines. Here's the example I used in my &lt;a href="https://dev.to/joaomcteixeira/don-t-make-cryptic-strings-for-formatting-fh2"&gt;previous post&lt;/a&gt;, where I use &lt;code&gt;_tmp&lt;/code&gt; to simplify the next logic step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reduce the long call to a short temporary variable
&lt;/span&gt;&lt;span class="n"&gt;_tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someobj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;somemethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"run_dir"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;somejob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;rundir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fileroot&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_tmp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;somevar&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;w'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for this Friday short post. Let me know your comments,&lt;br&gt;
Cheers,&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>python</category>
      <category>codequality</category>
      <category>codereview</category>
    </item>
    <item>
      <title>Make readable string formatting</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Thu, 19 Aug 2021 09:31:05 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/don-t-make-cryptic-strings-for-formatting-fh2</link>
      <guid>https://dev.to/joaomcteixeira/don-t-make-cryptic-strings-for-formatting-fh2</guid>
      <description>&lt;p&gt;I run into this example some days ago while reading someone's else code (variable names were modified to &lt;code&gt;some...&lt;/code&gt; or &lt;code&gt;var...&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;somejob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{}{}{}{}{}{}{}{}"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;rundir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;fileroot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;someobj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;somemethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"run_dir"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
    &lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;some_var&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s"&gt;"w"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is wrong with this example?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, can you actually understand the final shape of the &lt;code&gt;somejob&lt;/code&gt; string? For me it is just utterly cryptic. &lt;em&gt;How many &lt;code&gt;{}&lt;/code&gt; are there?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, what is wrong with this case and how can we improve it?&lt;/p&gt;

&lt;p&gt;The problem with this string formatting example is that &lt;strong&gt;constants&lt;/strong&gt; are being passed as &lt;strong&gt;variables&lt;/strong&gt;, hiding the shape of the string. It becomes much easier to read if the developer defines the constant sub-strings in the main string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;somejob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{}/{}_{}_{}w"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;rundir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;fileroot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;someobj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;somemethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"run_dir"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
    &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;somevar&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still, because in this particular case we are using &lt;code&gt;paths&lt;/code&gt;, I would go further and advocate for using &lt;code&gt;pathlib.Path&lt;/code&gt; instead of the hardcoded &lt;code&gt;/&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reduce the long call to a short temporary variable
&lt;/span&gt;&lt;span class="n"&gt;_tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someobj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;somemethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"run_dir"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;somejob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;rundir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fileroot&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_tmp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;somevar&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;w'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I truly believe these modifications improve code reading and facilitates the life of maintainers. What do you think? Does the last example reads better than the first one?&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

</description>
      <category>python</category>
      <category>strings</category>
      <category>codeclean</category>
      <category>refactorit</category>
    </item>
    <item>
      <title>I write about...</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Wed, 18 Aug 2021 14:49:05 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/i-write-about-34n7</link>
      <guid>https://dev.to/joaomcteixeira/i-write-about-34n7</guid>
      <description>&lt;p&gt;I like to write about and discuss Python implementations, designs, strategies, patterns, and alike. I don't intend to compose tutorials nor explain the basic concepts. Instead, I want to engage with you in fruitful discussions and explore more advanced topics. I currently have two post series here at DEV.to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://dev.to/joaomcteixeira/series/14163"&gt;"quick-reads"&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/joaomcteixeira/series/14162"&gt;"long-reads"&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the first series, I comment on Pythonic tips, tricks, or behaviours I encounter in daily projects. I dedicate the second series to long, highly contextualized discussions and conceptualizations on specific topics - most likely software architecture. I don't write in any defined order; I post as things come to mind.&lt;/p&gt;

&lt;p&gt;I used to post on another blog - &lt;a href="https://pythonicthoughtssnippets.github.io/"&gt;Pythonic Thoughts Snippets&lt;/a&gt; -, but I am migrating my programming writing activity entirely here to DEV.to.&lt;/p&gt;

</description>
      <category>python</category>
      <category>blog</category>
      <category>writing</category>
      <category>about</category>
    </item>
    <item>
      <title>Time needed to search in a list or set in Python</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Wed, 18 Aug 2021 11:10:31 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/time-needed-to-search-in-a-list-or-set-in-python-2if6</link>
      <guid>https://dev.to/joaomcteixeira/time-needed-to-search-in-a-list-or-set-in-python-2if6</guid>
      <description>&lt;p&gt;The time it takes to search an item in a &lt;code&gt;list&lt;/code&gt; depends on the size of the list and the position of the item in it. While for &lt;code&gt;set&lt;/code&gt; the time cost is maintained. Here, we do not consider the time needed to create the lists and sets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;l100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;s100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;l100k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100_000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;s100k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100_000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;895 ns ± 31.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;25.8 ns ± 1.56 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="mi"&gt;100_000&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l100k&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;874 µs ± 22.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="mi"&gt;100_000&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s100k&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;25.6 ns ± 0.812 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l100k&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;33.5 ns ± 0.589 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;%%&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s100k&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;26.4 ns ± 1.13 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)&lt;/p&gt;

</description>
      <category>python</category>
      <category>performance</category>
    </item>
    <item>
      <title>Python Singletons</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Tue, 23 Feb 2021 18:51:40 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/python-singletons-2p8</link>
      <guid>https://dev.to/joaomcteixeira/python-singletons-2p8</guid>
      <description>&lt;p&gt;A long time has passed since I last wrote a Singleton. Definitively, we rarely write singleton patterns in Python because we can represent singletons as variables in a module, and import them everywhere - see my strategy for &lt;a href="https://dev.to/joaomcteixeira/setting-up-python-logging-for-a-library-app-6ml"&gt;logging library wide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, how should we construct a singleton class which represents a state that is initialized (loaded/read) together with the class first instantiation and for which we don't want the whole loading process to be repeated in case the singleton class is instantiated again at a different stage of the runtime?&lt;/p&gt;

&lt;p&gt;Well, here it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'State already exists, returning existing state.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt;

        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;


&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;  &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enjoy and Cheers,&lt;/p&gt;

</description>
      <category>python</category>
      <category>oop</category>
      <category>architecture</category>
      <category>singleton</category>
    </item>
    <item>
      <title>Fibonacci without recursiveness in Python - a better way</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Mon, 25 Jan 2021 15:16:39 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/fibonacci-without-recursiveness-in-python-a-better-way-4hm</link>
      <guid>https://dev.to/joaomcteixeira/fibonacci-without-recursiveness-in-python-a-better-way-4hm</guid>
      <description>&lt;p&gt;&lt;strong&gt;¡Short post!&lt;/strong&gt; (not so short after the edit)&lt;/p&gt;

&lt;p&gt;Some time ago we had a great discussion in a post &lt;em&gt;(see also comments)&lt;/em&gt; about &lt;a href="https://dev.to/imjoseangel/from-100-to-0-cpu-with-memoization-2pca"&gt;From 100% to 0% CPU with memoization&lt;/a&gt; applied to Fibonacci series.&lt;/p&gt;

&lt;p&gt;From that post comes the idea that explaining recursive functions using the Fibonacci example might fail the very purpose of Python in the first place.&lt;/p&gt;

&lt;p&gt;What about solving the Fibonacci problem this way?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EDIT 20 Feb 2021&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The original &lt;code&gt;for&lt;/code&gt; loop was &lt;code&gt;for i in range(2, i -1):&lt;/code&gt;, I corrected it to the following bellow. Thanks to &lt;a class="mentioned-user" href="https://dev.to/paddy3118"&gt;@paddy3118&lt;/a&gt; for spotting that repetition of &lt;code&gt;i&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# first definitions
&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;  &lt;span class="c1"&gt;# number of elements in the final Fibonacci series
&lt;/span&gt;&lt;span class="n"&gt;fib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# fixed seed
&lt;/span&gt;&lt;span class="n"&gt;fiba&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;  &lt;span class="c1"&gt;# short cut to the append method
&lt;/span&gt;
&lt;span class="c1"&gt;# Calculate the Fibonacci series
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;fiba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/paddy3118"&gt;@paddy3118&lt;/a&gt; in his/her comment also suggested the use of numpy to store the values and just fill the results in the indexes. Obviously, that requires using &lt;code&gt;numpy&lt;/code&gt; and that might not be an option, however, if it is, let's investigate how much time it costs to use that approach:&lt;/p&gt;

&lt;p&gt;This is how I understood &lt;a class="mentioned-user" href="https://dev.to/paddy3118"&gt;@paddy3118&lt;/a&gt; comment, please Paddy correct me I am wrong.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="n"&gt;fib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my machine with Jupyter &lt;code&gt;%%timeit&lt;/code&gt; I receive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The slowest run took 5.29 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 5: 23 µs per loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From which: &lt;code&gt;100000 loops, best of 5: 2.42 µs per loop&lt;/code&gt; refer to the array creation and &lt;code&gt;10000 loops, best of 5: 21.5 µs per loop&lt;/code&gt; to the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;While if I &lt;code&gt;%%timeit&lt;/code&gt; the approach with lists, we have for the &lt;strong&gt;whole process&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100000 loops, best of 5: 4.66 µs per loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actually, it is much faster to perform this operation with lists. Maybe contrarily to expectations?&lt;/p&gt;

&lt;p&gt;Is that what you were referring to &lt;a class="mentioned-user" href="https://dev.to/paddy3118"&gt;@paddy3118&lt;/a&gt;? Thanks for your comment, I enjoy it a lot!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second EDIT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On a later discussion with a friend, we saw that if the list grows considerably, the &lt;code&gt;append&lt;/code&gt; version becomes much slower than the version where the list is defined beforehand (see example below). Hence, if the number &lt;code&gt;i&lt;/code&gt; is known, and that number is large, the version presented next is much faster.&lt;/p&gt;

&lt;p&gt;This latter version avoids the list being (internally) copied over to larger allocated memory blocks as new values are &lt;code&gt;append&lt;/code&gt;ed. So, depending on the size of the target lists you expect to produce and on the performance requirements, you could choose one or the other. I believe this last version is more in line with what &lt;a class="mentioned-user" href="https://dev.to/paddy3118"&gt;@paddy3118&lt;/a&gt; was referring to before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50_000&lt;/span&gt;
&lt;span class="n"&gt;fib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Runs in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 loops, best of 5: 4.1 ms per loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the first version with &lt;code&gt;i = 50_000&lt;/code&gt; runs in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 loops, best of 5: 68.8 ms per loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I started this post to discuss that the Fib series might not be a good example to explain recursion. We ended up finding a very nice situation where Fib can (maybe) serve as a more adequate example. I will look forward to writing a small post on how lists work internally in Python and explain the behavior here observed.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

</description>
      <category>python</category>
      <category>memoization</category>
      <category>codequality</category>
      <category>recursive</category>
    </item>
    <item>
      <title>Python Project Template</title>
      <dc:creator>João M.C. Teixeira</dc:creator>
      <pubDate>Sat, 23 Jan 2021 02:12:08 +0000</pubDate>
      <link>https://dev.to/joaomcteixeira/python-project-template-4fi2</link>
      <guid>https://dev.to/joaomcteixeira/python-project-template-4fi2</guid>
      <description>&lt;p&gt;I have updated my &lt;strong&gt;Python Project Template&lt;/strong&gt; repository. This is a fully working template where you will find configured continuous integration services with GitHub actions, Codecov, tox, ReadTheDocs, and others.&lt;/p&gt;

&lt;p&gt;You can use this template as a reference to configure your own CI protocol or as an actual GitHub template for your repository.&lt;/p&gt;

&lt;p&gt;I also discuss many aspects on how to structure a Python application/library.&lt;/p&gt;

&lt;p&gt;Let me know your thoughts,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/joaomcteixeira/python-project-skeleton"&gt;https://github.com/joaomcteixeira/python-project-skeleton&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>ci</category>
      <category>github</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
