<?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: Manisha Naidu</title>
    <description>The latest articles on DEV Community by Manisha Naidu (@manishanaidu).</description>
    <link>https://dev.to/manishanaidu</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%2F263742%2F8d33748e-9dc2-4688-97e2-dab960486617.jpg</url>
      <title>DEV Community: Manisha Naidu</title>
      <link>https://dev.to/manishanaidu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manishanaidu"/>
    <language>en</language>
    <item>
      <title>What is a Jenkinsfile? how to write one?</title>
      <dc:creator>Manisha Naidu</dc:creator>
      <pubDate>Tue, 19 Jul 2022 07:40:50 +0000</pubDate>
      <link>https://dev.to/manishanaidu/what-is-a-jenkinsfile-how-to-write-one-hec</link>
      <guid>https://dev.to/manishanaidu/what-is-a-jenkinsfile-how-to-write-one-hec</guid>
      <description>&lt;p&gt;If you know about Jenkins then I am sure you would know about Jenkins pipeline. It is a suite of tasks which are linked to each other and are executed to achieve continuous integration and delivery.&lt;/p&gt;

&lt;p&gt;And Jenkinsfile is the heart of this pipeline. It provides Jenkins pipeline a definition, with a text based file which contains everything that Jenkins pipeline needs to know for its execution.&lt;/p&gt;

&lt;p&gt;Jenkinsfile is not the only way to define a pipeline, you can also do it in UI. However, it is a best practice to do it using Jenkinsfile and check-in to your SCM.&lt;/p&gt;

