<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://vallisrisista.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://vallisrisista.github.io/" rel="alternate" type="text/html" /><updated>2026-10-11T05:01:06+00:00</updated><id>https://vallisrisista.github.io/feed.xml</id><title type="html">AI ML Bites</title><subtitle>AI, Machine Learning,Python, Java, Backend Development </subtitle><author><name>Vallisri Sista</name></author><entry><title type="html">OS Module in Python</title><link href="https://vallisrisista.github.io/python/2026/08/19/os_python.html" rel="alternate" type="text/html" title="OS Module in Python" /><published>2026-08-19T00:00:00+00:00</published><updated>2026-08-19T00:00:00+00:00</updated><id>https://vallisrisista.github.io/python/2026/08/19/os_python</id><content type="html" xml:base="https://vallisrisista.github.io/python/2026/08/19/os_python.html"><![CDATA[<p>If you have worked with unix, you might be aware of commands like mkdir,chmod,pwd, etc to perform operations on files and directories.
We can perform those operations in python using “os” module.
Below are few functions that can be performed using os.</p>

<table>
  <thead>
    <tr>
      <th style="text-align: left">method</th>
      <th style="text-align: center">functionality</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align: left">os.getcwd()</td>
      <td style="text-align: center">returns current working directory</td>
    </tr>
    <tr>
      <td style="text-align: left">os.mkdir(path)</td>
      <td style="text-align: center">creates a directory</td>
    </tr>
    <tr>
      <td style="text-align: left">os.chdir(path)</td>
      <td style="text-align: center">to change to a different path</td>
    </tr>
    <tr>
      <td style="text-align: left">os.listdir(path)</td>
      <td style="text-align: center">lists file and directories in the file path</td>
    </tr>
    <tr>
      <td style="text-align: left">os.remove(file)</td>
      <td style="text-align: center">deletes the file. Does NOT delete a directory. If given a dir, it will return OSError</td>
    </tr>
    <tr>
      <td style="text-align: left">os.rmdir(path)</td>
      <td style="text-align: center">removes an empty directory.</td>
    </tr>
    <tr>
      <td style="text-align: left">os.chmod(file,permissions)</td>
      <td style="text-align: center">changes the file permissions as specified</td>
    </tr>
    <tr>
      <td style="text-align: left">os.chown(file,user_id,group_id)</td>
      <td style="text-align: center">changes the ownership of the file as specified</td>
    </tr>
  </tbody>
</table>

<p>OS has a submodule ‘path’ which offers few functions to verify or modify path.
These as useful while loading the required files from our directory.</p>

<table>
  <tbody>
    <tr>
      <td>os.path.exisits(file_path)</td>
      <td>checks is a file exists, return true/false</td>
    </tr>
    <tr>
      <td>os.path.getsize(filename)</td>
      <td>returns size of the file</td>
    </tr>
    <tr>
      <td>os.path.join(*paths)</td>
      <td>joins the given paths to form a single string, generally used when we have to append a filename to a path</td>
    </tr>
    <tr>
      <td>os.path.getmtime(file)</td>
      <td>returns last modification time stamp</td>
    </tr>
  </tbody>
</table>

<p>Os has open(),close(),read(),write(). These methods return file descriptors instead of file objects.
It is generally recommended to use built in open(), etc, for file operations and only go for os.open , etc when we need low level operations to be performed.</p>

<p>os.environ[] is another important function which is generally used to store environment variables, like API keys. It stores them as a dictionary.</p>]]></content><author><name>Vallisri Sista</name></author><category term="Python" /><category term="python" /><category term="OS Module" /><summary type="html"><![CDATA[If you have worked with unix, you might be aware of commands like mkdir,chmod,pwd, etc to perform operations on files and directories. We can perform those operations in python using “os” module. Below are few functions that can be performed using os.]]></summary></entry><entry><title type="html">Decorators in Python</title><link href="https://vallisrisista.github.io/python/2026/07/02/decorators_python.html" rel="alternate" type="text/html" title="Decorators in Python" /><published>2026-07-02T00:00:00+00:00</published><updated>2026-07-02T00:00:00+00:00</updated><id>https://vallisrisista.github.io/python/2026/07/02/decorators_python</id><content type="html" xml:base="https://vallisrisista.github.io/python/2026/07/02/decorators_python.html"><![CDATA[<p>Let’s say you run a bakery. You prepare cakes on order.
You are asked for a Vanilla cake. You bake it and decorate it with whipping cream.
You are asked for a butterscotch cake. You bake it and decorate it with whipping cream.
You are asked for a chocolate cake. You bake it and decorate it with whipping cream.</p>

<p>Now, you bake each cake separately as the procedure is different for each of them. But the decor? Since it is the same whipping cream, you don’t prepare it every time you get a cake order. You keep the prepared whipping cream and get it to decorate every time you bake a cake. 
This is exactly what a decorator in python can do!</p>

