<?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: SK RAJIBUL</title>
    <description>The latest articles on DEV Community by SK RAJIBUL (@sk_rajibul_9ce58a68c43bb5).</description>
    <link>https://dev.to/sk_rajibul_9ce58a68c43bb5</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%2F952462%2F4328a724-e249-4a90-a9f0-6457069bba82.jpg</url>
      <title>DEV Community: SK RAJIBUL</title>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sk_rajibul_9ce58a68c43bb5"/>
    <language>en</language>
    <item>
      <title>bedrock with postgres</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Thu, 12 Dec 2024 20:17:40 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/bedrock-with-postgres-20p7</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/bedrock-with-postgres-20p7</guid>
      <description></description>
      <category>postgres</category>
      <category>database</category>
    </item>
    <item>
      <title>bedrock with knowledge space</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Sun, 24 Nov 2024 10:03:35 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/bedrock-with-knowledge-space-15e</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/bedrock-with-knowledge-space-15e</guid>
      <description></description>
    </item>
    <item>
      <title>Building Better: Harnessing the Factory Method Pattern in Python</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Thu, 18 Apr 2024 19:36:46 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/building-better-harnessing-the-factory-method-pattern-in-python-56hp</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/building-better-harnessing-the-factory-method-pattern-in-python-56hp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;br&gt;
In the world of software development, there are clever tricks called design patterns that help us solve common problems. One such pattern is the Factory Method Pattern. It's like having a recipe for making different types of things, but letting others tweak the ingredients and steps as needed. In this blog, we'll dive into what the Factory Method Pattern is, when to use it (and when not to), and we'll provide a simple example in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the Factory Method Pattern?&lt;/strong&gt;&lt;br&gt;
The Factory Method Pattern is a way to make objects without specifying the exact type of object beforehand. It's like having a machine that can produce different kinds of products, but you can customize what it makes by changing some settings. This pattern lets us create objects in a flexible and reusable way, without tying our code too closely to specific classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use the Factory Method Pattern:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When you want others to decide what kind of object to create:&lt;/strong&gt; Just like how a recipe can be adjusted to make different flavors of cake, the Factory Method Pattern lets subclasses decide exactly what kind of object to create.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When you want to make objects without knowing all the details:&lt;/strong&gt; Imagine you have a magic box that can create different toys. You don't need to know how the box works; you just ask it for a toy, and it gives you one. That's the essence of the Factory Method Pattern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When you want to keep your code flexible and easy to maintain:&lt;/strong&gt; By using this pattern, you're separating the part of your code that creates objects from the part that uses them. This separation makes it easier to change or add new types of objects without messing up the rest of your code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use the Factory Method Pattern:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When you already know exactly what kind of object you need:&lt;/strong&gt; If you're always making the same type of object and don't need any customization, using the Factory Method Pattern might be overkill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When the creation process is straightforward and doesn't vary much:&lt;/strong&gt; If making objects is as simple as following a basic recipe with no room for customization, you might not need the extra complexity of the Factory Method Pattern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When adding the pattern makes your code harder to understand:&lt;/strong&gt; Sometimes, adding design patterns can make your code harder to read and maintain, especially if they're not really needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Simple Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;Woof!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;Meow!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&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;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;dog&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="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&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="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Usage
&lt;/span&gt;&lt;span class="n"&gt;animal1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;dog&lt;/span&gt;&lt;span class="sh"&gt;"&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;animal1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Woof!
&lt;/span&gt;
&lt;span class="n"&gt;animal2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;"&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;animal2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Meow!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;create_animal&lt;/code&gt; function acts as the factory method. Depending on the kind of animal requested, it creates and returns an instance of either a &lt;code&gt;Dog&lt;/code&gt; or &lt;code&gt;Cat&lt;/code&gt; class. This way, we can create animals without worrying about the specifics of how each one is made.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
The Factory Method Pattern is a handy tool in a developer's toolkit. It's like having a magic box that can create different things based on your needs. By delegating the responsibility of object creation to subclasses, it keeps your code flexible, maintainable, and ready for whatever comes its way. Just remember, like any tool, it's best used when it actually solves a problem, not just for the sake of using it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>developer</category>
      <category>development</category>
    </item>
    <item>
      <title>Python's MAP/FILTER/REDUCE: Streamlining Data Manipulation in Seconds</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Wed, 17 Apr 2024 19:30:16 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/pythons-mapfilterreduce-streamlining-data-manipulation-in-seconds-34e5</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/pythons-mapfilterreduce-streamlining-data-manipulation-in-seconds-34e5</guid>
      <description>&lt;p&gt;Introduction:&lt;br&gt;
Python offers powerful built-in functions for working with collections of data: MAP, FILTER, and REDUCE. In just 30 seconds, let's dive into what these functions do and how they can simplify your code.&lt;/p&gt;
&lt;h2&gt;
  
  
  MAP:
&lt;/h2&gt;

&lt;p&gt;The map function takes in 1) a function and 2) an iterable. The purpose of the function is to apply some sort of transformation to each element inside our iterable (think list). It then applies the function to every element in the iterable, and returns a new iterable.&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;# Double each number in a list
&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&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;doubled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [2, 4, 6, 8, 10]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  FILTER:
&lt;/h2&gt;

&lt;p&gt;The filter function takes in 1) a function and 2) an iterable. The purpose of the function is to decide which iterables to keep or to discard in the new iterable. It does not apply any transformation on the elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;mylist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&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;larger5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;

&lt;span class="n"&gt;newlist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;larger5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mylist&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;newlist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# [6, 7, 8]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;each function call returns either True or False&lt;/li&gt;
&lt;li&gt;if True is returned, the original element is kept&lt;/li&gt;
&lt;li&gt;if False is returned, the original element is discarded&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  REDUCE:
&lt;/h2&gt;

