<?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: Arka Bhowmik</title>
    <description>The latest articles on DEV Community by Arka Bhowmik (@javalin).</description>
    <link>https://dev.to/javalin</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%2F331288%2Ff2a19ff2-2c78-4cb7-9ad6-68bbe590443d.jpeg</url>
      <title>DEV Community: Arka Bhowmik</title>
      <link>https://dev.to/javalin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/javalin"/>
    <language>en</language>
    <item>
      <title>API-Log Management | ETL -Vector | Searching and Debugging in Production :</title>
      <dc:creator>Arka Bhowmik</dc:creator>
      <pubDate>Wed, 03 Mar 2021 06:46:31 +0000</pubDate>
      <link>https://dev.to/javalin/api-log-management-etl-vector-searching-and-debugging-in-production-23af</link>
      <guid>https://dev.to/javalin/api-log-management-etl-vector-searching-and-debugging-in-production-23af</guid>
      <description>&lt;p&gt;Liquid syntax error: 'raw' tag was never closed&lt;/p&gt;
</description>
      <category>webdev</category>
      <category>linux</category>
      <category>nginx</category>
      <category>microservices</category>
    </item>
    <item>
      <title>HyperCubed !! Easy State Management simplifying 2^n conditions</title>
      <dc:creator>Arka Bhowmik</dc:creator>
      <pubDate>Wed, 24 Jun 2020 09:13:42 +0000</pubDate>
      <link>https://dev.to/javalin/hypercubed-easy-state-management-simplifying-2-n-conditions-5bai</link>
      <guid>https://dev.to/javalin/hypercubed-easy-state-management-simplifying-2-n-conditions-5bai</guid>
      <description>&lt;p&gt;Do you have backend code looking like this ? or something similar?&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;if&lt;/span&gt; &lt;span class="n"&gt;condition1&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;condition1&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;do&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;2.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condtion2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This arises when you have unplanned state management arising from unstructured conditions. Having too many nested conditions will affect readability and mantainability of your code. So lets simplify this. Lets have no branching at all . &lt;/p&gt;

&lt;h1&gt;
  
  
  NOTE:
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;The approach followed below assumes that the states we are working with are somewhat related to one another.&lt;br&gt;
Also, this approach is only useful for cases where the nested branching level is &amp;gt;=3&lt;br&gt;
The approach focuses on identifying the core components of a condition and minimize redundant checks and transitions&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The simplicity of Bit Logic:
&lt;/h3&gt;

&lt;p&gt;We formulate each condition as a state. Let's try with an example where you have 8 different outcomes of your problem which means that there are 8 different output states. Let's number them from State0, State1, State2 to State7.&lt;br&gt;
So the binary representation of every state, based on its index will be from  [000 to 111]. For our simple example these states are the human states of mind:&lt;br&gt;
[HAPPY ,IRRITATED ,BUSY ,ANGRY, LONELY, BAD, SAD ,EVIL]&lt;br&gt;
Based on our mood, we will perform some action. But do we need 8 branching conditions for this ? Also, what happens if our mental state changes to a different mood, we write new code for that ? NEVER AGAIN !!!!&lt;/p&gt;
&lt;h3&gt;
  
  
  Finding a relation between the states by Filtering CORE conditions/states:
&lt;/h3&gt;

&lt;p&gt;Our motive is simple, we need to plan our states in such a way that every bit corresponding to the state's index, has some information associated with it. For this we map all states in a visualization space and see the interdependencies the states have between eachother. The interdependencies will be such that if one state leads to another state by changing a core condition/state, we can say that they are separated by one bit or 1 &lt;a href="https://www.tutorialspoint.com/what-is-hamming-distance#:~:text=Hamming%20distance%20is%20a%20metric,d(a%2Cb)."&gt;hamming distance&lt;/a&gt;.&lt;br&gt;
 Lets clear this up by looking at the diagram of the &lt;a href="https://en.wikipedia.org/wiki/Hypercube#:~:text=In%20geometry%2C%20a%20hypercube%20is,and%20of%20the%20same%20length."&gt;hypercube&lt;/a&gt; with 3 dimensions where we have mapped these states in an open space, with arrows denoting state transitions/ dependencies.&lt;/p&gt;

&lt;p&gt;( number of dimension in hypercube = number of bits = number of CORE conditional elements in your state)&lt;/p&gt;