<p>You have a function, you want to enhance its behaviour but not change the function itself - you use a decorator.</p>

<p>A decorator is also a function which accepts another function and returns by wrapping it with some enhancement.</p>

<p>Here is a sample code for the above process.</p>

<p>Each function below specifically has process to bake that particular cake.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">vanilla_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking vanilla cake"</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">chocolate_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking chocolate cake"</span><span class="p">)</span>    
    
<span class="k">def</span> <span class="nf">butterscotch_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking butterscotch cake”)
</span></code></pre></div></div>

<p>Now, we want to decorate it with whipping cream.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">whipping_cream</span><span class="p">():</span>        
	<span class="k">print</span><span class="p">(</span><span class="s">"spread whipping cream on the given cake"</span><span class="p">)</span>
</code></pre></div></div>

<p>One way of doing this without decorators, is by just calling the whipping_cream function inside each cake.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">vanilla_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking vanilla cake”)
    whipping_cream()

def chocolate_cake():
    print("</span><span class="n">Baking</span> <span class="n">chocolate</span> <span class="n">cake</span><span class="s">")   
    whipping_cream() 
    
def butterscotch_cake():
    print("</span><span class="n">Baking</span> <span class="n">butterscotch</span> <span class="n">cake</span><span class="err">”</span><span class="p">)</span>
    <span class="n">whipping_cream</span><span class="p">()</span>
</code></pre></div></div>

<p>But, here we are changing the actual function to enhance it. To d it with out touching the function from inside, we use decorators.</p>

<p>In a decorator, we accept the actual function and wrap it with our enhancement and return it.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">whipping_cream</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">wrap</span><span class="p">():</span>
        <span class="n">func</span><span class="p">()</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"spread whipping cream on the given cake"</span><span class="p">)</span>
     
    <span class="k">return</span> <span class="n">wrap</span>
</code></pre></div></div>

<p>To decorate a function. With it, we simply use @ annotation while defining the function.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">@</span><span class="n">whipping_cream</span>
<span class="k">def</span> <span class="nf">vanilla_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking vanilla cake"</span><span class="p">)</span>

<span class="o">@</span><span class="n">whipping_cream</span>
<span class="k">def</span> <span class="nf">chocolate_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking chocolate cake"</span><span class="p">)</span>    
    
<span class="o">@</span><span class="n">whipping_cream</span>
<span class="k">def</span> <span class="nf">butterscotch_cake</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking butterscotch cake”)

vanilla_cake()
chocolate_cake()
butterscotch_cake()
</span></code></pre></div></div>
<p><strong><em>Output:</em></strong>
Baking vanilla cake
spread whipping cream on the given cake
Baking chocolate cake
spread whipping cream on the given cake
Baking butterscotch cake
spread whipping cream on the given cake</p>

<p>We can pass arguments to a decorator.</p>

<p>Here is code where the functions accept the weight of cake in grams as argument for the function.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">whipping_cream</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">g</span><span class="p">):</span>
        <span class="n">func</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"spread whipping cream on "</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="s">" grams"</span><span class="p">,</span> <span class="s">" cake"</span><span class="p">)</span>
        
    <span class="k">return</span> <span class="n">wrap</span>


<span class="o">@</span><span class="n">whipping_cream</span>
<span class="k">def</span> <span class="nf">vanilla_cake</span><span class="p">(</span><span class="n">g</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Weight="</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="s">" grams"</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking vanilla cake"</span><span class="p">)</span>

<span class="o">@</span><span class="n">whipping_cream</span>
<span class="k">def</span> <span class="nf">chocolate_cake</span><span class="p">(</span><span class="n">g</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking chocolate cake"</span><span class="p">)</span>    
    
<span class="o">@</span><span class="n">whipping_cream</span>
<span class="k">def</span> <span class="nf">butterscotch_cake</span><span class="p">(</span><span class="n">g</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Baking butterscotch cake"</span><span class="p">)</span>



<span class="n">vanilla_cake</span><span class="p">(</span><span class="mi">500</span><span class="p">)</span>
<span class="n">chocolate_cake</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="n">butterscotch_cake</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>   
</code></pre></div></div>

<p><strong><em>Output:</em></strong></p>
<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Weight= 500  grams
Baking vanilla cake
spread whipping cream on  500  grams  cake
Baking chocolate cake
spread whipping cream on  1000  grams  cake
Baking butterscotch cake
spread whipping cream on  1000  grams  cake
</code></pre></div></div>]]></content><author><name>Vallisri Sista</name></author><category term="Python" /><category term="python" /><category term="decorators" /><summary type="html"><![CDATA[Let’s say you run a bakery. You prepare cakes on order. You are asked for a Vanilla cake. You bake it and decorate it with whipping cream. You are asked for a butterscotch cake. You bake it and decorate it with whipping cream. You are asked for a chocolate cake. You bake it and decorate it with whipping cream.]]></summary></entry></feed>