&lt;p&gt;REDUCE applies a rolling computation to pairs of items in an iterable and returns a single result. It's like gradually combining items together until you get a final result. For 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="n"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pear&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pineapple&lt;/span&gt;&lt;span class="sh"&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;add&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;reduce&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;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fruits&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;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# apple-orange-pear-pineapple
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;  add('apple', 'orange') returns 'apple-orange'&lt;/li&gt;
&lt;li&gt;    add('apple-orange', 'pear') returns 'apple-orange-pear'&lt;/li&gt;
&lt;li&gt;    add('apple-orange-pear', 'pineapple') returns 'apple-orange-pear-pineapple'&lt;/li&gt;
&lt;li&gt;    And this is why we get 'apple-orange-pear-pineapple' in the end&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
In just 100 seconds, you've learned about Python's MAP, FILTER, and REDUCE functions. These powerful tools can help you manipulate data in a concise and expressive way, making your code more readable and efficient. Happy coding!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>development</category>
      <category>developer</category>
    </item>
    <item>
      <title>Simplifying Test Execution with pytest.main()</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Wed, 17 Apr 2024 19:07:08 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/simplifying-test-execution-with-pytestmain-2hc2</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/simplifying-test-execution-with-pytestmain-2hc2</guid>
      <description>&lt;p&gt;Introduction:&lt;br&gt;
In the world of software development, testing is an integral part of ensuring the quality and reliability of your code. Automated testing frameworks like pytest have gained popularity due to their simplicity, flexibility, and powerful features. One common practice is to run tests using the pytest command line interface (CLI). However, did you know that you can also programmatically run pytest tests within your Python code? In this blog post, we'll explore how to use pytest.main() to execute tests directly from your Python code, offering greater control and automation in your testing workflow.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is pytest.main()?
&lt;/h2&gt;

&lt;p&gt;pytest.main() is a function provided by the pytest framework that allows you to programmatically invoke pytest and execute tests from within your Python code. Instead of running tests from the command line interface, you can integrate pytest directly into your scripts or applications, providing greater flexibility and automation in your testing process.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Use pytest.main()?
&lt;/h2&gt;

&lt;p&gt;Using pytest.main() offers several benefits over running tests from the command line:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automation&lt;/strong&gt;: You can automate the execution of tests as part of your continuous integration (CI) pipeline, build process, or other automated workflows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration&lt;/strong&gt;: You can seamlessly integrate testing into your Python applications, allowing for tighter coupling between testing and development processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Control&lt;/strong&gt;: You have fine-grained control over the test execution process, allowing you to customize behavior and handle test results programmatically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: You can dynamically generate test parameters, select specific tests to run based on conditions, and perform other advanced testing scenarios.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Getting Started with pytest.main()
&lt;/h2&gt;

&lt;p&gt;To get started with pytest.main(), you'll need to have the pytest framework installed in your Python environment. You can install pytest via pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once pytest is installed, you can use pytest.main() to execute tests programmatically. Here's a simple 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;pytest&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;pytest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;path/to/tests_directory&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, replace &lt;code&gt;"path/to/tests_directory"&lt;/code&gt; with the path to the directory containing your test files. When you run this script, pytest will discover and run all test files in the specified directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Usage and Options
&lt;/h2&gt;

&lt;p&gt;pytest.main() supports a variety of options and arguments that you can use to customize test execution. For example, you can pass additional command line options to pytest, such as specifying test markers, filtering tests by name, enabling plugins, and more. 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;pytest&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;pytest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;path/to/tests_directory&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-v&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--cov=your_module&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--cov-report=html:coverage_report&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, we're passing options to enable verbose output (&lt;code&gt;-v&lt;/code&gt;) and measure test coverage using the pytest-cov plugin (&lt;code&gt;--cov=your_module&lt;/code&gt;, &lt;code&gt;--cov-report=html:coverage_report&lt;/code&gt;). Adjust these options according to your testing requirements.&lt;/p&gt;

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

&lt;p&gt;In this blog post, we've explored how to use pytest.main() to programmatically execute tests from within your Python code. By leveraging pytest.main(), you can automate testing, integrate testing into your applications, and gain greater control and flexibility over your testing process. Whether you're building a CI pipeline, developing a testing framework, or simply looking to streamline your testing workflow, pytest.main() offers a powerful tool for simplifying test execution and ensuring the quality of your software.&lt;/p&gt;

&lt;p&gt;Happy testing!&lt;/p&gt;

</description>
      <category>pytest</category>
      <category>testing</category>
      <category>testautomation</category>
      <category>unittest</category>
    </item>
    <item>
      <title>Leveraging Python's Built-In Decorator for Improved Performance</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Wed, 17 Apr 2024 19:03:02 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/leveraging-pythons-built-in-decorator-for-improved-performance-2pkd</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/leveraging-pythons-built-in-decorator-for-improved-performance-2pkd</guid>
      <description>&lt;p&gt;In Python, decorators provide a powerful mechanism to modify or enhance the behavior of functions or methods. One particularly useful decorator for performance optimization is &lt;code&gt;@cache&lt;/code&gt;, which is available in Python 3.9 and later versions. This decorator automatically caches the results of function calls, reducing redundant computations and improving overall performance.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll explore how to utilize the &lt;code&gt;@cache&lt;/code&gt; decorator effectively, determine the optimal &lt;code&gt;maxsize&lt;/code&gt; parameter, quantify the performance improvements, and identify scenarios where using &lt;code&gt;@cache&lt;/code&gt; may not be suitable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing the @cache Decorator
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;@cache&lt;/code&gt; decorator is a built-in feature introduced in Python 3.9, available in the &lt;code&gt;functools&lt;/code&gt; module. It offers a simplified interface compared to &lt;code&gt;functools.lru_cache&lt;/code&gt;, automatically caching function results with sensible defaults.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Computing Fibonacci Numbers
&lt;/h2&gt;

