<?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: suvhotta</title>
    <description>The latest articles on DEV Community by suvhotta (@suvhotta).</description>
    <link>https://dev.to/suvhotta</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%2F321888%2F0b1a3237-5460-4870-b386-f1e946483be3.jpg</url>
      <title>DEV Community: suvhotta</title>
      <link>https://dev.to/suvhotta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suvhotta"/>
    <language>en</language>
    <item>
      <title>Understanding Object Copying in Python</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Fri, 17 Nov 2023 11:54:53 +0000</pubDate>
      <link>https://dev.to/suvhotta/understanding-object-copying-in-python-19k1</link>
      <guid>https://dev.to/suvhotta/understanding-object-copying-in-python-19k1</guid>
      <description>&lt;h2&gt;
  
  
  My Initial Struggle with List Copying
&lt;/h2&gt;

&lt;p&gt;When I began my programming journey in Python, I encountered a scenario that many new programmers face: copying a list and passing it to a function for further modification. Here's the approach I initially used and why it didn't work as expected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
def square_nums(nums_list):
  ...

original_list = [1, 2, 3, 4, 5]

copied_list = original_list

squared_nums = square_nums(copied_list)

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

&lt;/div&gt;



&lt;p&gt;However, I soon realized a critical issue: any modifications made to copied_list within the function were also reflected in original_list. This was puzzling and led me to delve deeper into Python's documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Revelation: Aliases vs. Actual Copies
&lt;/h2&gt;

&lt;p&gt;Upon reading the official documentation, I figured out that the way I was copying the contents of the original_list was fundamentally flawed. In fact, as per Python semantics, I wasn't creating a copy, instead I was just creating an &lt;em&gt;alias&lt;/em&gt; using the &lt;em&gt;assignment operator&lt;/em&gt;. Any number of aliases we create, they will all point to the same object. That's why when the object itself was getting modified, I was getting altered values in the &lt;em&gt;original_list&lt;/em&gt; in my code.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eJTUBbIu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxxv93von9bfslt22q92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eJTUBbIu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxxv93von9bfslt22q92.png" alt="Alias creation in python" width="493" height="367"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Correct Approach to Copying in Python
&lt;/h2&gt;

&lt;p&gt;Python provides 2 ways of doing so:&lt;br&gt;
1) Shallow Copy&lt;br&gt;
2) Deep Copy&lt;br&gt;
Both of these are available in the &lt;a href="https://docs.python.org/3/library/copy.html"&gt;copy&lt;/a&gt; module provided as part of Python's standard library.&lt;/p&gt;
&lt;h3&gt;
  
  
  Shallow Copy
&lt;/h3&gt;

&lt;p&gt;In shallow copy, a new Container/Object is created but the containing items are still a reference to the original Container/Object.&lt;/p&gt;

&lt;p&gt;This can be further verified by checking the memory of the containers and their individual items using the &lt;code&gt;id()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;This way of copying works best if we've immutable items.&lt;/p&gt;

&lt;p&gt;This is achieved by using the &lt;code&gt;copy.copy()&lt;/code&gt; function of the copy module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZcsuTBWr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuefuxbtd3j4vp3rubyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZcsuTBWr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuefuxbtd3j4vp3rubyo.png" alt="Shallow Copy" width="586" height="293"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import copy

original_list = [1, 2, 3]

# Creates a shallow copy
copied_list = copy.copy(original_list)

# Will print True
print(id(original_list[0]) == id(copied_list[0]))

original_list[0] = 0

# Will print False
print(id(original_list[0]) == id(copied_list[0]))

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

&lt;/div&gt;



&lt;p&gt;In the above code, the last print statement will still be &lt;code&gt;False&lt;/code&gt; because integers are immutable. So once &lt;code&gt;original_list[0]&lt;/code&gt; is re-assigned a new value, the reference is updated to the new integer value i.e. &lt;code&gt;0&lt;/code&gt;. But &lt;code&gt;copied_list[0]&lt;/code&gt; continues to reference the original integer object 1. This behaviour would've been different if &lt;code&gt;original_list&lt;/code&gt; contained mutable objects (like other lists, dictionaries, etc.), where a change in the mutable object inside &lt;code&gt;original_list&lt;/code&gt; would be reflected in &lt;code&gt;original_list&lt;/code&gt; as well due to the shared references.&lt;/p&gt;

&lt;p&gt;Due to potential issues that could arise when mutable objects are involved, copy module has another offering: deepcopy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deep copy
&lt;/h3&gt;