&lt;p&gt;Now imagine &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l5YGOKdV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/okrto844lnl20fy3lsli.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l5YGOKdV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/okrto844lnl20fy3lsli.png" alt="Alt Text" width="679" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For simplicity of understanding, we work with the human emotional state, where one mood can be altered by another mood state . Lets assign the bits at each of the 3 positions from MSB to LSB to denote the states :&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;IRRITATED = 1      LSB  set for irritated (001)&lt;br&gt;
BUSY = 2           MiddleSB set for busy. (010)&lt;br&gt;
LONELY = 4         MSB set for lonely.    (100)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;these are our core conditional elements in our state transition diagram whose occurence can alter any state. This basically means that between all possible states. All different combinations of these 3 core states, will give us all the different states we have defined below:&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HAPPY = 0          # 000 NO BAD FLAGS SET [ INITIAL STATE]
IRRITATED = 1      # 001 [CORE STATE LSB]
BUSY = 2           # 010 [CORE STATE mid-bit]
ANGRY = 3          # 011 BUSY   | IRRITATED
LONELY = 4         # 100 [CORE STATE MSB]
BAD = 5            # 101 LONELY | IRRITATED
SAD = 6            # 110 LONELY | BUSY
EVIL = 7           # 111 SAD    | IRRITATED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And we have a planned dependency between all our states !. We can now map what we intend to do when we are in that state, by mapping a function to that state. Check out the code snippet below. Each index [i] in the to_do list defines what the person wants to do when he in the [i]th state . If your states are not of 2^n , consider having a hashmap with :&lt;br&gt;
{STATE_NAME: corr_function_name}&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;# DEMO HYPERCUBE STYLE PROGRAM DESIGN
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Moods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# seggregating basic states which can alter mood state logically
&lt;/span&gt;    &lt;span class="c1"&gt;#IRRITATED = 1      LSB  set for irritated
&lt;/span&gt;    &lt;span class="c1"&gt;#BUSY = 2           MiddleSB set for busy
&lt;/span&gt;    &lt;span class="c1"&gt;#LONELY = 4         MSB set for lonely
&lt;/span&gt;
    &lt;span class="n"&gt;HAPPY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;          &lt;span class="c1"&gt;# 000 NO BAD FLAGS SET
&lt;/span&gt;    &lt;span class="n"&gt;IRRITATED&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;# 001
&lt;/span&gt;    &lt;span class="n"&gt;BUSY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;           &lt;span class="c1"&gt;# 010
&lt;/span&gt;    &lt;span class="n"&gt;ANGRY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;          &lt;span class="c1"&gt;# 011 BUSY   | IRRITATED
&lt;/span&gt;    &lt;span class="n"&gt;LONELY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;         &lt;span class="c1"&gt;# 100
&lt;/span&gt;    &lt;span class="n"&gt;BAD&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;# 101 LONELY | IRRITATED
&lt;/span&gt;    &lt;span class="n"&gt;SAD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;            &lt;span class="c1"&gt;# 110 LONELY | BUSY
&lt;/span&gt;    &lt;span class="n"&gt;EVIL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;           &lt;span class="c1"&gt;# 111 SAD  | IRRITATED
&lt;/span&gt;
    &lt;span class="c1"&gt;# a list or a dict mapped corresponding to  above states. can be a list of function-pointers,
&lt;/span&gt;    &lt;span class="c1"&gt;# responses, emails and what not , based on your functional requirements!
&lt;/span&gt;    &lt;span class="n"&gt;things_to_do&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"I'll go on a trip"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"I need to break a vase or something"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s"&gt;"I'll post an article on dev.to"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"I'll eat an entire chocolate cake "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"I'll go to sleep"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"I'll jump on my best friend"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="s"&gt;"I'll cry in a corner"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"I kill some people in GTA V"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mood&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Moods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HAPPY&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# performs a function based on current state. (customize this as required)
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;things_to_do&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mood&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;alter_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;added_mood&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# alters current state based on external state or intervention
&lt;/span&gt;        &lt;span class="c1"&gt;# you can XOR it for neutralizing current state too, if you want instead
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mood&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mood&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;added_mood&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Since we know our state dependencies, lets run our code now and try to change our state to another state without having to touch another branching condtion.&lt;br&gt;
Lets make Timm'ys mood BAD.&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;timmy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Moods&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;timmy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_action&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;timmy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alter_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Moods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BAD&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;timmy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_action&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I'll go on a trip
I'll jump on my best friend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like Timmy responded to his mood swings ! That too without getting our hands dirty. &lt;/p&gt;