&lt;p&gt;Let's illustrate the usage of the &lt;code&gt;@cache&lt;/code&gt; decorator with an example of computing Fibonacci numbers, similar to the previous 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;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;

&lt;span class="nd"&gt;@cache&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;else&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;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Test the function
&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;fibonacci&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="c1"&gt;# Output: 55
&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;fibonacci&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="c1"&gt;# Output: 6765
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we decorate the &lt;code&gt;fibonacci&lt;/code&gt; function with &lt;code&gt;@cache&lt;/code&gt;. This automatically caches the results of function calls, eliminating redundant computations and improving performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Determining the Optimal maxsize Parameter
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;@cache&lt;/code&gt; decorator accepts an optional &lt;code&gt;maxsize&lt;/code&gt; parameter, which determines the maximum number of results to cache. Determining the optimal &lt;code&gt;maxsize&lt;/code&gt; depends on factors such as available memory, the nature of the function, and the expected usage pattern.&lt;/p&gt;

&lt;p&gt;For memory-bound applications, setting &lt;code&gt;maxsize&lt;/code&gt; to a lower value conserves memory but may result in cache evictions and increased computation. Conversely, setting &lt;code&gt;maxsize&lt;/code&gt; too high may lead to excessive memory usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quantifying Performance Improvements
&lt;/h2&gt;

&lt;p&gt;To quantify the performance improvements achieved by using &lt;code&gt;@cache&lt;/code&gt;, you can measure the execution time of the function with and without caching enabled. Python's &lt;code&gt;timeit&lt;/code&gt; module is a useful tool for benchmarking performance:&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="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;

&lt;span class="c1"&gt;# Fibonacci function with @cache decorator
&lt;/span&gt;&lt;span class="nd"&gt;@cache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# No limit on cache size
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fibonacci_cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;else&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;fibonacci_cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci_cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Fibonacci function without caching
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;else&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;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Measure execution time with caching
&lt;/span&gt;&lt;span class="n"&gt;time_with_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fibonacci_cached(30)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Measure execution time without caching
&lt;/span&gt;&lt;span class="n"&gt;time_without_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fibonacci(30)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Calculate performance improvement
&lt;/span&gt;&lt;span class="n"&gt;performance_improvement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time_without_cache&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;time_with_cache&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;time_without_cache&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&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;Performance improvement with caching (maxsize=None): &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;performance_improvement&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;%&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Measure execution time with caching (maxsize=128)
&lt;/span&gt;&lt;span class="n"&gt;time_with_cache_maxsize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fibonacci_cached(30)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Calculate performance improvement
&lt;/span&gt;&lt;span class="n"&gt;performance_improvement_maxsize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time_without_cache&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;time_with_cache_maxsize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;time_without_cache&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&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;Performance improvement with caching (maxsize=128): &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;performance_improvement_maxsize&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;%&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;output of the program&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;# Performance improvement with caching (maxsize=None): 100.00%
# Performance improvement with caching (maxsize=128): 99.98%
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first line indicates the performance improvement achieved with caching when &lt;code&gt;maxsize=None&lt;/code&gt;. The improvement is &lt;code&gt;100.00%&lt;/code&gt;, indicating that all function calls were cached, resulting in a significant reduction in execution time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The second line indicates the performance improvement achieved with caching when &lt;code&gt;maxsize=128&lt;/code&gt;. The improvement is &lt;code&gt;99.98%&lt;/code&gt;, indicating that caching was still highly effective even with a limited cache size of 128.&lt;/p&gt;
&lt;h2&gt;
  
  
  When Not to Use @cache
&lt;/h2&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While &lt;code&gt;@cache&lt;/code&gt; can greatly improve performance for many scenarios, there are cases where it may not be suitable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non-deterministic Functions&lt;/strong&gt;: Functions with non-deterministic behavior (e.g., functions that depend on external state or produce different results on each call) are not suitable for caching.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large or Variable Input Space&lt;/strong&gt;: Caching may not be effective for functions with a large or variable input space, where caching all possible inputs consumes excessive memory or where most inputs are unique.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mutating Functions&lt;/strong&gt;: Functions that mutate external state or have side effects should not be cached, as caching may lead to unexpected behavior or incorrect results.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In this blog post, we've explored how to leverage Python's &lt;code&gt;@cache&lt;/code&gt; decorator for improved performance. By automatically caching function results, &lt;code&gt;@cache&lt;/code&gt; reduces redundant computations and enhances overall efficiency. We've also discussed how to determine the optimal &lt;code&gt;maxsize&lt;/code&gt; parameter, quantify performance improvements, and identify scenarios where using &lt;code&gt;@cache&lt;/code&gt; may not be suitable.&lt;/p&gt;

&lt;p&gt;Next time you encounter computationally expensive functions in your Python code, consider using &lt;code&gt;@cache&lt;/code&gt; to optimize performance effortlessly and enhance the responsiveness of your applications.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>performance</category>
      <category>development</category>
    </item>
    <item>
      <title>Coding Laws That Would Make You A Decent Programmer — All about comments🚀</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Tue, 09 Apr 2024 07:59:42 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/coding-laws-that-would-make-you-a-decent-programmer-all-about-comments-1ndi</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/coding-laws-that-would-make-you-a-decent-programmer-all-about-comments-1ndi</guid>
      <description>&lt;p&gt;These practices differentiate the pro from the amateure and a lot of them can also be adapted for various programming languages.&lt;/p&gt;

&lt;p&gt;Avoid using comments as much as possible. Comments can sometimes say things that aren’t true, making it harder for people to understand what the code really does. This can cause problems when the code gets changed or updated. Eventually, the comment might not match the code anymore, and then people have to figure out what’s really going on.&lt;/p&gt;

&lt;p&gt;A comment signifies that the writer was mentally incapable of providing a well-descriptive class, function, or variable name. It exposes the lackluster attitude of the programmer and forces the team to inherit such an attitude. That way, people can understand the code without needing extra explanations.&lt;/p&gt;