&lt;p&gt;Here is the most basic pipeline to build, test and deploy a software,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipeline{
    Agent any
    stages{
        stage("build") {
            steps{
                echo"This is the build step"
            }
        }
        stage("test"){
            steps{
                echo"This is the test step"
            }
        }
        stage("deploy"){
            steps{
                echo"This is the deployment stage"
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mentioning agent is crucial, as it tells Jenkins to provide a host and workspace for execution.&lt;/p&gt;

&lt;p&gt;Steps is where you will mention all the commands that needs to be executed for each stage.&lt;/p&gt;

&lt;p&gt;As I said, this is a very basic pipeline and we can do much more than this in Jenkinsfile, such as, using environment variables to add conditions for stages/steps or even create your own variables.&lt;/p&gt;

&lt;p&gt;We can also run the stages in parallel instead of sequential to increase the performance of pipeline.&lt;/p&gt;

&lt;p&gt;I will be writing an in-depth article in a few days, which will cover all the essentials of Jenkinsfile and how we can use it to achieve CI-CD.&lt;/p&gt;

&lt;p&gt;Hope this gave you an overview to Jenkinsfile.&lt;/p&gt;

&lt;p&gt;Have a great day 🙂&lt;/p&gt;

&lt;p&gt;If you want to support my work,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/manishanaidu"&gt;https://www.buymeacoffee.com/manishanaidu&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jenkins</category>
      <category>cicd</category>
      <category>devops</category>
      <category>build</category>
    </item>
    <item>
      <title>Why Do We Need Design Principles?</title>
      <dc:creator>Manisha Naidu</dc:creator>
      <pubDate>Tue, 14 Sep 2021 09:59:01 +0000</pubDate>
      <link>https://dev.to/manishanaidu/why-do-we-need-design-principles-4lk9</link>
      <guid>https://dev.to/manishanaidu/why-do-we-need-design-principles-4lk9</guid>
      <description>&lt;p&gt;Are design principles really required while developing a software? Is it worth spending time on it instead of just focusing on making the code work?&lt;/p&gt;

&lt;p&gt;Today I would like to talk about design principles, what are these? why do we need it? and some examples of them. Because I believe it is highly underrated while building a software or even during code reviews. &lt;/p&gt;

&lt;p&gt;Now you might think that we have built some amazing programs, which works flawlessly in production and we did not waste time on following any design principles. &lt;/p&gt;

&lt;p&gt;And you are probably right, but guess what? such programs are not going to last long. Most of the software we build will be used for a long period of time and as the days goes by, the same software is going to evolve with new features every week, if not every day. Moreover, there are going to be multiple developers working on the same software and everyone has different styles of coding. So if the code is not maintainable, even a small change in code will have a ripple-effect elsewhere and it will be very hard to reuse any of it and fixing even a small bug will be expensive.&lt;/p&gt;

&lt;p&gt;So how do we make sure that our software is scalable and less prone to bugs in long run?&lt;/p&gt;

&lt;p&gt;Well, first and foremost step is to make our code robust, reusable and chances of failure to be reduced. This can be done by giving importance to the quality of code and not just to functionality or output of the code. However, at the speed at which new version software are being released, it becomes a tad bit challenging to spend time on code quality.&lt;/p&gt;

&lt;p&gt;Since not every developer needs to break their head to find new ways or standards which would best fit their software, there are already some treasured design principles out their, which are mostly suited for all types of software.&lt;/p&gt;

&lt;p&gt;Be noted that these design principles are not the actual implementation for the system, instead, it provides you a way to achieve quality code for your implementation which will help in the long run for,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Flexibility&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Reusability&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Cost efficient&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;And most important, easily changeable, because the only constant in the tech industry is, you know what I'm going to say next, the &lt;strong&gt;&lt;em&gt;CHANGE&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, there are multiple principles which you can choose from such as, &lt;/p&gt;

&lt;p&gt;DRY - Don't repeat yourself&lt;/p&gt;

&lt;p&gt;KISS - Keep it simple, stupid&lt;/p&gt;

&lt;p&gt;YAGNI - You ain't gonna need it&lt;/p&gt;

&lt;p&gt;We will be discussing SOLID principle, which is my favorite and covers almost everything you would ever need to use. Here is the description of the principle with some examples,&lt;/p&gt;

&lt;p&gt;S: Single responsibility ~ "A class should have only one reason to change. If there is any other reason create another class."&lt;/p&gt;

&lt;p&gt;While the definition is quite clear, it means that a class should have only one job. This principle focuses on coupling and cohesion. The classes should be very loosely coupled, whereas the relations inside a class should be tightly coupled.&lt;/p&gt;

&lt;p&gt;Eg. class Order, should deal with only the orders of an e-commerce site and not include billing functionality in it.&lt;/p&gt;

&lt;p&gt;O: Open-Closed Principle ~ "Entities like classes, modules, functions or objects should be open for extension but closed for modification."&lt;/p&gt;

&lt;p&gt;Once a class/function is built, it should only be "open" for fixing bugs or adding new functionality and "closed" for anything else.&lt;/p&gt;

&lt;p&gt;L: Liskov substitution states that "Derived class must be able to substitute its base class"&lt;/p&gt;

&lt;p&gt;When we have a class inherited from a base class, the inherited class can be used instead of its base class by any pointer or reference.&lt;/p&gt;

&lt;p&gt;Eg. If Customer class is derived from User class, then wherever we use User class, we should be able to use Customer class too.&lt;/p&gt;

&lt;p&gt;I: Interface segregation "Clients should not be forced to depend on methods in interfaces that they don’t use."&lt;/p&gt;

&lt;p&gt;This principle states that the client should not have to have a functionality in it which it does not use, but for some reason it still needs to be present in it.&lt;/p&gt;

&lt;p&gt;Eg. class Person is an abstract class which has methods such as, show_subjects, enroll_lectures, get_job, get_salary. If a class Employee is implementing the class Person, it will have to implement show_subjects and enroll_lectures even if it does not need it. Hence, class Person needs to be separated into two interfaces , one will have methods useful to Students and other to Employees.&lt;/p&gt;

&lt;p&gt;D: Dependency inversion states that "the high-level modules should not depend on low-level modules, it should rather depend on abstraction"&lt;/p&gt;

&lt;p&gt;This helps in reducing coupling between classes by adding an abstraction layer.&lt;/p&gt;

&lt;p&gt;Eg. class Teacher defining the methods of adding new lectures into the courses,&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;Teacher&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;__init__&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;english&lt;/span&gt; &lt;span class="o"&gt;=&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;spanish&lt;/span&gt; &lt;span class="o"&gt;=&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;addEnglishStd&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;engStd&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;english&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engstd&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;addSpanishStd&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;spanStd&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;spanish&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spanStd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;English&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&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;__init__&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="s"&gt;"english student enrolled"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Spanish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&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;__init__&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="s"&gt;"spanish student enrolled"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this approach, if another language to be added under a teacher, we have to make changes to complete Teacher class, which is against dependency inversion principle. To avoid this we can create a class Language,&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;Language&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;write&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Teacher&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;__init__&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;languages&lt;/span&gt; &lt;span class="o"&gt;=&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;addLanguage&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;language&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;languages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;language&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;English&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Language&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;speak&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="s"&gt;"Student to learn speaking english"&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;write&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="s"&gt;"Student to learn writing english"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Spanish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&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;speak&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="s"&gt;"Student to learn speaking spanish"&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;write&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="s"&gt;"Student to learn writing spanish"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if any new language can be added to Teacher without having to change the Teacher class.&lt;/p&gt;

&lt;p&gt;Python provides a module called “abc” for Abstract Base Class, which can be explored for creating such interfaces.&lt;/p&gt;

&lt;p&gt;Hope you gained some insights on design principles and would like to spend some time on it in your upcoming project, more articles on system design coming soon in this space.&lt;/p&gt;

&lt;p&gt;If you want to support my work, &lt;a href="https://www.buymeacoffee.com/manishanaidu"&gt;https://www.buymeacoffee.com/manishanaidu&lt;/a&gt;&lt;/p&gt;

</description>
      <category>designprinciples</category>
      <category>codequality</category>
      <category>programming</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Python/Django Developer Interview Questions and Answer (#1)</title>
      <dc:creator>Manisha Naidu</dc:creator>
      <pubDate>Mon, 03 May 2021 10:34:37 +0000</pubDate>
      <link>https://dev.to/manishanaidu/python-django-developer-interview-questions-and-answer-1-3l82</link>
      <guid>https://dev.to/manishanaidu/python-django-developer-interview-questions-and-answer-1-3l82</guid>
      <description>&lt;p&gt;Most of the software engineers are intimidated by technical rounds and I have recently started to give interviews. So, this time I am planning to write up all the questions I get asked in my technical rounds. Hope this will be helpful to some of you to get an idea on what type of questions are being asked. Use this series as a guide to prepare and ace that interview.&lt;/p&gt;

&lt;p&gt;Some questions might be specific to Python and Django but most of them will be applicable to any software engineering role and web application developers.&lt;/p&gt;

&lt;p&gt;This interview was for a Python developer role and they were looking for a candidate proficient in Django too, hence you will be seeing many questions related to Django architecture and design as well.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Let’s work on a recursive implementation of Fibonacci series.
&lt;/h4&gt;

&lt;p&gt;passed "n", return the fibonacci value at the nth position.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Fibonacci series are created in one of the two ways, iterative and recursive. Interviewer was looking for a recursive solution only.&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;def&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution worked fine for smaller numbers, &lt;br&gt;
n=10, took 0:00:00.001004sec&lt;br&gt;
n=35, took 0:00:05.157989sec which is too long.&lt;br&gt;
So next I was asked to optimize this.&lt;/p&gt;

&lt;p&gt;Ideal Solution: Using Dynamic programming, I am storing each value found in a dictionary, so that the next it looks for fibonacci of that number it can just look-up from "value_dict"&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;def&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# the first two values of fibonacci is always 0,1
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="c1"&gt;# if fibonacci(n) is already calculated then return it from dict 
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="c1"&gt;#else store it in dict and return next time it is called for.
&lt;/span&gt;    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt; 
        &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. What are decorators? Write a decorator that logs the functions arguments.
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;:&lt;br&gt;
Decorator is any function that takes in another function and adds functionality to it without manipulating the function itself. It does this by using a wrapper function.&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;def&lt;/span&gt; &lt;span class="nf"&gt;decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Logging the parameter of "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;@decorator is just a syntactic sugar, it is equivalent to &lt;br&gt;
&lt;code&gt;res = decorator(operation(5,20))&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  3. What GIL (global interpreter lock)? How does it allow multi-threading in python?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: GIL is a lock which makes sure python interpreter is held by only one thread at a time. That means only one thread can be in a execution status at any point of time. And this is why python is a single threaded programming language. This was introduced to solve the reference count problem which is used by python for garbage collection.&lt;/p&gt;

&lt;p&gt;Then how is multi-threading module working in python, you may ask!&lt;br&gt;
Well, this global interpreter lock applies only for CPU bound activities. So if there is any code which affects or uses CPU it automatically starts acting as a single threaded. But all the general programs can still work in a multi-threaded fashion.&lt;/p&gt;
&lt;h4&gt;
  
  
  4. What is WSGI and UWSGI?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;:&lt;br&gt;
Web server gateway interface, as the name suggests it is a interface between the application itself and web server. &lt;br&gt;
Django provides runserver for development server and for debugging, but for production we use WSGI in combination of any web servers like nginx, apache, or even uwsgi server.&lt;br&gt;
wsgi and uwsgi are both protocols, and these work with web servers to serve good performance application in production.&lt;/p&gt;

&lt;p&gt;So when client makes a request to django application, the request is read by nginx or any web server. Then nginx passes this request to the uwsgi service which intern passes it to concerned django app.&lt;/p&gt;
&lt;h4&gt;
  
  
  5. Can we write a custom queryset in dango? How?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;:&lt;br&gt;
Yes, we can create a custom queryset by customizing Managers. A Manager is the interface through which database query operations are provided to Django models. Each model contains atleast one Manager.&lt;br&gt;
We can override Managers base queryset by override Manager.get_queryset() method.&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;Employees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On running, &lt;code&gt;Employees.objects.all()&lt;/code&gt; it will return all the employees in database.&lt;br&gt;
Now, if we want to customize this to return only the employees from Toronto location then below can be done using Manager.&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;TorontoEmployeesManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Manager&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;get_queryset&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;return&lt;/span&gt; &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get_queryset&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Toronto"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="n"&gt;objects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Manager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="n"&gt;toronto_objects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TorontoEmployeesManager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;code&gt;Employees.toronto_objects.all()&lt;/code&gt; will return only the employees from Toronto location.&lt;/p&gt;

&lt;h4&gt;
  
  
  6. arr1 = list(range(10)); arr2 = arr1; arr3 = arr1[:]
&lt;/h4&gt;

&lt;p&gt;Is arr2 == arr3 ?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;:&lt;br&gt;
No, &lt;code&gt;arr2 = arr1&lt;/code&gt; creates a new reference of arr1 and assigns it to arr2. whereas, &lt;code&gt;arr3 = arr1[:]&lt;/code&gt; copies the content of arr1 and assigns it to new variable called arr3.&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="n"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr1&lt;/span&gt;
&lt;span class="n"&gt;arr3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;[:]&lt;/span&gt;
&lt;span class="n"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# [1,2,3,4,5]
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# [1,2,3,4]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  7. How to hide a class variable from getting accessed outside the class?
&lt;/h4&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;Sample&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;__init__&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a and b should be hidden from outside of class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&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;Sample&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;__init__&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;__a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="c1"&gt;# using double underscore hides class variable
&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;__b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  8. What are middlewares in Django? How do you create custom middleware.
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Middleware is a low-level plugin that can be used to hook into Django's request/response cycle. There are already middlewares available by default when a django project is created and it can found in settings.py.&lt;br&gt;
We can create a custom middleware and include it in the list of middleware with the existing ones.&lt;/p&gt;

&lt;p&gt;To create a custom middleware following structure needs to be followed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Middleware class should always consist __init__() and __call__()
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyCustomMiddleware&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;__init__&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;get_response&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;get_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt; &lt;span class="c1"&gt;#get_response is the view which will be called after this middleware/ or its the next middleware in the list.
&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="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
          &lt;span class="c1"&gt;# write the code to be executed before calling view here
&lt;/span&gt;
          &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&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;get_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#pass the request to view and get the response back, which will be returned.
&lt;/span&gt;
          &lt;span class="c1"&gt;# write the code to be executed after calling view here.
&lt;/span&gt;          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add this middleware into the "Middleware" section in settings.py. Also make sure the order of the middleware is correct. Because it is called from top to down while processing requests and then bottom to up while returning response.&lt;/p&gt;

&lt;p&gt;Hope this was helpful!&lt;/p&gt;

&lt;p&gt;If you want to support my work, &lt;a href="https://www.buymeacoffee.com/manishanaidu"&gt;https://www.buymeacoffee.com/manishanaidu&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Most important Python concepts for beginners to intermediate</title>
      <dc:creator>Manisha Naidu</dc:creator>
      <pubDate>Tue, 12 Jan 2021 10:58:22 +0000</pubDate>
      <link>https://dev.to/manishanaidu/most-important-python-concepts-for-beginners-to-intermediate-4pmo</link>
      <guid>https://dev.to/manishanaidu/most-important-python-concepts-for-beginners-to-intermediate-4pmo</guid>
      <description>&lt;p&gt;If you have 0-3 years of experience working in tech and you are going to apply for python developer role or if you are starting with python to build a project, then you must know some crucial concepts of the language. &lt;br&gt;
Below you will find the most important and frequently asked topics in python interviews.&lt;br&gt;
There are some amazing links to learn and master python at the end of this blog.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Different data structures in python

&lt;ul&gt;
&lt;li&gt;List&lt;/li&gt;
&lt;li&gt;Tuple&lt;/li&gt;
&lt;li&gt;Sets&lt;/li&gt;
&lt;li&gt;Dictionaries&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; Valid types of keys in dictionary and their properties.

&lt;ul&gt;
&lt;li&gt;Iterating a dict&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; Zip function&lt;/li&gt;
&lt;li&gt; Difference between arrays and list in python.&lt;/li&gt;
&lt;li&gt; String manipulation and slicing operation&lt;/li&gt;
&lt;li&gt; Regex&lt;/li&gt;
&lt;li&gt; List comprehension&lt;/li&gt;
&lt;li&gt; Lambda/anonymous functions&lt;/li&gt;
&lt;li&gt; Filter, map, reduce functions&lt;/li&gt;
&lt;li&gt;Generators&lt;/li&gt;
&lt;li&gt;Decorators&lt;/li&gt;
&lt;li&gt;Exception handling&lt;/li&gt;
&lt;li&gt;Files input and output using with&lt;/li&gt;
&lt;li&gt;Deep and shallow copy&lt;/li&gt;
&lt;li&gt;Memory management in python&lt;/li&gt;
&lt;li&gt;Modules in python&lt;/li&gt;
&lt;li&gt;Local vs global variables&lt;/li&gt;
&lt;li&gt;_&lt;em&gt;init_&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;*args, **kwargs&lt;/li&gt;
&lt;li&gt;Multithreading&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Python’s official &lt;a href="https://docs.python.org/3/tutorial/index.html"&gt;documentation&lt;/a&gt; is very precise and coherent, so going through that would be helpful to understand above concepts. But if you are someone who understands better visually than reading, then here are some useful links,&lt;/p&gt;

&lt;p&gt;For absolute beginners into programming/python: &lt;br&gt;
&lt;a href="https://youtu.be/oVp1vrfL_w4"&gt;python for beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important data structures in python&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://youtu.be/D3JvDWO-BY4"&gt;Data structures in python video&lt;/a&gt;                 2. &lt;a href="https://docs.python.org/3/tutorial/datastructures.html"&gt;Documentation of DS&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Comprehensions: &lt;a href="https://youtu.be/3dt4OGnU5sM"&gt;Python Comprehensions&lt;/a&gt;&lt;br&gt;
Generators in python: &lt;a href="https://youtu.be/bD05uGo_sVI"&gt;Python generators&lt;/a&gt;&lt;br&gt;
Decorators: [Python decorators]&lt;a href="https://youtu.be/FsAPt_9Bf3U"&gt;https://youtu.be/FsAPt_9Bf3U&lt;/a&gt;&lt;br&gt;
Multiprocessing in python: &lt;a href="https://youtu.be/fKl2JW_qrso"&gt;multiprocessing&lt;/a&gt;&lt;br&gt;
File operations: &lt;a href="https://docs.python.org/3/tutorial/inputoutput.html"&gt;Input/Output operations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The definitions of these concepts can be quite clear; however, you will need to understand them exceedingly to truly use them in your projects. The interviewers also ask tricky questions which can be answered only if you know the concepts thoroughly. &lt;/p&gt;

</description>
      <category>python</category>
      <category>interview</category>
      <category>programming</category>
      <category>basics</category>
    </item>
    <item>
      <title>Connect to ActiveMQ using python STOMP</title>
      <dc:creator>Manisha Naidu</dc:creator>
      <pubDate>Tue, 12 Jan 2021 09:16:44 +0000</pubDate>
      <link>https://dev.to/manishanaidu/connect-to-activemq-using-python-stomp-mdj</link>
      <guid>https://dev.to/manishanaidu/connect-to-activemq-using-python-stomp-mdj</guid>
      <description>&lt;p&gt;&lt;a href="https://activemq.apache.org/"&gt;ActiveMQ&lt;/a&gt; is an open source message-oriented middleware which is used to transfer messages between multiple applications. It consists of a queue which will hold in the messages and will only transfer when the receiver is available, hence we can be rest assured that the messages won’t be lost if the server is down. The applications could be entirely different and hosted on heterogenous platforms, ActiveMQ can be used to communicate between such systems as well.&lt;/p&gt;

&lt;p&gt;For example, large applications built in microservices architecture and written in disparate programming languages, where communication is often but an offline service should not cause any loss of messages.&lt;/p&gt;

&lt;p&gt;Working of ActiveMQ:&lt;br&gt;
There are two endpoints in ActiveMQ,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Producers – who send the messages to the message broker.&lt;/li&gt;
&lt;li&gt; Consumers – who read these messages one at a time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ActiveMQ stores the messages in the form of queue or topic. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Queue&lt;/strong&gt;: A message is sent to a single consumer and if the consumer is unavailable then it is held by activemq until the consumer is available to receive the message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Topic&lt;/strong&gt;: A message copy is sent to all of the subscribers who are interested in a particular topic. Unavailable consumers will not be receiving the past messages. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let us see how to create this consumer code and subscribe to an ActiveMQ topic in Python.&lt;br&gt;
Python provides a client library called &lt;strong&gt;stomp.py&lt;/strong&gt; for connecting with any type of message brokers using &lt;strong&gt;STOMP protocol&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Step1: install the latest version of stomp available.&lt;br&gt;
&lt;code&gt;pip install stomp.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step2: The consumer script.&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;import&lt;/span&gt; &lt;span class="nn"&gt;stomp&lt;/span&gt;
&lt;span class="c1"&gt;#Establish a connection
&lt;/span&gt;&lt;span class="n"&gt;con&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stomp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StompConnection&lt;/span&gt;&lt;span class="p"&gt;([(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;Host&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
&lt;span class="c1"&gt;#listener class to be instantiated.
&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MsgListener&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_listener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;name_of_listener&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#wait will ensure it waits till connection is established and acknowledged.
&lt;/span&gt;&lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;usernanme&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#subscribe to a particular topic or queue by giving the path and headers if required by the server.
&lt;/span&gt;&lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step3: Listener class for your processing.&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="n"&gt;Class&lt;/span&gt; &lt;span class="n"&gt;MsgListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stomp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionListener&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;__init__&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="c1"&gt;# to keep the count of messages received
&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;msg_recieved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_error&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;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;received&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="err"&gt;”’&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;message&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;on_message&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;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;msg_received&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="c1"&gt;# add your logic based on the message received here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;code&gt;con.subscribe&lt;/code&gt; is being done to a topic, then a published message will go to all the subscribers who have active subscription at the time of message published.&lt;br&gt;
Whereas, if it is a queue only one consumer will receive one message. Messages are load balanced among the available consumers.&lt;/p&gt;

</description>
      <category>python</category>
      <category>activemq</category>
      <category>stomp</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