&lt;h3&gt;
  
  
  Some possible use-cases:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;API for Sending emails to people based on -&amp;gt; Their ROLE + ACTION_TYPE + SENDER&lt;/li&gt;
&lt;li&gt;API responses&lt;/li&gt;
&lt;li&gt;State based games &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some related articles for better coding approaches in API programming:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/javalin/zero-boilerplate-zero-runtime-errors-coding-with-monads-26n9"&gt;Avoiding Boilerplates&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also I came across this amazing video by Computerphile ! which helped me coin a Terminology for this programming style of mine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=WPoQfKQlOjg"&gt;The Perfect Code - Computerphile&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.oodesign.com/dependency-inversion-principle.html"&gt;SOLID design pattern principles in OOPS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=srQt1NAHYC0"&gt;FUNCTIONAL DESIGN PATTERNS&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>architecture</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Zero Boilerplate , Zero Runtime errors : Coding with Monads</title>
      <dc:creator>Arka Bhowmik</dc:creator>
      <pubDate>Sat, 13 Jun 2020 08:44:55 +0000</pubDate>
      <link>https://dev.to/javalin/zero-boilerplate-zero-runtime-errors-coding-with-monads-26n9</link>
      <guid>https://dev.to/javalin/zero-boilerplate-zero-runtime-errors-coding-with-monads-26n9</guid>
      <description>&lt;p&gt;Note: &lt;em&gt;this post will be updated with time&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;API backend programming can be stressful and repetitive. For the past one year, having refactored and rewritten over 10k lines of backend code, I understood the importance of following a proper structure/ discipline while coding backend APIs. &lt;br&gt;
Thankfully, the concept of &lt;a href="https://en.wikipedia.org/wiki/Monad_(functional_programming)#:~:text=In%20functional%20programming%2C%20a%20monad,needed%20by%20the%20program%20logic.&amp;amp;text=Since%20monads%20make%20semantics%20explicit,to%20implement%20convenient%20language%20features."&gt;Monadic programming&lt;/a&gt; already existed in languages like Haskell and AngularJS. &lt;/p&gt;

&lt;p&gt;This post will show you a template to implement a coding discipline like monads in Python ,which if followed will let you have zero runtime issues and minimal to no boilerplate in your code. For me, this helped me reduce my backend code volume to 1/3rd of its size, enhancing logging and stability, with zero performance latencies added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create the monad class&lt;/strong&gt;&lt;br&gt;
Monads are pure functions. This implicitly means that the type of input it takes in and type of output it produces will be the same. In this case, that state is a dict{}. This also means, that you cannot have exceptions or None type returns in your functions.&lt;/p&gt;

&lt;p&gt;Our Monad class is defined to have :&lt;br&gt;
 &lt;em&gt;state&lt;/em&gt;: This is the current state we have in our monad which represents the data we have currently processed. This is also the argument which we pass to the functions in out monads. We create a mondaic class which takes in a dict as the initial state. For API backend code, this state can be your request parameters. Think of it as if your'e passing the same variables in the function, just that they are wrapped in a dict and the variables names are the keys in the dict.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;fns&lt;/em&gt;: It has a list of functions, that are function pointers to functions to be executed in order. for example, you can design your sign_up flow as:&lt;br&gt;
[process_request_params, check_username_password, create_token, establish_session]&lt;/p&gt;

&lt;p&gt;The .execute will call these functions in a loop till till the final state is reached. The final state should effectively be your response output. Here's what the flow looks like from one state to another state&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QMM7X6Hg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/458xe9vz3oobohrgcu3k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QMM7X6Hg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/458xe9vz3oobohrgcu3k.png" alt="Alt Text" width="606" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;efn&lt;/em&gt;: suppose your code breaks in-between function in the pipeline. The error function &lt;em&gt;efn&lt;/em&gt; will process the current state of the monad and output the error as you require in the response. This saves you from getting 500 server errors from backend.&lt;/p&gt;