&lt;p&gt;example how bad comments are used to explain the code:&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;# Program to calculate the factorial of a number
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&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;# Function to calculate factorial
&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;1&lt;/span&gt; &lt;span class="c1"&gt;# Initialize result variable
&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="c1"&gt;# Loop from 1 to n
&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="c1"&gt;# Multiply result by current number
&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="c1"&gt;# Return factorial
&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;5&lt;/span&gt; &lt;span class="c1"&gt;# Input number
&lt;/span&gt;&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Calculate factorial
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Factorial&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="ow"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Print result
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the code works and the comments provide some clarity, it’s considered better practice to use descriptive names whenever possible to make the code more readable and understandable without relying heavily on comments.&lt;/p&gt;

&lt;p&gt;Here’s the same program with clear and descriptive names without the need for comments:&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;calculate_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="n"&gt;factorial_result&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;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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;factorial_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;factorial_result&lt;/span&gt;

&lt;span class="n"&gt;input_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;result_factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_number&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="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Factorial&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="ow"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result_factorial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using descriptive names, the code becomes self-explanatory, eliminating the need for comments to clarify its functionality.&lt;/p&gt;

&lt;p&gt;When Is A Comment Bad?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Noise comments:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# adds to animal list
&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This comment is unnecessary because it merely repeats what the code itself already clearly states. It adds no useful information and only clutters the code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Non-local information:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Calculate total price (global variable)
&lt;/span&gt;&lt;span class="n"&gt;total_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This comment refers to a global variable &lt;code&gt;total_price&lt;/code&gt;, which is not directly related to the calculation happening in this specific line of code. It introduces information that is not relevant to the local context.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Unobvious comments:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Increment count (unclear without context)
&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This comment lacks clarity because it doesn’t explain why the &lt;code&gt;count&lt;/code&gt; variable is being incremented. Without proper context, it’s unclear what the purpose of this action is.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Short functions:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&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;calculate_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&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;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This function is short and straightforward, and its purpose is evident from its name &lt;code&gt;calculate_area&lt;/code&gt;. It doesn’t require a comment because the function name itself adequately describes its functionality.&lt;/p&gt;

&lt;p&gt;Situations Where Comments Are Acceptable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Informative comments that clarify the workings of code, especially when well-descriptive names aren’t sufficient.&lt;/li&gt;
&lt;li&gt;TODO comments indicating unfinished tasks or areas for improvement, which should be addressed in the future.&lt;/li&gt;
&lt;li&gt;Comments warning about potential consequences or pitfalls in the code, such as resource-intensive operations.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def perform_database_cleanup():
    """
    Perform cleanup operations on the database.

    This function deletes old records from the database.
    WARNING: This operation is irreversible and may result in data loss.

    Returns:
        None
    """
    # WARNING: Deleting old records from the database
    # This operation is resource-intensive and may affect database performance
    # Use with caution
    # Cleanup logic goes here
    pass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Comments that provide essential information for understanding the code’s purpose or behavior, especially in complex or non-obvious situations.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>development</category>
      <category>programming</category>
      <category>cleancode</category>
      <category>python</category>
    </item>
    <item>
      <title>🚀 Coding Laws That Would Make You A Decent Programmer — All about comments🚀</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Tue, 09 Apr 2024 07:58:14 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/coding-laws-that-would-make-you-a-decent-programmer-all-about-comments-1316</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/coding-laws-that-would-make-you-a-decent-programmer-all-about-comments-1316</guid>
      <description>&lt;p&gt;These practices differentiate the pro from the amateure and a lot of them can also be adapted for various programming languages.&lt;/p&gt;

&lt;p&gt;Avoid using comments as much as possible. Comments can sometimes say things that aren’t true, making it harder for people to understand what the code really does. This can cause problems when the code gets changed or updated. Eventually, the comment might not match the code anymore, and then people have to figure out what’s really going on.&lt;/p&gt;

&lt;p&gt;A comment signifies that the writer was mentally incapable of providing a well-descriptive class, function, or variable name. It exposes the lackluster attitude of the programmer and forces the team to inherit such an attitude. That way, people can understand the code without needing extra explanations.&lt;/p&gt;

&lt;p&gt;example how bad comments are used to explain the code:&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;# Program to calculate the factorial of a number
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&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;# Function to calculate factorial
&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;1&lt;/span&gt; &lt;span class="c1"&gt;# Initialize result variable
&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="c1"&gt;# Loop from 1 to n
&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="c1"&gt;# Multiply result by current number
&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="c1"&gt;# Return factorial
&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;5&lt;/span&gt; &lt;span class="c1"&gt;# Input number
&lt;/span&gt;&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Calculate factorial
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Factorial&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="ow"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Print result
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the code works and the comments provide some clarity, it’s considered better practice to use descriptive names whenever possible to make the code more readable and understandable without relying heavily on comments.&lt;/p&gt;

&lt;p&gt;Here’s the same program with clear and descriptive names without the need for comments:&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;calculate_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="n"&gt;factorial_result&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;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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="n"&gt;factorial_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;factorial_result&lt;/span&gt;

&lt;span class="n"&gt;input_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;result_factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_number&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="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Factorial&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="ow"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result_factorial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using descriptive names, the code becomes self-explanatory, eliminating the need for comments to clarify its functionality.&lt;/p&gt;

