<?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: Rahul Banerjee</title>
    <description>The latest articles on DEV Community by Rahul Banerjee (@rahulbanerjee99).</description>
    <link>https://dev.to/rahulbanerjee99</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%2F519143%2F04968747-ae72-43c8-bb92-ec8f0698f035.png</url>
      <title>DEV Community: Rahul Banerjee</title>
      <link>https://dev.to/rahulbanerjee99</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahulbanerjee99"/>
    <language>en</language>
    <item>
      <title>How to Transform Data into Profit with Python and Flask</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Wed, 05 Apr 2023 18:03:13 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-transform-data-into-profit-with-python-and-flask-2hol</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-transform-data-into-profit-with-python-and-flask-2hol</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;This article was originally posted on &lt;a href="https://www.realpythonproject.com/how-to-transform-data-into-profit-with-python-and-flask/"&gt;realpythonproject.com&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In today's digital landscape, APIs play an essential role in connecting services, sharing data, and enabling developers to create innovative applications. If you have unique data or functionality to offer, creating your own API and monetizing it can be a profitable venture. In this article, I will walk you through the process of building a custom API using Python and Flask, using &lt;a href="https://musclewiki.com/"&gt;MuscleWiki&lt;/a&gt; as an example. Additionally, we will explore how to monetize your API through rate-limiting and access control.&lt;/p&gt;

&lt;p&gt;I recently scraped the data from MuscleWiki, created an API, and added it to RapidAPI. Below are some of the things I learned and some ideas I'd like to share.&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/rahulbanerjee26/MuscleWikiAPI"&gt;https://github.com/rahulbanerjee26/MuscleWikiAPI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RapidAPI: &lt;a href="https://rapidapi.com/rahulbanerjee26/api/musclewiki"&gt;https://rapidapi.com/rahulbanerjee26/api/musclewiki&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Brainstorming and Identifying a Unique API Idea
&lt;/h2&gt;

&lt;p&gt;To build a successful API, you must start with a unique and valuable idea. In the case of MuscleWiki, the API provides users with detailed information on exercises, muscles, and workout routines. This data can be integrated into fitness applications, websites, or other services.&lt;/p&gt;

&lt;p&gt;Some tips for coming up with an API idea:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Look for websites with valuable data that can be accessed and used more efficiently through an API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Identify pain points or areas where existing APIs could be improved or expanded upon.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consider niche markets or specific industries that could benefit from specialized APIs&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One approach is to look for websites or applications that offer valuable data or services but do not have an API. You can then scrape the website or reverse-engineer the application to create your own API that provides access to that data or functionality.&lt;/p&gt;

&lt;p&gt;To come up with your own API idea, consider the following steps:&lt;/p&gt;

&lt;p&gt;a. Identify your target audience: Consider the specific group of users or developers you want to target with your API. This could be a niche market or a broader segment of users with a particular interest. Defining your target audience will help you tailor your API features and functionality to meet their specific needs and preferences.&lt;/p&gt;

&lt;p&gt;b. Research the market and competition: Examine existing APIs in the same domain or industry to ensure that your idea stands out from the competition. Assess the unique value proposition your API can offer and analyze any potential gaps in the market that your API can fill. You can use platforms like RapidAPI to explore existing APIs and gather insights into their features, popularity, and pricing models. Additionally, engage with online developer communities and forums to gather feedback on their pain points and unmet needs.&lt;/p&gt;

&lt;p&gt;c. Evaluate demand and profitability: Conduct market research to estimate the potential demand for your API. Consider factors such as the size of the target audience, the potential for growth, and the expected revenue. To gather this information, you can use tools like Google Trends, keyword research tools, and industry reports to gauge the interest in your API's domain. Analyze user behavior, pain points, and needs through surveys, interviews, or focus groups. Based on your research findings, develop a revenue model and pricing strategy that aligns with the value your API provides and the market's willingness to pay.&lt;/p&gt;

&lt;p&gt;By following these steps, you can come up with a unique and valuable API idea that addresses the needs of your target audience, fills a gap in the market, and holds the potential for profitability. This strong foundation will help ensure the success of your API as you move forward with development, deployment, and monetization.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will use Muscle Wiki. It is a website that provides valuable information about different exercises but does not have an API. We will scrape the website to collect data about each exercise and create an API that allows users to search for exercises based on different criteria.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Before proceeding with a similar idea, make sure to do your due diligence and ensure that you're not violating any terms of service or infringing on intellectual property rights.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Data Collection: Scraping and Organizing Data
&lt;/h2&gt;

&lt;p&gt;If your API relies on external data sources, you will need to collect and organize this data. In the case of MuscleWiki, the data is scraped from &lt;a href="https://musclewiki.com/"&gt;the Muscle WIki website&lt;/a&gt; and compiled into a structured format. To achieve this, you can use Python libraries like Beautiful Soup and requests.&lt;/p&gt;

&lt;p&gt;a. Choose your data sources: Identify the websites or data sources you will scrape to gather the information your API will provide. Make sure the data is accurate, reliable, and up-to-date.&lt;/p&gt;

&lt;p&gt;b. Scrape the data: Use Python libraries like Beautiful Soup and requests to extract the desired data from the chosen websites. For example, you can fetch the HTML content of a page and parse it to extract specific elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scraping Data from Muscle Wiki
&lt;/h3&gt;

&lt;p&gt;To scrape data from Muscle Wiki, we will use Python's &lt;code&gt;requests&lt;/code&gt; and &lt;code&gt;BeautifulSoup&lt;/code&gt; libraries. We will define a function called &lt;code&gt;get_exercise_data()&lt;/code&gt; that takes in a URL for a specific exercise on Muscle Wiki and retrieves relevant data about the exercise, such as its target muscles, difficulty level, and steps.&lt;/p&gt;

&lt;p&gt;We will also define a function called &lt;code&gt;get_musclewiki_data()&lt;/code&gt; that loops through all of the exercises listed on Muscle Wiki and calls &lt;code&gt;get_exercise_data()&lt;/code&gt; for each exercise. This function saves all of the data to a JSON file for later use.&lt;/p&gt;

&lt;p&gt;Here's the code snippet that shows how we scraped data from Muscle Wiki. I have removed some of the code, you can find the &lt;a href="https://github.com/rahulbanerjee26/MuscleWikiAPI/blob/master/muscleWiki.py"&gt;complete source code here&lt;/a&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;def&lt;/span&gt; &lt;span class="nf"&gt;muscleList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Function to get targetted muscles for an exercise
&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;html_table_to_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;exerciseRowJson&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Function to convert html table to json
&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;getSteps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stepList&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Function to get the steps to do an exercise
&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;getVideos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Function to get the Videos of an exercise
&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;get_exercise_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exercise_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;exerciseRowJson&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;muscleList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;html_table_to_json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;getSteps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;getVideos&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;cleanData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="c1"&gt;# Function to clean the data
&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;organzieData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="c1"&gt;# Function to organize the data
&lt;/span&gt;  &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="c1"&gt;# Function to get the workout data from muscle wiki
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_musclewiki_data&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://musclewiki.com/directory"&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&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;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'html.parser'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;exercises&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'table'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'wikitable'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;musclewiki_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;count&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;for&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;exercises&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;exerciseRowJson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_exercise_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;exerciseRowJson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;cleanData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;organizeData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;musclewiki_data&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;exerciseRowJson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;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;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'musclewiki_data.json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&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;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;musclewiki_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&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;get_musclewiki_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  API Design: Defining Endpoints, Routes, and Data Format
&lt;/h2&gt;

&lt;p&gt;With your data ready, you can now begin designing your API. This involves:&lt;/p&gt;

&lt;p&gt;a. Defining the endpoints and routes for your API: Determine the set of endpoints your API will offer, and design the routes that developers will use to access them. Keep the routes descriptive, consistent, and easy to understand.&lt;/p&gt;

&lt;p&gt;b. Deciding on the data format: Choose a data format (e.g., JSON or XML) for your API responses. JSON is a popular choice due to its lightweight nature and compatibility with most programming languages.&lt;/p&gt;

&lt;p&gt;c. Designing a logical and user-friendly API structure: Organize your API's endpoints, routes, and data format in a way that is logical and easy to understand for developers. This will enhance&lt;/p&gt;

&lt;h3&gt;
  
  
  Building the MuscleWiki API with Python and Flask
&lt;/h3&gt;

&lt;p&gt;Python and Flask are excellent tools for building APIs. Flask is a lightweight web framework that enables you to quickly create RESTful APIs with minimal effort. In this section, we'll discuss the process of building your API using Python and Flask.&lt;/p&gt;

&lt;p&gt;a. Set up your Flask application: First, create a new Flask application and define the necessary routes for your API endpoints.&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;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/apiexercises'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'GET'&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_exercises&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Replace this with your actual data fetching logic
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b. Implement the data fetching logic: Write the necessary Python functions to fetch the data from your chosen data storage solution, such as a database or an in-memory data structure.&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;fetch_exercise_data&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Logic to fetch exercise data from your data storage
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the musclewiki API, I used a simple local JSON file for storage. However, if you plan to monetize your API, you'd have to use 3p services to store your data.&lt;/p&gt;

&lt;p&gt;c. Process and serve the data: Process the fetched data and serve it through your API endpoints. This might involve filtering, sorting, or transforming the data before returning it to the user.&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;get_exercises&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;exercise_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fetch_exercise_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Process and filter the data as needed
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'exercises'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;exercise_data&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API for MuscleWiki has the following endpoints&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="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Home&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;exercises&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Get&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;exercises&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;exercises&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Get&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;exercises&lt;/span&gt;&lt;span class="o"&gt;/&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;exercise_id&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Get&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;specific&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the code for the endpoint to get all exercises. You can find the complete source &lt;a href="https://github.com/rahulbanerjee26/MuscleWikiAPI/blob/master/api.py"&gt;over here&lt;/a&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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&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="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Load workout data
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'workout-data.json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;workout_data&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;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/exercises'&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_exercises&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Get query parameters
&lt;/span&gt;    &lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&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="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'category'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;difficulty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&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="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'difficulty'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Filter exercises based on query parameters
&lt;/span&gt;    &lt;span class="n"&gt;filtered_exercises&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;workout_data&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;category&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Category'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;difficulty&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;difficulty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;exercise&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Difficulty'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="n"&gt;filtered_exercises&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;exercise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response_class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;response&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;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filtered_exercises&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;mimetype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'application/json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&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;response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploying the API to Vercel
&lt;/h2&gt;

&lt;p&gt;Vercel is a platform that allows you to deploy your API for free. To deploy your Flask API on Vercel, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Sign up for a Vercel account and install the Vercel CLI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initialize your project by running &lt;code&gt;vercel init&lt;/code&gt; in your project directory. This command will create a &lt;code&gt;vercel.json&lt;/code&gt; file that you can configure to deploy your Flask app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure the &lt;code&gt;vercel.json&lt;/code&gt; file to use the correct Python runtime and set the entry point for your Flask app. Additionally, make sure to include any environment variables necessary for your application, such as API keys or database credentials. You can find MuscleWiki's vercel.json &lt;a href="https://github.com/rahulbanerjee26/MuscleWikiAPI/blob/master/vercel.json"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deploy your API by running &lt;code&gt;vercel deploy &amp;amp;&amp;amp; vercel deploy --prod&lt;/code&gt; . Vercel will provide you with a unique URL to access your API. Make note of this URL, as you'll need it when registering your API on RapidAPI.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more information on deploying Flask apps on Vercel, refer to &lt;a href="https://vercel.com/docs"&gt;Vercel's documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Monetizing the API: Implementing Rate-limiting and Access Control
&lt;/h2&gt;

&lt;p&gt;To monetize your API effectively, you must implement rate-limiting and access control. I haven't added rate-limiting or access-control to MuscleWiki. The only rate limiting is done by Vercel's free plan.&lt;/p&gt;

&lt;p&gt;Rate-limiting helps protect your API from abuse and ensures fair usage among your users. Access control allows you to provide different levels of access based on the user's subscription plan.&lt;/p&gt;

&lt;p&gt;a. &lt;a href="https://www.realpythonproject.com/how-to-add-rate-limiting-to-your-flask-api-in-2-lines-of-code/"&gt;Implement rate-limiting with Flask-Limiter&lt;/a&gt;: Flask-Limiter is a popular extension that enables you to set rate limits on your API endpoints easily. By using this extension, you can define limits based on the user's subscription plan, IP address, or custom criteria.&lt;/p&gt;

&lt;p&gt;b. Implement access control using decorators: Create custom decorators to check the user's API key and determine their subscription level. Apply these decorators to your API endpoints to restrict access based on the user's subscription plan.&lt;/p&gt;

&lt;p&gt;c. Integrate rate-limiting and access control into your API: Combine the rate-limiting and access control implementations to create a robust and monetizable API.&lt;/p&gt;

&lt;p&gt;Below is some sample code to give you a better idea and to help you get started.&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;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&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="n"&gt;jsonify&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask_limiter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Limiter&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Limiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_limits&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"100 per day"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"10 per hour"&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;check_subscription&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subscription_level&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;decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&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;decorated_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&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="s"&gt;'X-Api-Key'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;user_subscription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_user_subscription&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_key&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;user_subscription&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;subscription_level&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;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Unauthorized'&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decorated_function&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/free_endpoint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;limiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"60 per hour"&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;free_endpoint&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;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'data'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'This is a free endpoint'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/premium_endpoint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;check_subscription&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'premium'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;limiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1000 per hour"&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;premium_endpoint&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;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'data'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'This is a premium endpoint'&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;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Launching and Promoting Your API
&lt;/h2&gt;

&lt;p&gt;Once your API is ready and you have implemented rate-limiting and access control, it's time to launch and promote your API to developers. Consider adding your API to &lt;a href="https://rapidapi.com/hub"&gt;RapidAPI&lt;/a&gt; to get more visibility. RapidAPI is a popular API marketplace where developers can discover, test, and connect to APIs. By adding your API to RapidAPI, you can potentially monetize it by offering paid subscription plans. Follow &lt;a href="https://docs.rapidapi.com/docs/creating-an-api"&gt;RapidAPI's documentation&lt;/a&gt; to add your API&lt;/p&gt;

&lt;p&gt;Here are some other key aspects to focus on:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Provide clear and concise documentation&lt;/strong&gt;: Good documentation is critical to the success of your API. Include detailed explanations of each endpoint, the expected inputs, and the returned data. Also, provide examples of how to use your API to help developers understand its value and benefits.&lt;/p&gt;

&lt;p&gt;b. &lt;strong&gt;Offer a free trial or limited free tie&lt;/strong&gt;r: Allowing developers to test your API before committing to a paid plan can encourage adoption and demonstrate its value. A free trial or limited free tier can help users evaluate your API and determine if it fits their needs.&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;Monitor usage and gather feedback&lt;/strong&gt;: Track how developers are using your API and gather their feedback. Use this information to continuously improve and refine your API. An up-to-date and responsive API will increase its value and attract more users.&lt;/p&gt;

&lt;p&gt;d*&lt;em&gt;. Collaborate and network&lt;/em&gt;*: Reach out to other developers, bloggers, or influencers in your target market to promote your API. By collaborating with others and increasing your API's visibility, you'll attract more potential users.&lt;/p&gt;

&lt;p&gt;e. &lt;strong&gt;Utilize social media and online forums&lt;/strong&gt;: Share your API on social media platforms, developer forums, and other online communities relevant to your target audience. Engaging with potential users and answering their questions can help establish your API as a valuable resource in the market.&lt;/p&gt;

&lt;p&gt;f. &lt;strong&gt;Create a dedicated website or landing page&lt;/strong&gt;: Design a professional website or landing page that showcases the features and benefits of your API. Include detailed information about pricing plans, API documentation, and use cases to help potential users understand the value of your API.&lt;/p&gt;

&lt;p&gt;g. &lt;strong&gt;Offer excellent customer support&lt;/strong&gt;: Providing responsive and helpful customer support can significantly contribute to the success of your API. Ensure that users have a clear channel of communication to report issues, ask questions, or provide feedback. A positive user experience can lead to increased user satisfaction, word-of-mouth promotion, and long-term success.&lt;/p&gt;

&lt;p&gt;By focusing on these aspects, you can create a well-documented, user-friendly API that caters to the needs of your target audience. With a solid implementation of rate-limiting and access control, you can monetize your API effectively and turn it into a profitable venture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Building and monetizing a custom API with Python and Flask can be a rewarding and profitable experience. By following this guide, you can create a unique and valuable API that attracts developers and users alike. Remember to focus on identifying a unique API idea, collecting and organizing data, designing your API, implementing rate-limiting and access control, and promoting your API effectively. With dedication and persistence, you can successfully launch and monetize your custom API.&lt;/p&gt;

</description>
      <category>python</category>
      <category>api</category>
      <category>sideprojects</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Use Decorators in Python?</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Sun, 05 Mar 2023 02:33:23 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-use-decorators-in-python-2inm</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-use-decorators-in-python-2inm</guid>
      <description>&lt;p&gt;Decorators in Python are a powerful tool that allows you to modify the behavior of functions and classes. In this tutorial, we'll take a deeper look at advanced decorators and how they can be used to solve common programming challenges.&lt;/p&gt;

&lt;p&gt;This tutorial will talk about the different kinds of decorators, decorator chaining, and some common useful decorators&lt;/p&gt;

&lt;h2&gt;
  
  
  Decorator Functions
&lt;/h2&gt;

&lt;p&gt;A decorator function takes a function as an argument and returns a modified version of it. It is used to modify the behavior of the original function.&lt;/p&gt;

&lt;p&gt;Here's an example of a simple decorator 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="c1"&gt;# The decorator function adds two numbers to the result of the original function
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_two&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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Original Function is invoked
&lt;/span&gt;        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# Result of original function is modified by adding 2
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="nd"&gt;@add_two&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="nd"&gt;@add_two&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;divide&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;multiply&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="c1"&gt;# 14
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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="c1"&gt;#7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;add_two&lt;/code&gt; decorator function takes the &lt;code&gt;multiply&lt;/code&gt; function as an argument and returns the &lt;code&gt;wrapper&lt;/code&gt; function, which adds two to the result of &lt;code&gt;multiply&lt;/code&gt;. The &lt;code&gt;@add_two&lt;/code&gt; syntax before the &lt;code&gt;multiply&lt;/code&gt; function is a shorthand for the following:&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;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="n"&gt;multiply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_two&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we do not know the number of parameters the original function might have &lt;a href="https://www.realpythonproject.com/day16-positional-arguments-keyword-arguments-and-the-terms-args-and-kwargs/" rel="noopener noreferrer"&gt;we use &lt;code&gt;args&lt;/code&gt; and &lt;code&gt;kwargs&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Decorator Classes
&lt;/h2&gt;

&lt;p&gt;In addition to decorator functions, you can also use decorator classes to modify the behavior of functions and classes.&lt;/p&gt;

&lt;p&gt;Here's an example of a simple decorator class that adds two numbers to the result of the original 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;class&lt;/span&gt; &lt;span class="nc"&gt;AddTwo&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="n"&gt;self&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;func&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="nd"&gt;@AddTwo&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="nd"&gt;@AddTwo&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;divide&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;multiply&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="c1"&gt;# 14
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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="c1"&gt;#7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;__init__&lt;/code&gt; and &lt;code&gt;__call__&lt;/code&gt; are called &lt;a href="https://www.realpythonproject.com/python-magic-oop-dunder/" rel="noopener noreferrer"&gt;dunder methods or magic methods&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the &lt;code&gt;AddTwo&lt;/code&gt; decorator class takes the &lt;code&gt;multiply&lt;/code&gt; function as an argument and returns a callable object (an instance of &lt;code&gt;AddTwo&lt;/code&gt;) that adds two to the result of &lt;code&gt;multiply&lt;/code&gt;. The &lt;code&gt;@AddTwo&lt;/code&gt; syntax before the &lt;code&gt;multiply&lt;/code&gt; function is a shorthand for the following&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;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="n"&gt;multiply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AddTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Parametrized Decorators
&lt;/h2&gt;