&lt;p&gt;In shallow copy, a new Container/Object is created and it recursively inserts copies of the objects(It doesn't always copy but depending on the type of the object) found in the original.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import copy

original_list = [1, 2, 3, [2,3,4]]

copied_list = copy.deepcopy(original_list)

# Will print False
print(id(original_list)==id(copied_list))

# Will print True
print(id(original_list[0])==id(copied_list[0]))

# Will print True
print(id(original_list[1])==id(copied_list[1]))

# Will print True
print(id(original_list[2])==id(copied_list[2]))

# Will print False
print(id(original_list[3])==id(copied_list[3]))

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

&lt;/div&gt;



&lt;p&gt;In the code above, we see an interesting thing i.e. when the objects are immutable like those at indexes 0, 1, 2 in &lt;code&gt;original_list&lt;/code&gt; then those in &lt;code&gt;copied_list&lt;/code&gt; also reference to the same object. But when the element is a mutable one like a &lt;code&gt;list&lt;/code&gt; then a separate copy is maintained in the &lt;code&gt;copied_list&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ed9b9KyK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y5lz8bijhixns405apz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ed9b9KyK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y5lz8bijhixns405apz.png" alt="Deepcopy" width="601" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Role of the memo Dictionary in Deepcopy
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoiding Redundant Copies:&lt;br&gt;
When an object is copied using deepcopy, the memo dictionary keeps track of objects that have already been copied in that copy cycle. This is particularly important for complex objects that may refer to the same sub-object multiple times. Without memo, each reference to the same sub-object would result in a new and separate copy, potentially leading to an inefficient duplication of objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Handling Recursive Structures: &lt;br&gt;
Recursive structures are objects that, directly or indirectly, refer to themselves. For example, a list that contains a reference to itself. deepcopy uses the memo dictionary to keep track of objects that have already been encountered in the copying process. If it encounters an object that’s already in the memo dictionary, it uses the existing copy from the memo rather than entering an infinite loop trying to copy the recursive reference.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation in User defined Classes
&lt;/h2&gt;

&lt;p&gt;In order to implement the functionalities of copy and deepcopy, we can use the magic methods &lt;strong&gt;copy&lt;/strong&gt;() and &lt;strong&gt;deepcopy&lt;/strong&gt;() within the class.&lt;br&gt;
Internally even the &lt;code&gt;list&lt;/code&gt; data structure is a class and that's how we get the &lt;code&gt;list.copy()&lt;/code&gt; feature.&lt;/p&gt;

&lt;p&gt;With this deeper insight into Python's object copying, you're now better prepared to handle the nuances of mutable and immutable objects in your coding adventures. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Multi-Threading in Python: Deep-dive</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Sat, 19 Mar 2022 15:54:11 +0000</pubDate>
      <link>https://dev.to/suvhotta/multi-threading-in-python-deep-dive-4pic</link>
      <guid>https://dev.to/suvhotta/multi-threading-in-python-deep-dive-4pic</guid>
      <description>&lt;p&gt;One universal truth about cPython: &lt;strong&gt;&lt;em&gt;One thread runs python, while 'N' others sleep or wait for I/O&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above holds true thanks to the Global Interpreter Lock (GIL). I won't dig deep into GIL here as that would be taking us off track from the current discussion. &lt;br&gt;
So if anybody is telling u that 2 or more threads can simultaneously do something with your cPython program, then its time to call their bluff. But I don't deny that Python isn't a multi-threaded programming language, its just that the multiple threads can't access your program at a time. Then comes the logical question, what's the use of such a multi-threading then? We'll be getting to the answer of that in a bit.&lt;/p&gt;

&lt;p&gt;Python supports 2 kinds of Multi-Tasking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Co-operative&lt;/li&gt;
&lt;li&gt;Preemptive&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Co-operative Multi-Tasking in Python:
&lt;/h2&gt;

&lt;p&gt;When Python knows that its trying to do some which might take  some time like performing some I/O or network operation, it voluntarily drops the GIL. In other words, the Python thread knows that it won't be doing any python work in a while and &lt;strong&gt;&lt;em&gt;co-operates&lt;/em&gt;&lt;/strong&gt; with other threads by voluntarily submitting the GIL access.&lt;/p&gt;

&lt;p&gt;Further explained through the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import socket

messages = []

def socket_connect():
    # This is an example of cooperative multi-tasking
    s = socket.socket()
    messages.append('connecting')
    s.connect(('python.org', 80))
    # Python thread drops the GIL while doing the socket connect operation.
    messages.append('connected')

threads = []

for i in range(3):
    t = threading.Thread(target=socket_connect)
    threads.append(t)
    t.start()

for t in threads:
    t.join()

print('\n'.join(messages))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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;connecting
connecting
connecting
connected
connected
connected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So why do we get 3 'connecting' strings followed by 3 'connected' strings instead of 3 pairs of 'connecting connected'?&lt;/p&gt;

&lt;p&gt;The answer lies in the cooperative multi-tasking performed by Python thread.&lt;/p&gt;

&lt;p&gt;Let's dig a bit deeper into what happens when we write the following line of code:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;s.connect(('python.org', 80))&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Internally it calls the following &lt;strong&gt;&lt;em&gt;internal_connect&lt;/em&gt;&lt;/strong&gt; function written in C: (&lt;a href="https://github.com/python/cpython/blob/main/Modules/socketmodule.c"&gt;https://github.com/python/cpython/blob/main/Modules/socketmodule.c&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Py_BEGIN_ALLOW_THREADS
res = connect(s-&amp;gt;sock_fd, addr, addrlen);
Py_END_ALLOW_THREADS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above follows a common paradigm of how GIL is released and re-acquired:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save the thread state in a local variable.&lt;/li&gt;
&lt;li&gt;Release the global interpreter lock.&lt;/li&gt;
&lt;li&gt;Do some blocking I/O operation.&lt;/li&gt;
&lt;li&gt;Reacquire the global interpreter lock.&lt;/li&gt;
&lt;li&gt;Restore the thread state from the local variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Py_BEGIN_ALLOW_THREADS, and Py_END_ALLOW_THREADS are 2 macros&lt;br&gt;
used to simplify the above processes. &lt;/p&gt;

&lt;p&gt;So we can see that the GIL is released prior to establishing  connect method being called and the same is acquired by another thread which again releases the GIL at the same point in the code. So by the time the 3rd thread has released the GIL, the 1st thread might have been successfully established the socket connection. If so, then the 1st thread is now ready to re-acquire the GIL and continue executing python code.&lt;/p&gt;

&lt;p&gt;To see what difference it would make, if we didn't have the network operation, lets run the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import socket

messages = []

def calculate_squares():
    messages.append('calculating')
    for i in range(5):
        i*i
    messages.append('calculated')

threads = []

for i in range(3):
    t = threading.Thread(target=calculate_squares)
    threads.append(t)
    t.start()

for t in threads:
    t.join()

print('\n'.join(messages))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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;calculating
calculated
calculating
calculated
calculating
calculated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, there isn't any network operation involved, hence the GIL isn't released co-operatively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-emptive Multi-Tasking in Python:
&lt;/h2&gt;

&lt;p&gt;When some thread has held the GIL for a certain specific amount of time(Default being 5ms), the interpreter pitches in to force the current thread to drop the GIL and pass it on to some other thread for execution. Here, the verb &lt;strong&gt;&lt;em&gt;preempt&lt;/em&gt;&lt;/strong&gt; means to halt the execution of a task with a view to resuming it later.&lt;/p&gt;

&lt;p&gt;The same can be understood better in the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;messages = []

def run(thread_name):
    for i in range(50):
        for j in range(50):
            pass
        messages.append(f'Thread: {thread_name}')

threads = []

for i in range(2):
    t = threading.Thread(target=run, args=(i,))
    threads.append(t)
    t.start()

for t in threads:
    t.join()

print('\n'.join(messages))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above code will be mostly 50 occurrences of 'Thread: 0' followed by 50 occurrences of 'Thread: 1'. If you change however the ranges inside the run function to 5000, you'll notice that there will be some occurrences of 'Thread: 0' and somewhat similar number of occurrences of 'Thread:1'. So in short, the GIL is being switched pre-emptively as per some default set interval.&lt;/p&gt;

&lt;p&gt;The following code can tell us what the default interval for switching the GIL between threads is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sys

print(sys.getswitchinterval())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default for python3.10 it is 5ms (&lt;a href="https://github.com/python/cpython/blob/main/Python/ceval_gil.h"&gt;https://github.com/python/cpython/blob/main/Python/ceval_gil.h&lt;/a&gt;) &lt;br&gt;
However the same can be changed as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sys

sys.setswitchinterval(0.0001)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above sets the switch interval to 0.1 ms.&lt;/p&gt;

&lt;p&gt;Let's say that there are 2 threads: Thread A, and Thread B. If Thread A holds a lock and gets preempted, then maybe Thread B could run instead of Thread A.&lt;br&gt;
If Thread B is waiting for the lock that Thread A is holding, then Thread B is not waiting for the GIL. In that case Thread A reacquires the GIL immediately after dropping it, and Thread A continues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Purpose:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Purpose of Co-operative multi-tasking in python is to finish tasks faster, in case they're waiting for I/O.&lt;/li&gt;
&lt;li&gt;The purpose of pre-emptive multi-tasking isn't to go faster but rather to simulate parallelism. So instead of one thread running till the end, there can be many threads that appear to run parallel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inspired from: &lt;a href="https://opensource.com/article/17/4/grok-gil#:%7E:text=By%20default%20the%20check%20interval,of%20bytecodes%2C%20but%2015%20milliseconds"&gt;https://opensource.com/article/17/4/grok-gil#:~:text=By%20default%20the%20check%20interval,of%20bytecodes%2C%20but%2015%20milliseconds&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>AWS Exception: PolicyLengthExceededException</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Mon, 14 Mar 2022 09:36:48 +0000</pubDate>
      <link>https://dev.to/suvhotta/aws-exception-policylengthexceededexception-1l3a</link>
      <guid>https://dev.to/suvhotta/aws-exception-policylengthexceededexception-1l3a</guid>
      <description>&lt;p&gt;The other day we'd an issue at work, where we were getting a PolicyLengthExceededException for our lambda permissions, which we were attaching programmatically during the runtime, every-time we were creating a resource. This wasn't a recurring issue but rather an intermittent one.&lt;/p&gt;

&lt;p&gt;Upon further debugging, I came to know that the resource based policy for a lambda function is limited at 20KB. &lt;br&gt;
The policy we were creating was overshooting the limit by 20 bytes. It's not much, but we've to abide by the limits set. &lt;/p&gt;

&lt;p&gt;To my astonishment, the parameters being passed on to the policy were more or less same as compared to those from the other policies that we'd earlier attached to the same lambda. &lt;/p&gt;

&lt;p&gt;So I'd now 2 genuine concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If there was really any limit breach, this wouldn't have been an intermittent issue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How did the earlier policies get added?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Upon some further R&amp;amp;D, found out that the policy size limit wasn't something that was being checked very rigorously. Instead the same was only being checked once you'd a large chunk of policies with similar names, performing similar jobs and which could have otherwise been clubbed into one using wildcards.&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;Added an aws_lambda_permission component to our terraform script which creates the corresponding permission every-time the lambda function is created.&lt;/p&gt;

&lt;p&gt;Note that I've generalised the &lt;strong&gt;&lt;em&gt;source_arn&lt;/em&gt;&lt;/strong&gt; using wildcard so that we don't have to create a policy every-time we create a resource and thus solving the problem of too many duplicate/similar policies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
resource "aws_lambda_permission" "allow_event_bridge_rule_permission" {
  statement_id  = "AllowLambdaInvocationFromEventBridge"
  action        = "lambda:InvokeFunction"
  function_name = "${var.func_name}"
  principal     = "events.amazonaws.com"
  source_arn    = "arn:aws:events:${var.aws_region}:${var.aws_account_id}:rule/model_*_name"
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; Even after adding the above code to the tf script, still we were getting the same exception while executing the script. I removed all the existing duplicate policies and re-ran the script and it went fine.&lt;/p&gt;

&lt;p&gt;Even if you're manually creating the policies from the console/CLI its the best practice to use wildcards in the source_arn so as to minimise the number of policies attached to a resource.&lt;/p&gt;

&lt;p&gt;Hope this helps you!  &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>aws</category>
      <category>cloud</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Mocking using pytest</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Sat, 11 Sep 2021 16:19:21 +0000</pubDate>
      <link>https://dev.to/suvhotta/mocking-using-pytest-210</link>
      <guid>https://dev.to/suvhotta/mocking-using-pytest-210</guid>
      <description>&lt;p&gt;&lt;strong&gt;Pytest&lt;/strong&gt; is a unit test module in python.&lt;br&gt;
&lt;strong&gt;Mocking&lt;/strong&gt; is a way of mimicking the working of a service in &lt;br&gt;
a way to substitute for the real one.&lt;/p&gt;

&lt;p&gt;In this post I'll be focussing on mocking different types of methods present in a class. &lt;/p&gt;

&lt;p&gt;Class ABC has been defined in a python file python_class.py&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;ABC&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;call&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;static_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;class_method&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;instance_method&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;_ABC__private_instance_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_ABC__private_class_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_ABC__private_static_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;staticmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;static_method&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;"static_method"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;class_method&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"class_method"&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;instance_method&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="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"instance method"&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;__private_instance_method&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="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"private instance method"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__private_class_method&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"private class method"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;staticmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__private_static_method&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;"private static method"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yeah I know this isn't how class methods and static methods should be used, but that's not on today's agenda.&lt;/p&gt;

&lt;p&gt;In the call method, we can see that there are 6 other methods being called. That's where we need to &lt;strong&gt;mock the methods - the place where they're being called/being used&lt;/strong&gt; instead of where they've been defined.&lt;/p&gt;

&lt;p&gt;But before that we need to install 2 packages: pytest, pytest-mock&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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;unittest.mock&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;python_class&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ABC&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestPythonClass&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;test_all_methods&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;mocker&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;directory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"python_class.ABC"&lt;/span&gt;
        &lt;span class="n"&gt;instance_mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&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;directory&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.instance_method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;static_mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&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;directory&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.static_method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;class_method_mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&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;directory&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.class_method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;pvt_instance_mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&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;directory&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;._ABC__private_instance_method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;pvt_class_mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&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;directory&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;._ABC__private_class_method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;pvt_static_mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&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;directory&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;._ABC__private_static_method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

        &lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;instance_mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;static_mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;class_method_mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;pvt_instance_mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;pvt_class_mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;mocker&lt;/strong&gt; is a fixture that is shipped with the pytest-mock module. We can simply pass it on as an argument during the test method definition without importing.&lt;/p&gt;

&lt;p&gt;In this way I've mocked 6 different types of methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;instance method&lt;/li&gt;
&lt;li&gt;class method&lt;/li&gt;
&lt;li&gt;static method&lt;/li&gt;
&lt;li&gt;private instance method&lt;/li&gt;
&lt;li&gt;private class method&lt;/li&gt;
&lt;li&gt;private static method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lemme know in case I missed something or I need to add anything.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>unittest</category>
      <category>pytest</category>
    </item>
    <item>
      <title>Git For Beginners - Part 2</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Wed, 19 Aug 2020 19:13:36 +0000</pubDate>
      <link>https://dev.to/suvhotta/git-for-beginners-part-2-1nn8</link>
      <guid>https://dev.to/suvhotta/git-for-beginners-part-2-1nn8</guid>
      <description>&lt;p&gt;This series is aimed to simplify the concept of git for beginners. In the first Part we've discussed about the basics of different working areas, different scenarios involving adding and committing changes to our repo.&lt;/p&gt;

&lt;p&gt;We will continue with our approach of looking into various used cases, instead of just memorizing git commands.&lt;/p&gt;

&lt;p&gt;Before going into that there was something about the git objects that I'd missed in my earlier post. We saw how the commit object stores info about the previous commits, so every child commit has a pointer to the parent commit and thus a linkage is established. &lt;/p&gt;

&lt;p&gt;But can't this be the other way round? There could be a possibility that the parent commit stores pointer for the child commit. However this isn't something that is being followed because the git objects are immutable. So they can't be edited once they've been created. Hence any chance of parent storing child commit pointers get nullified. &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Branches:
&lt;/h2&gt;

&lt;p&gt;Branch is just a pointer to any commit. Branches are required to work in parallel towards multiple issues related to a project. The default branch that gets created with our repo is called &lt;strong&gt;master&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every time we commit, the master branch pointer moves forward automatically.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git branch &amp;lt;branch_name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command can be used to create a new branch.&lt;/p&gt;

&lt;p&gt;However the above command just created a new branch, but it won't automatically get switched to this branch.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout &amp;lt;branch_name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command checks out the newly created branch and we can begin our adding our files and commits to this branch.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout -b &amp;lt;branch_name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Most often than not, we want to create a branch and switch to it simultaneously i.e. combining the work done by the first and the second commands. &lt;/p&gt;

&lt;h2&gt;
  
  
  Merging Branches:
&lt;/h2&gt;

&lt;p&gt;Suppose we've created a new branch to work on any enhancement of the project. Once the development of the same is completed, we would want to merge it with our initial branch i.e. master.&lt;/p&gt;

&lt;p&gt;But before that we can check what all are the differences between the master and our new branch by the following command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git diff master..&amp;lt;new_branch&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;There are 2 types of merges namely &lt;strong&gt;Fast forward merge&lt;/strong&gt; and &lt;strong&gt;Three way merge&lt;/strong&gt;. Fast forward merge is when we try to merge one commit with a commit that can directly be reached by following the first commit's history, thus git directly moves the pointer forward as there isn't any diversion in the path of merging.&lt;/p&gt;

&lt;p&gt;In case of Three way merge, the development has diverged somewhere from some past commits in either of the branches and thus a Fast forward merge is no longer possible. In this case Git performs a three way merge using the two snapshots provided by the branch tips and common ancestor of the two branches. Instead of just moving the pointer ahead as was the case in fast forward merge, git creates a new snapshot (commit) and this is called a merge commit. &lt;/p&gt;

&lt;p&gt;The MERGE_HEAD pointer points to the head of the other branch. (The one getting merged)&lt;/p&gt;

&lt;p&gt;steps for merging:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checkout the branch where you wish to merge into:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;git checkout branch1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;merge the branch
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;git merge branch2&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Squash and merge:
&lt;/h2&gt;

&lt;p&gt;At times, there might be multiple commits in a branch but we wish to merge them as a single commit in our master, in such a case we opt to squash the commits.&lt;/p&gt;

&lt;p&gt;Will group all feature branch commits into one commit then append it in the front of the master branch. Will add an extra dummy commit.&lt;/p&gt;

&lt;p&gt;steps for squash and merge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checkout the branch where you wish to merge:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;git checkout branch1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;merge the other branch
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;git merge --squash new_branch&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Squashing doesn't delete the original commits that are squashed to form the new commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Re-base:
&lt;/h2&gt;

&lt;p&gt;The strategy used so far to merge branches works absolutely fine. However the commit history is all messed up, esp. if there are multiple people working.&lt;/p&gt;

&lt;p&gt;To avoid this issue, there is a cleaner way to merge the branches.&lt;/p&gt;

&lt;p&gt;git re-base will append all commits history of the feature branch in the front of the master branch and won't add any extra dummy commit.&lt;/p&gt;

&lt;p&gt;git forms a copy of the feature branch on top of the master branch, hence the name re-base.&lt;br&gt;
steps:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout feature&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;git rebase master&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;this would rebase the feature branch on to the latest commits of the master branch. So, this sort of changes the commit history for the feature branch.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout master&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;git rebase feature&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Since git rebase changes the chronology of the commits, it isn't advisable to use it on commits which others depend on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oQX9gy5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/hUtiB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oQX9gy5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/hUtiB.png" alt="git merge rebase image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting Branches
&lt;/h2&gt;

&lt;p&gt;Most of the times after merging a branch, we don't need the branch anymore and that branch can be deleted.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git branch -d &amp;lt;branch_name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Git For Beginners - Part 1</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Mon, 10 Aug 2020 15:56:21 +0000</pubDate>
      <link>https://dev.to/suvhotta/git-for-beginners-part-1-j2</link>
      <guid>https://dev.to/suvhotta/git-for-beginners-part-1-j2</guid>
      <description>&lt;p&gt;I've watched numerous videos on how to use git as a version control system. But I used to forget the stuffs every once in a while. That's when I decided to jump into the git internals and learn git from a different aspect this time. I didn't memorize the git commands, rather studied extensively on various practical used cases of those commands. Surely such a mental mapping has helped me in learning the basics of git faster and retain them in a better way.&lt;/p&gt;

&lt;p&gt;Let the fun begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  Working Areas:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Current Working Directory:&lt;br&gt;
This refers to the physical area where we as developers actually write our code. It is here that we make our changes and expect our code to reach out to our colleague who might be working remotely thousands of KMs away.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Staging Area/Index:&lt;br&gt;
Staging area is the intermediate area where the files reside before getting saved into the repository. Staging area is the penultimate stage where modifications are still possible before data gets stored into the repository.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;.git repository:&lt;br&gt;
.git is the repo where our transferable code is stored. Whenever we clone a repo using git clone, it is this .git directory which actually gets downloaded as a local copy and development starts on top of this.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The basic Git workflow goes something like this:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You modify files in your working tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Starting a new Repo:
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;git init&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Inside any directory if we type the above commands then it would create a .git hidden directory and would track all the changes to the files and folders of the parent directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Staging our files:
&lt;/h2&gt;

&lt;p&gt;Suppose we initialized our directory and are about to add the first file to the staging area, this can be done by the following command for a single file:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add &amp;lt;filename&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;for multiple files to be staged:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add .&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now that we've added our first files to the staging area, let's see how git stores them under the hood.&lt;/p&gt;

&lt;p&gt;git actually stores objects in a key store format.&lt;/p&gt;

&lt;p&gt;For files, these are stored as blob(Binary Large Objects) SHA-1 key is generated by passing the contents of the file through the encryption algorithm and the 40-character checksum hash now corresponds to the file. &lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; There is no file name attached to the blob object.&lt;/p&gt;

&lt;p&gt;8f94139338f9404f26296befa88755fc2598c289 is an example of 40 digit checksum hash.&lt;/p&gt;

&lt;p&gt;Now you must be wondering how does the file name gets mapped on to the blob contents?&lt;/p&gt;

&lt;p&gt;This is done by another git object - tree.&lt;/p&gt;

&lt;p&gt;Tree is basically used to store the filenames as well as the directories. A single tree object contains one or more entries, each of which is the SHA-1 hash of a blob or sub-tree with its associated mode, type, and filename.&lt;/p&gt;

&lt;p&gt;100644 blob a906cb2a4a904a152e80877d4088654daad0c859      README&lt;br&gt;
100644 blob 8f94139338f9404f26296befa88755fc2598c289      Rakefile&lt;br&gt;
040000 tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0      lib&lt;/p&gt;

&lt;p&gt;The above is an example of a simple tree.&lt;/p&gt;

&lt;p&gt;Git normally creates a tree by taking the state of your staging area or index and writing a series of tree objects from it. So, to create a tree object, you first have to set up an index by staging some files.&lt;/p&gt;
&lt;h2&gt;
  
  
  Commit our files:
&lt;/h2&gt;

&lt;p&gt;Once we've staged our files in the staging area/index we can then commit those changes to the local Repo using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git commit -m "some sample message"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;if we wish to write multi-line commits, we can simply write:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git commit&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;and it would open a text editor to write the multi-line commits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Commits&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The format for a commit object is simple: it specifies the top-level tree for the snapshot of the project at that point; the parent commits if any (the commit object described above does not have any parents); the author/committer information (which uses your user.name and user.email configuration settings and a timestamp); a blank line, and then the commit message.&lt;/p&gt;
&lt;h2&gt;
  
  
  Checking the differences between various areas :
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;git diff&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;this command shows the differences between the staged files and their corresponding versions in the working directory. It however doesn't check for those files which haven't been added to the staging area.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git diff --staged&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;this command marks the contrast between the committed and the staged files.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git status&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Git status does 2 things:&lt;br&gt;
it compares the index file with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the current working directory changes are reported as Changes not staged for commit&lt;/li&gt;
&lt;li&gt;the HEAD commit — changes are reported as Changes to be committed&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Undoing any changes:
&lt;/h2&gt;

&lt;p&gt;If suppose we've made some changes which we later feel aren't of much use. Then we can undo them before actually committing.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git reset --hard HEAD&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This resets the file of the working directory and of the staging area to the commit pointed in HEAD.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Bringing back a version of file:
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;git checkout &amp;lt;file_name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command replaces the working directory version of the file with the prev version that has been staged.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout &amp;lt;commit_hash&amp;gt; --&amp;lt;file_name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command replaces the working directory and staged versions of the file with the prev version that has been committed.&lt;/p&gt;

&lt;p&gt;Please note that both the above commands, replace the current working directory content and this change can't be undone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Un-staging a file:
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;git rm --cached &amp;lt;filepath&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command removes the file from the staging area and keeps an untracked copy in the current working directory. If a commit is made now, it would remove that from the repo as well.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git reset HEAD &amp;lt;filepath&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;In the case where the file is already in the repo, it replaces the index version of the file with the one from repo (HEAD), effectively unstaging the modifications to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remove a file:
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;git rm &amp;lt;filepath&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The above command removes a file from both the working directory and staging area. &lt;br&gt;
You might be thinking that we can simply delete the file from our system and that would work equally good. But no that isn't the case, git rm removes the file from both working directory and staging area while the rm command of operating system only deletes that from the working directory and we need to add the change again to get reflected in the staging area. Something kind of adding a negation to reflect the change.&lt;/p&gt;
&lt;h2&gt;
  
  
  Checking the commit graph:
&lt;/h2&gt;

&lt;p&gt;git log provides us a log of all the commits that have been made to the repo. However to get a better representation use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git log --all --decorate --oneline --graph&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;There is a shortcut that I use to remember the above command - git log &lt;strong&gt;A DOG&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Under the hood : Python Dictionary</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Fri, 17 Jul 2020 16:28:46 +0000</pubDate>
      <link>https://dev.to/suvhotta/under-the-hood-python-dictionary-4j8f</link>
      <guid>https://dev.to/suvhotta/under-the-hood-python-dictionary-4j8f</guid>
      <description>&lt;p&gt;Dictionary has always been a developer's go to tool because of the unique relation between the key and value pairs. So whenever there is any need of capturing frequencies, unique objects etc. in any algorithm, dictionary is the first thing that pops up to our head.&lt;/p&gt;

&lt;p&gt;Let's dig deep into how python dictionary works under the hood.&lt;/p&gt;

&lt;p&gt;To explain it better I'll try to relate it with a phone-book.&lt;/p&gt;

&lt;p&gt;Dictionary is implemented by using a data structure - hash table. The hash table again is a combination of two arrays -  one for storing the indexes and the other for storing the key, values.&lt;/p&gt;


&lt;center&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r-Hk50vQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d33wubrfki0l68.cloudfront.net/3d138e7af1264833715e47967d2dbfa8c696fb7e/ee634/images/hash-tables/hash-table-cpython-structure-high-level.svg"&gt;&lt;/center&gt;
&lt;h2&gt;
  
  
  Adding a new key:
&lt;/h2&gt;

&lt;p&gt;Initially a hash-table is always provided with a size of 8, which increases with the increase in num of keys.&lt;/p&gt;

&lt;p&gt;To calculate the index of the key, a hash function is used. However if we calculate the hash of 'a' in a 64 bit system, it would return a value of 12416037344. So to avoid creation of a hash-table with such a gigantic size, a mask is used in a bit-wise &amp;amp; operation along with the hash result.&lt;/p&gt;

&lt;p&gt;For a hash table of size 'n' the mask used is (2^n - 1).&lt;br&gt;
This ensures that for the key, the index is never exceeding the size of the existing hash table.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Conflict:&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Let's take our phone-book example and think of a hash table with a size of 26 each for each letter of the alphabet. &lt;/p&gt;

&lt;p&gt;Let's save a contact by the name 'Ajay' and the hashing algorithm assigns it to the index of 5. &lt;/p&gt;

&lt;p&gt;After a few month, a new intern 'Ashok' joins the company, and we've to save his number. The algorithm again assigns the same index 5 to Ashok, but Ajay's contact info is already present there in key, value form.&lt;/p&gt;

&lt;p&gt;This kind of a situation is called a &lt;em&gt;conflict.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let's think of different ways to resolve this conflict. One approach would be to use linked lists at each index of the hash table and the each individual nodes of the linked list represent a key, value pair. Such an approach would be a good one when we have a limited amount of data, however if the data size is huge, then we won't be able to offer a read operation within O[1]. &lt;/p&gt;

&lt;p&gt;The solution that CPython uses for conflict resolution is &lt;em&gt;re-sizing.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Resizing:&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Whenever a new key is added to a dict, CPython checks if the 2/3&lt;sup&gt;rd&lt;/sup&gt; of the existing size is filled or not. This ratio is called the &lt;em&gt;&lt;strong&gt;load factor&lt;/strong&gt;&lt;/em&gt;, and it is an indication of whether re-sizing is required or not. If the load factor is 2/3 then hash table needs a re-sizing&lt;/p&gt;

&lt;p&gt;How much new space is to be added is calculated by the num_of_occupied spaces * 3. However the size should be a power of 2, hence the next highest power of 2 is chosen.&lt;/p&gt;

&lt;p&gt;Initaly it starts with a size of 8, let's say 6 out of 8 spaces get occupied, then it should allocate a further more of 18 (6*3) but it increases the total size of the hash table to 32.&lt;/p&gt;

&lt;p&gt;Accordingly the indexes for the hash table are adjusted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting a key:
&lt;/h2&gt;

&lt;p&gt;Similarly when an element is deleted or say multiple elements are deleted, the hash-table size undergoes a shrinkage. However delete item operation doesn’t trigger an array resize. If the number of used slots is much less that the total number of slots. However, when a key/value pair is added, the need for resize is based on the number of used slots + dummy slots so it can shrink the array too.&lt;/p&gt;

&lt;p&gt;This was an overview on the processes happening during the lifetime of a python dictionary. For more details on various edge cases and optimizations, I suggest you reading the source code.&lt;/p&gt;

&lt;p&gt;Hope it was informative for you!&lt;/p&gt;

</description>
      <category>python</category>
      <category>advanced</category>
      <category>datastructure</category>
      <category>dictionary</category>
    </item>
    <item>
      <title>How Javascript works</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Fri, 27 Mar 2020 11:04:05 +0000</pubDate>
      <link>https://dev.to/suvhotta/how-javascript-works-19cd</link>
      <guid>https://dev.to/suvhotta/how-javascript-works-19cd</guid>
      <description>&lt;p&gt;Javascript is a single threaded language, at least the majority of it. Things are executed in a certain order. But at times it does work in a non-blocking manner which sort of gives it an asynchronous appearance. But how does the Javascript engine handle all of the script, Browser APIs everything at once ? I can't do all of it at once, even I got like 10 hands.&lt;/p&gt;

&lt;p&gt;Before explaining how things work I'll give a briefing of different parts involved in running Javascript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://camo.githubusercontent.com/2f12852c45cf6c9f9f7c3f03c07c063a83b7ede0/68747470733a2f2f626c6f672d6173736574732e726973696e67737461636b2e636f6d2f323031362f31302f7468652d4e6f64652d6a732d6576656e742d6c6f6f702e706e67" class="article-body-image-wrapper"&gt;&lt;img src="https://camo.githubusercontent.com/2f12852c45cf6c9f9f7c3f03c07c063a83b7ede0/68747470733a2f2f626c6f672d6173736574732e726973696e67737461636b2e636f6d2f323031362f31302f7468652d4e6f64652d6a732d6576656e742d6c6f6f702e706e67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Call Stack:
&lt;/h3&gt;

&lt;p&gt;Javascript is single threaded, it means it has one call stack. Call Stack is a data structure which keeps track of function calls in our program. When ever we call a function for its execution, we are pushing it to the stack. It is popped out of the stack when the execution is completed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9d9o26nV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://discourse-user-assets.s3.amazonaws.com/original/3X/c/c/cc0f31516a9120bee3c21d48e6e20e3b1b83ec44.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9d9o26nV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://discourse-user-assets.s3.amazonaws.com/original/3X/c/c/cc0f31516a9120bee3c21d48e6e20e3b1b83ec44.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Heap:
&lt;/h3&gt;

&lt;p&gt;This is where all the memory allocation happens for your variables, that you have defined in your program.&lt;/p&gt;

&lt;h1&gt;
  
  
  Tasks
&lt;/h1&gt;

&lt;p&gt;Tasks ensure that actions are happening sequentially. A task can be as simple as any function scheduled in the Javascript. Between the tasks, the browser may render updates. All the things mentioned here are generally &lt;em&gt;Macro Tasks&lt;/em&gt;. Callbacks of such tasks are usually dealt by a data structure called Task Queue.&lt;/p&gt;

&lt;p&gt;There is also something called &lt;em&gt;Micro Task&lt;/em&gt;. Micro Tasks are useful to make something async without taking the penalty of a whole new task. &lt;br&gt;
As per MDN:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A microtask is a short function which is executed after the function or program which created it exits and only if the JavaScript execution stack is empty, but before returning control to the event loop being used by the user agent to drive the script's execution environment. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With ES6, Promises have been introduced into Javascript, which along with Mutation Observer, use the microtask queue to run their callbacks.&lt;/p&gt;
&lt;h3&gt;
  
  
  Task Queue:
&lt;/h3&gt;

&lt;p&gt;Task Queue is a JavaScript run-time messaging queue which handles task that is allocated by different Web APIs. This queue is dedicated to handle the Web API callbacks. The messages are processed once the call stack is clear.&lt;/p&gt;
&lt;h3&gt;
  
  
  Microtask Queue:
&lt;/h3&gt;

&lt;p&gt;As mentioned above, Microtask queue handles callbacks with respect to Promises and Mutation Observer.&lt;/p&gt;
&lt;h3&gt;
  
  
  Event Loop:
&lt;/h3&gt;

&lt;p&gt;It has responsibility to see weather the call-stack is empty and does the task queue contains pending task to process. If the call-stack is empty, it will push the task to the call-stack from the queue and the task gets processed.&lt;/p&gt;

&lt;p&gt;Here is how the Event Loop handles different tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, each time a task exits, the event loop checks to see if the task is returning control to other JavaScript code. If not, it runs all of the microtasks in the microtask queue. The microtask queue is, then, processed multiple times per iteration of the event loop, including after handling events and other callbacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second, if a microtask adds more microtasks to the queue by calling queueMicrotask(), those newly-added microtasks execute before the next task is run. That's because the event loop will keep calling microtasks until there are none left in the queue, even if more keep getting added.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of it can be better illustrated by the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Time-out 1&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Time-out 2&lt;/span&gt;&lt;span class="dl"&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Promise 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Promise 2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;end&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output on Chrome would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start
end
Promise 1
Promise 2
Time-out 1
Time-out 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now why is that so?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Firstly, the two script jobs are scheduled on to the Call Stack as the first task. The logging start is picked up from the task queue and executed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set Timeout is handled by the Browser as it is a Browser window object. The timer and everything is calculated by the Browser as  waits for a given delay then schedules a new task for its callback in the Task Queue. This is same for both the Timeouts. So there are two Timeout tasks waiting in the Task queue. As logging script end is part of the first task, so setTimeout is logged in a separate task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After that, the Promises are run and their callbacks are pushed to the Microtask queue. They're pushed to the queue because the first task isn't yet completed and microtasks callbacks can't be run when one task is in mid-execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then the logging end as it is also part of the first task which is yet to be completed. Note that we have already two Promise callbacks pending in the microtask queue, thus as soon as there is no task in middle of completion, the event loop prioritizes the completion of the microtasks callbacks(over the timeouts) and pushes them to the Call Stack. This process continues until the Mirco Task queue is empty. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then the rest of the task queue is pushed to the Call Stack. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I Hope the above process was clear.&lt;/p&gt;

&lt;p&gt;However the output will vary from Browser to Browser. This is sort-of excusable, as promises come from ECMAScript rather than HTML. ECMAScript has the concept of "jobs" which are similar to microtasks, but the relationship isn't explicit aside from vague mailing list discussions. However, the general consensus is that promises should be part of the microtask queue, and for good reason.&lt;/p&gt;

&lt;p&gt;Before concluding this write-up, a brief intro on &lt;strong&gt;&lt;em&gt;Web-Workers&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
Web-workers help to handle the intensive tasks, which would have blocked the UI. Web-workers help in making the best use of multi-threading.&lt;/p&gt;

&lt;p&gt;But wait, Wasn’t Javascript a single-threaded language? The very first sentence of this writeup says so?&lt;/p&gt;

&lt;p&gt;Well, I stand true to my words. This is because, the Web workers aren’t part of the Javascript, rather they’re a built in features of the browsers which are being accessed through Javascript. Web Workers are in-browser threads that can be used to execute JavaScript code without blocking the event loop.&lt;/p&gt;

&lt;p&gt;Will be covering them in a separate article shortly.&lt;/p&gt;

&lt;p&gt;Till then Happy Quarantine, Stay Safe and Keep Learning!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
    <item>
      <title>Processes and Threads - Part 1</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Mon, 16 Mar 2020 04:48:48 +0000</pubDate>
      <link>https://dev.to/suvhotta/processes-and-threads-part-1-pm2</link>
      <guid>https://dev.to/suvhotta/processes-and-threads-part-1-pm2</guid>
      <description>&lt;p&gt;Ask anyone  &lt;strong&gt;What is the difference between a Process and a Thread?&lt;/strong&gt;. Most of them would first take a short pause and reply you "Ummmn they're actually the same thing."(Meanwhile deep-down there would be this inner battle to find out the exact answer)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N6xE01DZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media0.giphy.com/media/3oz8xZvvOZRmKay4xy/200.webp%3Fcid%3D790b7611907d0a581c184c5d688ed65f3bcd86aa16d2061e%26rid%3D200.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N6xE01DZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media0.giphy.com/media/3oz8xZvvOZRmKay4xy/200.webp%3Fcid%3D790b7611907d0a581c184c5d688ed65f3bcd86aa16d2061e%26rid%3D200.webp"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a beginner, I was also using both the terms interchangeably, till I realized it was a Cardinal Sin to do that. &lt;br&gt;
Hence decided to make a two part series on what both of them do in our OS.&lt;/p&gt;

&lt;h1&gt;
  
  
  Process
&lt;/h1&gt;

&lt;p&gt;Whenever any program is running on our computer, it can have one or multiple processes associated with it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Thread
&lt;/h1&gt;

&lt;p&gt;Thread can be thought of as the basic unit of execution of any process. Thus one process can consist of one or many threads.&lt;/p&gt;

&lt;p&gt;Threads within the same Process run within a shared memory space while different Processes are allocated separate memory spaces.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IIRseSE9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.perl.com/images/_pub_2002_09_04_threads/figure1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IIRseSE9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.perl.com/images/_pub_2002_09_04_threads/figure1.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Process Management and scheduling:
&lt;/h1&gt;

&lt;p&gt;When a process is being scheduled, it may appear due to the Multi-Programming aspect that we are able to switch between typing a document, simultaneously while playing some music. This is something that happens by the help of the time sharing. Objective of time sharing is to switch the CPU between the programs so fast that the users can interact with each program while it is running and it would feel as if everything is happening at the same time. But if we dig deep into it, its the CPU which is actually switching at a very high rate between all these programs, to give a real time feel and run all of them in parallel without any lag. &lt;/p&gt;

&lt;p&gt;This luxury isn't available in a &lt;em&gt;&lt;strong&gt;Single Processor System&lt;/strong&gt;&lt;/em&gt;. There will never be more than one running process in such systems.&lt;/p&gt;

&lt;p&gt;In a &lt;em&gt;&lt;strong&gt;Multi Processor System&lt;/strong&gt;&lt;/em&gt;, there is a process scheduler, which takes care of the end to end scheduling process of various programs. If there are multiple processes queued up, then the job of the process/job scheduler is to allot the CPU to the processes depending on the priority. If there is a low priority process running and the OS gets a command to run a higher priority task, then the lower priority one is pushed to the waiting queue and the CPU resources are prioritized towards the higher priority process in hand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0QQCixa---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.tutorialspoint.com/operating_system/images/queuing_diagram.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0QQCixa---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.tutorialspoint.com/operating_system/images/queuing_diagram.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, Kudos to your OS,arranging the execution of applications so that you believe that there are several things happening at once. This is complicated because the CPU can only do one thing at a time. Today's multi-core processors and multi-processor machines can handle more work, but each processor core is still capable of managing one task at a time.&lt;/p&gt;

&lt;p&gt;In order to give the appearance of lots of things happening at the same time, the operating system has to switch between different processes thousands of times a second. Here's how it happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A process occupies a certain amount of RAM. It also makes use of registers, stacks and queues within the CPU and operating-system memory space.&lt;/li&gt;
&lt;li&gt;When two processes are multi-tasking, the operating system allots a certain number of CPU execution cycles to one program.&lt;/li&gt;
&lt;li&gt;After that number of cycles, the operating system makes copies of all the registers, stacks and queues used by the processes, and notes the point at which the process paused in its execution.&lt;/li&gt;
&lt;li&gt;It then loads all the registers, stacks and queues used by the second process and allows it a certain number of CPU cycles.&lt;/li&gt;
&lt;li&gt;When those are complete, it makes copies of all the registers, stacks and queues used by the second program, and loads the first program.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Interprocess Communication:
&lt;/h1&gt;

&lt;p&gt;When there are several dependent processes which need to pass data among themselves, then there is a clear need for internal communication. This is done by the Inter process Communication.&lt;/p&gt;

&lt;p&gt;Two Methods are widely used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Shared-Memory:&lt;br&gt;
There would be a portion of memory shared by the processes and in that space, all the values to be passed on are written and communicated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Message-Passing:&lt;br&gt;
In the message passing model, the processes communicate with each other through messages.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1kwXTLzb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.w3schools.in/wp-content/uploads/2017/10/communications-models.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1kwXTLzb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.w3schools.in/wp-content/uploads/2017/10/communications-models.png" alt="Markdown Monster icon"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are also other options to distribute resources among different processes and help in inter-process communication - &lt;em&gt;Semaphores&lt;/em&gt;, &lt;em&gt;signals&lt;/em&gt; etc.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>beginners</category>
      <category>architecture</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>What is HTTP?</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Mon, 24 Feb 2020 15:25:28 +0000</pubDate>
      <link>https://dev.to/suvhotta/what-is-http-2508</link>
      <guid>https://dev.to/suvhotta/what-is-http-2508</guid>
      <description>&lt;p&gt;HTTP is &lt;strong&gt;H&lt;/strong&gt;yper &lt;strong&gt;T&lt;/strong&gt;ext &lt;strong&gt;T&lt;/strong&gt;ransfer &lt;strong&gt;P&lt;/strong&gt;rotocol which involves protocols involving communication between web servers and clients. It can be thought as the messenger of the web. Client is the one who makes the request(HTTP Message) and the server responds(HTTP Message) to that request.&lt;/p&gt;

&lt;p&gt;HTTP is state-less i.e. it doesn't remember anything about the previous page when we go from one webpage to another(Cookies and Sessions are additional stuffs, and aren't part of HTTP core). Each request can be thought of as a single transaction. &lt;/p&gt;

&lt;p&gt;HTTP is connection-less i.e. there is no fixed 24*7 connection between the client and the server, rather the connection happens with each request and response cycle. For further requests, there will be more new connections.&lt;/p&gt;

&lt;p&gt;A HTTP Message has 3 parts:&lt;br&gt;
&lt;em&gt;Start line&lt;/em&gt;, &lt;em&gt;Headers&lt;/em&gt; and &lt;em&gt;Body&lt;/em&gt;. The message body is optional&lt;br&gt;
The info present in the 3 section varies depending if that is a request or a response.&lt;/p&gt;

&lt;h3&gt;
  
  
  For Request:
&lt;/h3&gt;

&lt;p&gt;startline has the method name telling the server what to do, URL and the version number. &lt;br&gt;
Headers can be thought as a few key-value pairs like host, accept-language etc.&lt;br&gt;
A message body is the one which carries actual HTTP request data (including form data and uploaded etc.) &lt;/p&gt;

&lt;h3&gt;
  
  
  For Response:
&lt;/h3&gt;

&lt;p&gt;No method is required in the startline of a response. However it contains the http version and the status code.&lt;br&gt;
A message body may contain HTTP response data from the server ( including files, images etc)&lt;/p&gt;

&lt;h2&gt;
  
  
  Some methods used in HTTP:
&lt;/h2&gt;

&lt;p&gt;Method is a command instructing the server what to do.Method names are always in uppercase.&lt;br&gt;
&lt;strong&gt;GET:&lt;/strong&gt; Every time we visit a webpage, we make a GET request to the server via HTTP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;POST:&lt;/strong&gt; When adding some data to the server like - submitting a form,creating a blog post, uploading an image. In a POST request, we send data to the server which is then stored in a database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PUT:&lt;/strong&gt; PUT request is used to update some already existing data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DELETE:&lt;/strong&gt; DELETE request deletes data from the server.&lt;/p&gt;

&lt;p&gt;Each Request and Response has a header and a body. &lt;br&gt;
The Response body is the html or json or whatever is being sent from the server end.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP STATUS CODES:
&lt;/h2&gt;

&lt;p&gt;As the name refers to, these are codes referring to different statuses.&lt;br&gt;
It has a range from 100 to 500.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;1XX : Informational&lt;/em&gt;&lt;br&gt;
Request received/processing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2XX : Success&lt;/em&gt;&lt;br&gt;
Successfully received, understood and accepted.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;3XX : Redirect&lt;/em&gt;&lt;br&gt;
Further action/Redirect.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;4XX : Client Error&lt;/em&gt;&lt;br&gt;
Request doesn't have what it needs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;5XX : Server Error&lt;/em&gt;&lt;br&gt;
Server couldn't fulfill some valid request.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>basics</category>
      <category>internet</category>
    </item>
    <item>
      <title>How does the Internet Work?</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Mon, 24 Feb 2020 11:31:38 +0000</pubDate>
      <link>https://dev.to/suvhotta/how-does-the-internet-work-dfa</link>
      <guid>https://dev.to/suvhotta/how-does-the-internet-work-dfa</guid>
      <description>&lt;p&gt;Let's deep dive into a journey of what happens in the backend when a client(user) accesses the internet and how the server renders the data.&lt;/p&gt;

&lt;p&gt;Whatever data we fetch over the internet are stored in some server inside a data center. Server can be thought of as a computer whose job is to provide us the content whenever requested.&lt;/p&gt;

&lt;p&gt;Now you must be pondering as how this data is then transmitted from the data-centers located at different corners of the world to the users requesting them. One solution is to use Satellite communication, but since the distance involved is high, it would lead to users facing severe latency. So one more solution is to use a wired medium - one of the best wired medium is infact used to move the data from the data-centers to the users, &lt;em&gt;drumrolls&lt;/em&gt; &lt;strong&gt;'Optical Fibers'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Optical fibers are a form of wired media, where in the data is transmitted in the forms of bits(zeros and ones) as light. This complex network of optical fibers can be generalized as the &lt;strong&gt;&lt;em&gt;Internet&lt;/em&gt;&lt;/strong&gt;. These optical fibers are scattered and placed throughout the world,covering all kinds of terrains.So from this we can figure out that we as clients are not usually directly connected to the internet, rather indirectly through an Internet service provider, while the datacenters' servers are directly connected to the internet. &lt;/p&gt;

&lt;h3&gt;
  
  
  How does the optical fibers identify the sender and receiver of the data?
&lt;/h3&gt;

&lt;p&gt;This is done by a bunch of unique number and dot combinations known as IP Address which forms the shipping address in the Internet world. IP Address can be analogous to a home address but the stark contrast being that it is mostly a fixed value and depends on the Internet Service Provider(ISP) or the network where our system is connected. These IP Addresses can be static or dynamic. Most of the IP Addresses assigned to devices of the end users are dynamic in nature. For getting a static IP, some premium is to be paid. If you want your device use the same IP Address both at home as well as when you visit your grandma in the country-side having a different ISP, it would cost you a hefty sum.&lt;/p&gt;

&lt;p&gt;Static IP Address is mostly used for dedicated Web,Database or other servers and also for accurate geological tracking purposes.&lt;/p&gt;

&lt;p&gt;But now that the problem of identifying the sender receiver has been resolved, we come across a new issue- how do we ip addresses. The solution is pretty straight forward - needn't remember them at all, use domain names like &lt;em&gt;facebook.com&lt;/em&gt;,&lt;em&gt;google.com&lt;/em&gt;,&lt;em&gt;youtube.com&lt;/em&gt; instead, which correspond to IP Addresses.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does a domain name gets converted into an IP Address?
&lt;/h3&gt;

&lt;p&gt;For this conversion, a phone directory like stuff called DNS is used. The DNS Server provides the ip address corresponding to the domain name that the user enters in the client computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summarizing the process:
&lt;/h2&gt;

&lt;p&gt;User enters domain name in browser &lt;strong&gt;-&amp;gt;&lt;/strong&gt; browser looks up for the ip address corresponding to that domain name in the DNS Server &lt;strong&gt;-&amp;gt;&lt;/strong&gt; this request is then forwarded to the data-center, or rather the respective server in the data-center &lt;strong&gt;-&amp;gt;&lt;/strong&gt; Once the request reaches the server, data starts flowing out of it in the form of light-pulses through optical fibers(in the form of well defined packets) &lt;strong&gt;-&amp;gt;&lt;/strong&gt; The router converts this light signals to EM signals and our laptop/mobile phones receive them.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>internet</category>
      <category>basics</category>
    </item>
    <item>
      <title>Python: Logging Overview</title>
      <dc:creator>suvhotta</dc:creator>
      <pubDate>Thu, 13 Feb 2020 11:21:59 +0000</pubDate>
      <link>https://dev.to/suvhotta/python-logging-overview-nbl</link>
      <guid>https://dev.to/suvhotta/python-logging-overview-nbl</guid>
      <description>&lt;p&gt;The first question popping up with every developer noob is my print function is working perfectly fine, and why the hell do I need a &lt;em&gt;log file&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;Well your 50 line code's debugging can be easily managed by the simplest of all functions - print() but when it comes to debugging those 1000 lines of code-work, most of which is a result of many sleepless night efforts, surely you need something more than print() to suffice your needs. &lt;strong&gt;Drum-rolls&lt;/strong&gt;: enter &lt;strong&gt;Log Files&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Python has an inbuilt logging module that fulfills all the logging needs of any program.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Things First:
&lt;/h2&gt;

&lt;p&gt;There are basically 5 log levels in Python associated with numeric values which are incremented in multiples of 10.&lt;br&gt;
Critical(50) - Highest Priority which needs immediate attention.&lt;br&gt;
Error(40)    - Minute errors which can be resolved and program can re-run.&lt;br&gt;
Warning(30)  - Note of caution like space issues.&lt;br&gt;
Info(20)     - Confirmation that something is working as expected.&lt;br&gt;
Debug(10)    - Detailed information, mostly used while debugging any issue.&lt;/p&gt;

&lt;h2&gt;
  
  
  logging.basicConfig():
&lt;/h2&gt;

&lt;p&gt;The function takes a keyword of arguments as parameters like filename, level,format.&lt;br&gt;
The default level in logging is Warning or 30, however that can be changed as passing the level as:&lt;br&gt;
level=logging.DEBUG or level=logging.INFO &lt;br&gt;
The format would specify what format of message do we want to store in the logfile.&lt;/p&gt;

&lt;p&gt;The Default logger is the root logger.&lt;/p&gt;

&lt;p&gt;However when a new file is imported and it has its own logger, the root logger of the parent file gets replaced by that of the child logger and all the logging info for the parent file gets lost.&lt;/p&gt;

&lt;p&gt;This problem can be resolved by creating separate loggers for each file and then configuring them according to our needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Logger:
&lt;/h2&gt;

&lt;p&gt;logger = logging.getLogger(&lt;strong&gt;name&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;After creating a logger, we need to create a filehandler.&lt;br&gt;
fileHandler = logging.FileHandler('employee.log')&lt;/p&gt;

&lt;p&gt;logger.addHandler(fileHandler)&lt;/p&gt;

&lt;p&gt;sample formatting can be added to the filehandler and can be set by passing on the formatter parameter to fileHandler.setFormatter()&lt;/p&gt;

&lt;p&gt;There are also handlers like StreamHandler,NullHandler etc.&lt;/p&gt;

</description>
      <category>python</category>
      <category>oop</category>
      <category>beginners</category>
      <category>logging</category>
    </item>
  </channel>
</rss>