&lt;p&gt;When Is A Comment Bad?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Noise comments:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# adds to animal list
&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This comment is unnecessary because it merely repeats what the code itself already clearly states. It adds no useful information and only clutters the code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Non-local information:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Calculate total price (global variable)
&lt;/span&gt;&lt;span class="n"&gt;total_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This comment refers to a global variable &lt;code&gt;total_price&lt;/code&gt;, which is not directly related to the calculation happening in this specific line of code. It introduces information that is not relevant to the local context.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Unobvious comments:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Increment count (unclear without context)
&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This comment lacks clarity because it doesn’t explain why the &lt;code&gt;count&lt;/code&gt; variable is being incremented. Without proper context, it’s unclear what the purpose of this action is.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Short functions:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&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;calculate_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&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;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: This function is short and straightforward, and its purpose is evident from its name &lt;code&gt;calculate_area&lt;/code&gt;. It doesn’t require a comment because the function name itself adequately describes its functionality.&lt;/p&gt;

&lt;p&gt;Situations Where Comments Are Acceptable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Informative comments that clarify the workings of code, especially when well-descriptive names aren’t sufficient.&lt;/li&gt;
&lt;li&gt;TODO comments indicating unfinished tasks or areas for improvement, which should be addressed in the future.&lt;/li&gt;
&lt;li&gt;Comments warning about potential consequences or pitfalls in the code, such as resource-intensive operations.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def perform_database_cleanup():
    """
    Perform cleanup operations on the database.

    This function deletes old records from the database.
    WARNING: This operation is irreversible and may result in data loss.

    Returns:
        None
    """
    # WARNING: Deleting old records from the database
    # This operation is resource-intensive and may affect database performance
    # Use with caution
    # Cleanup logic goes here
    pass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Comments that provide essential information for understanding the code’s purpose or behavior, especially in complex or non-obvious situations.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>development</category>
      <category>programming</category>
      <category>cleancode</category>
      <category>python</category>
    </item>
    <item>
      <title>Mastering Efficient String Matching Techniques in Python using regex</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Sun, 07 Apr 2024 07:13:41 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/mastering-efficient-string-matching-techniques-in-python-using-regex-2m23</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/mastering-efficient-string-matching-techniques-in-python-using-regex-2m23</guid>
      <description>&lt;p&gt;Python, with its built-in re module, provides robust support for regex operations. One of the most crucial aspects of regex is the utilization of quantifiers. Understanding and mastering quantifiers can significantly enhance your ability to craft precise and efficient regex patterns for string matching tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Asterisk (*) Quantifier:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The asterisk quantifier matches zero or more occurrences of the preceding character or group.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "ab*c"&lt;/code&gt; will match "ac", "abc", "abbc", "abbbc", and so on.
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ab*c&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ac&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abb&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Plus (+) Quantifier:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The plus quantifier matches one or more occurrences of the preceding character or group.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "ab+c"&lt;/code&gt; will match "abc", "abbc", "abbbc", and so on, but not "ac".
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ab+c&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ac&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abb&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Question Mark (?) Quantifier:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The question mark quantifier matches zero or one occurrence of the preceding character or group.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "ab?c"&lt;/code&gt; will match "ac" and "abc", but not "abbc".
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ab?c&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ac&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abb&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Curly Braces ({}) Quantifier:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The curly braces quantifier allows specifying the exact number of occurrences or a range of occurrences of the preceding character or group.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "ab{2,4}c"&lt;/code&gt; will match "abbc", "abbbc", and "abbbbc", but not "ac" or "abc".
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ab{2,4}c&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ac&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abb&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certainly! Parentheses &lt;code&gt;()&lt;/code&gt; and square brackets &lt;code&gt;[]&lt;/code&gt; serve distinct purposes in regular expressions (regex).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Parentheses &lt;code&gt;()&lt;/code&gt;and square brackets &lt;code&gt;[]&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;parentheses are used for grouping and creating capturing  groups, while square brackets define character classes for matching specific sets of characters. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Combining Parentheses and Square Brackets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can combine parentheses and square brackets to create complex patterns that involve grouping and character matching simultaneously.&lt;/li&gt;
&lt;li&gt;For instance, &lt;code&gt;"(ab)*[0-9]+"&lt;/code&gt; will match zero or more occurrences of "ab" followed by one or more digits.
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(ab)*[0-9]+&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abab123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: ['ab', 'ab']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Combining quantifiers with other regex elements and with each other allows you to create highly flexible and intricate patterns for matching strings.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Combining Quantifiers with Character Classes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can use character classes (denoted by square brackets []) along with quantifiers to match a specific set of characters occurring a variable number of times.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "[a-z]+[0-9]*"&lt;/code&gt; will match strings containing one or more lowercase letters followed by zero or more digits.
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[a-z]+[0-9]*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abc123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;def&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;xyz456&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello123world&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;789&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Combining Quantifiers with Anchors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Anchors such as &lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; can be combined with quantifiers to match patterns at the beginning or end of a string.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "^ab+c$"&lt;/code&gt; will match strings that start with "a", followed by one or more "b"s, and end with "c".
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;^ab+c$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abbbbc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abcc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Combining Multiple Quantifiers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple quantifiers can be combined to create complex repetition patterns.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;pattern = "a{2,4}b{3,5}c{1,}"&lt;/code&gt; will match strings containing 2 to 4 "a"s followed by 3 to 5 "b"s, and at least one "c".
&lt;/li&gt;
&lt;/ul&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;re&lt;/span&gt;

&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a{2,4}b{3,5}c{1,}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aabbbccc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aaabbbbccc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aaaabbbbccc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aaaaabbbbccc&lt;/span&gt;&lt;span class="sh"&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;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Escaping special characters with a backslash is necessary when matching them literally.
&lt;/h2&gt;