&lt;p&gt;So far, we've looked at simple decorators that modify the behavior of a function in a fixed way. However, you can also create parametrized decorators that take parameters to control their behavior.&lt;/p&gt;

&lt;p&gt;Here's an example of a parametrized decorator function that adds a specified number to the result of the original 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;add_n&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;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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&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;wrapper&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;

&lt;span class="nd"&gt;@add_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="nd"&gt;@add_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&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;divide&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;multiply&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="c1"&gt;# 22
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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="c1"&gt;# 17
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;add_n&lt;/code&gt; decorator function takes a parameter &lt;code&gt;n&lt;/code&gt; and returns a decorator function &lt;code&gt;decorator&lt;/code&gt; that adds &lt;code&gt;n&lt;/code&gt; to the result of the original function. The &lt;code&gt;@add_n(10)&lt;/code&gt; syntax before the &lt;code&gt;multiply&lt;/code&gt; function is a shorthand for the following:&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;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="n"&gt;multiply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is an image comparing our first decorator &lt;code&gt;add_two&lt;/code&gt; and the parameterized decorator &lt;code&gt;add_n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.realpythonproject.com%2Fwp-content%2Fuploads%2F2023%2F03%2FScreen-Shot-2023-03-04-at-5.10.56-PM-1.png%2520align%3D" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.realpythonproject.com%2Fwp-content%2Fuploads%2F2023%2F03%2FScreen-Shot-2023-03-04-at-5.10.56-PM-1.png%2520align%3D" alt="Decorators with parameters in Python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Before we were wrapping the original function and returning the wrapper&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, we are wrapping the decorator (that wraps the original function) and returning the decorator&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Decorating Classes
&lt;/h2&gt;

&lt;p&gt;In our previous examples, we looked at how to decorate functions. But you can decorate classes as well&lt;/p&gt;

&lt;p&gt;Here's an example of a simple class decorator that adds a class attribute &lt;code&gt;n&lt;/code&gt; to the decorated class:&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;add_attribute&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;def&lt;/span&gt; &lt;span class="nf"&gt;class_decorator&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="n"&gt;cls&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="n"&gt;n&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;class_decorator&lt;/span&gt;

&lt;span class="nd"&gt;@add_attribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyClass&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;# 10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;add_attribute&lt;/code&gt; decorator function takes a parameter &lt;code&gt;n&lt;/code&gt; and returns a class decorator function &lt;code&gt;class_decorator&lt;/code&gt; that adds the class attribute &lt;code&gt;n&lt;/code&gt; to the decorated class. The &lt;code&gt;@add_attribute(10)&lt;/code&gt; syntax before the &lt;code&gt;MyClass&lt;/code&gt; class is a shorthand for the following:&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;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="n"&gt;MyClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_attribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is another example fo decorating classes&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;add_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;def&lt;/span&gt; &lt;span class="nf"&gt;new_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;setattr&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new_method&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_method&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;cls&lt;/span&gt;

&lt;span class="nd"&gt;@add_method&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="n"&gt;my_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new_method&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;# Hello, World
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It simply adds &lt;code&gt;new_method&lt;/code&gt; to every instance of &lt;code&gt;MyClass&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Decorator Chaining
&lt;/h2&gt;

&lt;p&gt;One of the most powerful features of decorators is the ability to chain them together to modify the behavior of a function or class in multiple ways. Remember, a decorator simply returns a modified function. This modified function can be passed to another decorator to further modify it.&lt;/p&gt;

&lt;p&gt;Here's an example of how to chain two decorators to modify the behavior of a 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="c1"&gt;# Adds n to the result of original function
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_n&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;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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&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;wrapper&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;

&lt;span class="c1"&gt;# Multiplies result of original function by n
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply_by_m&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&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;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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;

&lt;span class="nd"&gt;@add_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@multiply_by_m&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;def&lt;/span&gt; &lt;span class="nf"&gt;multiply&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="nd"&gt;@multiply_by_m&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="nd"&gt;@add_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&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;divide&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;multiply&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="c1"&gt;# 70
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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="c1"&gt;#85
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have two parameterized decorators to multiply and add. The order of applications of the decorators chained is from bottom to top. I.e the last decorator is applied first and the first decorator is applied last.&lt;/p&gt;

&lt;p&gt;In this example, the &lt;code&gt;multiply&lt;/code&gt; function is decorated by both the &lt;code&gt;add_n(10)&lt;/code&gt; and &lt;code&gt;multiply_by_m(5)&lt;/code&gt; decorators. The &lt;code&gt;multiply_by_m(5)&lt;/code&gt; decorator is applied first, followed by the &lt;code&gt;add_n(10)&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;In the second case, however, The &lt;code&gt;add_n(12)&lt;/code&gt; decorator is applied first and &lt;code&gt;multiply_by_m(5)&lt;/code&gt; is applied second.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases of Decorators
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Timing Decorator
&lt;/h3&gt;

&lt;p&gt;A timing decorator can be used to measure the time taken by a function to execute. Here's an example:&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="n"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;timing_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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Time taken: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; seconds&lt;/span&gt;&lt;span class="sh"&gt;"&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;result&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="nd"&gt;@timing_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;long_running_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&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="nf"&gt;long_running_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;timing_decorator&lt;/code&gt; decorator adds timing information to the &lt;code&gt;long_running_function&lt;/code&gt; function. When the &lt;code&gt;long_running_function&lt;/code&gt; is executed, the &lt;code&gt;wrapper&lt;/code&gt; function provided by the decorator measures the time taken for the function to execute and prints the result.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Authorization Decorator
&lt;/h3&gt;

&lt;p&gt;An authorization decorator can be used to enforce access control on a function. Here's an example:&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;authorized&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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_current_user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_authenticated&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Unauthorized&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="nd"&gt;@authorized&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;restricted_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Access granted&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;authorized&lt;/code&gt; decorator adds an authorization check to the &lt;code&gt;restricted_function&lt;/code&gt; function. When the &lt;code&gt;restricted_function&lt;/code&gt; is executed, the &lt;code&gt;wrapper&lt;/code&gt; function provided by the decorator retrieves the current user and checks if the user is authenticated. If the user is not authenticated, the wrapper raises an exception.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Logging Decorator
&lt;/h3&gt;

&lt;p&gt;A logging decorator can be used to log the execution of a function. Here's an example:&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;logging_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="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Calling function: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Returning from function: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&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;result&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="nd"&gt;@logging_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&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="k"&gt;return&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;b&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;some_function&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="c1"&gt;# 7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;logging_decorator&lt;/code&gt; decorator adds logging information to the &lt;code&gt;some_function&lt;/code&gt; function. When the &lt;code&gt;some_function&lt;/code&gt; is executed, the &lt;code&gt;wrapper&lt;/code&gt; function provided by the decorator logs the function name before and after the function is executed.&lt;/p&gt;

&lt;p&gt;We can chain this with the timing decorator&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="nd"&gt;@logging_decorator&lt;/span&gt;
&lt;span class="nd"&gt;@timing_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;long_running_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&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="sh"&gt;'''&lt;/span&gt;&lt;span class="s"&gt;
Calling function: wrapper
Time taken: 3.00 seconds
Returning from function: wrapper
&lt;/span&gt;&lt;span class="sh"&gt;'''&lt;/span&gt;
&lt;span class="nf"&gt;long_running_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Cache Decorator
&lt;/h3&gt;

&lt;p&gt;A cache decorator can be used to cache the results of a function for faster access. Here's an example:&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;cache_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="n"&gt;cache&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;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;args&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="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache&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;key&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Calling function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Getting value from cache&lt;/span&gt;&lt;span class="sh"&gt;"&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&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;wrapper&lt;/span&gt;

&lt;span class="nd"&gt;@cache_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;expensive_function&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;result&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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&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;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;

&lt;span class="sh"&gt;'''&lt;/span&gt;&lt;span class="s"&gt;
{}
Calling function
4950
{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;100&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;: 4950}
Getting value from cache
4950
&lt;/span&gt;&lt;span class="sh"&gt;'''&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;expensive_function&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="c1"&gt;# 4950
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;expensive_function&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="c1"&gt;# 4950
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;cache_decorator&lt;/code&gt; decorator adds a cache to the &lt;code&gt;expensive_function&lt;/code&gt; function. When the &lt;code&gt;expensive_function&lt;/code&gt; is executed, the &lt;code&gt;wrapper&lt;/code&gt; function provided by the decorator checks if the result is already present in the cache. If it is, the cached result is returned, otherwise the function is executed and the result is added to the cache.&lt;/p&gt;

&lt;p&gt;Notice how the &lt;code&gt;cache&lt;/code&gt; a dictionary is still able to persist between calls to the decorated function because the wrapper function created by the decorator retains a reference to the &lt;code&gt;cache&lt;/code&gt; dictionary. This allows the wrapper function to access and modify the &lt;code&gt;cache&lt;/code&gt; dictionary.&lt;/p&gt;

&lt;p&gt;If a new function is decorated with the &lt;code&gt;cache&lt;/code&gt; decorator, the wrapper function will have a reference to a new empty &lt;code&gt;cache&lt;/code&gt; dictionary&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="sh"&gt;'''&lt;/span&gt;&lt;span class="s"&gt;
{}
Calling function
100
&lt;/span&gt;&lt;span class="sh"&gt;'''&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;outputResult&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="sh"&gt;'''&lt;/span&gt;&lt;span class="s"&gt;
{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;100&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;: 4950}
Getting value from cache
4950
&lt;/span&gt;&lt;span class="sh"&gt;'''&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;expensive_function&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Singleton Class Decorator
&lt;/h3&gt;

&lt;p&gt;A singleton class decorator can be used to ensure that only one instance of a class is created. Here's an example:&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;singleton&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="n"&gt;instances&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;get_instance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;instances&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;instances&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instances&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;return&lt;/span&gt; &lt;span class="n"&gt;get_instance&lt;/span&gt;

&lt;span class="nd"&gt;@singleton&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;obj3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;obj3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;obj3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;singleton&lt;/code&gt; decorator is used to decorate the &lt;code&gt;MyClass&lt;/code&gt; class. The decorator works by creating a &lt;code&gt;get_instance&lt;/code&gt; function that stores an instance of the class in a dictionary. When the &lt;code&gt;get_instance&lt;/code&gt; function is called, it checks if an instance of the class already exists and returns it if it does, otherwise, it creates a new instance of the class and adds it to the dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, decorators are a powerful feature in Python that allow you to add functionality to a function without modifying its original code. Decorators can be used to add caching, logging, validation, and other functionality to functions in a modular and reusable way.&lt;/p&gt;

&lt;p&gt;In this tutorial, we learned how to define and use decorators in Python, including how to define decorator functions and decorator classes, and how to use decorators to add caching to a function.&lt;/p&gt;

&lt;p&gt;If you'd like a more in-depth tutorial about decorators, you should watch &lt;a href="https://youtu.be/MjHpMCIvwsY" rel="noopener noreferrer"&gt;this&lt;/a&gt; awesome Pycon Talk&lt;/p&gt;

&lt;p&gt;Overall, decorators are a valuable tool in your Python toolbox, and mastering decorators can help you write more modular, reusable, and maintainable code&lt;/p&gt;




&lt;p&gt;This article was originally published on &lt;a href="https://www.realpythonproject.com/how-to-use-decorators-in-python/" rel="noopener noreferrer"&gt;RealPythonProject.com&lt;/a&gt; on March 4th,2023.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Create a Testable CLI using TypeScript?</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Mon, 30 Jan 2023 05:18:41 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-create-a-testable-cli-using-typescript-3im</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-create-a-testable-cli-using-typescript-3im</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article was originally posted on &lt;a href="http://www.realpythonproject.com"&gt;www.realpythonproject.com&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A CLI is a tool that allows users to interact with a computer program through the command line. It can perform various tasks, such as running commands, executing scripts, and interacting with other programs. This tutorial will teach you how to build a simple CLI in TypeScript. We will create a CLI that has the following commands&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; command print a name&lt;/li&gt;



&lt;li&gt;command to perform basic arithmetic operations with unit tests&lt;/li&gt;



&lt;li&gt;command to make an API call&lt;/li&gt;



&lt;li&gt;We will also format our output by adding some color to it!&lt;/li&gt;



&lt;li&gt;Finally, we will use mocha and chai to write unit tests for our CLI functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;Create a new folder and set up a project.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir my-cli
cd my-cli
npm init -y&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create a new directory called &lt;code&gt;my-cli&lt;/code&gt; and initialize it as a npm project with default settings&lt;/p&gt;

&lt;p&gt;Now, let's install some of the required packages&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install typescript @types/node --save-dev
npm install commander&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/commander"&gt;Commander.js&lt;/a&gt; is an NPM package that makes it easier to build CLI tools. You can find its documentation &lt;a href="https://github.com/tj/commander.js"&gt;over here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Configure Typescript for your project by creating a &lt;code&gt;tsconfig.json&lt;/code&gt; with the following&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "strict": true
  },
  "include": ["src/**/*", "test/cli.spec.ts"]
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Create two folders &lt;code&gt;src&lt;/code&gt; and &lt;code&gt;test&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;src&lt;/code&gt; folder, create a file named &lt;code&gt;index.ts&lt;/code&gt; and add a Hello World Statment&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console.log("Hello World!");&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can either compile the above file and use node to execute the result javascript file&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npx tsc &amp;amp;&amp;amp; node ./dist/src/index.js&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or alternatively you can install &lt;code&gt;tsnode&lt;/code&gt; and directly execute the typescript file&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install ts-node --save-dev

npx ts-node src/index.ts&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I recommend installing ts-node since it'll be used when setting up mocha for unit testing.&lt;/p&gt;

&lt;h2&gt;Commands&lt;/h2&gt;