&lt;p&gt;Our monadic code relies on the "status" parameter in status dict, to get "error" reports in-between functions.&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;MaybeErrorMonad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;efn&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;  &lt;span class="c1"&gt;# current state (dict) in monadic pipeline
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fns&lt;/span&gt;      &lt;span class="c1"&gt;# a list of functions in order of execution
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error_fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;efn&lt;/span&gt; &lt;span class="c1"&gt;# error function that reports any error, based on self.state
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;finish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt; &lt;span class="c1"&gt;# a flag to indicate that the self.state is final state
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# a wrapper class that does execution of functions on current self.state in order
&lt;/span&gt;        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_fun&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;
            &lt;span class="n"&gt;computation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;computation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;computation&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;computation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computation&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;finish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_fun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we define some functions for testing our monad. I have added an error_handler_wrapper  &lt;a href="https://wiki.python.org/moin/FunctionWrappers"&gt;wrapper function&lt;/a&gt; which will ensure that our functions always return a proper response in a dict , if anything breaks mid-way. You can design this function to include logs with the function name and arguments, with current parameters for better debugging in production code. We also define our custom error function to output our error response , when we get error status midway.&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;err_handler_wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# a error handler wrapper function to report any execution errors in dict
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"reason"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Girlfriend not found"&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;state&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;efn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Rejected :'("&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"reason"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'reason'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;err_handler_wrapper&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_flowers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"got flowers for %s"&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'girlfriend'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;"flowers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&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;state&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;err_handler_wrapper&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_chocolates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"got chocolates for %s"&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'girlfriend'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;"chocolates"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&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;state&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;err_handler_wrapper&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;propose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'flowers'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'chocolates'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Success! she said yes"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"she said no"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Lets run our example where a guy tries to propose his crush.&lt;br&gt;
if he buys just flowers, he will get rejected.&lt;br&gt;
if he buys chocolates and flowers, he gets accepted.&lt;br&gt;
if there is no 'girlfriend' in initial state, theres an error.&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;# CASE 1
&lt;/span&gt;&lt;span class="n"&gt;to_do&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;get_flowers&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;get_chocolates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;propose&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;initial_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'girlfriend'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;executor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MaybeErrorMonad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initial_state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_do&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;efn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"CASE 1: %s &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nb"&gt;str&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;#CASE 2
&lt;/span&gt;&lt;span class="n"&gt;initial_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;# no girlfriend
&lt;/span&gt;&lt;span class="n"&gt;executor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MaybeErrorMonad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initial_state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_do&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;efn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"CASE 2: %s &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nb"&gt;str&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;# CASE 3
&lt;/span&gt;&lt;span class="n"&gt;to_do&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;get_flowers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;propose&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# rejection beacuse of not enough efforts :P
&lt;/span&gt;&lt;span class="n"&gt;initial_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'girlfriend'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;executor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MaybeErrorMonad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initial_state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_do&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;efn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"CASE 3: %s &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nb"&gt;str&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And ...&lt;br&gt;
Here's our output as expected.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v6mxWiWG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/q4sio58ttczi7u9ryy1v.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v6mxWiWG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/q4sio58ttczi7u9ryy1v.jpeg" alt="Alt Text" width="493" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was a fairly simple example. Imagine the power of now being able to execute huge functions in just a simple sequential todo list with minimal branching. TRY IT NOW !!!&lt;/p&gt;

&lt;p&gt;In the next update, we will learn how to structure our dict states and how to handle exceptions , and get parameters from proper context of execution.&lt;/p&gt;

&lt;p&gt;For more ideas of elimination boilerplate, read this:&lt;br&gt;
&lt;a href="https://dev.to/javalin/hypercubed-easy-state-management-simplifying-2-n-conditions-5bai"&gt;Hypercubed!!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/javalin/avoid-nestingboilerplate-in-java-map-reduce-filter-1b6m-temp-slug-5226814?preview=c8ecbaab444c06f258c105353517b1b905d0287b5dd555a09326c0587bf001bc98388d7c9130bd16b3b59a1206a77395b461d2ee8f5fb4ea50bd2142"&gt;Eliminate nested loops&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read more about monads here:-&lt;br&gt;
&lt;a href="https://jameswestby.net/weblog/tech/how-to-build-a-monad-handler.html"&gt;Monadic Programming&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pypi.org/project/PyMonad/"&gt;Monads In python: PyMonad&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>haskell</category>
    </item>
    <item>
      <title>Handy Linux hacks for Freshers at Work</title>
      <dc:creator>Arka Bhowmik</dc:creator>
      <pubDate>Sun, 09 Feb 2020 11:01:00 +0000</pubDate>
      <link>https://dev.to/javalin/handy-linux-hacks-for-freshers-at-work-4j2n</link>
      <guid>https://dev.to/javalin/handy-linux-hacks-for-freshers-at-work-4j2n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Are you a freshser&lt;/strong&gt; feeling lost in the world of servers, devboxes, debugging realtime systems etc?. We've been sailing in the same boat. Here are a few handy stuff that will be your lifesaver till you are a fresher. &lt;br&gt;
