<?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: Josua Blejeru </title>
    <description>The latest articles on DEV Community by Josua Blejeru  (@josuablejeru).</description>
    <link>https://dev.to/josuablejeru</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%2F351043%2Ff9fdb47a-97b2-449c-a695-67cac23069fe.jpeg</url>
      <title>DEV Community: Josua Blejeru </title>
      <link>https://dev.to/josuablejeru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/josuablejeru"/>
    <language>en</language>
    <item>
      <title>Enhance your Python scripts</title>
      <dc:creator>Josua Blejeru </dc:creator>
      <pubDate>Sun, 15 Mar 2020 17:42:35 +0000</pubDate>
      <link>https://dev.to/josuablejeru/enhance-your-python-workflow-in-the-cli-3eeh</link>
      <guid>https://dev.to/josuablejeru/enhance-your-python-workflow-in-the-cli-3eeh</guid>
      <description>&lt;p&gt;Maybe you remember some commands from documentations like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew upgrade &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; brew update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yea, you see right. This command will execute &lt;code&gt;brew upgrade&lt;/code&gt; and &lt;code&gt;brew update&lt;/code&gt;&lt;br&gt;
one after another. But did you know you can do something similar to get the same result?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew upgrade&lt;span class="p"&gt;;&lt;/span&gt; brew update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between them is simple and has to do with status codes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Statuscodes and how can I use them?
&lt;/h2&gt;

&lt;p&gt;Here is a example:&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;# script.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doSomething&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;i&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;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&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="s"&gt;"done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;exit&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"ok ok... I get it... but wait!" Exactly. &lt;code&gt;sys.exit(0)&lt;/code&gt; is used to exit a program and returns a status code of 0.&lt;/p&gt;

&lt;p&gt;Try to run this command in you command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python script.py &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"finish"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing happend...right?&lt;/p&gt;

&lt;p&gt;Now change &lt;code&gt;sys.exit(0)&lt;/code&gt; with &lt;code&gt;sys.exit(1)&lt;/code&gt; and rerun it.&lt;/p&gt;

&lt;p&gt;This is the point where you will expect to see "finish" after the "4" but &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; is preventing the execution because of the status code of 1.&lt;/p&gt;

&lt;p&gt;You see... status codes can help controling your workflow in the CLI.&lt;/p&gt;

&lt;p&gt;Maybe you don`t care how your program is ending, so try this instead:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;bash&lt;br&gt;
python script.py; echo "finish"&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The semicolumn is also used to execute one command after another, but our semicolumn don`t care about the status of the last executed program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I think at this point the difference should be clear...&lt;/p&gt;

&lt;p&gt;Both operators can help you preventing the executions of commands if something went wrong in your execution. Sometimes you need to know how your program has exit and sometimes you wish code get executed anyway... you decide!&lt;/p&gt;

&lt;p&gt;If it was usefull for you please share it with a friend and leave a comment! Thanks!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>beginners</category>
      <category>bash</category>
    </item>
  </channel>
</rss>