&lt;h3&gt;Hello World Command&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;utils.ts&lt;/code&gt;, this will contain our utility functions &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export const printName = (name: string) =&amp;gt; {
  console.log(`Hello, ${name || "World"}!`);
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It accepts a string as an argument. If no string is provided, it simply outputs &lt;code&gt;Hello,World!&lt;/code&gt;. If a string is provided, it will output the name &lt;code&gt;Hello,[name]!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of &lt;code&gt;console.log&lt;/code&gt;, you can also use other methods such &lt;code&gt;console.error&lt;/code&gt; and &lt;code&gt;console.warn&lt;/code&gt;. In this tutorial, we will only work with &lt;code&gt;console.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now go back to &lt;code&gt;index.ts&lt;/code&gt;. We will create an instance of &lt;code&gt;Commander&lt;/code&gt; and register our first command.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import { printName } from "./utils";
import { Command } from "commander";

const program = new Command();

program
    .name("tcli")
    .description("Learn how to build a CLI using TypeScript")
    .version("1.0.0");

program
    .command("hello [name]")
    .action(printName);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Update the name, description and version number of your CLI as needed. When registering a command, the following syntax is used&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;${command name} ${[arguement1]} ${[arguement2]} ${&amp;lt;arguement3&amp;gt;} ......&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If an arguement/flag is required, it should be specified inside &lt;code&gt;&amp;lt;..&amp;gt;&lt;/code&gt; and if it is optional, it should be specified inside &lt;code&gt;[...]&lt;/code&gt; In this case, we want the arguement to be optional since we have a default output &lt;code&gt;Hello, World!&lt;/code&gt;. You can alternatively register a command in the following way as well&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program
    .command("Hello")
    .argument("[name]")
    .action(printName);

program.parse(process.argv);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next we will pass the &lt;code&gt;printName&lt;/code&gt; function as a callback method.&lt;/p&gt;

&lt;p&gt;Now run the following in your terminal to run the CLI&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npx ts-node src/index.ts hello&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npx ts-node src/index.ts hello Jake&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, let's add some color to the output&lt;/p&gt;

&lt;p&gt;For this, we will have to install a npm package called &lt;code&gt;&lt;a href="https://www.npmjs.com/package/chalk" rel="noopener" title=""&gt;Chalk&lt;/a&gt;&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install chalk&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The various color functions provided by &lt;code&gt;Chalk&lt;/code&gt; take a string as an argument and return a string, so we basically need to wrap our strings in one of Chalk's functions and can directly use it with &lt;code&gt;console.log&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Go back to &lt;code&gt;utils.ts&lt;/code&gt; and update the &lt;code&gt;printName&lt;/code&gt; function as follows&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import * as chalk from "chalk";

export const printName = (name: string) =&amp;gt; {
  console.log(chalk.yellow(`Hello, ${name || "World"}!`));
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will print the output in yellow.&lt;/p&gt;

&lt;p&gt;Try running the below command again to see the output&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npx ts-node src/index.ts hello&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can even add the above command as a script in &lt;code&gt;package.json&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  "scripts": {
    "local-cli": "ts-node src/index.ts"
  }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you could run &lt;code&gt;index.ts&lt;/code&gt; with the following command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run local-cli&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are using the script from package.json, you need to use -- before adding the command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run local-cli -- hello&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run local-cli -- hello Jake&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Command to Perform Operations on Numbers&lt;/h3&gt;

&lt;p&gt;First, let's write the a function to perform various operations&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export const performOperation = (
  num1: number,
  num2: number,
  options: { operation: string }
) =&amp;gt; {
  let result: number;
  switch (options.operation) {
    case "multiply":
      result = num1 * num2;
      break;
    case "divide":
      result = num1 / num2;
      break;
    default:
      console.error(chalk.red("Invalid operation"));
      process.exit(1);
  }
  console.log(chalk.green(result));
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is a pretty simple function, it accepts a couple of numbers and the operation to perform. It has a switch case block to perform either multiplication or division. If any other operation is provided, it prints an error message in red and exits.&lt;/p&gt;

&lt;p&gt; For this command we need 3 values, the 2 numbers and the operation.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import {performOperation} from "./utils";

program
  .command("calculate &amp;lt;num1&amp;gt; &amp;lt;num2&amp;gt;")
  .option(
    "-o, --operation &amp;lt;operation&amp;gt;",
    "operation to perform: multiply, or divide"
  )
  .action(performOperation);

program.parse(process.argv);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt; In the code above, we add an option for the operation. We can also provide a help message if the user uses &lt;code&gt;-h&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Test the above using the following command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run local-cli -- calculate 1 2 -o multiply&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The following command should return an error message&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run local-cli -- calculate 1 2 -o add&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Command to make an API call&lt;/h3&gt;

&lt;p&gt;To make the API call, we will be using &lt;code&gt;axios&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install axios&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We will be using the &lt;a href="https://agify.io/" rel="noopener" title=""&gt;agify API&lt;/a&gt;, it tries to predict the age of a name. Below is the function to make a request to the API&lt;/p&gt;

&lt;p&gt;Like before, write it in the &lt;code&gt;utils.ts&lt;/code&gt; file&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import axios from "axios";

export const getAge = async (name: string) =&amp;gt; {
  const response = await axios.get(`https://api.agify.io/?name=${name}`);
  const data = await response.data;
  console.log(`${chalk.yellow(data["name"])} - ${chalk.gray(data["age"])} `);
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Registering the command is pretty straight forward&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import {getAge} from "./utils";

program
    .command('age [name]')
    .action(getAge)

program.parse(process.argv);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Test it using the below command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run local-cli -- age rahul&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;How to make the CLI Tool "Global"?&lt;/h2&gt;

&lt;p&gt;Right now, we have to manually run the script to run our CLI Tool but this is not optimal. We want users to be able to install it as an npm package and then use a command like &lt;code&gt;tcli&lt;/code&gt; to run it. Below is how you can add that functionality&lt;/p&gt;

&lt;p&gt;Add the following shebang line at the top of your &lt;code&gt;index.ts&lt;/code&gt; or the file where you created an instance of &lt;code&gt;Commander&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/env node&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, update your &lt;code&gt;package.json&lt;/code&gt; file by adding the following&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  "bin": {
    "tcli": "./dist/src/index.js"
  }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;They key is what the user would have the call to run the CLI and the value is the path to the compiled javascript file. In this case, the user would have to type &lt;code&gt;tcli&lt;/code&gt; to run our CLI tool.&lt;/p&gt;

&lt;p&gt;You can also test it by installing your package globally &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm i -g&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now if you type &lt;code&gt;tcli&lt;/code&gt; in your CLI, it should run your CLI tool&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tcli hello&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can uninstall the package as well&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm uninstall typescriptcli -g&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also update the &lt;code&gt;scripts&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt; with some more helpful commands&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; "scripts": {
    "install-cli": "npx tsc &amp;amp;&amp;amp; npm i -g",
    "uninstall-cli": "npm uninstall typescriptcli -g",
    "test-cli": "tcli hello",
  }&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;How to Test a CLI Tool?&lt;/h2&gt;

&lt;p&gt;We will use &lt;code&gt;mocha&lt;/code&gt; and &lt;code&gt;chai&lt;/code&gt; for unit testing. Install the required dependencies &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm i mocha chai @types/mocha @types/chai --save-dev&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Create a folder called &lt;code&gt;test&lt;/code&gt; and a file called &lt;code&gt;cli.spec.ts&lt;/code&gt; inside it&lt;/p&gt;

&lt;p&gt;Before any of the unit tests are run, we need to install our package globally so that our unit tests can test the CLI&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import { before, after, test } from "mocha";
import * as path from "path";
import * as childProcess from "child_process";

before(() =&amp;gt; {
  childProcess.execSync(`npm install -g`, { cwd: path.join(__dirname, "..") });
  console.log("Installed CLI");
});

after(() =&amp;gt; {
  childProcess.execSync(`npm uninstall -g`, { cwd: path.join(__dirname, "..") });
  console.log("Uninstalled CLI");
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We will use &lt;code&gt;childProcess.execSync&lt;/code&gt; to install and later uninstall the CLI. Now in the same file, let's add some unit tests to test the &lt;code&gt;hello&lt;/code&gt; command and the &lt;code&gt;calculate&lt;/code&gt; command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;test("should print hello world", () =&amp;gt; {
  // Run the hello command and capture the output
  const output = childProcess.execSync(`tcli hello`, {
    encoding: "utf8",
  });

  // Check that the output is correct
  expect(output).to.equal("Hello, World!\n");
});

test("should print a greeting", () =&amp;gt; {
  // Run the hello command and capture the output
  const output = childProcess.execSync(`tcli Hello Rahul`, {
    encoding: "utf8",
  });

  // Check that the output is correct
  expect(output).to.equal("Hello, Rahul!\n");
});

test("should perform the specified operation", () =&amp;gt; {
  // Run the calculate command and capture the output
  const output = childProcess.execSync(`tcli calculate 3 4 -o multiply`, {
    encoding: "utf8",
  });

  // Check that the output is correct
  expect(output).to.equal("12\n");
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will only check the string value output in the console, it doesn't check the color of the output. Similarly you add an unit test for the &lt;code&gt;age&lt;/code&gt; command as well.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;A CLI is a powerful tool and I hope you found this tutorial useful.  Here a few other articles that you may find interesting&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.realpythonproject.com/what-is-axios-create-in-javascript/"&gt;https://www.realpythonproject.com/what-is-axios-create-in-javascript/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>My Software Development Blogging Journey: A 2022 Retrospective</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Fri, 06 Jan 2023 09:16:45 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/my-software-development-blogging-journey-a-2022-retrospective-56an</link>
      <guid>https://dev.to/rahulbanerjee99/my-software-development-blogging-journey-a-2022-retrospective-56an</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This piece was originally posted over &lt;a href="https://realjavascriptproject.com/my-software-development-blogging-journey-a-2022-retrospective/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is going to be a retrospective piece going over my Blogging stats and earnings in 2022. I will also include some tips based on my personal experience if you would like to consider Blogging either for a side income or as a hobby.&lt;/p&gt;

&lt;p&gt;My strategy is pretty simple, I post my article on my wordpress blog and then cross-post it along with a canonical link on Medium, Dev.to, and Hashnode. So basically, it is the same content but it's being shown to different audience groups. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create Once, Publish Everywhere (COPE)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you would like to read more about COPE, this is a &lt;a href="https://www.theeword.co.uk/blog/create-once-publish-everywhere-for-better-content-marketing-campaigns/" rel="noopener" title=""&gt;great article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have even written a tutorial on &lt;a href="https://www.realpythonproject.com/how-to-use-python-to-post-on-popular-blogging-websites/" rel="noopener" title=""&gt;how to automate cross-posting&lt;/a&gt; to various different platforms.&lt;/p&gt;

&lt;h2&gt;Blog #1  RealPythonProject Stats&lt;/h2&gt;

&lt;p&gt;This was my first wordpress blog and I created it in the Winter of 2020. I integrated it with Ezoic on 5th January 2022 and have been using Ezoic to display ads on it ever since.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GPxiRT9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-02-at-10.46.11-PM-1024x604.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GPxiRT9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-02-at-10.46.11-PM-1024x604.png" alt="" width="880" height="519"&gt;&lt;/a&gt;&lt;em&gt;Ezoic Analytics for 2022&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In 2022, my blog got 168,402 views.&lt;/p&gt;

&lt;p&gt;Initially, I had some trouble integrating Ezoic with my website and hence there are a few missing data points. In the month of April, a lot of my website pages became unavailable and sometime in mid-April, my entire website went down. It was due to some issues with Bluehost, my website's hosting service. The website's speed was affected as well and most pages were taking 15-20 seconds to load. After a bit of back and forth with the Bluehost Team, I managed to resolve the issue. However, for some reason, some of my articles got taken out of Google's Search Index, and as a result, I had to apply to re-index multiple of my pieces.&lt;/p&gt;

&lt;p&gt;In the last few months of 2022, my view started dropping significantly. There were a couple of reasons for it&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I started posting less content since I was pretty busy&lt;/li&gt;



&lt;li&gt;Google had a &lt;a href="https://developers.google.com/search/updates/ranking" rel="noopener" title=""&gt;core Search Algorithm update&lt;/a&gt; in September and I believe this affected my blog. As a result, the views started tanking.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Blog #2 RealJavascriptProject Stats&lt;/h2&gt;

&lt;p&gt;I created this blog in the summer of 2022. It hasn't grown as much as I would have liked. That is mainly because I have not been consistent in posting articles.&lt;/p&gt;

&lt;p&gt;I don't have Ezoic or Google Analytics setup so below is a screenshot from Jetpack Insights&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4dCKXWN2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.13.26-PM-1024x431.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4dCKXWN2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.13.26-PM-1024x431.png" alt="" width="880" height="370"&gt;&lt;/a&gt;&lt;em&gt;Jetpack Stats for 2022&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In 2023, I will focus more on this blog and try monetizing it. &lt;/p&gt;

&lt;h2&gt;CrossPosting Stats&lt;/h2&gt;

&lt;h3&gt;Medium&lt;/h3&gt;

&lt;p&gt;Unfortunately, there is no easy way to get yearly stats from Medium. So I'll have to sum up the views for every month. Below is a screenshot of my best month on Medium in 2022&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--srPEiU5---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.16.17-PM-1024x546.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--srPEiU5---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.16.17-PM-1024x546.png" alt="" width="880" height="469"&gt;&lt;/a&gt;&lt;em&gt;Medium Stats for March'22&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In 2022, I got a total of 140132 views which is almost on par with my main wordpress Blog. I had started off initially on Medium and managed to gain a few hundred followers. &lt;/p&gt;

&lt;p&gt;I have been pretty inconsistent over the past few months but still manage to get around 10k views a month. Medium does a pretty good job at promoting your articles and a lot of top search results are often Medium articles. For anyone getting started, I highly recommend Medium as your first platform.&lt;/p&gt;

&lt;h3&gt;Dev.to&lt;/h3&gt;

&lt;p&gt;Unfortunately, Dev.to doesn't support showing yearly stats as well. Below is a screenshot of my stats from last month&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DUIqAXZl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.34.46-PM-1-1024x494.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DUIqAXZl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.34.46-PM-1-1024x494.png" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;em&gt;Dev.to stats&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I used the Dev.to API to get my stats for 2022. My Dev.to posts got 11499 views in 2022.&lt;/p&gt;

&lt;h3&gt;Hashnode&lt;/h3&gt;

&lt;p&gt;Unfortunately, Hashnode has not worked out for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2PjHYbmt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.42.35-PM-1024x559.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2PjHYbmt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-05-at-11.42.35-PM-1024x559.png" alt="" width="880" height="480"&gt;&lt;/a&gt;&lt;em&gt;Hashnode Stats&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;IMO, Hashnode has the best UI compared to other blogging platforms and comes with a ton of features. However, I think like me, most people use it only to cross-post their articles and hence there aren't that many readers. It is still fairly new as compared to dev.to and Medium. Or maybe I am just missing out on something. If you have managed to crack the Hashnode Algorithm and are reading this, please send me some tips.&lt;/p&gt;

&lt;h2&gt;Earnings&lt;/h2&gt;

&lt;p&gt;Now the good stuff!&lt;/p&gt;

&lt;h3&gt;Money Spent&lt;/h3&gt;

&lt;p&gt;I spent 156 USD on both my blog's domains, hosting, etc via Bluehost.&lt;/p&gt;

&lt;p&gt;As I talked about in the &lt;a href="https://rbanerjee.substack.com/p/issue-3-realpythonproject-november-22-12-07" rel="noopener" title=""&gt;previous newsletter&lt;/a&gt;, I spent 175 CAD on some SEO services on Fiverr.&lt;/p&gt;

&lt;p&gt;I used Ezoic to display ads and since June I have been paying 36 USD per month to gain access to Ezoic Premium.&lt;/p&gt;

&lt;p&gt;Total Money Spent on Blog: ~540 USD&lt;/p&gt;

&lt;h3&gt;Ezoic Ads&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HdG1C-e9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-06-at-12.07.13-AM-1024x618.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HdG1C-e9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-06-at-12.07.13-AM-1024x618.png" alt="" width="880" height="531"&gt;&lt;/a&gt;&lt;em&gt;Ezoic Earnings for 2022&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The drop from August'22 was due to 3 main reasons&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The biggest reason was I decided to add hard limits on the number of ads to show per page. Initially, I did not set any ad number limits and earned around 100-200 bucks per month. However, in the long term, you need to find the sweet spot between the number of ads to display and also having a good user experience. I still haven't managed to figure it out, Ezoic has some helpful tools for this as well.&lt;/li&gt;



&lt;li&gt;Lower views due to SEO issues&lt;/li&gt;



&lt;li&gt;Didn't publish much during the last few months.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Medium Partner Program&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ad7ovKUC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-06-at-12.10.58-AM-608x1024.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ad7ovKUC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-06-at-12.10.58-AM-608x1024.png" alt="" width="608" height="1024"&gt;&lt;/a&gt;&lt;em&gt;Medium Earnings&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Total Earnings in 2022: 980 USD&lt;/p&gt;

&lt;p&gt;As I mentioned earlier, I only cross-post my articles on Medium. So each article of mine earns me via ads and via medium partner programs. Medium also allows you to refer users and you get a portion of the membership payment. I earn around 25 USD a month (included in the snapshot above) from referrals.&lt;/p&gt;

&lt;p&gt;Most of my time on Medium is spent reading other writers' work.&lt;/p&gt;

&lt;h3&gt;Guest Posts on Real Python Project&lt;/h3&gt;

&lt;p&gt;Surprisingly, a couple of people reached out to me to guest post on my blog and were willing to pay 50 USD for it. I had 4 guest posts and got 200 bucks for doing nothing other than reviewing and posting their article on my blog.&lt;/p&gt;

&lt;h3&gt;Net Income from my Blog&lt;/h3&gt;

&lt;p&gt;Total spent: 540 USD&lt;/p&gt;

&lt;p&gt;Total earned: 2494 USD&lt;/p&gt;

&lt;p&gt;Net Income: 1954 USD&lt;/p&gt;

&lt;h2&gt;Tips for Getting Started with Software Blogging and Making a Side Income&lt;/h2&gt;

&lt;p&gt;TLDR: Cross-posting and Posting on Sub-reddits &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initially start by creating content on Medium and cross-posting on Dev.to/Hashnode. You will not have to deal with any SEO/Website issues and can simply focus on writing content.&lt;/li&gt;



&lt;li&gt;Once you have gained a few followers on any of the above platforms, consider buying a domain and publishing over there. Don't stop cross-posting on Medium and other platforms. &lt;/li&gt;



&lt;li&gt;Be consistent. As you saw from my stats when you stop posting, your views and earnings drop. It's hard to stay motivated and be consistent so you'll have to manage your expectations for earnings. The more consistent you are, the more you will earn.&lt;/li&gt;



&lt;li&gt;Shamelessly promote your articles on subreddits. Just post it and forget about it. Reddit will lead to a lot of traffic to your articles.&lt;/li&gt;



&lt;li&gt;Write about stuff you learned at work/school or challenges you faced at work/school. Most of my articles are about challenges I faced during my day-to-day life as a developer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;About Me&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this  articles, please consider &lt;a href="http://click.revue.email/ss/c/2aF1LB2nVgZXMnRmiR3eaFtwaV3gJ19TNaH-t8vVcx9D9jyerVICDEyTsf05m1diKdmWQhSCvHUtYW0E3SEPIHCz4drM2niMEyibaUay1TH0m1TZH0HlRcCvYPEnsWR8ImwaZZ1MHsyeXHD9KV2pvA4gRJpsLQPA_1iB3os2oWpbfgHyFPImxVcJXU-V2ZhsJJ-MKruz-7m7kVu9REJM_A/3qy/vjDgQEGkQeCuo4PWl29eLw/h27/kdsQ0U7yYJGdsQauz8eZgrcGfECSqh4OBVTe6QvRnSw?utm_campaign=Rahul%20Banerjee%27s%20Newsletter%20%F0%9F%92%AF&amp;amp;utm_medium=email&amp;amp;utm_source=Revue%20newsletter"&gt;supporting me by buying me a Kofi&lt;/a&gt;. I send monthly newsletter revealing my stats/earnings for the month. If you are interested in reading more content similar to this article, &lt;a href="https://rbanerjee.substack.com/" rel="noopener" title=""&gt;subscribe to my newsletter&lt;/a&gt;. Read my &lt;a href="https://realjavascriptproject.com/category/earning-reports/" rel="noopener" title=""&gt;previous issues here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I recently graduated with a BaSc in Computer Engineering from University of Toronto. I moved to Vancouver shortly after graduation and started working as a Software Developer at Amazon. I like to play around with new APIs/ Libraries and write about them. You can connect with me on &lt;a href="http://click.revue.email/ss/c/XN2t88CAhalHja1RClwc6nFZJoeK_57ZAYDkml9IKehiYMrWkxNFTWps42GLZWL1RNKs-rDTwGNV1d5nE8oTD3pmh390LYVxyVqBQyThLu9Zg08KuLzqlrBB5rZm-uK93rkZ2uD3ge9bUpplq7VXJkBQK1BYHS-8mQr4iVwvLsl3bBIx_zhSPz4HXjuOteQ7sl1jlA9f52aAuefq9Ojm5A/3qy/vjDgQEGkQeCuo4PWl29eLw/h28/p0pqXNjP_DdN9U8cap-B59UThjfk12yGa6k03IMv9iE?utm_campaign=Rahul%20Banerjee%27s%20Newsletter%20%F0%9F%92%AF&amp;amp;utm_medium=email&amp;amp;utm_source=Revue%20newsletter"&gt;LinkedIn&lt;/a&gt; or &lt;a href="http://click.revue.email/ss/c/OvZMTmFNG_ogo9mVNMFA3x0cNXJP9O8F4tPbzTfbex8qTjyQ6_ynRfpWxFjum8lgh1TLhrRkP9fY_mrGWoGC8TBNmzbOwZMDuuD6blqW-VK1BH4ITxleXPG0J4nWzJffwV0YVG0ZjoQMJ6mqBQEP7HLRROAt118Npq060ogRPRY8UyWb7wm5Ra64K-zxzqRUQ2KxrLD5ecxJm5VOvvGvog/3qy/vjDgQEGkQeCuo4PWl29eLw/h29/vo4J3EkTY2LW9diPUTOb5keaJuy5phMq7-BmIcBu7e0?utm_campaign=Rahul%20Banerjee%27s%20Newsletter%20%F0%9F%92%AF&amp;amp;utm_medium=email&amp;amp;utm_source=Revue%20newsletter"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Affiliate Links&lt;/h3&gt;

&lt;p&gt;Note: If you use any of my affiliate links, I will earn some money at no extra cost to you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ezoic.com/?tap_a=6182-5778c2&amp;amp;tap_s=2583088-3c5683"&gt;Ezoic&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rahul1999.medium.com/membership"&gt;Medium&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>How to Send and Receive Data in Flask</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Mon, 14 Nov 2022 08:49:36 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-send-and-receive-data-in-flask-14cn</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-send-and-receive-data-in-flask-14cn</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;This article was originally posted on &lt;a href="https://www.realpythonproject.com/how-to-send-and-receive-data-in-flask/"&gt;realpythonproject.com&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;








&lt;p&gt;I’ll be using &lt;a href="https://replit.com/~" rel="noreferrer noopener"&gt;Repl.it&lt;/a&gt; for writing all the code. Repl.it lets you quickly deploy your API and is really convenient. You can find my &lt;a href="https://replit.com/@rahulbanerjee26/FlaskReceive" rel="noreferrer noopener"&gt;Repl here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the 4 types of data we will be looking at&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sending/Receiving Parameters&lt;/li&gt;



&lt;li&gt;Sending/Receiving Form Data&lt;/li&gt;



&lt;li&gt;Sending/Receiving JSON String Data&lt;/li&gt;



&lt;li&gt;Sending/Receiving Files&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Why would you want to learn how to Receive/Send Data in Flask?&lt;/h3&gt;

&lt;p&gt;In a lot of cases when you are developing a Flask API that interacts with 3p services such as other APIs, Webhooks, Serverless Functions, etc, you won’t have control over what kind of data you receive. However, knowing how to receive different kinds of data in Flask will help you handle the data appropriately. If the 3p service’s source code is in Python, you can go over this article to find the relevant section. Being able to send different formats of data is also helpful since it’ll allow you to write unit tests for your API.&lt;/p&gt;

&lt;h3&gt;Project Setup&lt;/h3&gt;

&lt;p&gt;Install Flask using the following command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pip3 install flask&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Below is a simple Flask API&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from flask import Flask,request
app = Flask('app')

@app.route('/')
def hello_world():
  return 'Hello, World!'


app.run(host='0.0.0.0', port=8080)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The endpoint simply returns a string.&lt;/p&gt;

&lt;h3&gt;Parameters&lt;/h3&gt;

&lt;h4&gt;How to Access Parameters from a Request in Flask&lt;/h4&gt;

&lt;p&gt;Below is a code snippet that can access the parameters that were sent.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@app.route('/receiveParameters')
def receiveParameters():
  data = request.args
  return {'data': data}, 200&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can use the &lt;code&gt;request.args&lt;/code&gt;to access the parameters. It returns the parameters as a dictionary.&lt;/p&gt;

&lt;h4&gt;How to Make a Request with Parameters Using Python&lt;/h4&gt;

&lt;p&gt;Below is the code snippet which makes a request to a URL and sends a couple of parameters.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import requests

url = 'https://FlaskReceive.rahulbanerjee26.repl.co'

response = requests.get(url+'/receiveParameters?name=Rahul&amp;amp;age=22')
print(response.json())


response = requests.get(url+'/receiveParameters',
params = {
  'name': 'Rahul',
  'age': 22
  }
)
print(response.json())

'''
OUTPUT
{'data': {'age': '22', 'name': 'Rahul'}}
{'data': {'age': '22', 'name': 'Rahul'}}
'''
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are two ways you can pass parameters, the first is to directly add it to the URL and the second is to add it in a JSON object.&lt;/p&gt;








&lt;h3&gt;Form Data&lt;/h3&gt;





&lt;h4&gt;How to Receive Form Data in Flask&lt;/h4&gt;





&lt;p&gt;Flask has a property called &lt;code&gt;request.form&lt;/code&gt; that lets it access form data. Below is an endpoint to receive form data&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@app.route('/receiveFormData',methods=['POST'])
def receiveFormData():
  name = request.form['name']
  age = request.form['age']
  print(name)
  print(age)
  return{
    'data': {
      'name': name,
      'age': age
    }
  },200&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It will accept &lt;code&gt;POST&lt;/code&gt; requests and simply return the data.&lt;/p&gt;

&lt;p&gt;We will look at two ways to send form data to the above endpoint. In the first way, we will create a simple form in HTML, and in the second way, we will send the form data using Python&lt;/p&gt;

&lt;h4&gt;How to send For Data using HTML to our Flask API Endpoint&lt;/h4&gt;

&lt;p&gt;I created a new repl for this. You can find it over &lt;a href="https://replit.com/@rahulbanerjee26/LumpyGlamorousPreprocessor" rel="noreferrer noopener"&gt;here&lt;/a&gt;. Below is the HTML form.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;h2&amp;gt;HTML Forms&amp;lt;/h2&amp;gt;

&amp;lt;form action="https://FlaskReceive.rahulbanerjee26.repl.co/receiveFormData" method='post'&amp;gt;
  
  &amp;lt;label for="name"&amp;gt;First name:&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="text" name='name'&amp;gt;&amp;lt;br&amp;gt;
  
  &amp;lt;label for="age"&amp;gt;Last name:&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="text" name='age'&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
  
  &amp;lt;input type="submit" value="Submit"&amp;gt;
&amp;lt;/form&amp;gt; 

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the &lt;code&gt;action&lt;/code&gt; attribute, I set the value to the Flask Endpoint we created in the above section. We also set &lt;code&gt;method&lt;/code&gt; to &lt;code&gt;POST&lt;/code&gt; since the endpoint only accepts Post Requests.&lt;/p&gt;

&lt;p&gt;This will create a form with two inputs and submit button. Whenever the user clicks on the submit button, the form will make a Post request to the &lt;code&gt;receiveFormData&lt;/code&gt; endpoint.&lt;/p&gt;

&lt;h4&gt;How to Send Form Data using Python&lt;/h4&gt;

&lt;p&gt;This could be useful when you are writing unit tests to test your Flask API.&lt;/p&gt;

&lt;p&gt;Below is a code snippet to test &lt;code&gt;request.form&lt;/code&gt; &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;data = {
  'name': 'Rahul',
  'age': 22
}
print("Request to /receiveFormData")
formResponse = requests.post(
  url+'/receiveFormData',
  data=data,
  headers= {
    'Content-Type': 'application/x-www-form-urlencoded'
  })
print(formResponse.json())&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;Content-Type&lt;/code&gt; is set to &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt; &lt;/p&gt;








&lt;h3&gt;JSON StringData&lt;/h3&gt;





&lt;p&gt;JSON is something that is common to most programming languages so this might be the most common case.&lt;/p&gt;

&lt;h4&gt;How to Receive JSON String Data in Flask&lt;/h4&gt;

&lt;pre&gt;&lt;code&gt;from flask import jsonify

@app.route('/receiveJson',methods=['POST'])
def receivePostData():
  data = jsonify(request.json)
  print(data)
  return  data,200&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can use &lt;code&gt;request.json&lt;/code&gt; to retrieve the data. To convert the json string object to JSON , we will use a Flask method called &lt;code&gt;jsonify&lt;/code&gt; . This will let you access keys similar to how you would do with a dictionary.&lt;/p&gt;

&lt;h4&gt;How to Send JSON String Data using Python&lt;/h4&gt;

&lt;pre&gt;&lt;code&gt;import json

print("Request to /receiveJson")
jsonData={
  'name': 'Rahul',
  'age': 22
}
jsonResponse = requests.post(
  url+'/receiveJson',
  json=json.dumps(jsonData)
)
print(jsonResponse.json())&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We will use &lt;code&gt;json.dumps&lt;/code&gt; to convert the dictionary to a JSON String before attaching it as a payload to the POST request.&lt;/p&gt;

&lt;p&gt;You can directly send the dictionary without converting it into a JSON string as well. In this case, you wouldn’t need to use &lt;code&gt;jsonify&lt;/code&gt; it before returning the response. &lt;/p&gt;








&lt;h3&gt;Files&lt;/h3&gt;





&lt;p&gt;I have created a &lt;a href="https://replit.com/@rahulbanerjee26/FlaskReceive#testFile.txt" rel="noreferrer noopener"&gt;simple text file&lt;/a&gt;. We will attach the text file as a payload to the API endpoint. The API will read the contents of the file and return the content.&lt;/p&gt;

&lt;h4&gt;How to Receive Files in Flask&lt;/h4&gt;

&lt;pre&gt;&lt;code&gt;@app.route('/receiveFile',methods=['POST'])
def receiveFile():
  file= request.files['textFile']
  data = file.read().decode('utf-8')
  print(data)
  return {
    "data": data
  },200&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;request.files&lt;/code&gt; returns an &lt;code&gt;FileStorage&lt;/code&gt; object, as a result, you can directly use &lt;code&gt;read&lt;/code&gt; to get the content of the file. We will need to decode it before sending it as response data.&lt;/p&gt;

&lt;p&gt;Working with files can get somewhat confusing, specifically the following line&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;data = file.read().decode('utf-8')&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Depending on the format of the file, you might not have to decode it. Instead, you might have to use something like &lt;code&gt;json.loads(byteData)&lt;/code&gt; to convert a byte object to JSON.&lt;/p&gt;

&lt;h4&gt;How to Send Files Using Python&lt;/h4&gt;

&lt;pre&gt;&lt;code&gt;print("Request to /receiveFile ")
with open('testFile.txt','r') as f:
  fileResponse = requests.post(
    url+'/receiveFile',
    files={
      'textFile': f 
    }
  )
print(fileResponse.json())&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Depending on if you open it using mode &lt;code&gt;r&lt;/code&gt; or &lt;code&gt;rb&lt;/code&gt; , the code you wrote to create the endpoint will change.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;In this article, we created 4 endpoints for our API. Each endpoint expected a different format of data. I hope you found this article useful.  If you liked this article, you might like another article that &lt;a href="https://www.realpythonproject.com/how-to-authenticate-using-keys-basicauth-oauth-in-python/" rel="noreferrer noopener"&gt;helps you get comfortable working with APIs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>How to Store Credentials as Environment Variables and Read in JavaScript</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Sat, 12 Nov 2022 01:07:21 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-store-credentials-as-environment-variables-and-read-in-javascript-37f1</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-store-credentials-as-environment-variables-and-read-in-javascript-37f1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Article was orginally posted on &lt;a href="https://realjavascriptproject.com/how-to-store-credentials-as-environment-variables-and-read-in-javascript/"&gt;https://realjavascriptproject.com/how-to-store-credentials-as-environment-variables-and-read-in-javascript/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Secrets are called secrets for a reason. We do not want others to know about them, be it in real life or in JavaScript. In this tutorial, you will learn how to store your credentials as environment variables and learn how to read them in JavaScript. We will discuss 3 different ways to store credentials and read them using JavaScript. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing them as Global Environment Variables&lt;/li&gt;



&lt;li&gt;Providing them as a Command Line Argument&lt;/li&gt;



&lt;li&gt;Storing them in a .env file&lt;/li&gt;



&lt;li&gt;Which is the best way to Read Credentials Locally in Javascript?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are interested in a similar tutorial for python, you can &lt;a href="https://www.realpythonproject.com/3-ways-to-store-and-read-credentials-locally-in-python/" rel="noreferrer noopener"&gt;check out this article&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Storing Credentials as Global Environment Variables&lt;/h2&gt;



&lt;p&gt;If the credentials are stored as Global Environment Variables, they can be accessed by any script running on your PC.&lt;/p&gt;

&lt;p&gt;To Create a Global Environment Variable, run this in your terminal&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export varName=varValue&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ensure there are no spaces between the ‘=’. If you get an error &lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;code&gt;“zsh: Bad Assignment”&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bash: export: `=': not a valid identifier&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;it is probably caused because of space between the ‘=’.&lt;/p&gt;

&lt;p&gt;Let’s create a couple of global environment variables&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export globalUser=secretUser&lt;br&gt;export globalKey=secretKey&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Windows, you might have to use “set” instead of “export”.&lt;/p&gt;

&lt;p&gt;Below is how you would access the variables in Node&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const USER= process.env.globalUser
const KEY = process.env.globalKey

console.log(USER) // secretUser
console.log(KEY) // secretKey&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;process.env&lt;/code&gt; returns an object with all your Global Environment Variable.&lt;/p&gt;

&lt;p&gt;You do not need to install any npm library since process is a built-in library in Node. The biggest con of Global Environment Variables is that you risk overwritting previously existing variables. As time goes by, it will be hard to keep track of previously created variables and avoid overwriting.&lt;/p&gt;

&lt;p&gt;If you want to remove the environment variable you created earlier&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;unset globalUser&lt;br&gt;unset globalKey&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Providing Credentials as Command Line Arguments&lt;/h2&gt;

&lt;p&gt;If you want to pass your credentials in the command line&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;globalUser=secretUser&lt;/code&gt; &lt;code&gt;globalKey=secretKey&lt;/code&gt; node app.js&lt;/pre&gt;

&lt;p&gt;You could access them in Node the same you way you accessed the global environment variables&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const USER= process.env.globalUser
const KEY = process.env.globalKey

console.log(USER) // secretUser
console.log(KEY) // secretKey&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A caveat to doing something like this is that you will have to remember to pass the credentials as command-line arguments each time you run your script. &lt;/p&gt;

&lt;h2&gt;Storing Credentials in a .env file&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--il64nh71--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2022/11/image-1-1024x276.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--il64nh71--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://realjavascriptproject.com/wp-content/uploads/2022/11/image-1-1024x276.png" alt="Code Snippet showing how to read environment variables from a .env file " width="880" height="237"&gt;&lt;/a&gt;Code Snippet showing how to read environment variables from a .env file&lt;/p&gt;

&lt;p&gt;You can create a file named &lt;code&gt;.env&lt;/code&gt; and store your credentials over there. Although you can store your credentials in any file and &lt;a href="https://realjavascriptproject.com/how-to-read-and-write-into-files-in-javascript/" rel="noreferrer noopener"&gt;read them from your file&lt;/a&gt;, conventionally credentials are stored in .env files.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;globalUser=secretUser&lt;br&gt;globalKey=secretKey&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;.env files are usually used to store secrets and credentials. Make sure to add it to your &lt;code&gt;.gitignore&lt;/code&gt; file so that you don’t commit it by mistake.&lt;/p&gt;

&lt;p&gt;To read variables from an .env file you will have to install &lt;code&gt;dotenv&lt;/code&gt; which is a npm package.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install dotenv --save&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;dotenv&lt;/code&gt; can be installed as a dev dependency since it will only be used during development to load variables from &lt;code&gt;.env&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;Below is the code to read the variables from the env file&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require('dotenv').config()

const USER= process.env.globalUser
const KEY = process.env.globalKey

console.log(USER) // globalUser
console.log(KEY) // globalKey&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see most of the code is same as before. The only addition being the &lt;code&gt;require&lt;/code&gt; statement at top.&lt;/p&gt;

&lt;h2&gt;Which is the best way to Read Credentials Locally in Javascript?&lt;/h2&gt;

&lt;p&gt;In my opinion, the best way is to store them inside a .env file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They are local to your project&lt;/li&gt;



&lt;li&gt;You do not need to worry about ‘unsetting’ them. &lt;/li&gt;



&lt;li&gt;If you add them to your .gitignore file, it is safe from the outside world&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only con is that if there is a Global Environment Variable with the same name as a variable in the &lt;code&gt;.env&lt;/code&gt; file, &lt;code&gt;process.env&lt;/code&gt; will return the Global Environment Variable.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Do you know these Software Engineering Terms?</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Mon, 17 Oct 2022 04:12:21 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/do-you-know-these-software-engineering-terms-156l</link>
      <guid>https://dev.to/rahulbanerjee99/do-you-know-these-software-engineering-terms-156l</guid>
      <description>&lt;p&gt;I recently saw &lt;a href="https://www.instagram.com/p/Cjp-LoOvO9H/?hl=en" rel="noreferrer noopener"&gt;this reel&lt;/a&gt; by &lt;a href="https://www.instagram.com/tiffintech/?hl=en" rel="noreferrer noopener"&gt;tiffintech&lt;/a&gt; and I went on a ‘Google Ride’ searching for some more funny terms in Software Engineering&lt;/p&gt;

&lt;p&gt;Software engineering is a complex field with its own language. If you’re new to the field, or even if you’ve been around for a while, there are terms that you may not be familiar with. In this blog post, we will explore some of these terms and what they mean. &lt;/p&gt;

&lt;p&gt;Will they help you impress an interviewer? Probably not! &lt;/p&gt;

&lt;h3&gt;Ostrich Algorithm&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AQsIlb8AzVWrZ-MjcSnUG7A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AQsIlb8AzVWrZ-MjcSnUG7A.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@photosimon?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Simon Infanger&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/ostrich?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what exactly is the ostrich algorithm? It’s a strategy that can be used when dealing with errors or other problems in your code. It is a strategy of ignoring potential problems on the basis that they may be exceedingly rare. Essentially, it involves ignoring the problem since it might be more cost-effective to leave the problem as it is as compared to spending time/error trying to solve it.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;to stick one’s head in the sand and pretend there is no problem&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Of course, this isn’t an ideal solution, but it can sometimes be helpful if you’re in a pinch. It can be used to solve &lt;a href="https://thevaishnud.wordpress.com/2020/05/30/ostrich-algorithm/" rel="noreferrer noopener"&gt;deadlocks in concurrent programming&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Ostrich_algorithm" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Yoda Conditions&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AKYENnzVLgNnFVmWm3FueLg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AKYENnzVLgNnFVmWm3FueLg.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@riku?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Riku Lu&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/yoda?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yoda Conditions are a style of coding where the programmer writes conditional statements in reverse or in a non-traditional way. For example, instead of writing:&lt;/p&gt;

&lt;pre&gt;if (x == 5) {&lt;br&gt;   // do something&lt;br&gt;}&lt;/pre&gt;

&lt;p&gt;They would write:&lt;/p&gt;

&lt;pre&gt;if (5 == x) {&lt;br&gt;  // do something&lt;br&gt;}&lt;/pre&gt;

&lt;blockquote&gt;&lt;p&gt;Yoda says “Become powerful you have, the dark side in you I sense.”&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;If you are interested, there is actually an &lt;a href="https://lingojam.com/EnglishtoYoda" rel="noreferrer noopener"&gt;English to Yoda Translator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The thinking behind this is that it’s less likely for a programmer to make a mistake and write x = 5 by accident, which would cause an error. While this may seem like a small thing, it can actually help to prevent a lot of bugs in your code.&lt;/p&gt;

&lt;p&gt;So next time you’re reading some software engineering code and you see an if statement written in reverse, you’ll know that isn’t incorrect and the programmer may just be a star wars fan.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Status 418 I’m a teapot&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AkWG8BXgsd5CTHifL8CwGXw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AkWG8BXgsd5CTHifL8CwGXw.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@joniludlow?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Joni Ludlow&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/teapot?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The term “Status 418 I’m a teapot” is derived from the HTTP status code 418, which is defined as “I’m a teapot”. The code was originally meant to indicate that the server was a teapot, but it has been repurposed to indicate that the client is attempting to brew coffee with a teapot.&lt;/p&gt;

&lt;p&gt;In simpler words, it is used to describe a situation where the client is trying to perform an action that is not possible. For example, if you try to brew coffee in a teapot, it will not work because the pot is not designed for that purpose. Similarly, if you try to do something that is not possible with a piece of software, it will also fail.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;I’m a teapot, I can not brew coffee&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;The term “I’m a teapot” can be used in both positive and negative situations. If you are trying to do something and you know it’s not possible, you can use the term to describe your situation. On the other hand, if someone else tries to do something that’s not possible and they succeed, you can use the term to describe their accomplishment.&lt;/p&gt;

&lt;p&gt;In either case, the term “I’m a teapot” is a way of describing a situation where someone is trying to do something that cannot be done.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Bugs&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2A_Na6jy8-JXpQHOjUHhWkAw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2A_Na6jy8-JXpQHOjUHhWkAw.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/es/@tumbao1949?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;James Wainscoat&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/bugs?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Heisenbug&lt;/h4&gt;

&lt;p&gt;Heisenbug is a term used to describe a software bug that disappears or changes behavior when an attempt is made to study or debug it. The name “heisenbug” is a pun on the Heisenberg Uncertainty Principle.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h4&gt;Hindenbug&lt;/h4&gt;

&lt;p&gt;A bug with catastrophic behavior. Think something along the scale of your entire database being wiped off. &lt;/p&gt;

&lt;p&gt;If you manually execute a SQL Delete statement without a where clause, it is more of a mistake than a bug. However, if you write a function to do the above, now that sounds like a Hindenbug.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h4&gt;The Gangnam Style Bug&lt;/h4&gt;

&lt;p&gt;When Psy release Gangnam style, he was about to literally break Youtube. Youtube initially stored its view count as a 32-bit number, i.e 2,147,483,647 views but Psy had different plans. Although &lt;a href="https://www.theverge.com/2014/12/3/7325819/gangnam-style-broke-youtube-view-counter" rel="noreferrer noopener"&gt;Youtube did update its code before the 2,147,483,648th viewer&lt;/a&gt; saw a weird number of views, this deserves to be on the list of funny terms.&lt;/p&gt;

&lt;p&gt;Youtube decided to move to a 64-bit counter so they are probably safe for a few more years. In case you are wondering, that's Nine Quintillion Views Lol.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://www.vox.com/2014/12/3/7326945/gangnam-style-got-so-many-views-that-it-nearly-broke-youtube" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Type of Code&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AD-cJk413AMal0i_4VAWVvQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AD-cJk413AMal0i_4VAWVvQ.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@imlst?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Valery Fedotov&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/jenga?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Jenga Code&lt;/h4&gt;

&lt;p&gt;Jenga Code is a term used to describe a style of programming that results in the whole thing collapsing when you touch a single block of code. Named after the game where players try to remove wooden blocks from a tower without them falling down.&lt;/p&gt;

&lt;p&gt;If you ever get a feeling that you don’t know what that line/piece of code does but removing it breaks everything down, well you might be working with some Jenga Code.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h4&gt;Spaghetti Code&lt;/h4&gt;

&lt;p&gt; This term is used to describe code that is messy and difficult to read. It’s often the result of bad coding practices or a rushed development process.&lt;/p&gt;

&lt;p&gt;If you’re working on a project with spaghetti code, it can be tough to make sense of it all. But with a little patience and perseverance, you should be able to untangle the mess and get things back on track.&lt;/p&gt;

&lt;h4&gt;Hydra Code&lt;/h4&gt;

&lt;p&gt;A little backstory about Hydra. &lt;/p&gt;

&lt;p&gt;Hydra was the &lt;a href="https://www.greekmythology.com/Myths/Monsters/Lernaean_Hydra/lernaean_hydra.html" rel="noreferrer noopener"&gt;offspring of Typhon and Echidna&lt;/a&gt;, the father and mother of all monsters respectively. Hydra’s lair was the lake of &lt;a href="https://en.wikipedia.org/wiki/Lerna" rel="noreferrer noopener"&gt;Lerna&lt;/a&gt;, which was claimed to be the entrance to the UnderWorld. Heracles’ second of the set of 12 labors was to slay the Lernaean Hydra. Hydra had a &lt;a href="https://en.wikipedia.org/wiki/Regeneration_%28biology%29" rel="noreferrer noopener"&gt;regeneration&lt;/a&gt; feature: for every head chopped off, Hydra would regrow two heads.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://www.greekmythology.com/Myths/Monsters/Lernaean_Hydra/lernaean_hydra.html" rel="noreferrer noopener"&gt;One head off, two heads Grow!&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Heracles eventually managed to cut off all of Hydra’s heads with assistance from Iolaus.&lt;/p&gt;

&lt;p&gt;Hydra Code is code that simply can not be fixed. For every new “fix”, two new bugs get introduced. Your best bet would be to rewrite the code.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h4&gt;Magic Code&lt;/h4&gt;

&lt;p&gt;Magic Code is Code that is usually taken verbatim from websites like StackOverflow. Although it might solve your problem, you don’t know how it works or why it works. It magically solves your problem.&lt;/p&gt;

&lt;h3&gt;Common Law Feature&lt;/h3&gt;

&lt;p&gt;A bug in the application that has existed so long that it is now part of the expected functionality, i.e it is a feature&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;It’s not a bug, it’s a feature&lt;/p&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Pokemon Exception Handling &lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AuGC0GHmVBEaGEU1QLQZQxw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1760%2F1%2AuGC0GHmVBEaGEU1QLQZQxw.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@thimo?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Thimo Pedersen&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/pokemon?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noreferrer noopener"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have watched Pokemon, you must be familiar with it’s catch-phrase&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Gotta Catch ’Em All&lt;/p&gt;&lt;/blockquote&gt;

&lt;pre&gt;&lt;code&gt;try:
    // Some Code
except Exception as Pokemon:
    // print("Catching all Pokemons", Pokemon)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ash Ketchum might have never managed to catch all the Pokemons, but you can!&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://blog.codinghorror.com/new-programming-jargon/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Yak Shaving&lt;/h3&gt;

&lt;p&gt;In software engineering, yak shaving is the term for when you have to do a bunch of small tasks in order to complete a bigger task. It is a task, that leads you to perform another related task, that leads you to perform yet another related task, and so on, and so on — all distracting you from your original goal. This is also sometimes called “going down the rabbit hole.”&lt;/p&gt;

&lt;p&gt;Yak shaving usually happens when you’re trying to do something that depends on something else that isn’t ready yet. For example, if you’re trying to deploy a new feature to your website but your database isn’t migrated yet, you might have to migrate the database first. Or if you’re trying to write a unit test but you can’t because one of the classes doesn’t have an interface yet, you might have to write the interface first.&lt;/p&gt;

&lt;p&gt;Yak shaving can be frustrating because it feels like you’re not making any progress on the main task — you’re just doing a bunch of busy work. But it’s important to remember that every task you complete brings you closer to your goal, and eventually you’ll get there.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://americanexpress.io/yak-shaving/" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Monkey Patch&lt;/h3&gt;

&lt;p&gt;Monkey patching is a technique used in software engineering to make changes to a program or library without changing the original source code. Monkey patches are typically used to add features or fix bugs in a program without waiting for the official release of a new version.&lt;/p&gt;

&lt;p&gt;Monkey patching can be dangerous because it can lead to code that is difficult to maintain and debug. When doing monkey patching, it is important to be aware of the potential side effects of your changes.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Monkey_patch" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;&lt;strong&gt;Faith-based programming&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;When you try running the same piece of code again with hopes that the error just gets fixed magically. &lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://ravirajsblog.blogspot.com/2011/11/funny-programming-terms-that-we-share.html" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;&lt;strong&gt;Hi-driven development&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;For most languages, there are a lot of debuggers at your disposal. You can add breakpoints and do a lot more to make it easier to debug a problem. However, a lot of programmers do not use Debuggers. What we do is, we add random &lt;code&gt;print&lt;/code&gt; or &lt;code&gt;console.log&lt;/code&gt; statements. If you see a statement like the below randomly in a function, there is a good chance the developer was debugging by practicing &lt;code&gt;Hi-driven development&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;console.log(`HI.... ${varValue}`)&lt;/pre&gt;

&lt;p&gt;Most of us have our own version of &lt;code&gt;Hi&lt;/code&gt; , personally, I use &lt;code&gt;Yooo&lt;/code&gt; and a bunch of &lt;code&gt;-x-x-x-x-x-x-x-&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href="https://ravirajsblog.blogspot.com/2011/11/funny-programming-terms-that-we-share.html" rel="noreferrer noopener"&gt;Reference&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Let me know if you know of any other such terms in Software Engineering.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Want to Connect?

Find me on LinkedIn: https://www.linkedin.com/in/rahulbanerjee2699/

Originally Posted on https://www.realpythonproject.com/do-you-know-these-software-engineering-terms/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>codenewbie</category>
      <category>programming</category>
      <category>software</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Use Python to Post on Popular Blogging Websites</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Sun, 25 Sep 2022 23:15:36 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-use-python-to-post-on-popular-blogging-websites-12jp</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-use-python-to-post-on-popular-blogging-websites-12jp</guid>
      <description>&lt;h2&gt;
  
  
  Create once, publish multiple times. Automate the manual work with Python 💯
&lt;/h2&gt;

&lt;p&gt;In this article, we will use Python to automate the following tasks&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creating gists for your code snippets&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Embed gists into the article with different requirements for Wordpress, Medium, Dev.to, Hashnode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Post the Article on WordPress&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Post the Article on Medium with WordPress Canonical Link&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Post the Article on Dev.to with WordPress Canonical Link&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Post the Article on Hashnode with WordPress Canonical Link&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Essentially we will be using Python to automate cross-posting to multiple websites. We will be using various APIs for doing the above tasks.&lt;/p&gt;

&lt;p&gt;You can find all the code &lt;a href="https://github.com/rahulbanerjee26/Automate_Cross_Poster"&gt;in my GitHub repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the Project 💻
&lt;/h2&gt;

&lt;p&gt;First, create a new folder for the project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;crossPoster
&lt;span class="nb"&gt;cd &lt;/span&gt;crossPoster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next, create a virtual environment and activate it&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv

&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Install the required libraries&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip3 &lt;span class="nb"&gt;install &lt;/span&gt;requests, Markdown, python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Create a file named '.env' in your root directory. This will be used to store all your credentials. You will also need a test markdown file to be able to cross-post. In this tutorial, I am going to assume that all the images have public URLs and are not loaded from local files.&lt;/p&gt;
&lt;h2&gt;
  
  
  1️⃣ How to use GitHub API to create Gists for Code Snippets
&lt;/h2&gt;

&lt;p&gt;Before proceeding, we will need a GitHub token. Go to &lt;a href="https://github.com/settings/tokens"&gt;your developer settings token page&lt;/a&gt; to get your GitHub token. When creating the token, make sure the option "Create Gist" is checked. This allows you to create gists using your GitHub token. Copy the token and save it in your .env file&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;GITHUB_TOKEN &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"YOUR_GITHUB_TOKEN"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;First, we will need to write a function to get all the code snippets from a markdown file. We will use a Regex pattern to get the code snippet blocks. For now, ignore the function "getSnippetName", just assume it returns a tuple consisting of the file name and actual code. If the code snippet has a language "bash", we ignore it. You can extend the list of languages to ignore.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We are going to assume that the user will begin their code snippet with a comment and the comment will have the name of the gist (basically like the above code snippet). If no such comment is provided, the gist will have a default name, Eg: index.js for a javascript file, init.py for a python file. Now let's look at the "getSnippetName" function&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It searches for a comment in the first line of the code snippet, if a comment is present it extracts the file name from the first line. If no comment is present it uses a dictionary to return the default file name. You can extend the code to support comments in other languages.&lt;/p&gt;

&lt;p&gt;Now we have an array of tuples, the first element in the tuple is the name of the gist and the second element is the content of the gist. We can use this to create our gists.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We will make a post request to the following endpoint&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;https://api.github.com/gists
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you want the created gists to be private, you can set the parameter "public" to False. The function will return all the URLs of the created gists.&lt;/p&gt;

&lt;p&gt;Now we need to replace the code snippets in the markdown file with the relevant gist URLs. Medium, Wordpress, Dev.to, and Hashnode have different formats to embed gists so let's create a dictionary for that&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Each of the lambda function will accept the gist URL as an argument and return the corresponding embed element. The logic to replace to code snippet is similar to the logic to get all the code snippets&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We keep a counter to keep track of which code snippet corresponds to which gist URL. Since we ignored "bash" when creating the gists, we increment the counter by 1 without actually replacing anything. In all other cases we replace the code snippet with the gist URL at "counter" index and increment counter by 1. This function is going to create new files for Wordpress, Medium, Dev.to, Hashnode.&lt;/p&gt;

&lt;p&gt;Check the below code snippet that uses the above-created functions. It will create public gists for the code snippets present in "index".md" and create a markdown for wordpress, a markdown for medium, etc&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  2️⃣ How to use the Wordpress API to Create a Post
&lt;/h2&gt;

&lt;p&gt;You can find the &lt;a href="https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/"&gt;documentation here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will have to install a plugin to generate an application password for your account. Search for the plugin &lt;a href="https://wordpress.org/plugins/application-passwords/"&gt;Application Password&lt;/a&gt; and install it. After you install it, go to your &lt;code&gt;Users&amp;gt;Profile&lt;/code&gt;. There should be an option called &lt;code&gt;Application Passwords&lt;/code&gt; and a button to &lt;code&gt;Add new application password&lt;/code&gt;. Give it a name and create a new application password. Copy the new password.&lt;/p&gt;

&lt;p&gt;Update your .env file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;WP_USERNAME &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"YOUR ADMIN USERNAME"&lt;/span&gt;
WP_PASSWORD &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"YOUR NEW APPLICATION PASSWORD"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Your wordpress username is the one that is shown in &lt;code&gt;Users&amp;gt;Profile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will create a class to post to wordpress. The wordpress api endpoint is of the following format&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;Your Website Domain&lt;span class="o"&gt;}&lt;/span&gt;/wp-json/wp/v2/&lt;span class="o"&gt;{&lt;/span&gt;route&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Check out the below code snippet that creates the class.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now will have to add some methods to this class that will be used to post to WordPress.&lt;/p&gt;

&lt;p&gt;The first method will be used to upload our cover image or header image to Wordpress.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It has 3 inputs - the path to the image, alternative text, and the caption of the image. A post request has to be made to the following endpoint&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;Your Website Domain&lt;span class="o"&gt;}&lt;/span&gt;/wp-json/wp/v2/media
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The response is going to have a field called &lt;code&gt;id&lt;/code&gt;. This will be used when we are posting our article. It will have another field called &lt;code&gt;jetpack_featured_media_url&lt;/code&gt; that contains the URL to the image.&lt;/p&gt;

&lt;p&gt;Next, we need a method to create tag IDs for our article&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;It has one input - the list of tags. For each tag, it will try to create a new tag. The response is either going to contain the id of the newly created tag. If the tag already exists, the response will contain the existing tag id. We will combine the ids and return them.&lt;/p&gt;

&lt;p&gt;A similar function can be created for categories as well.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, we will add a method to post to wordpress.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The function logic itself is pretty simple, let's look at the json data that we will be adding to our post request&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;title: A string that is the title of your article&lt;/li&gt;
&lt;li&gt;content: Your article's content (in HTML, I'll show you how to convert your markdown file to HTML)&lt;/li&gt;
&lt;li&gt;status: A string "publish" or "draft" depending on if you want to post your article as a draft or not&lt;/li&gt;
&lt;li&gt;categories: The Category IDs returned by the &lt;code&gt;get_category_ids&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;tags: The Tag IDs returned by the &lt;code&gt;get_category_ids&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;featured_media: The ID of the cover/header image returned by the &lt;code&gt;upload_media&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To convert your markdown file to HTML, we will use a library called &lt;code&gt;markdown&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip3 &lt;span class="nb"&gt;install &lt;/span&gt;Markdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We will need to write the following helper function to do the conversion&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Below is a sample usage of the &lt;code&gt;wordpress class&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you are posting a draft, you can create a new variable to store the post slug. The post slug is the URL to your article after it has been posted. The slug should be present in the response object.&lt;/p&gt;

&lt;h2&gt;
  
  
  3️⃣ How to use the Medium API to Create a Post
&lt;/h2&gt;

&lt;p&gt;We will be using Medium's REST API for this part. You can find the documentation &lt;a href="https://github.com/Medium/medium-api-docs"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To get your access token, go to your &lt;a href="https://medium.com/me/settings"&gt;profile settings&lt;/a&gt; and go to &lt;code&gt;Integration Tokens&lt;/code&gt;. Enter a description and click on the 'Get Integration Token' button.&lt;/p&gt;

&lt;p&gt;Save the token in your .env file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;MEDIUM_TOKEN &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Your Meidum Token"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Similar to the class we created for Wordpress, we will create a class for Medium as well.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now let's define the &lt;code&gt;getUserID&lt;/code&gt; method.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This simply makes a GET request to the following endpoint and returns your user ID. Note: this is different from your username.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;https://api.medium.com/v1/me
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, we can add the method to create a medium post&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let's take a look at the json data added to the post request.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;title: The title of your article&lt;/li&gt;
&lt;li&gt;content: The content of your article in HTML&lt;/li&gt;
&lt;li&gt;publishStatus: A string 'draft' , 'published' or 'unlisted'&lt;/li&gt;
&lt;li&gt;contentFormat: In our case this will be 'HTML'&lt;/li&gt;
&lt;li&gt;tags: A list of strings that are the tags of the article.&lt;/li&gt;
&lt;li&gt;notifyFollowers: A boolean value&lt;/li&gt;
&lt;li&gt;canonicalUrl: If applicable, the canonical URL. In my case, I am going to set it to the WordPress Post's URL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's take a look at a sample usage of the class&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  4️⃣ How to use the Dev.to API to Create a Post
&lt;/h2&gt;

&lt;p&gt;The Dev.to implementation is pretty similar and I am not going to go much in detail. You can find your token &lt;a href="https://dev.to/settings/extensions"&gt;here&lt;/a&gt;. Scroll to the bottom to generate a token and update your .env file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;DEVTO_TOKEN &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"GCP1itdmDAmVzfTjEQRRnnFc"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can find the documentation &lt;a href="https://developers.forem.com/api/v0"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the dev.to class&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;For dev.to, we do not need to convert our markdown file to HTML. We can directly use the markdown content.&lt;/p&gt;

&lt;h2&gt;
  
  
  5️⃣ How to use the Hashnode API to Create a Post
&lt;/h2&gt;

&lt;p&gt;Hashnode has a GraphQL API and it took me quite some time to figure out the correct query and how to make the correct request. You should be able to directly use the code snippet shown in this section. You can generate your token &lt;a href="https://hashnode.com/settings/developer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can play around with their API over &lt;a href="https://api.hashnode.com/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the implementation of the Hashnode class&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you are not familiar with GraphQL, try playing with the API in their API Playground. Let's take a look at some of the variables that are passed to the query&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;title: The title of your article.&lt;/li&gt;
&lt;li&gt;content: Your article's content in markdown. Similar to dev.to, you do not need to convert to HTML.&lt;/li&gt;
&lt;li&gt;publicationID: This is a required parameter. If you do not add it to your query, your article won't be listed under your profile but it'll still be posted. It'll be under &lt;code&gt;hashnode.com&lt;/code&gt; and not your personal hasnode blog or account. To find your publication ID, go to your dashboard and copy it from the URL
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;https://hashnode.com/&lt;span class="o"&gt;{&lt;/span&gt;YOUR PUBLICATION ID&lt;span class="o"&gt;}&lt;/span&gt;/dashboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;tags: A list of tags&lt;/li&gt;
&lt;li&gt;canonicalUrl: The Canonical URL of the article&lt;/li&gt;
&lt;li&gt;hideFromHashnodeFeed: The API doesn't support posting drafts (or at least I couldn't find a way to post draft). Setting hideFromHashnodeFeed to True posts the article but it won't show up on anyone else's feed. You can toggle this in the hashnode editor as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Future Improvements
&lt;/h2&gt;

&lt;p&gt;You can find a sample usage of all the above classes to create an "automated pipeline" in this &lt;a href="https://github.com/rahulbanerjee26/Automate_Cross_Poster/blob/main/sample.py"&gt;file in the repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can think of this as a POC to automate your process. It obviously doesn't cover all use cases. Feel free to clone the repo and edit it as you like. Here are some improvements that can be made&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support for local images - The logic should be similar to how code snippets are converted to gists. Either one of the existing APIs can be used or a new API like imgurr can be used&lt;/li&gt;
&lt;li&gt;Extend this to automatically share your article to various subreddits or Twitter as well&lt;/li&gt;
&lt;li&gt;Add support for other blog editors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please let me know in the comments if you found this article useful or if you have any suggestions on features that can be automated, improvements, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;This article was originally posted on 
https://www.realpythonproject.com/how-to-use-python-to-post-on-popular-blogging-websites/

Connect with me on LinkedIn: 
https://www.linkedin.com/in/rahulbanerjee2699/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>tutorial</category>
      <category>automation</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Advanced SQL: Going beyond the Basics</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Tue, 19 Jul 2022 04:15:38 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/advanced-sql-going-beyond-the-basics-890</link>
      <guid>https://dev.to/rahulbanerjee99/advanced-sql-going-beyond-the-basics-890</guid>
      <description>&lt;p&gt;Structured Query Language (SQL) makes a great addition to your toolbox. It may not be as popular as Python or JavaScript, but knowing SQL, especially advanced SQL, offers a number of benefits.&lt;/p&gt;

&lt;p&gt;For one thing, knowing advanced SQL techniques can help you get better insights from your data, so that you can communicate your ideas more effectively. And since SQL is subject to the same performance bottlenecks and performance issues as other languages, a deeper knowledge will help you optimize your queries.&lt;/p&gt;

&lt;p&gt;Finally, additional knowledge will help you stand out from other developers, who might know CRUD operations but not advanced SQL concepts. This gives you a career advantage.&lt;/p&gt;

&lt;p&gt;This article will introduce you to some advanced SQL concepts, including subqueries, running totals, and common table expressions (CTEs). To follow along, you can use any database that supports SQL and an editor of your choice. You can also use an &lt;a href="https://sqliteonline.com/"&gt;online SQL editor&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The following examples use an online SQL editor along with an SQLite database. If you are working with a database such as PostgreSQL, you might have to change the syntax; however, the underlying concepts are the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  UNION, INTERSECT, and EXCEPT Operators
&lt;/h2&gt;

&lt;p&gt;You can use sample data from &lt;a href="https://www.w3resource.com/sql/sql-table.php"&gt;this resource&lt;/a&gt;. In this example, all the commands have been combined into a single script in &lt;a href="https://github.com/rahulbanerjee26/CloudQuery-Sql-data/blob/main/agents.sql"&gt;this repository&lt;/a&gt;. Run the script and it will create three tables with sample data. Below is the schema of the tables:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PEuD0Y0u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ocSDFRO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PEuD0Y0u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ocSDFRO.png" alt="Agents table schema" width="645" height="362"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3resource.com/sql/sql-table.php"&gt;&lt;em&gt;Image courtesy of w3resource&lt;/em&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The first five rows of each table are shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hwgvIpLW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/xrGR34t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hwgvIpLW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/xrGR34t.png" alt="First five rows" width="880" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UNION&lt;/code&gt;, &lt;code&gt;INTERSECT&lt;/code&gt;, and &lt;code&gt;EXCEPT&lt;/code&gt; operators help you combine results from two queries. Operators in SQL are similar to math operators while working with sets. You must remember the following rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The number of columns and the order of the columns in the table returned by the first query must be equal to the number of columns and the order of the columns in the table returned by the second query. Moreover, the data type of the corresponding columns must be compatible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below is the general syntax for using operators in SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;TABLE1&lt;/span&gt;
&lt;span class="k"&gt;OPERATOR&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;TABLE2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  UNION Operator
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;UNION&lt;/code&gt; operator returns a combination of the tables from the two queries. By default, the result will not have duplicate values. However, you can use the &lt;code&gt;UNION ALL&lt;/code&gt; operator if you want to keep the duplicates.&lt;/p&gt;

&lt;p&gt;The following command will return a list of all distinct cities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;working_area&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;AGENTS&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;cust_city&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following command will return a list of all cities, including duplicates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;working_area&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;AGENTS&lt;/span&gt; 
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;cust_city&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is an image comparing the outputs of &lt;code&gt;UNION&lt;/code&gt; and &lt;code&gt;UNION ALL&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SoN-tsZv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/e11nJO0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SoN-tsZv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/e11nJO0.png" alt="UNION vs. UNION ALL" width="880" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, in the right table, London and Bangalore are repeated.&lt;/p&gt;

&lt;h3&gt;
  
  
  INTERSECT Operator
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;INTERSECT&lt;/code&gt; operator returns the common elements in the tables returned by the first and second query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Returns a list of common cities in both columns&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;working_area&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;AGENTS&lt;/span&gt; 
&lt;span class="k"&gt;INTERSECT&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;cust_city&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MR3Gw-n0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/aKCTwdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MR3Gw-n0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/aKCTwdw.png" alt="INTERSECT operator" width="880" height="639"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  EXCEPT Operator
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;EXCEPT&lt;/code&gt; operator returns all the elements in the table returned by the first query that are not also in the table returned by the second query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;working_area&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;AGENTS&lt;/span&gt; 
&lt;span class="k"&gt;EXCEPT&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;cust_city&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command will return a table with a single row containing Vancouver. You’ll note that  Vancouver is the only city that is in &lt;code&gt;CUSTOMER.cust_city&lt;/code&gt; but not in &lt;code&gt;AGENTS.working_area&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Subqueries
&lt;/h2&gt;

&lt;p&gt;In SQL, you can have a query inside a query, also known as a subquery. A subquery can also have a query inside it. Subqueries take three main forms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As part of a &lt;code&gt;SELECT&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;After &lt;code&gt;FROM&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;After &lt;code&gt;WHERE&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Subqueries help you break down complex joins and unions into smaller parts to make your query more readable and maintainable, especially since you can use results from existing queries. Subqueries also make debugging easier, because you can debug each query independently. Understanding subqueries also helps you write recursive SQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subquery with SELECT
&lt;/h3&gt;

&lt;p&gt;Look at the tables named &lt;code&gt;AGENTS&lt;/code&gt; and &lt;code&gt;ORDERS&lt;/code&gt;. You are tasked with returning a table with two columns. The first column should contain the &lt;code&gt;agent_code&lt;/code&gt; from the &lt;code&gt;AGENTS&lt;/code&gt; table, and the second should contain the sum of the &lt;code&gt;ORDERS.ord_amount&lt;/code&gt; column. The second column should have the same value, or the sum repeated multiple times.&lt;/p&gt;

&lt;p&gt;First, return the sum of all order amounts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;ORDERS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command will be your subquery. You could use it as shown below to obtain the required result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; 
      &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;FROM&lt;/span&gt; 
      &lt;span class="n"&gt;ORDERS&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;AGENTS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command in the second line is the subquery. It returns the sum of the &lt;code&gt;ORDERS.ord_amount&lt;/code&gt; column. You can also name the second column, either by using &lt;code&gt;AS&lt;/code&gt; or by including your preferred name after the subquery. Below is a screenshot of the partial result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BnHF7oFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/l5hKbJT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BnHF7oFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/l5hKbJT.png" alt="Partial result" width="880" height="690"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Subquery with FROM
&lt;/h3&gt;

&lt;p&gt;The following SQL command returns a table with the total number of order amounts, an average of order amounts, and the number of orders sold by an agent to a customer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;cust_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;total_amnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;avg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;avg_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;ORDERS&lt;/span&gt; 
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;cust_code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You must use the result to find the total number of orders sold by each agent. You can use the above command as a subquery along with &lt;code&gt;FROM&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; 
      &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="n"&gt;cust_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="k"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;total_amnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="k"&gt;avg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;avg_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; 
    &lt;span class="k"&gt;FROM&lt;/span&gt; 
      &lt;span class="n"&gt;ORDERS&lt;/span&gt; 
    &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; 
      &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="n"&gt;cust_code&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command returns the total number of customers that each agent sold to. Instead of selecting from an existing table, you select from the table returned by your subquery. Below is the partial result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iOzIF7le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/WHe6CDr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iOzIF7le--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/WHe6CDr.png" alt="Partial result" width="880" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Subquery with WHEN
&lt;/h3&gt;

&lt;p&gt;Now use the &lt;code&gt;AGENTS&lt;/code&gt; and &lt;code&gt;CUSTOMER&lt;/code&gt; tables to return a table that only contains common names shared by agents and customers.&lt;/p&gt;

&lt;p&gt;First, return a table with the names of every customer, using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;cust_name&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now use the &lt;code&gt;IN&lt;/code&gt; operator and the table returned by the above query to find the common names. Below is the command, using the previous command as a subquery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;agent_name&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;AGENTS&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; 
  &lt;span class="n"&gt;agent_name&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; 
      &lt;span class="n"&gt;cust_name&lt;/span&gt; 
    &lt;span class="k"&gt;FROM&lt;/span&gt; 
      &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;WHERE&lt;/code&gt; clause checks if the &lt;code&gt;AGENTS.agent_name&lt;/code&gt; is present within the table returned by your subquery. It will return a table with two rows: Benjamin and Charles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Table Expressions (CTEs)
&lt;/h2&gt;

&lt;p&gt;In SQL, you create temporary variables to store the results from your subqueries. These variables are only accessible within the outer query. For the earlier example, in which you returned a table with the number of orders sold by each agent, you can store that result as a CTE to make your queries more readable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;CTE_AGENT_CUSTOMER&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;cust_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;total_amnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;avg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;avg_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; 
  &lt;span class="k"&gt;FROM&lt;/span&gt; 
    &lt;span class="n"&gt;ORDERS&lt;/span&gt; 
  &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; 
    &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;cust_code&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amnt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amnt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;CTE_AGENT_CUSTOMER&lt;/span&gt; 
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; 
  &lt;span class="n"&gt;agent_code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CTEs are required when working with recursive SQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recursive CTEs in SQL
&lt;/h2&gt;

&lt;p&gt;Recursion in SQL is like recursion in any other language. You divide a bigger problem into a smaller problem and keep on solving the smaller problem till you reach the terminating condition. If you don’t provide a terminating condition, it will send your SQL command into an infinite loop.&lt;/p&gt;

&lt;p&gt;The following are two common problems that can be solved using recursion.&lt;/p&gt;

&lt;h3&gt;
  
  
  First N Numbers
&lt;/h3&gt;

&lt;p&gt;Your task is to use recursion to print a table with numbers from one to twenty. Below is the recursive code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;NUMBER_CTE&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="c1"&gt;-- Starting Table&lt;/span&gt;
  &lt;span class="k"&gt;UNION&lt;/span&gt; 
    &lt;span class="c1"&gt;-- Combine Starting Table with Table from Below Query&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;num&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;as&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="c1"&gt;-- Update Rule&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; 
    &lt;span class="n"&gt;NUMBER_CTE&lt;/span&gt; &lt;span class="c1"&gt;-- CTE calling itself, ie recursion&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; 
    &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="c1"&gt;-- Terminating Condition&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="o"&gt;*&lt;/span&gt; 
&lt;span class="k"&gt;from&lt;/span&gt; 
  &lt;span class="n"&gt;NUMBER_CTE&lt;/span&gt; &lt;span class="c1"&gt;-- Displaying the recursive CTE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consider the first and the second query of the &lt;code&gt;UNION&lt;/code&gt; operator separately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="c1"&gt;-- Starting Table&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is a straightforward query that returns a column called &lt;code&gt;num&lt;/code&gt; with the value of &lt;code&gt;1&lt;/code&gt; in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;num&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;as&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="c1"&gt;-- Update Rule&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;NUMBER_CTE&lt;/span&gt; &lt;span class="c1"&gt;-- CTE calling itself, ie recursion&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; 
  &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="c1"&gt;-- Terminating Condition&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query increments the value of column &lt;code&gt;num&lt;/code&gt; by one as long as the original value is less than &lt;code&gt;20&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the first run, the result from the first subquery will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num
------
1
------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the result from the second query will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num
------
2
------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;UNION&lt;/code&gt; operator will combine both tables and update your CTE to the following table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num
------
1
------
2
------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second query, since you select from your CTE, the two queries along with the &lt;code&gt;UNION&lt;/code&gt; operator will be executed again. The first query will again return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num
------
1
------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second query will use the CTE from the previous run and increment each row by one, returning the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num
------
2
------
3
------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;UNION&lt;/code&gt; operator again combines the tables from both queries. The updated CTE will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num
------
1
------
2
------
3
------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process will keep going until the column contains a row with the value &lt;code&gt;20&lt;/code&gt;. In this case, the &lt;code&gt;WHEN&lt;/code&gt; condition won’t be satisfied and you will stop the recursion process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Factorial
&lt;/h3&gt;

&lt;p&gt;Now try a SQL command that creates a table with factorial values. Try to find the factorial of &lt;code&gt;5&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;FACTORIAL&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;NUM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;PRODUCT&lt;/span&gt; &lt;span class="c1"&gt;-- Starting Table&lt;/span&gt;
  &lt;span class="k"&gt;UNION&lt;/span&gt; 
    &lt;span class="c1"&gt;-- Combine Starting Table with Table from Below Query&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;NUM&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;AS&lt;/span&gt; &lt;span class="n"&gt;NUM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;PRODUCT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;NUM&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;PRODUCT&lt;/span&gt; &lt;span class="c1"&gt;-- Update Rule&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; 
    &lt;span class="n"&gt;FACTORIAL&lt;/span&gt; &lt;span class="c1"&gt;-- CTE calling itself, i.e recursion&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; 
    &lt;span class="n"&gt;NUM&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;-- Terminating Condition&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="o"&gt;*&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;FACTORIAL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your initial table should have two columns—one to store the current multiplier and one to store the current product. The product will be &lt;code&gt;1&lt;/code&gt; while the first multiplier will be &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The result from the first query will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num    product

------ ---------
5        1
------ ---------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result from the second query will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num        product
--------   --------------
5-1 = 4     5*1 = 5
--------   --------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new CTE will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num    product
------ ---------
5         1
------ ---------
4         5
------ ---------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the next run, the result from the first query will be the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num    product
------ ---------
5        1
------ ---------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result from the second query will be calculated based on the CTE from the previous run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num         product
---------   ---------
5 - 1 = 4   1 * 5 = 5
---------   ---------
4 - 1 = 3   5 * 4 = 20
---------   ---------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;UNION&lt;/code&gt; operator will combine both tables. The updated CTE will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num     product
------  ---------
5         1
------  ---------
4         5
------  ---------
3        20
------  ---------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process will keep going until you reach the terminating condition.&lt;/p&gt;

&lt;h3&gt;
  
  
  General Syntax for Recursive CTEs
&lt;/h3&gt;

&lt;p&gt;Based on the above problems, you can see the general syntax for recursive CTEs in SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;CTE&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;-- Starting Table&lt;/span&gt;
    &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="c1"&gt;-- Combine Starting Table with Table from Below Query&lt;/span&gt;
    &lt;span class="c1"&gt;-- Updated Table with Update conditions if any&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;CTE&lt;/span&gt;&lt;span class="c1"&gt;-- CTE calling itself, i.e recursion&lt;/span&gt;
    &lt;span class="c1"&gt;-- Terminating Condition&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;CTE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The terminating condition doesn’t necessarily have to be a &lt;code&gt;WHERE&lt;/code&gt; condition; it can also be a &lt;code&gt;JOIN&lt;/code&gt; condition. You’ll look at terminating conditions with &lt;code&gt;JOIN&lt;/code&gt; in the following cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Sample Data
&lt;/h3&gt;

&lt;p&gt;Recursion is commonly used to find hierarchical structures in a company or when working with family trees. You’ll need to create a new table in your database. The first column will be the agent’s code, and the second will be their manager’s agent code. If an agent has no manager, the value in the second column will be &lt;code&gt;NULL&lt;/code&gt;. Below is the hierarchical structure you will be adding to your database:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hb5Zmp89--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ca7bGua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hb5Zmp89--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ca7bGua.png" alt="Hierarchical structure" width="880" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find the script to add the data &lt;a href="https://github.com/rahulbanerjee26/CloudQuery-Sql-data/blob/main/hierarchy.sql"&gt;in this repo&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Task One
&lt;/h3&gt;

&lt;p&gt;Say you’re given an agent’s ID and are asked to return a table with the agent’s manager, skip-manager, skip-skip-manager, etc., all the way to the top. Below is the command to complete this task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt; 
  &lt;span class="k"&gt;from&lt;/span&gt; 
    &lt;span class="k"&gt;HIERARCHY&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt; 
  &lt;span class="k"&gt;WHERE&lt;/span&gt; 
    &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'A006'&lt;/span&gt; 
  &lt;span class="k"&gt;UNION&lt;/span&gt; 
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt; 
  &lt;span class="k"&gt;FROM&lt;/span&gt; 
    &lt;span class="k"&gt;HIERARCHY&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt; 
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AGENT_CODE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="o"&gt;*&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, consider each query individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="k"&gt;HIERARCHY&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; 
  &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'A006'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query returns the agent’s code and their manager’s agent code. Below is the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;agent_code   manager_code
---------   ------------
A006         A005
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now look at the second query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="k"&gt;HIERARCHY&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt; 
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AGENT_CODE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This performs a &lt;code&gt;JOIN&lt;/code&gt; between the &lt;code&gt;HIERARCHY&lt;/code&gt; table and the CTE you created. It tries to find the rows where the &lt;code&gt;agent_code&lt;/code&gt; from &lt;code&gt;HIERARCHY&lt;/code&gt; matches the &lt;code&gt;manager_code&lt;/code&gt; for the CTE. It then returns the agent’s code and their manager’s code, or &lt;code&gt;A006&lt;/code&gt;’s manager and &lt;code&gt;A006&lt;/code&gt;’s skip-manager.&lt;/p&gt;

&lt;p&gt;Below is the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;agent_code  manager_code
---------   ------------
A005        A003
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The union returns the following table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;agent_code  manager_code
---------   ------------
A006        A005
---------   ------------
A005        A003
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second run, the first query will again return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;agent_code   manager_code
---------    ------------
A006           A005
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second query, however, will include &lt;code&gt;A003&lt;/code&gt;’s manager as well. Below is the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;agent_code manager_code
--------- ------------
A005        A003
--------- ------------
A003        A001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;UNION&lt;/code&gt; operator combines the tables. Your updated CTE will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;agent_code    manager_code
---------    ------------
A006           A005
---------    ------------
A005           A003
---------    ------------
A003           A001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;A001&lt;/code&gt; has no manager. In the next run, you will hit the terminating condition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Task Two
&lt;/h3&gt;

&lt;p&gt;In this task, you’ll assign a level to each agent. The agents with no boss are level 1, the agents who report to them are level 2, and so on. Below is the command that returns this table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt; 
  &lt;span class="k"&gt;from&lt;/span&gt; 
    &lt;span class="k"&gt;HIERARCHY&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt; 
  &lt;span class="k"&gt;WHERE&lt;/span&gt; 
    &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; 
  &lt;span class="k"&gt;UNION&lt;/span&gt; 
  &lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;level&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;AS&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt; 
  &lt;span class="k"&gt;from&lt;/span&gt; 
    &lt;span class="k"&gt;HIERARCHY&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt; 
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent_code&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="o"&gt;*&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;HIERARCHY_CTE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is similar to the command from your previous task. The main difference is that you start from the top agent, rather than the bottom.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Totals
&lt;/h2&gt;

&lt;p&gt;Running totals are also known as cumulative sums. Consider the &lt;code&gt;ORDERS&lt;/code&gt; table in your database. It has columns called &lt;code&gt;ord_amount&lt;/code&gt; and &lt;code&gt;ord_date&lt;/code&gt;. Try to calculate the cumulative sum on each date record in the &lt;code&gt;ord_date&lt;/code&gt; column. Below is the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;ord_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; 
      &lt;span class="n"&gt;ord_date&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;Running_Total&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;ORDERS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;OVER&lt;/code&gt; command tells SQL that you want to find the cumulative sum of the &lt;code&gt;ord_amount&lt;/code&gt; column over the whole data set. The column in the &lt;code&gt;ORDER BY&lt;/code&gt; clause is used to sort the results by date.&lt;/p&gt;

&lt;h2&gt;
  
  
  CASE WHEN Statements
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;CASE WHEN&lt;/code&gt; statements in SQL are similar to if-else or switch-case statements in other languages. Below is the general syntax for &lt;code&gt;CASE WHEN&lt;/code&gt; statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CASE&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;CONDITION&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="k"&gt;RESULT&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;CONDITION&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="k"&gt;RESULT&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;CONDITION&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="k"&gt;RESULT&lt;/span&gt;
&lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="k"&gt;RESULT&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;Column_Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result in &lt;code&gt;ELSE&lt;/code&gt; is returned if none of the conditions return true.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;CUSTOMER&lt;/code&gt; table has a column called &lt;code&gt;grade&lt;/code&gt; with values from &lt;code&gt;0-4&lt;/code&gt;. You are tasked with creating a new column called &lt;code&gt;grade_letter&lt;/code&gt; that maps &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;B&lt;/code&gt;, and everything else to &lt;code&gt;F&lt;/code&gt;. You can use &lt;code&gt;CASE WHEN&lt;/code&gt; statements to complete this task. Below is the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="k"&gt;CASE&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;grade&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;THEN&lt;/span&gt; &lt;span class="s1"&gt;'A'&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'B'&lt;/span&gt; &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="s1"&gt;'F'&lt;/span&gt; &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="n"&gt;grade_letter&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; 
  &lt;span class="n"&gt;CUSTOMER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This guide offered you some hands-on experience in working with more advanced SQL topics. Implementing these tools will help you gain more insights from your data so that you can write better queries in SQL.&lt;/p&gt;

&lt;p&gt;If you are working with SQL, you might be interested in &lt;a href="https://www.cloudquery.io/"&gt;CloudQuery&lt;/a&gt;. The SQL-based open source cloud asset inventory integrates with major cloud providers including Azure, AWS, and GCP. You can use it to audit and evaluate your cloud assets from a current and historical perspective.&lt;/p&gt;

&lt;p&gt;For more on what CloudQuery can do for you, &lt;a href="https://docs.cloudquery.io/docs/intro"&gt;check out its documentation&lt;/a&gt;.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This article was originally posted on 
https://www.realpythonproject.com/advanced-sql-going-beyond-the-basics/

Connect with me on LinkedIn: 
https://www.linkedin.com/in/rahulbanerj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>codenewbie</category>
      <category>programming</category>
      <category>sql</category>
      <category>datascience</category>
    </item>
    <item>
      <title>The concept of Triple CCC: Currying, Closure, Callback in JavaScript</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Wed, 06 Jul 2022 04:05:38 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/the-concept-of-triple-ccc-currying-closure-callback-in-javascript-5a14</link>
      <guid>https://dev.to/rahulbanerjee99/the-concept-of-triple-ccc-currying-closure-callback-in-javascript-5a14</guid>
      <description>&lt;p&gt;JavaScript is a programming language that is used in both FrontEnd and BackEnd to build interactive web applications. It is the only scripted language that the browser can understand. As far today, almost 98% of websites use this language on the client-side for web page behavior. It is the most popular programming language in the world.&lt;/p&gt;

&lt;p&gt;But the journey of this language was not so smooth. This language was designed by &lt;code&gt;Brendan Eich&lt;/code&gt; and first appeared on December 4, 1995. At the initial moment, it was only used to bring interactivity to the web pages while the HTML and CSS are used to structure and design the web page.&lt;/p&gt;

&lt;p&gt;The Game-changing moment happens when &lt;code&gt;Ryan Dahl&lt;/code&gt; introduces NodeJS for the first time in 2010. It is nothing but an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.&lt;/p&gt;

&lt;p&gt;Since that moment this language has been used widely. In 2022, it is worth learning this amazing language to build a strong career in the Web Development sector. To make your learning journey smooth and also make your base solid, today, I will discuss the concepts of JavaScript those are Currying, Closure, and Callback (CCC). &lt;/p&gt;

&lt;p&gt;No matter which position you are in now! If you want to make yourself a good JavaScript developer then you need to have a solid understanding of these topics. Moreover, it will help you to crack interview questions because most of the time there’s a common question that interviewers ask about these topics. At the end of this tutorial, you will get a clear understanding of Currying, Closure, and Callback in JavaScript.&lt;/p&gt;

&lt;h2&gt;&lt;strong&gt;Things You will learn from this tutorial&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;We will cover the following topics in this tutorial:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function as First-Class Citizen&lt;/li&gt;
&lt;li&gt;Currying&lt;/li&gt;
&lt;li&gt;Closure&lt;/li&gt;
&lt;li&gt;Callback&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Function as First-Class Citizen&lt;/h2&gt;

&lt;p&gt;Before learning about the CCC’s concepts, you need to understand the concept of a First-Class Citizen. In JavaScript, a function is said to be a First-Class Citizen for the following three reasons&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function can be assigned as a value to a variable&lt;/li&gt;
&lt;li&gt;A function can be passed as an argument to other functions&lt;/li&gt;
&lt;li&gt;A function can be returned by another function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a look at some code snippets that show how a function in JavaScript can satisfy the above three reasons&lt;/p&gt;

&lt;h3&gt;A function can be assigned as a value to a variable&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! `&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: Hello, Rahul!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you can see that we have declared an arrow function and later on, we have stored it into a variable named &lt;code&gt;myFunc&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;A function can be passed as an argument to other functions&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! `&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bonjour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bonjour&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Salut&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bonjour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result_3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&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="nx"&gt;result_1&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="nx"&gt;result_2&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="nx"&gt;result_3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: &lt;/span&gt;
&lt;span class="c1"&gt;// Hello, Rahul! &lt;/span&gt;
&lt;span class="c1"&gt;// Bonjour, Rahul! &lt;/span&gt;
&lt;span class="c1"&gt;// Salut, Rahul! &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of hard-coding "Hello" in our function &lt;code&gt;myFunc&lt;/code&gt;, we now accept a parameter which is a function. As you can see, there are three different functions &lt;code&gt;hello&lt;/code&gt;,&lt;code&gt;bonjour&lt;/code&gt; and &lt;code&gt;salut&lt;/code&gt; and each of them are passed as an argument to &lt;code&gt;myFunc&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;A function can be returned by another function&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outerFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s2"&gt;Outer Function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;innerFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s2"&gt;Inner Function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;innerFunc&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nx"&gt;outerFunc&lt;/span&gt;&lt;span class="p"&gt;()()&lt;/span&gt;
&lt;span class="c1"&gt;// OUTPUT&lt;/span&gt;
&lt;span class="c1"&gt;// Outer Function&lt;/span&gt;
&lt;span class="c1"&gt;// Inner Function&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outerFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// OUTPUT&lt;/span&gt;
&lt;span class="c1"&gt;// Outer Function&lt;/span&gt;
&lt;span class="c1"&gt;// Inner Function&lt;/span&gt;
&lt;span class="c1"&gt;// Inner Function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a function named &lt;code&gt;outerFunc&lt;/code&gt;. It doesn't do much, it simply prints a statement to the console. Another function called &lt;code&gt;innerFunc&lt;/code&gt; is created inside. The &lt;code&gt;innerFunc&lt;/code&gt; also doesn't do much, it simply prints a statement to the console. The &lt;code&gt;outerFunc&lt;/code&gt; returns the &lt;code&gt;innerFunc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are a couple of ways this can be used. The first way is to use two braces together. The first pair of brackets invokes &lt;code&gt;outerFunc&lt;/code&gt; and the second pair of brackets invokes the object returned by &lt;code&gt;outerFunc&lt;/code&gt;, i.e the function &lt;code&gt;innerFunc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the second way, we store the returned function in a variable and invoke it it like a normal function&lt;/p&gt;

&lt;h2&gt;Currying&lt;/h2&gt;

&lt;p&gt;In simple terms, Currying is a technique of chaining multiple functions. It’s a functional programming feature. Let’s understand this concept by solving a common  interview question :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Look at the below code snippet.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;z&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="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;// Output: 60&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The task you need to perform is to call the sum function like sum(10)(20)(30) instead of sum(10, 20, 30) this.&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="c1"&gt;// Current way of Invoking function&lt;/span&gt;
&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Desired way of Invoking function&lt;/span&gt;
&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How can you perform this action? The answer is simple, the term you need to use is Currying. See the below code example&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&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="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;// Output: 60&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you can see that we have declared a function inside another and these have created a function chaining. You can also see we are getting exactly the same output and this process is known as Currying in JavaScript.&lt;/p&gt;

&lt;p&gt;Currying is used in popular javascript libraries such as &lt;a href="https://lodash.com/" rel="noreferrer noopener"&gt;Lodash&lt;/a&gt; and &lt;a href="https://ramdajs.com/" rel="noreferrer noopener"&gt;Rambda&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Closure&lt;/h2&gt;

&lt;p&gt;A closure in JavaScript is nothing but a function that references variables in the outer scope from its inner scope. Let’s see the problem that closure solves in the below section and by doing so, you will be able to know why we need&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;inner&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;inner&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;abc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sayHello&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="nx"&gt;abc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Output: &lt;/span&gt;
&lt;span class="c1"&gt;// [Function: inner]&lt;/span&gt;
&lt;span class="c1"&gt;// Hello Rahul&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you can see that we are getting the output as expected. But the main question is how we are getting this? Let’s break it down in the below section:&lt;/p&gt;

&lt;p&gt;At first, we declared a function named &lt;code&gt;sayHello()&lt;/code&gt; and stored a string inside the &lt;code&gt;name&lt;/code&gt; variable. Later on, we declared another function and print the name with a &lt;code&gt;Hello&lt;/code&gt; message, and returned the &lt;code&gt;inner()&lt;/code&gt; function. Finally, we have stored the returned function into the &lt;code&gt;abc&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Now, when JavaScript will execute the code it will not find the &lt;code&gt;name&lt;/code&gt; variable as the &lt;code&gt;sayHello()&lt;/code&gt; function’s memory has been destroyed. But how we are getting the output as expected?&lt;/p&gt;

&lt;p&gt;The main reason behind this is that Closure. Here, it is true that the &lt;code&gt;sayHello()&lt;/code&gt; function’s memory has been destroyed but it creates a reference of that function and as a result, while executing the &lt;code&gt;inner()&lt;/code&gt; it can access the variable from the reference of the &lt;code&gt;sayHello()&lt;/code&gt; function. To perform this action, you don’t need to write one line of extra code. Because the JavaScript engine is smart enough to perform this action. All you need to do is to understand the concept of closure and it is created by default every time when a function has been created in JavaScript.&lt;/p&gt;

&lt;h2&gt;Callback&lt;/h2&gt;

&lt;p&gt;A callback is a function passed as an argument to another function. Let's look at a use case below.&lt;/p&gt;

&lt;p&gt; Suppose, we have an array of names saved in our database. We want to add a new name and later on, we want to print the names with the newly added name. To perform this action we have written the below code:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rohit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rohan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addNewName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printNames&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="kd"&gt;function&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="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;addNewName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;printNames&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="c1"&gt;// [ 'Rahul', 'Rohit', 'Rohan' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you can see that, we have added the functionality for adding new names but in the output, the new name has not been added. The main reason behind this is the &lt;code&gt;asynchronous&lt;/code&gt; behavior of JavaScript. We know that for tasks that take much time, JavaScript does not wait for them, rather they work on other lighter tasks and keep the heavy task in the queue. The same things happen here. &lt;/p&gt;

&lt;p&gt;We need to use a callback function so that we can break the &lt;code&gt;asynchronous&lt;/code&gt; behavior of JavaScript and it can wait for us based on a specific task. See the below code example to understand it more clearly:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rohit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rohan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addNewName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cb&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printNames&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="kd"&gt;function&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="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;addNewName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;printNames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="c1"&gt;// [ 'Rahul', 'Rohit', 'Rohan', 'Alex' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have passed a function (printNames) as an argument to another function (addNewName). This time &lt;code&gt;printNames&lt;/code&gt; will execute only after the previous functions have been executed. This concept is known as the Callback and you can see that we have called this function after the previous function finished its tasks. That’s the reason we have mentioned Callback as Call later.&lt;/p&gt;

&lt;p&gt;There are many advanced concepts that have been built on top of the Callback. You may also have heard about the concept of &lt;code&gt;Callback hell&lt;/code&gt;. I'll be discussing this in one of the future articles.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In this tutorial, we have covered three essential concepts of JavaScript (Currying, Closure, and Callback). I hope you found this article helpful and informative.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This article was originally posted on 
https://realjavascriptproject.com/javascript-currying-closure-callback/

Connect with me on LinkedIn: 
https://www.linkedin.com/in/rahulbanerjee2699/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Share API Changes with Your Team</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Tue, 05 Jul 2022 02:21:20 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-544c</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-544c</guid>
      <description>&lt;p&gt;If your team is developing or updating an API as part of your software project, you need to be careful about making changes to that API. Those changes could cause unexpected bugs or failures and leave an unpleasant surprise for team members unaware of the changes. A lack of communication or coordination can turn even small issues into bigger ones for your organization.&lt;/p&gt;

&lt;p&gt;It’s important to document and share API changes with your team as well as other stakeholders. In this article, you’ll learn some best practices you can use to share API changes and avoid bugs due to API updates. You’ll also learn about tools like &lt;a href="https://www.useoptic.com/"&gt;Optic&lt;/a&gt; that you can use to track and share API changes.&lt;/p&gt;

&lt;h2&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#what-api-changes-cause-problems"&gt;&lt;/a&gt;What API Changes Cause Problems?&lt;/h2&gt;

&lt;p&gt;The following are some examples of API changes that could cause bugs in your software.&lt;/p&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#changing-the-response-object-of-an-endpoint"&gt;&lt;/a&gt;Changing the Response Object of an Endpoint&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cXeLqUVE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/nRLlbqr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cXeLqUVE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/nRLlbqr.png" alt="" width="880" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assume you have a simple Flask API. On the left is your original API with a single endpoint, which returns information like ID, first name, last name, and full name. You decide that first name and last name are redundant since they can be derived from the full name using a simple split operation; on the right is the same endpoint with the updated response object.&lt;/p&gt;

&lt;p&gt;If one of your team members consumes your API as shown in the code snippet below, the change to your API would break their code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import requests

responseObject = requests.get(apiUrl).json()

print(f"Your id is {responseObject['id']}")

print(f"Your name is {responseObject['fname']} {responseObject['lname']}")&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This sort of bug could also occur if you update the names or data types of some of the fields in your response object.&lt;/p&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#changing-the-name-or-structure-of-an-endpoint"&gt;&lt;/a&gt;Changing the Name or Structure of an Endpoint&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jN0_b-tA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/KhWveiR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jN0_b-tA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/KhWveiR.png" alt="" width="880" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The two code snippets above differ by only a single letter. On the left, the endpoint uses &lt;code&gt;user&lt;/code&gt;, while on the right it uses &lt;code&gt;users&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If a team member consumes the API as shown below, the update would break their code, because the request would be made to a nonexistent URL:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import requests

responseObject = requests.get('www.api.com/user/1').json()

print(f"Your id is {responseObject['id']}")

print(f"Your name is {responseObject['name']}")&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#updating-the-response-code-of-an-endpoint"&gt;&lt;/a&gt;Updating the Response Code of an Endpoint&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LyOFjYIK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/7hzlfJj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LyOFjYIK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/7hzlfJj.png" alt="" width="880" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you update the error response code for one of your API endpoints from &lt;code&gt;500&lt;/code&gt; to &lt;code&gt;404&lt;/code&gt;, it could cause a bug. Consider the code snippet below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import requests

responseObject = requests.get(apiUrl)

if responseObject.status_code == 500:
    # Handle Error
    pass
else:
    # Use data from API
    pass
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If someone implements error handling but checks for a status code of &lt;code&gt;500&lt;/code&gt;, the new response code of &lt;code&gt;404&lt;/code&gt; would not be handled. As a result, the code in the &lt;code&gt;else&lt;/code&gt; block would be executed and lead to an error.&lt;/p&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#making-optional-parameters-required"&gt;&lt;/a&gt;Making Optional Parameters Required&lt;/h3&gt;

&lt;p&gt;If you make optional parameters required, you could potentially run into another issue. Assume you have an endpoint that returns books from a book database. The endpoint accepts an optional parameter for the number of books to be returned, set to ten by default. If you later decide to make the number of books a required parameter, anybody who had previously used your endpoint without passing the optional parameter would face errors.&lt;/p&gt;

&lt;p&gt;The above are just some of the possible problematic changes. Even updating things such as authentication or rate-limiting values can cause bugs in your team members’ code.&lt;/p&gt;

&lt;h2&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#why-share-api-changes-with-your-team"&gt;&lt;/a&gt;Why Share API Changes with Your Team?&lt;/h2&gt;

&lt;p&gt;In addition to the possibility of breaking changes, the following are other reasons to share changes with your team:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Team members could provide valuable feedback that might otherwise get lost. It's important to consider the potential implications of any API change before taking action, and having your team on board will help ensure that you're making the right choice.&lt;/li&gt;
&lt;li&gt;They need to prepare for API changes. This might include testing the changes or making sure that their code is compatible with the new API. They need to be able to account for the changes in their code; otherwise, they could be caught off guard by unexpected issues or errors, which could damage relationships between team members.&lt;/li&gt;
&lt;li&gt;Not only could changes break your API, but they could cause damage to other applications that use the API. If your team didn’t know about the changes, they wouldn’t be prepared to handle the consequences.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#how-should-you-share-api-changes"&gt;&lt;/a&gt;How Should You Share API Changes?&lt;/h2&gt;

&lt;p&gt;There are multiple ways that you can keep your team informed of changes. Here are some options.&lt;/p&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#code-reviews"&gt;&lt;/a&gt;Code Reviews&lt;/h3&gt;

&lt;p&gt;You can set up a code review system to ensure any updates to your API are reviewed by your team members before they’re finalized. This gives your team a chance to identify issues and suggest enhancements.&lt;/p&gt;

&lt;p&gt;Tools like &lt;a href="https://www.useoptic.com/"&gt;Optic&lt;/a&gt; allow you to review these types of changes. Below is a sample API changelog generated by Optic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B0b2bnXQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/CK4a47x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B0b2bnXQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/CK4a47x.png" alt="" width="880" height="381"&gt;&lt;/a&gt;&lt;a href="https://www.useoptic.com/"&gt;&lt;em&gt;Image courtesy of Optic&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Optic also offers a &lt;a href="https://www.useoptic.com/blog/launching-the-optic-gitbot/"&gt;GitBot&lt;/a&gt; that can add API changelogs to every pull request you make:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0eDRMBJV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/RdjYrmy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0eDRMBJV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/RdjYrmy.png" alt="" width="880" height="748"&gt;&lt;/a&gt;&lt;a href="https://www.useoptic.com/"&gt;&lt;em&gt;Image courtesy of Optic&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#communication-channels"&gt;&lt;/a&gt;Communication Channels&lt;/h3&gt;

&lt;p&gt;One way to make sure that your team is aware of any API changes is to use a separate channel for sharing these changes—for instance, a Slack channel or an email list. This will ensure that everyone knows what to expect. Below are a few tips to manage such a channel:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make it a read-only channel to avoid any discussions. Your team should be able to check notifications without having to scroll through other messages.&lt;/li&gt;
&lt;li&gt;Ensure that potential breaking changes are clearly noted. You could add &lt;code&gt;[IMPORTANT]&lt;/code&gt; at the beginning of your message or add a red background.&lt;/li&gt;
&lt;li&gt;Communicate about such changes well in advance. This will give your team time to prepare for them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#api-documentation-updates"&gt;&lt;/a&gt;API Documentation Updates&lt;/h3&gt;

&lt;p&gt;When it comes to API changes, having comprehensive and up-to-date documentation is essential. This will help ensure that everyone on your team understands the changes and knows how to implement them.&lt;/p&gt;

&lt;p&gt;The documentation can either be updated manually or through automated services such as Swagger or Postman. &lt;a href="https://www.useoptic.com/"&gt;Optic&lt;/a&gt;, for instance, analyzes requests, responses, and traffic to your API to more accurately document it and present changes. &lt;a href="https://www.useoptic.com/blog/four-approaches-for-documenting-apis#method-4-documentation-diffs-with-optic"&gt;Read more&lt;/a&gt; about how Optic handles API changes in documentation.&lt;/p&gt;

&lt;h4&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#api-contract-testing"&gt;&lt;/a&gt;API Contract Testing&lt;/h4&gt;

&lt;p&gt;If you are updating your API based on user feedback, you can work with your team or stakeholders to build an API contract describing how your API should function. You can use JSON schemas to document your API’s endpoint response codes and set up the contract. The contract must be accepted by all parties. Then, any changes made to the API must pass the contract test before being approved. Contract testing can be automated and incorporated into your CI/CD pipeline. Contract testing tools include &lt;a href="https://docs.pact.io/"&gt;Pact&lt;/a&gt; and &lt;a href="https://spring.io/projects/spring-cloud-contract"&gt;Spring Cloud Contract&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#documentationdriven-development"&gt;&lt;/a&gt;Documentation-Driven Development&lt;/h4&gt;

&lt;p&gt;This is similar to test-driven development, but instead of writing tests first, you will write the documentation before developing your API. This means any changes made to your API will have to be documented first. You can follow OpenAPI standards to write the documentation, or use a service like &lt;a href="https://www.useoptic.com/"&gt;Optic&lt;/a&gt; to compile the documentation instead of doing it manually.&lt;/p&gt;

&lt;h3&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#versioned-apis"&gt;&lt;/a&gt;Versioned APIs&lt;/h3&gt;

&lt;p&gt;Using versioned APIs allows team members and end users to continue working with an older version of your API while they update their code to work with the newer version. This significantly reduces the impact of any breaking API changes. But it comes at a cost, since you might still have to provide support for older versions. You will also need to maintain additional documentation to ensure users can still use older versions.&lt;/p&gt;

&lt;p&gt;Ensure your API is backward compatible; if a user is able to use a previous version of your API, they should either be able to use future versions of your API without any changes or still be able to use the previous version. This can be done by providing support for both versions of the API and adding a deprecation warning to the old API to let the user know they will have to migrate soon.&lt;/p&gt;

&lt;p&gt;You could also define your API URLs so that the version number is included. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;www.myapi.com/v1/getBooks&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This would ensure that any updates or new releases of your API don’t break your team member’s or end user’s code if they use the previous version.&lt;/p&gt;

&lt;h2&gt;
&lt;a href="https://dev.to/rahulbanerjee99/how-to-share-api-changes-with-your-team-3p3a-temp-slug-8365991/edit#conclusion"&gt;&lt;/a&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Even small changes to your API’s response object or URL can have a big impact on other team members or stakeholders. It’s important to keep your team informed. Having a good workflow to share API changes, document them, and test them will prevent errors or other problems down the line.&lt;/p&gt;

&lt;p&gt;One way to improve your API development workflow is to use &lt;a href="https://www.useoptic.com/"&gt;Optic&lt;/a&gt; to automate testing, documentation, and transparency of changes. The tool tracks and reviews API changes by generating a changelog, then uses requests, responses, and traffic to your API to generate documentation.&lt;/p&gt;

&lt;p&gt;Want to know more? Optic is an open source project, so you can &lt;a href="https://www.useoptic.com/docs/"&gt;join the community&lt;/a&gt; working hard to make writing documentation easy&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Make an AI-Assisted Phone Service with Twilio</title>
      <dc:creator>Rahul Banerjee</dc:creator>
      <pubDate>Fri, 24 Jun 2022 02:51:07 +0000</pubDate>
      <link>https://dev.to/rahulbanerjee99/how-to-make-an-ai-assisted-phone-service-with-twilio-1mbf</link>
      <guid>https://dev.to/rahulbanerjee99/how-to-make-an-ai-assisted-phone-service-with-twilio-1mbf</guid>
      <description>&lt;p&gt;This article was originally posted on&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://www.realpythonproject.com/how-to-make-an-ai-assisted-phone-service-with-twilio/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;If you’ve ever called a business to book an appointment or tried to chat on a website to get your questions answered, there’s a good chance you have already interacted with an AI-assisted phone service. AI-assisted phone services have existed for a long time, and with the &lt;a href="https://www.topbots.com/leading-nlp-language-models-2020/"&gt;recent advancements in natural language processing (NLP)&lt;/a&gt;, they have become more sophisticated.&lt;/p&gt;





&lt;p&gt;AI-assisted phone services can be used in a number of different ways. For example, chatbots can be used to answer customer questions, while interactive voice responses (IVRs) can be used to take orders or book appointments. Additionally, AI-assisted phone services can be used to gather customer feedback and track customer satisfaction. This feedback can then be used to improve customer experience and ensure that customers are happy with the services they receive.&lt;/p&gt;





&lt;p&gt;In this tutorial, you will build a bot that will be used to save a user’s information in a database. This bot will be built using &lt;a href="https://www.twilio.com/"&gt;Twilio&lt;/a&gt; and developed in &lt;a href="https://replit.com/"&gt;Replit&lt;/a&gt;. Replit is a powerful browser-based integrated development environment (IDE) that lets you develop your projects online. You will be using various features provided by Replit, such as their unit testing and their live servers and database. Here is a high-level overview of how the bot will work:&lt;/p&gt;





&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PkErUgdZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/FIEOW3a.png" alt="Architecture of the AI-assisted phone service" width="880" height="329"&gt;





&lt;h2&gt;What Are AI-Assisted Phone Services&lt;/h2&gt;





&lt;p&gt;AI-assisted phone services are powered by voice recognition software and NLP, meaning that they can understand the user’s voice and respond in a way that is natural for humans. This makes the user experience more like talking to a human than using a traditional automated phone service.&lt;/p&gt;





&lt;p&gt;There are two general classes of AI-assisted phone services:&lt;/p&gt;





&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Personal assistant phone services:&lt;/strong&gt; They can do things like provide information about local businesses, book appointments, and connect people with customer service representatives. They are helpful in cases where the user cannot look up the information themselves, such as when driving.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Customer service phone services:&lt;/strong&gt; They provide customer support for businesses. They can take orders, book appointments, cancel reservations, and transfer people to the right department. They are helpful in cases where these tasks need to be done without human intervention or when it would be more practical to do so.&lt;/li&gt;
&lt;/ol&gt;





&lt;p&gt;Here are some of the benefits of AI-assisted phone services:&lt;/p&gt;





&lt;ul&gt;
&lt;li&gt;They are typically cheaper than hiring multiple people to work in shifts and around the clock.&lt;/li&gt;
&lt;li&gt;They have the ability to simultaneously handle multiple customers.&lt;/li&gt;
&lt;li&gt;They can be programmed to automate tasks, like creating tickets and saving users’ feedback.&lt;/li&gt;
&lt;li&gt;They enable users to experience a more efficient and less frustrating customer service experience.&lt;/li&gt;
&lt;/ul&gt;





&lt;p&gt;As mentioned earlier, the most common AI-assisted phone services are chatbots and IVRs. Chatbots are computer programs that can mimic human conversation via text. They are commonly used to simulate a human’s conversation in online customer service. They are also used to communicate with customers on behalf of brands and businesses, such as in a phone app that lets people order food or ask for a local business’ operating hours. In &lt;a href="https://developers.facebook.com/products/messenger/"&gt;Facebook Messenger&lt;/a&gt;, they can be added as a friend and sent messages to make exchanges feel more humanlike. Chatbots can also be used in &lt;a href="https://discord.com/"&gt;Discord&lt;/a&gt; and &lt;a href="https://slack.com/"&gt;Slack&lt;/a&gt;.&lt;/p&gt;





&lt;p&gt;IVRs are computer programs that interact with people by voice. They are also commonly used to provide customer service. IVRs can be used to ask people questions about their accounts or to direct them to the right department, to provide information about a product or service, or even to take orders over the phone. A customer can call an IVR and press the appropriate buttons corresponding to the information they have or need, and the IVRs then automatically direct them to the right department or provide the details the customer has requested.&lt;/p&gt;





&lt;p&gt;Many companies use these technologies in their online support structures. Although it does not completely eliminate the requirement of an actual person for customer support, it makes the process more efficient.&lt;/p&gt;





&lt;h2&gt;Building Your Own AI-Assisted Phone Service&lt;/h2&gt;





&lt;p&gt;This tutorial is divided into two sections. First, you will build your own AI-assisted phone service using Twilio. Second, you will build a Flask API to receive data from the phone service and store it in a database. All the development will be done using &lt;a href="https://replit.com/"&gt;Replit&lt;/a&gt;. You can &lt;a href="https://replit.com/signup"&gt;sign up for free here&lt;/a&gt;.&lt;/p&gt;





&lt;p&gt;Here are some useful links for easy access:&lt;/p&gt;





&lt;ul&gt;
&lt;li&gt;&lt;a href="https://replit.com/@rahulbanerjee26/TwilioAIPhoneServiceAPI?v=1"&gt;API repl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://replit.com/@rahulbanerjee26/TwilioAIPhoneServiceBot?v=1"&gt;Twilio bot repl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/rahulbanerjee26/Twilio-Bot"&gt;GitHub repo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;





&lt;h3&gt;Working with Twilio&lt;/h3&gt;





&lt;p&gt;You will need a &lt;a href="https://www.twilio.com/try-twilio"&gt;Twilio account&lt;/a&gt; to follow along. After signing up, you should be redirected to your dashboard.&lt;/p&gt;





&lt;p&gt;To start, go to &lt;strong&gt;Account&lt;/strong&gt; &amp;gt; &lt;strong&gt;API keys &amp;amp; tokens&lt;/strong&gt;.&lt;/p&gt;





&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SjnVvdWa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/hkoe9ug.png" alt="Twilio Dashboard" width="880" height="504"&gt;





&lt;p&gt;You should see a button to create an API key, which you will need in order to consume Twilio’s API.&lt;/p&gt;





&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nlq57a4---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/z0bZ26I.png" alt="Twilio API" width="880" height="465"&gt;





&lt;p&gt;After you create your API key, you will need to save a couple of strings: the Account SID and the Auth Token.&lt;/p&gt;





&lt;p&gt;Now, create a new Python project on &lt;a href="https://replit.com/"&gt;Replit&lt;/a&gt;.&lt;/p&gt;





&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kio5UGZa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/TVNybYP.png" alt="Create a repl" width="880" height="504"&gt;





&lt;p&gt;Normally, you would have to create an .env file in which to store your credentials. However, Replit has an alternative for this. Click on the &lt;strong&gt;lock&lt;/strong&gt; icon on your sidebar.&lt;/p&gt;





&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JmS8c15---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ANTkUcs.png" alt="Lock icon" width="880" height="504"&gt;





&lt;p&gt;Save your Twilio Account SID (&lt;code&gt;account_sid&lt;/code&gt;) and Auth Token (&lt;code&gt;account_key&lt;/code&gt;), and then scroll down. You should see an option to insert the code to read them using Python.&lt;/p&gt;





&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AvXzy4-l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/UJr0gX6.png" alt="Use secrets in Python" width="521" height="632"&gt;




&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Twilio has a &lt;a href="https://www.twilio.com/docs/libraries/python"&gt;Python wrapper&lt;/a&gt; for their API, and Replit provides a simple way to install packages. On the sidebar, select the cube-like icon and search for Twilio. Click on the &lt;strong&gt;plus&lt;/strong&gt; icon to install the package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8AcdxbHh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6ZVWBFl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8AcdxbHh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6ZVWBFl.png" alt="Install Twilio" width="581" height="685"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will have to create an &lt;a href="https://www.twilio.com/docs/autopilot/api/assistant"&gt;Assistant resource&lt;/a&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can use the code to create the Assistant inside a try-except block to avoid creating a new resource every time you run the script. The code in the except block is run when an Assistant resource with the same unique name is created. In this case, you can simply fetch the existing Assistant resource. You can run the repl to create your own. By default, Replit will run the file named &lt;code&gt;main.py&lt;/code&gt;. However, if you wish to, you can click on &lt;strong&gt;shell&lt;/strong&gt; and run your file using the &lt;code&gt;python3&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oiPs9aBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/vb0dbR2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oiPs9aBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/vb0dbR2.png" alt="Run command" width="880" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replit has a really cool feature that can explain what a code snippet does. Simply highlight the code snippet and click on &lt;strong&gt;Explain Code&lt;/strong&gt;. Note that you will need to upgrade to the &lt;a href="https://replit.com/pricing"&gt;Hacker Plan&lt;/a&gt; in order to use this feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c7xWEJ6i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/gzRYwPI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c7xWEJ6i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/gzRYwPI.png" alt="Explain Code" width="832" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replit will analyze your code comment and generate an explanation for you. You can also create a thread to add comments (similar to how you would do in code reviews). This was the explanation generated for the highlighted code snippet:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1. The code creates a variable called the assistant and sets it to None.

2. The code then tries to create an assistant with the unique name "twilio-bot-repl-assistant".

3. If that fails, then the code fetches the assistant with the unique name "twilio-bot-repl-assistant".&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, it is pretty accurate.&lt;/p&gt;

&lt;p&gt;Next, you can add a task to the above Assistant. A task is basically an action your Assistant resource can perform. You can read more about it in the &lt;a href="https://www.twilio.com/docs/autopilot/api/task"&gt;Twilio Docs&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Like the Assistant resource, you can use a try-except block again to avoid creating multiple tasks. Right now, an empty object is being passed to the actions parameter. This will be updated with the action you want your Assistant resource to perform. You can read more about the actions Twilio supports &lt;a href="https://www.twilio.com/docs/autopilot/actions"&gt;in their docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, we want the Assistant resource to ask the user for their name and phone number. For our use case, we will use the &lt;a href="https://www.twilio.com/docs/autopilot/actions/collect"&gt;&lt;code&gt;collect&lt;/code&gt; action&lt;/a&gt;. It is equivalent to an HTML form element in many ways.&lt;/p&gt;

&lt;p&gt;Below is the syntax for a &lt;code&gt;collect&lt;/code&gt; action:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can add multiple actions, but for now, we just need a single &lt;code&gt;collect&lt;/code&gt; action. You can add multiple questions to a single &lt;code&gt;collect&lt;/code&gt; action. Here’s what mine looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vD1-UBA_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/tnFX8mJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vD1-UBA_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/tnFX8mJ.png" alt="&amp;lt;code&amp;gt;collect&amp;lt;/code&amp;gt; action" width="880" height="470"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I used the &lt;a href="https://www.twilio.com/docs/autopilot/built-in-field-types"&gt;built-in types&lt;/a&gt; provided by Twilio. Optionally, you can define your types by following &lt;a href="https://www.twilio.com/docs/autopilot/api/field-type"&gt;these instructions&lt;/a&gt;. In the value for the key &lt;code&gt;uri&lt;/code&gt; in &lt;code&gt;redirect&lt;/code&gt;, you will notice a variable &lt;code&gt;api_url&lt;/code&gt;. If you have a functioning API endpoint, you can create a variable named &lt;code&gt;api_url&lt;/code&gt; and set it to your API’s URL. If you do not have a functioning API, you do not need to create a variable and can leave the value for &lt;code&gt;uri&lt;/code&gt; as it is. You can follow the subsequent sections to build an API and update the value for &lt;code&gt;uri&lt;/code&gt; later.&lt;/p&gt;

&lt;p&gt;Now that the task is created, you will need to add sample phrases to trigger this task:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can add multiple samples. Twilio recommends adding ten samples per task, but for this tutorial, one will suffice. Whenever the user says “register,” your Assistant resource will perform the collection action you declared earlier.&lt;/p&gt;

&lt;p&gt;Now you can build your model:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This process will take some time. Head over to your dashboard and click on &lt;strong&gt;Explore Products&lt;/strong&gt; &amp;gt; &lt;strong&gt;Autopilot&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6EexY7KV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/m3R7vrG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6EexY7KV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/m3R7vrG.png" alt="Autopilot" width="880" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Your bots&lt;/strong&gt; and check the &lt;strong&gt;Last model build&lt;/strong&gt; time for your Assistant resource.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IkkZroIW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/gPHDqns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IkkZroIW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/gPHDqns.png" alt="Last model build" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the build process is complete, select your model and click on &lt;strong&gt;Simulator&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--szO_vJYh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/fs6AZa6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--szO_vJYh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/fs6AZa6.png" alt="Simulator" width="880" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will allow you to test your model. Since you do not have an API right now, the Assistant will simply collect the information and will not send it to an API. Turn on the &lt;strong&gt;Debug view&lt;/strong&gt; to check which task is being performed by the Assistant. You can also see the values being stored in the variables you created using the &lt;code&gt;collect&lt;/code&gt; action earlier.&lt;/p&gt;

&lt;p&gt;On the right-hand menu, you should see an option called &lt;strong&gt;Channels&lt;/strong&gt;. This will let you add your bot to a Twilio phone number so the bot can work as a chatbot or an IVR.&lt;/p&gt;

&lt;h3&gt;Building the API&lt;/h3&gt;

&lt;p&gt;To start, create a new Python project on Replit using one of their existing templates. In the &lt;code&gt;main.py&lt;/code&gt; file, you should see text asking you if you would like to choose an example. Click on the examples link and select &lt;strong&gt;Server (Flask)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jzZTi_GJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JDofu8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jzZTi_GJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JDofu8u.png" alt="Server (Flask)" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the &lt;strong&gt;Run&lt;/strong&gt; button to run the Flask server. Replit will automatically install all the required dependencies.&lt;/p&gt;

&lt;p&gt;Normally, you would have to use a service like &lt;a href="https://ngrok.com/"&gt;ngrok&lt;/a&gt; to expose your Flask server via a public URL, but Replit handles that for you. As long your server is running, it can be publically accessible by anyone via the URL shown on the right:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---VLCjkGq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/mvlh71r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---VLCjkGq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/mvlh71r.png" alt="URL" width="880" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can set your repl to &lt;strong&gt;Always On&lt;/strong&gt;, which basically means that your Flask server will be running even if your close repl, so in a way, it deploys your Flask server. Note that this feature is only available on the Hacker Plan, so you will have to upgrade if you want to use it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2ztXuHE5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Oin1U7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2ztXuHE5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Oin1U7x.png" alt="**Always On**" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now create a new endpoint that will accept both GET requests and POST requests. A GET request will return the users who have previously registered, while the POST request will save the information of the user. The Twilio bot you created in the previous section will make a POST request.&lt;/p&gt;

&lt;p&gt;Replit has a built-in database that can be used to store the user information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wK3DNSS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Fky9fme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wK3DNSS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Fky9fme.png" alt="Replit DB" width="880" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some helpful code snippets to perform create, read, update, and delete (CRUD) operations will be shown when you click on the database icon, almost like a cheat sheet. Below is the code snippet to handle GET requests:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This code simply reads all the rows in the database and returns them as an array. Replit also allows you to add unit tests. You can add a unit test to verify that the endpoint handles GET requests as expected. The code in the image below creates a simple unit test that checks the response status code of the GET requests and compares the length of the data in the database to the length of the data returned by the GET request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4f_BEt-u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/H87kd2E.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4f_BEt-u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/H87kd2E.png" alt="Unit test" width="880" height="504"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You will have to add a couple of import statements for your unit tests to work. Click on &lt;strong&gt;Setup&lt;/strong&gt; and add the following imports:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pZ719MEb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/IoV5i1h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pZ719MEb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/IoV5i1h.png" alt="Imports" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Run tests&lt;/strong&gt;, and you should see a success message saying that all your unit test cases passed.&lt;/p&gt;

&lt;p&gt;Now add the code to handle the POST requests:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, the Twilio bot sends a deeply nested object. You can print the data in &lt;code&gt;request.form&lt;/code&gt; to check the format of the data. The above code gets the username and phone number, and stores them in the Replit database. Then it returns a Twilio action object so the bot can respond to the user once their information is saved.&lt;/p&gt;

&lt;h3&gt;Adding the API URL to Your Twilio Bot&lt;/h3&gt;

&lt;p&gt;Before testing the bot, you will have to add your API’s endpoint to the Twilio task you created earlier. This can be done using either the Twilio Console or the Twilio API.&lt;/p&gt;

&lt;h4&gt;Using Twilio Console&lt;/h4&gt;

&lt;p&gt;Go to the Twilio Console and navigate to your Twilio bot, as shown in the previous sections. Then go to &lt;strong&gt;Tasks&lt;/strong&gt; and select the task you already created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oapgCsL8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/4d97IrU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oapgCsL8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/4d97IrU.png" alt="Tasks" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Initially, you had the following in your action:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Update the value for &lt;code&gt;uri&lt;/code&gt; with your API’s URL. In my case, it looks like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Save the new task and build the model.&lt;/p&gt;

&lt;h4&gt;Using the Twilio API&lt;/h4&gt;

&lt;p&gt;Alternatively, if you’d prefer, you can update the API URL value using code. The task resource you created earlier has an update method that can be used to pass a new action with your API URL. You can read more about it in the &lt;a href="https://www.twilio.com/docs/autopilot/api/task?code-sample=code-update-a-task-to-set-task-actions&amp;amp;code-language=Python&amp;amp;code-sdk-version=7.x"&gt;Twilio Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Go to the file where you wrote the code for your Twilio bot. In the section where you create a Task resource, replace &lt;code&gt;assistant.tasks.create()&lt;/code&gt; with &lt;code&gt;assistant.tasks.update()&lt;/code&gt;. This will ensure that your script will update the action instead of creating a new one. Update the JSON object to include your API URL. Once you are done, run the script again to update your action and build the model.&lt;/p&gt;

&lt;p&gt;After you have added your API’s URL to your action resource—either via the Twilio Console or the Twilio API—your bot should work as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yir_GRTa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/FEk37dn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yir_GRTa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/FEk37dn.png" alt="Final Twilio bot" width="880" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In this tutorial, you built an AI-assisted phone service using Twilio to collect a user’s name and phone number. You also built an API to save this information inside a database.&lt;/p&gt;

&lt;p&gt;As you’ve likely noticed by now, &lt;a href="https://replit.com/"&gt;Replit&lt;/a&gt; has a lot of useful features that allow you to code online. Although this article only showed its support for Python, it equally supports other programming languages, such as C++, JavaScript, and TypeScript; and frameworks, such as &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; or &lt;a href="https://vuejs.org/"&gt;Vue.js&lt;/a&gt;. It comes with a built-in browser to preview your frontend or API. Additionally, it also has a built-in database and support for unit testing and storing environment variables.&lt;/p&gt;

&lt;p&gt;Furthermore, Replit’s Explain Code feature is able to generate explanations for code snippets. Replit also has support for &lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;, allowing you to add commits or revert to previous ones as needed. Be sure to &lt;a href="https://replit.com/"&gt;check out&lt;/a&gt; this powerful online IDE that can be used to make phone services, among many other things.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