&lt;em&gt;We can all do a&lt;/em&gt; &lt;code&gt;man&lt;/code&gt; &lt;em&gt;now and understand what the command actually does, but here's a cheatsheet to get you started quick.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Returning complete Exceptions/Trace from logs using grep&lt;/strong&gt;&lt;br&gt;
You can use the -B and -A to print lines before and after the match.&lt;br&gt;
&lt;code&gt;grep -i -A 10 'some.java.Exception' my-server.log&lt;/code&gt;&lt;br&gt;
This will print 10 lines after the Exception, including the line with exception. similarly -A will print lines before the exception&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Print all files , with  ports, parent processes etc&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;list the files/processes:&lt;/em&gt; &lt;code&gt;sudo lsof -i -P -n&lt;/code&gt;&lt;br&gt;
&lt;em&gt;[For Debugging] LISTENING to:&lt;/em&gt; &lt;code&gt;sudo lsof -i -P -n | grep LISTEN&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3tk6rKD---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pzuse7np7gxihoba8kd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3tk6rKD---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pzuse7np7gxihoba8kd2.png" alt="Alt Text" width="656" height="286"&gt;&lt;/a&gt;&lt;br&gt;
Want more detail?: &lt;code&gt;ps aux&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B0NMh5fB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/1in6h1bdkfykhs76g01z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B0NMh5fB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/1in6h1bdkfykhs76g01z.png" alt="Alt Text" width="656" height="81"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;a:- This option prints the running processes from all users.&lt;br&gt;
u:- This option shows user or owner column in output.&lt;br&gt;
x:- This option prints the processes those have not been executed from the terminal.&lt;/em&gt;&lt;br&gt;
When you know the PID of the process, you can use &lt;code&gt;kill PID_OF_THAT_PROCESS&lt;/code&gt; to kill it &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tracking/ Debugging Network connections&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;netstat pntl&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FRPIhrc2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/orj9ujuoil87jwuqahec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FRPIhrc2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/orj9ujuoil87jwuqahec.png" alt="Alt Text" width="656" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Digging deeper into networking | fixing 502s, 504s&lt;/strong&gt;&lt;br&gt;
Did you get a Gateway Timeout or a Bad Gateway response? Do you know if the packet is even reaching that server ? Try to do a &lt;code&gt;tcpdump&lt;/code&gt; to see what packets are going in and coming into your system. &lt;br&gt;
&lt;code&gt;tcpdump -nnvvS src 172.31.1.237 and dst port 80&lt;/code&gt;&lt;br&gt;
this command will track all packets coming to port 80 of your system from 172.31.1.237. If we do the same on 172.31.1.237 and use our system's public IP on that device, we can see the packets arriving to that system from our server.&lt;/p&gt;

&lt;p&gt;If you want to analyse network dum on private cloud cluster you can generate a pcap file and analyse it on Wireshark&lt;/p&gt;

&lt;p&gt;`sudo ip -s -s link show&lt;/p&gt;

&lt;p&gt;sudo nstat `&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use curl instead of POSTMAN for request/response debugging&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;curl -k -i -X POST your_url_of_concern.com -d"{your data in json or urlencoded text}" -H   -H 'set cookie: some_cookie_expected_in_request=someid'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The -k is for avoiding checks for ssl, which might be unnecessary inside your own dev-box or organization's internal APIs.&lt;/p&gt;

&lt;p&gt;the -i option will print the headers, cookies information, which is highly useful while debugging server failures, session tracking&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;replace edit delete occurence of text in file&lt;/strong&gt;&lt;br&gt;
 &lt;em&gt;this one replaces unix with linux in geekfile.txt&lt;/em&gt;&lt;br&gt;
