<?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: Vighnesh SK</title>
    <description>The latest articles on DEV Community by Vighnesh SK (@booterror).</description>
    <link>https://dev.to/booterror</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%2F99066%2F62e21dc5-d5dd-471f-8731-99395e0f58d6.jpg</url>
      <title>DEV Community: Vighnesh SK</title>
      <link>https://dev.to/booterror</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/booterror"/>
    <language>en</language>
    <item>
      <title>Python Decorators for Error handling</title>
      <dc:creator>Vighnesh SK</dc:creator>
      <pubDate>Mon, 15 Oct 2018 09:23:31 +0000</pubDate>
      <link>https://dev.to/booterror/python-decorators-for-error-handling-50eb</link>
      <guid>https://dev.to/booterror/python-decorators-for-error-handling-50eb</guid>
      <description>&lt;p&gt;So I had a little python script that scraps a certain website for product data. A couple of functions that fetches data, parses for one type of content, another type of data. &lt;/p&gt;

&lt;p&gt;Now I had this issue where the css selectors returns None which breaks all my sneaky list indices. &lt;/p&gt;

&lt;p&gt;Currently my program looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;get_x_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

&lt;span class="c1"&gt;# piece of code that works all the time
&lt;/span&gt;
&lt;span class="c1"&gt;# piece of code that crashes
&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Applying &lt;code&gt;try and except&lt;/code&gt; to the piece of code that crashes wasn't quite convincing because of the tedious task of doing this to all of the probable volatile code. And I wasn't quite sure of what part might crash. &lt;/p&gt;

&lt;p&gt;Hence, &lt;strong&gt;Python Decorators&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A syntactic sugar for wrapping functions with a reusable code. &lt;/p&gt;

&lt;p&gt;Learn about it &lt;a href="https://realpython.com/primer-on-python-decorators/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is how I used decorators error handle the functions that throws errors most of the time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;safe_run&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;func_wrapper&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;try&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;func&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;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;func_wrapper&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now apply &lt;code&gt;safe_run&lt;/code&gt; as a decorator to the function declarations in the main code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;safe_run&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_x_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="o"&gt;....&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Quick terrible fix that works :)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>decorators</category>
      <category>errors</category>
    </item>
  </channel>
</rss>