&lt;p&gt;In regular expressions (regex), many characters have special meanings, such as &lt;code&gt;.&lt;/code&gt; (dot), &lt;code&gt;*&lt;/code&gt; (asterisk), &lt;code&gt;+&lt;/code&gt; (plus), &lt;code&gt;?&lt;/code&gt; (question mark), &lt;code&gt;{&lt;/code&gt; (left curly brace), &lt;code&gt;[&lt;/code&gt; (left square bracket), &lt;code&gt;(&lt;/code&gt; (left parenthesis), &lt;code&gt;)&lt;/code&gt; (right parenthesis), &lt;code&gt;\&lt;/code&gt; (backslash), etc. These special characters are used to define patterns for matching text.&lt;/p&gt;

&lt;p&gt;If you want to match these characters literally, as they appear in the text, you need to escape them using a backslash (&lt;code&gt;\&lt;/code&gt;). This tells the regex engine to interpret them as ordinary characters rather than their special regex meanings.&lt;/p&gt;

&lt;p&gt;Here's an example demonstrating the need to escape special characters:&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;re&lt;/span&gt;

&lt;span class="c1"&gt;# Pattern to match a literal dot (.)
&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end\.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# The backslash (\) escapes the dot
&lt;/span&gt;
&lt;span class="c1"&gt;# Test strings
&lt;/span&gt;&lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;enddot&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Loop through strings and check for matches
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;strings&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;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&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="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;Match found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, without escaping the dot, the pattern &lt;code&gt;"end."&lt;/code&gt; would match any string starting with "end" followed by any single character, which is not the intended behavior. By escaping the dot with a backslash (&lt;code&gt;"end\."&lt;/code&gt;), we ensure that the pattern matches "end." specifically.&lt;/p&gt;

</description>
      <category>regex</category>
      <category>python</category>
      <category>softwaredevelopment</category>
      <category>coding</category>
    </item>
    <item>
      <title>Building Resilient Serverless Architectures on AWS: Leveraging Lambda, Dead Letter Queue, and Event Bridge</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Mon, 25 Mar 2024 20:01:24 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/building-resilient-serverless-architectures-on-aws-leveraging-lambda-dead-letter-queue-and-event-bridge-1hal</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/building-resilient-serverless-architectures-on-aws-leveraging-lambda-dead-letter-queue-and-event-bridge-1hal</guid>
      <description>&lt;h3&gt;
  
  
  Objective:
&lt;/h3&gt;

&lt;p&gt;The objective of this blog is to demonstrate how to construct a robust serverless environment on Amazon Web Services (AWS) by leveraging key services such as AWS Lambda, Dead Letter Queue (DLQ), and EventBridge. Through detailed instructions, code examples, and explanations, this blog aims to equip readers with the knowledge and understanding necessary to implement fault-tolerant architectures that can handle failures gracefully.&lt;/p&gt;

&lt;p&gt;Below is a diagram illustrating the design of the serverless architecture using AWS services such as Lambda, Dead Letter Queue (DLQ), and EventBridge.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         +----------------+          +---------------------+
         |                |          |                     |
         |   EventBridge  +--------&amp;gt; |   Lambda Function   |
         |                |          |                     |
         +-------+--------+          +----------+----------+
                 |                              |
                 |                              |
                 |                              |
                 |       +---------------+      |
                 |       |               |      |
                 +-----&amp;gt; | Dead Letter   | &amp;lt;----+
                         | Queue (DLQ)   |
                         |               |
                         +---------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;EventBridge&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Serves as the central event bus that receives events from various sources within your AWS environment.&lt;/li&gt;
&lt;li&gt;Acts as the trigger for invoking the Lambda function.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lambda Function&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receives events from EventBridge and executes business logic or processing tasks.&lt;/li&gt;
&lt;li&gt;Configured with a Dead Letter Queue (DLQ) to capture events that result in failures during execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dead Letter Queue (DLQ)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Acts as a safety net for capturing events that could not be processed successfully by the Lambda function.&lt;/li&gt;
&lt;li&gt;Stores failed events for later analysis and processing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Design Flow:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Event Generation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Events are generated within the AWS environment or externally and are sent to EventBridge.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Event Processing&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EventBridge forwards the events to the Lambda function based on configured rules.&lt;/li&gt;
&lt;li&gt;The Lambda function processes the events and executes the associated business logic.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Failure Handling&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the Lambda function encounters errors during execution, it forwards the failed events to the Dead Letter Queue (DLQ) for further analysis.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Error Resolution&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another Lambda function or process periodically checks the DLQ for failed events.&lt;/li&gt;
&lt;li&gt;Failed events are retrieved from the DLQ, processed, and potentially retried for successful execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Benefits:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fault Tolerance&lt;/strong&gt;: The architecture can gracefully handle failures by capturing failed events and providing mechanisms for error resolution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Leveraging serverless components like Lambda and EventBridge enables automatic scaling based on event load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliability&lt;/strong&gt;: By using managed services like DLQ, the architecture enhances the reliability of event processing and ensures no event is lost even in case of failures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This design offers a robust and scalable solution for building event-driven applications on AWS, ensuring high availability and fault tolerance in handling events within your system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Examples:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. CloudFormation Template for Lambda Function and Dead Letter Queue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;MyLambdaFunction&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::Lambda::Function&lt;/span&gt;
    &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;Handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;index.handler&lt;/span&gt;
      &lt;span class="na"&gt;Runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3.8&lt;/span&gt;
      &lt;span class="na"&gt;Code&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;ZipFile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;import json&lt;/span&gt;
          &lt;span class="s"&gt;def handler(event, context):&lt;/span&gt;
              &lt;span class="s"&gt;# Your Lambda function code goes here&lt;/span&gt;
              &lt;span class="s"&gt;pass&lt;/span&gt;
      &lt;span class="na"&gt;Timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;60&lt;/span&gt;
      &lt;span class="na"&gt;DeadLetterConfig&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;TargetArn&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!GetAtt&lt;/span&gt; &lt;span class="s"&gt;MyDeadLetterQueue.Arn&lt;/span&gt;

  &lt;span class="na"&gt;MyDeadLetterQueue&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::SQS::Queue&lt;/span&gt;

  &lt;span class="na"&gt;MyEventBridgeRule&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::Events::Rule&lt;/span&gt;
    &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;EventPattern&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;source&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;aws.events&lt;/span&gt;
        &lt;span class="na"&gt;detail-type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Scheduled&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Event"&lt;/span&gt; &lt;span class="c1"&gt;# You can modify this based on your event type&lt;/span&gt;
      &lt;span class="na"&gt;State&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ENABLED&lt;/span&gt;
      &lt;span class="na"&gt;Targets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Arn&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!GetAtt&lt;/span&gt; &lt;span class="s"&gt;MyLambdaFunction.Arn&lt;/span&gt;
          &lt;span class="na"&gt;Id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Target1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Lambda Function to Process Dead Letter Queue
&lt;/h4&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;boto3&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lambda_handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;sqs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sqs&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;queue_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;YOUR_QUEUE_URL&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="c1"&gt;# Receive messages from the queue
&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;sqs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;receive_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;QueueUrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;queue_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;MaxNumberOfMessages&lt;/span&gt;&lt;span class="o"&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;VisibilityTimeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;WaitTimeSeconds&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Messages&lt;/span&gt;&lt;span class="sh"&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;for&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Process the message
&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;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Body&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

        &lt;span class="c1"&gt;# Delete the message from the queue
&lt;/span&gt;        &lt;span class="n"&gt;receipt_handle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ReceiptHandle&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;sqs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;QueueUrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;queue_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;ReceiptHandle&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;receipt_handle&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;CloudFormation Template&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines a Lambda function (&lt;code&gt;MyLambdaFunction&lt;/code&gt;) with a specified runtime (Python 3.8), timeout, and a Dead Letter Queue configuration pointing to &lt;code&gt;MyDeadLetterQueue&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Creates a Dead Letter Queue (&lt;code&gt;MyDeadLetterQueue&lt;/code&gt;) where failed Lambda invocations will be sent.&lt;/li&gt;
&lt;li&gt;Sets up an EventBridge rule (&lt;code&gt;MyEventBridgeRule&lt;/code&gt;) that triggers &lt;code&gt;MyLambdaFunction&lt;/code&gt; based on specified event patterns.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lambda Function&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This Lambda function (&lt;code&gt;lambda_handler&lt;/code&gt;) is responsible for processing messages from the Dead Letter Queue.&lt;/li&gt;
&lt;li&gt;It retrieves messages from the queue, processes them, and deletes them from the queue once processed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By integrating these components into your AWS architecture, you can ensure the reliability and fault tolerance of your serverless applications, thereby providing a seamless experience for your users.&lt;/p&gt;

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

&lt;p&gt;In conclusion, creating a resilient serverless environment on AWS is essential for building highly available and fault-tolerant applications. By utilizing services like Lambda, Dead Letter Queue, and EventBridge, developers can design architectures that automatically respond to events, recover from failures, and scale seamlessly based on demand. Through careful planning and implementation, organizations can unlock the full potential of serverless computing on AWS, enabling them to deliver reliable and scalable solutions to their customers.&lt;/p&gt;

&lt;p&gt;With the knowledge gained from this blog, readers are empowered to architect and deploy resilient serverless applications on AWS, paving the way for innovation and growth in the cloud-native landscape.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>infrastructureascode</category>
      <category>python</category>
    </item>
    <item>
      <title>Elevate Your DevOps and Cloud Engineering Journey</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Mon, 25 Mar 2024 19:30:30 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/elevate-your-devops-and-cloud-engineering-journey-49d0</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/elevate-your-devops-and-cloud-engineering-journey-49d0</guid>
      <description>&lt;p&gt;In the rapidly evolving landscape of DevOps and Cloud Engineering, mastering tools is just one piece of the puzzle. True mastery lies in understanding the foundational building blocks that sustain your expertise in the long run. Let’s delve into these essential pillars:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Decoding Networking:
&lt;/h2&gt;

&lt;p&gt;Understanding the intricacies of networking is paramount. Networking Essentials:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;TCP/IP: The fundamental protocol suite governing communication on the internet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DNS: Translates domain names to IP addresses, enabling easy access to websites.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HTTP/S: Protocols for transferring hypertext (web) data securely over the internet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;VPNs: Virtual Private Networks provide secure connections over public networks, ensuring privacy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Load Balancers: Distribute incoming network traffic across multiple servers to optimize resource utilization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Firewalls: Act as barriers between internal networks and external networks, filtering traffic based on predefined security rules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Network Protocols: Rules and conventions for communication between devices on a network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Subnetting: Dividing a single network into smaller, manageable subnetworks for better organization and security.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Navigating Database Choices:
&lt;/h2&gt;

&lt;p&gt;Choosing the right database is more than a coin toss. Explore the difference of SQL vs. NoSQL, ACID Properties, Scalability, and Data Modeling to make informed decisions tailored to your project’s needs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;SQL vs. NoSQL: Structured Query Language (SQL) databases are relational, while NoSQL databases are non-relational, offering flexibility and scalability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ACID Properties: Consistency, Isolation, Durability, and Atomicity ensure reliability and integrity of transactions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scalability: The ability of a system to handle increasing workload by adding resources or scaling horizontally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data Modeling: Designing the structure of databases to accurately represent real-world entities and relationships.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Crafting Secure Systems:
&lt;/h2&gt;

&lt;p&gt;Security is non-negotiable. Learn the principles of secure system design, encompassing Encryption, Authentication, Authorization, OWASP Top 10, Security Policies and Risk Assessment.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Encryption: Converting data into a ciphertext to prevent unauthorized access.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authentication: Verifying the identity of users or systems attempting to access resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authorization: Granting or denying access rights to authenticated users based on their privileges.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OWASP Top 10: Common security risks in web applications, such as injection attacks and broken authentication.Ex-&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Injection: Sending untrusted data to an interpreter, leading to SQL, NoSQL, or OS command injection.&lt;/li&gt;
&lt;li&gt;Broken Authentication: Weak or default passwords, session fixation, mishandling credentials.&lt;/li&gt;
&lt;li&gt;Sensitive Data Exposure: Exposing credit card numbers, passwords, or personal data due to weak encryption or access controls.&lt;/li&gt;
&lt;li&gt;XML External Entities (XXE): Parsing XML input insecurely, enabling access to sensitive files or denial-of-service attacks.&lt;/li&gt;
&lt;li&gt;Broken Access Control: Lack of proper authorization checks, insecure object references.&lt;/li&gt;
&lt;li&gt;Security Misconfiguration: Default settings, unnecessary services, or missing security headers.&lt;/li&gt;
&lt;li&gt;Cross-Site Scripting (XSS): Injecting malicious scripts into web pages for client-side attacks.&lt;/li&gt;
&lt;li&gt;Insecure Deserialization: Deserializing untrusted data without validation, leading to remote code execution.&lt;/li&gt;
&lt;li&gt;Using Components with Known Vulnerabilities: Using outdated or vulnerable software components.&lt;/li&gt;
&lt;li&gt;Insufficient Logging and Monitoring: Inadequate practices hindering detection and response to security incidents.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Security Policies: Guidelines and procedures defining how organizations protect their assets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Risk Assessment: Evaluating potential threats and vulnerabilities to determine security risks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Unraveling Storage Mysteries:
&lt;/h2&gt;

&lt;p&gt;Storage is not one-size-fits-all. Discover the realms of Block, Object, File, NAS, SAN, and Cloud Storage (AWS S3, Azure Blob), alongside the distinctions between SSD and HDD.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Block, Object, File Storage: Different methods of storing and accessing data, each suited for specific use cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NAS, SAN: Network Attached Storage and Storage Area Network provide centralized storage resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cloud Storage: Services like AWS S3 and Azure Blob offer scalable, durable storage in the cloud.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SSD vs. HDD: Solid State Drives are faster but more expensive, while Hard Disk Drives offer higher capacity at a lower cost.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Architecting Disaster Recovery:
&lt;/h2&gt;

&lt;p&gt;Prepare for the worst with different DR strategies like Backup and Restore, Pilot Light, Warm Standby, Multi-site setups, and grasp the significance of RTO and RPO.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Backup and Restore: Regularly copying data to secure storage and restoring it in case of data loss.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pilot Light: Keeping essential components running at a minimal level to quickly scale up during a disaster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Warm Standby: Maintaining a partially active secondary system ready to take over operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multi-site setups: Distributing resources across multiple locations to ensure redundancy and resilience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RTO and RPO: Recovery Time Objective and Recovery Point Objective define the acceptable downtime and data loss in a disaster scenario.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Mastering Data Replication:
&lt;/h2&gt;

&lt;p&gt;Data replication is the backbone of resilience. Explore techniques like Master-Slave, Peer-to-Peer, Synchronous vs. Asynchronous replication, ensuring data consistency across various topologies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Master-Slave: One database (master) replicates data changes to other databases (slaves).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Peer-to-Peer: Multiple databases replicate data changes bidirectionally among each other.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Synchronous vs. Asynchronous: Data replication occurs either in real-time (synchronous) or with a delay (asynchronous), balancing performance and consistency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data Consistency: Ensuring that replicated data remains accurate and up-to-date across all nodes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Harnessing the Power of Caching:
&lt;/h2&gt;

&lt;p&gt;Boost performance with caching mechanisms such as In-memory Caches (Redis, Memcached), CDNs, and understand the nuances of Cache Invalidation, Write-through vs. Write-back Cache, and Cache Hit Ratio optimization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In-memory Caches: Storing frequently accessed data in memory for faster retrieval, using tools like Redis and Memcached.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CDNs: Content Delivery Networks distribute cached content to servers closer to the end-users, reducing latency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cache Invalidation: Updating or removing cached data when it becomes stale or invalid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write-through vs. Write-back Cache: Strategies for managing data updates in cache.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cache Hit Ratio: Measure of how often requested data is found in the cache, indicating cache effectiveness.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools and frameworks may evolve, but the fundamentals endure. Without mastering these essentials, your journey in DevOps and Cloud Engineering remains incomplete.&lt;/p&gt;

&lt;p&gt;Remember, greatness is built on fundamentals. 🌟 #DevOps #CloudEngineering #FundamentalsMatter&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>devops</category>
      <category>systemdesign</category>
      <category>security</category>
    </item>
    <item>
      <title>Loops and Vectorization in Python</title>
      <dc:creator>SK RAJIBUL</dc:creator>
      <pubDate>Sat, 16 Mar 2024 19:07:42 +0000</pubDate>
      <link>https://dev.to/sk_rajibul_9ce58a68c43bb5/loops-and-vectorization-in-python-2hpb</link>
      <guid>https://dev.to/sk_rajibul_9ce58a68c43bb5/loops-and-vectorization-in-python-2hpb</guid>
      <description>&lt;p&gt;Using Loops:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import time
start = time.time ()
# iterative sum
total =0
# iterating through 1.5 Million numbers
for item in range(0, 1500000) :
        total = total + item
        print('sum is:' + str(total))
end = time. time ()
print(end - start)

#1124999250000
#0.14 Seconds


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

&lt;/div&gt;



&lt;p&gt;Using Vectorization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

start = time.time ()
# vectorized sum - using numpy for vectorization
# np.arange create the sequence of numbers from 0 to #1499999
print(np. sum(np.arange (1500000)))
end = time.time
print(end - start)

##1124999250000
##0.008 Seconds

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

&lt;/div&gt;



&lt;p&gt;Vectorization took ~18x less time to execute as compared to the iteration using the range function.&lt;/p&gt;

&lt;p&gt;This difference will become more significant while working with Pandas DataFrame.&lt;/p&gt;

</description>
      <category>pythton</category>
      <category>datascience</category>
      <category>developers</category>
      <category>dataengineering</category>
    </item>
  </channel>
</rss>