&lt;code&gt;sed 's/unix/linux/' geekfile.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Allowing an app through firewall&lt;/strong&gt;&lt;br&gt;
First list all your apps requiring the feature&lt;br&gt;
&lt;code&gt;sudo ufw app list&lt;/code&gt;&lt;br&gt;
Now allow the app through firewall&lt;br&gt;
&lt;code&gt;sudo ufw allow 'APP_NAME'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transferring files to-from remote host over SCTP&lt;/strong&gt;&lt;br&gt;
This can come in handy when you want to get some downloaded or config files from/to servers to run some stuff locally&lt;/p&gt;

&lt;p&gt;&lt;em&gt;without keys:&lt;/em&gt; &lt;code&gt;scp -r  source_host_full_dir_path destination_host_full_dir_path&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9dUvm--u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/4mt2hl5n1lglv6aamor5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9dUvm--u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/4mt2hl5n1lglv6aamor5.png" alt="Alt Text" width="800" height="95"&gt;&lt;/a&gt;&lt;br&gt;
In this example, we transfer s.py from local to 172.31.1.237 remote machine&lt;/p&gt;

&lt;p&gt;&lt;em&gt;if using ssh key:&lt;/em&gt; &lt;code&gt;scp -i your_ssh_key_path -r  source_host_full_dir_path destination_host_full_dir_path&lt;/code&gt;&lt;br&gt;
&lt;em&gt;The -r is for including subfolders and files inside that folder. you wont need that for single files&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If ever you get permission denied during transfer to or from a server, go to the source's directory and change ownership of the file to the current logged in user&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trouble sending certificates in json body ?&lt;/strong&gt;*&lt;br&gt;
certificates are in PEM format and line breaks are a hassle to pass through in json format. If you have to pass the plaintext of a cert in json, better convert those linebreaks into '\n'&lt;/p&gt;

&lt;p&gt;&lt;code&gt;awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' your-cert.pem&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shortcut links to long directory paths&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Do you have very long directorypaths, which you frequently need to access from current directory? why not have a shortcut to that file in your current directory. A symlink will create a pointer to that actual file from the current directory. All changes will be reflected on the original link.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;ln sourcefilepath destinationfilepath&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xxwc2-YI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ive91g08ynx96wk820je.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xxwc2-YI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ive91g08ynx96wk820je.png" alt="Alt Text" width="800" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Executing commands directly in remote box&lt;/strong&gt;&lt;br&gt;
example : &lt;code&gt;ssh -t username@host 'pwd'&lt;/code&gt;&lt;br&gt;
*The -t is required if you are executing multiple commands or if you want the shell to be interactive. Not required if you are not doing any of that. However if you want to execute a shell script from your local to your remote machine , you have to scp file transfer the script to that machine and then execute it. credits to this *&lt;a href="https://www.unix.com/302889378-post2.html?s=28ba6d8f25831ed014498356dcd786fe"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follwing up .logs using tail&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;tail -10f /location/of/your/.log&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c2leTj3W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ws7z7r66zeeswgxn6hn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c2leTj3W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ws7z7r66zeeswgxn6hn8.png" alt="Alt Text" width="573" height="94"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;the "10" denotes that it will tail/print the last 10 lines, in that file. You can set it to whatever value you want. This will update in real-time as your log file updates.&lt;br&gt;
You can use *.log instead of specific filename to track all log files in that directory&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up ENVIRONMENT VARIABLES :&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;export variablename=dont_add_spaces_here_whateveryouwant&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D8KBdAdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/hiidw0lhdaiswan4b3iv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D8KBdAdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/hiidw0lhdaiswan4b3iv.png" alt="Example: export SERTVERURL=127.0.0.1:8000" width="340" height="35"&gt;&lt;/a&gt;&lt;br&gt;
use &lt;code&gt;printenv variablename&lt;/code&gt; to print them&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparing Floats in a shell script&lt;/strong&gt;&lt;br&gt;
Yes, it's a pain to compare floating point numbers in shell. This is not an issue in bash though ( yes bash and sh are different, in case you didn't know that). Here is an example to do so&lt;/p&gt;

&lt;p&gt;&lt;code&gt;num1=18.04&lt;br&gt;
num2=18.04&lt;br&gt;
OUTPUT=$(echo "$num1 == $num2" |bc -l)&lt;/code&gt;&lt;br&gt;
will produce output 1 since they are equal.&lt;br&gt;
Now, to use this OUTPUT, you again have to compare it using &lt;br&gt;
&lt;code&gt;[ $OUTPUT -ne 0]&lt;/code&gt; &lt;br&gt;
this checks if the value of OUTPUT is not equal to zero. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Debugging | Logging stdouts from shell scripts&lt;/strong&gt;&lt;br&gt;
There might be occassions where you have to debug stuff inside a shell script, but there is no elegant way of logging inside of shell ... or is it ?&lt;/p&gt;

