<?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: hppranaav02</title>
    <description>The latest articles on DEV Community by hppranaav02 (@hppranaav02).</description>
    <link>https://dev.to/hppranaav02</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%2F1645396%2F2fcda8f2-ad25-47a2-aa74-100cbb6a7aa5.png</url>
      <title>DEV Community: hppranaav02</title>
      <link>https://dev.to/hppranaav02</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hppranaav02"/>
    <language>en</language>
    <item>
      <title>Summer coding projects</title>
      <dc:creator>hppranaav02</dc:creator>
      <pubDate>Tue, 18 Jun 2024 12:39:38 +0000</pubDate>
      <link>https://dev.to/hppranaav02/summer-coding-projects-2iko</link>
      <guid>https://dev.to/hppranaav02/summer-coding-projects-2iko</guid>
      <description>&lt;p&gt;As summer break approaches, I wondered how I can learn something new everyday and build something interesting along the way. This is when I came across &lt;a href="//codingchallenges.fyi"&gt;coding challenges&lt;/a&gt;, which is a set of application building challenges. &lt;/p&gt;

&lt;p&gt;Through this, I aim to learn a new language and/or polish my skills on the ones I know. I will update this post with the links to posts pertaining to individual challenges along the way!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/hppranaav02/wc-tool-in-python-3lke"&gt;wc tool&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>wc tool in python</title>
      <dc:creator>hppranaav02</dc:creator>
      <pubDate>Tue, 18 Jun 2024 12:39:24 +0000</pubDate>
      <link>https://dev.to/hppranaav02/wc-tool-in-python-3lke</link>
      <guid>https://dev.to/hppranaav02/wc-tool-in-python-3lke</guid>
      <description>&lt;h1&gt;
  
  
  WC Tool
&lt;/h1&gt;

&lt;p&gt;This is an implementation of the &lt;code&gt;wc&lt;/code&gt; command present in unix systems, in python. The &lt;code&gt;wc&lt;/code&gt; command is used to count the number of bytes, words, and lines in a file. It can also be used to count the number of charaters in a file based on the locale settings. The input to the command can also be a pipe from the standard input such as from the &lt;code&gt;cat&lt;/code&gt; command.&lt;br&gt;
\newline&lt;br&gt;
The &lt;code&gt;wc&lt;/code&gt; command can be used in the following way:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

python3 ccwc.py  &lt;span class="o"&gt;[&lt;/span&gt;OPTION]... &lt;span class="o"&gt;[&lt;/span&gt;FILE]...


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

&lt;/div&gt;

&lt;p&gt;The options that can be used with the &lt;code&gt;wc&lt;/code&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-c&lt;/code&gt; : This option is used to count the number of bytes in a file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-m&lt;/code&gt; : This option is used to count the number of characters in a file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-l&lt;/code&gt; : This option is used to count the number of lines in a file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-w&lt;/code&gt; : This option is used to count the number of words in a file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;wc&lt;/code&gt; command can be used with multiple files as well. In that case, the &lt;code&gt;wc&lt;/code&gt; command will display the total number of bytes, words, and lines in all the files. If no option is provided, the &lt;code&gt;wc&lt;/code&gt; command will display the number of bytes, words, and lines in the file along with the file name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learnings
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Handling command line arguments
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First was to parse command line arguments in python. You can make use of &lt;code&gt;sys.argv&lt;/code&gt; parameter, which holds the list of command line arguments with sys.argv[0] being the script name, followed by others. Another, and more convinient method is to use the &lt;code&gt;argparse&lt;/code&gt; module. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;argparse&lt;/code&gt; gives us better control on what, which type and actions on the command line arguments the script accepts. It also provides Exception handling for misuse or incorrect command line arguments.&lt;br&gt;
In my case, I had to provide just a single input parameter, which is the filename or standard input, and a some flags which determine the output(what is done with the file). For this reason, i went with the below config&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9r5zfypme1fb57udf8kb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9r5zfypme1fb57udf8kb.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The args are mentioned in the order they are expected and the type of input the accept. For the flags, I have set the action to set value to &lt;code&gt;True&lt;/code&gt; is it is included. When the arguments are parsed, it is available as a list of tuples.&lt;br&gt;
The flag values can be accessed as &lt;code&gt;args.c&lt;/code&gt; as value associated is implicitly a Boolean. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For the file name argument, it gets a bit tricky as we also need to accept standard input through a pipe (&lt;code&gt;&amp;lt;stdin&amp;gt;&lt;/code&gt;), hence we cannot just acccept a string type.&lt;br&gt;
For this, we accept arguments of type &lt;code&gt;argparse.FileType('r')&lt;/code&gt;. This allows you to pass an argument of both a filename which will be loaded in buffer as a &lt;code&gt;_io.TextIOWrapper&lt;/code&gt; object (simillar to opening a file with the &lt;code&gt;open()&lt;/code&gt; method), and accept standard input pipes straight into the object buffer. we provide the default as &lt;code&gt;&amp;lt;stdin&amp;gt;&lt;/code&gt;.&lt;br&gt;
We supply the number of args attribute as &lt;code&gt;nargs='?'&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Custom Print
&lt;/h3&gt;

&lt;p&gt;The output to be shown required multiple print statements on the same line. Here I used the &lt;code&gt;end=&lt;/code&gt; attribute in the print statement to describe a custom ending for every print. By default, it is &lt;code&gt;end='\n'&lt;/code&gt;, which is why every print results in a newline.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/library/stdtypes.html#str.splitlines" rel="noopener noreferrer"&gt;splitlines()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/30686701/python-get-size-of-string-in-bytes" rel="noopener noreferrer"&gt;Size of string in bytes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/library/io.html#io.TextIOBase" rel="noopener noreferrer"&gt;_io.IOBase module docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/library/argparse.html" rel="noopener noreferrer"&gt;argparse docs&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/hppranaav02/wc-tool" rel="noopener noreferrer"&gt;Github link&lt;/a&gt;
&lt;/h3&gt;

</description>
    </item>
  </channel>
</rss>