&lt;p&gt;We can dynamically open, close, and copy stdout to achieve logging operations. Let’s redirect stdout (FD 1) to the log file:&lt;br&gt;
&lt;code&gt;#! /bin/bash&lt;br&gt;
script_log="/tmp/log_&lt;/code&gt;date +%F&lt;code&gt;.log"&lt;br&gt;
exec 1&amp;gt;&amp;gt;$script_log&lt;br&gt;
echo "This will be written into log file rather than terminal.."&lt;br&gt;
echo "This too.."&lt;/code&gt;&lt;br&gt;
&lt;a href="https://www.baeldung.com/linux/exec-command-in-shell-script"&gt;Learn more&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;commits between 2 git hashes for better gitblame&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git log &amp;lt;older commit hash&amp;gt;..&amp;lt;newer commit hash&amp;gt; --oneline | cut -d " " -f 1&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;easy git update between branches without rebase&lt;/strong&gt;&lt;br&gt;
Is rebasing causing conflicts for you even though you know your diff is perfectly fine and conflicts have already been resolved in upcoming commits ? In this case forget rebasing and do patching. take a diff between your current branch and the branch you wanna update (say master)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git diff origin/master --no-color &amp;gt; ~/Documents/your_patch_file.patch&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
now checkout to a fresh master branch and&lt;br&gt;
&lt;code&gt;git apply ~/Documents/your_patch_file.patch&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you will have all the changes of your current branch, in this one now, without any conflicts.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local git branch messed up? fix to latest working remote branch&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;sudo git clean -f -d; sudo git reset HEAD —hard&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;remove untracked files from git commits&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;git status | grep 'modified:' | cut -d' ' -f4|xargs sudo git rm -f&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;if you want to untrack deleted files too:&lt;/em&gt;&lt;br&gt;
&lt;code&gt;git status | grep 'deleted:' | cut -d' ' -f5|xargs sudo git rm -f&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;that&lt;/em&gt; &lt;code&gt;cut -d' ' -f_number&lt;/code&gt; &lt;em&gt;means you are splitting the current line by spaces and picking up the string corresponding to your file. so that f_number should be the position your file occurs in. It might vary in your case&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;clean unused old libraries from .m2 repository&lt;/strong&gt;&lt;br&gt;
cleans all files more than an hour of last access from m2. They will be accessed if you had built within the past hour&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;find ~/.m2 -amin +60 -iname '*.pom' | while read pom; do parent=`dirname "$pom"`; rm -Rf "$parent"; done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using anaconda? Do you clear the unused files?&lt;/strong&gt;&lt;br&gt;
if you are thinking where all my disk space is going, go and check your anaconda package directory. Mine was 17.6GB. its crazy. Anaconda does not automatically clean files after installation.&lt;br&gt;
&lt;code&gt;conda clean -a&lt;/code&gt; will remove all tarballs, and setup packages of already installed packages. My directory size is now 10GB.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A few stuff for precautions:&lt;/strong&gt;&lt;br&gt;
Here are a few things to avoid when you are using &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;1.NEVER sudo in the root folder. This will deadlock your system.&lt;br&gt;
2.To avoid using sudo in every place else, all the time, use sudo -s. and from there onwards, you need not type sudo again.&lt;br&gt;
3.To avoid sudo in some situations,( especially when you are just testing some stuff), maybe consider changing ownership of files in that folder to your name so you can liberally access them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chown -R your_username /foldername&lt;/code&gt; &lt;em&gt;will change ownership of all files subfolders, in that directory to you&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learning regex and VIM&lt;/strong&gt;&lt;br&gt;
This will save you hours of typing time and your reputation for faster prod-issue debugging, once you get used to it. I recommend the below places to learn these skills in the fastest way possible:&lt;br&gt;
&lt;a href="https://regexone.com/lesson/repeating_characters?"&gt;Regexone&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=OnUiHLYZgaA&amp;amp;t=630s"&gt;thoughtbot&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working with JAVA ? Read about tweaking JVM with these&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://geekflare.com/important-jvm-options/"&gt;Java Options&lt;/a&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
      <category>beginners</category>
      <category>career</category>
    </item>
  </channel>
</rss>
