<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Girish Deshpande on Medium]]></title>
        <description><![CDATA[Stories by Girish Deshpande on Medium]]></description>
        <link>https://medium.com/@girishdeshpande?source=rss-ea1e3051dcb0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HtmjFgnCM84JiagQ.</url>
            <title>Stories by Girish Deshpande on Medium</title>
            <link>https://medium.com/@girishdeshpande?source=rss-ea1e3051dcb0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 01:00:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@girishdeshpande/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AWS CDK Undocumented use of ?????]]></title>
            <link>https://girishdeshpande.medium.com/aws-cdk-undocumented-use-of-4941743a93ea?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/4941743a93ea</guid>
            <category><![CDATA[cdk]]></category>
            <category><![CDATA[aws-secrets-manager]]></category>
            <category><![CDATA[wildcard]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Tue, 18 Jan 2022 01:52:30 GMT</pubDate>
            <atom:updated>2022-01-18T01:52:30.928Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS CDK Undocumented use of ?????</h3><h3>Requirement</h3><ol><li>Create an IAM role to be used by a lambda function.</li><li>Create a lambda function to access given AWS SecretManager secret.</li><li>Create an AWS SecretManager secret.</li><li>Grant read permission for the secret to the IAM role created in step #1.</li></ol><h3>Solution</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b65b2c4573a768d3f47eba79316af43/href">https://medium.com/media/4b65b2c4573a768d3f47eba79316af43/href</a></iframe><h3>Observation</h3><p>Open the newly created IAM role. Notice that there is a new inline permission attached to the role. This is expected as per the CDK code above.</p><p>Open/expand the inline policy and notice that for the secret ARN there is a ‘-??????’ suffix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cZc6koyCNuPLOnBl-nd18g.png" /></figure><p>If you visit the secret on AWS SecretManager console, you will notice that the ‘??????’ are actually random 6 character.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*bCCR5vTA8wZtNVjdAwf9SQ.png" /></figure><p>The question is, when CDK was able to find the secret (ARN) using any of cdk.aws_secretsmanager.Secret.fromSecretXXX method, why it is using the ‘?’ suffix?</p><p>I don’t see it documented in CDK SDK <a href="https://docs.aws.amazon.com/cdk/api/v1/docs/aws-secretsmanager-readme.html">here</a>.</p><p>I wanted to put this information here because the first time encountered the issue, I thought it is using ‘?’ to replace junk characters and wondered from where in my string I have junk chars. And I wasn’t confident deploying with the ‘?’ suffixed to the ARN.</p><p>I dug in the AWS CDK code on GitHub to finally figure it out. <a href="https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-secretsmanager/lib/secret.ts#L389">Here</a> is the exact line link. The code comment is useful to understand that it is just to cover the ARN format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OS3nRQOUqEw8KFYugroAuA.png" /></figure><p>I experimented further more to find that one can use ‘*’ and ‘?’ to fill in the ARN format to allow for various combination of wild-card formats. Example below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1JB8sCflUV_3xH92N_vgBg.png" /></figure><p>It appears that AWS only cares about fully qualified name for the service and opens up the ‘shared responsibility’ on engineer to use tailored ARN.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4941743a93ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Peculiar AWS CloudFormation failure]]></title>
            <link>https://awstip.com/peculiar-aws-cloudformation-failure-8ccb3f7730a8?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ccb3f7730a8</guid>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-cloudformation]]></category>
            <category><![CDATA[aws-cdk]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Sun, 26 Dec 2021 02:49:16 GMT</pubDate>
            <atom:updated>2021-12-31T17:51:09.920Z</atom:updated>
            <content:encoded><![CDATA[<p>Developers and ops engineers commonly use AWS <a href="https://aws.amazon.com/cloudformation/">CloudFormation</a> (CF) for deployment of cloud resources. We use AWS <a href="https://aws.amazon.com/cdk/">CDK</a> for all our deployment needs. CDK internally uses CF to create stack, changeSets and do the deployments.</p><p>This article describes a problem you may face when using AWS CF and AWS Lambda Event Source Mapping (ESM). And how to rectify it with minimal disruption to the workflow.</p><p>Let us take a simple example AWS stack:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LGb_xUTjyu14V0k-zsI3Og.png" /><figcaption>Simple app stack</figcaption></figure><p>In this stack we have an AWS Kinesis data stream (K1), a lambda function (L1)that has an event source mapping to K1. The L1 function processes the data and persists into an S3 bucket (S1).</p><p>This is a classic (yet simplified) backend design for stream processing applications like IoT, log aggregation, video streaming and many more.</p><p>In order to deploy this stack, you can use a simple CDK(2.0) as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fb24c3c36ec0f2dc7adb824a7c810a0/href">https://medium.com/media/7fb24c3c36ec0f2dc7adb824a7c810a0/href</a></iframe><p>Here is the image showing newly created Lambda function and the triggers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6jjcB9wgdI4fVQ5q7MLVOg.png" /></figure><p>Suppose you decided to change the “Batch size” for the kinesis event source, the only option is to delete and recreate the EventSourceMapping. If you do it from CDK, great. But you do it manually, how it will impact the CF stack? Let us see. But before that, lets run this command:</p><pre>aws lambda list-event-source-mappings</pre><p>It looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YWeeP44_ChHyaWyjSXuIZA.png" /></figure><p>Notice that unlike other resources in AWS, ESM has an UUID instead of ARN.</p><p>CF stack uses this UUID to refer the ESM resource.</p><p>Now, lets manually delete the ESM and re-add the same event source but with batchSize of 20.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*58GtMmSG3ZpBGyinXiq6AA.png" /></figure><p>Lets run this command again:</p><pre>aws lambda list-event-source-mappings</pre><p>It looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZabonhInd8SuNufzVzGM8w.png" /></figure><p>Note that ESM cannot be updated (as mentioned above), so any change to ESM is will always result in new UUID. And you can confirm the same in above image.</p><h3>Is the CF stack going to break with every new change we introduce now?</h3><p>Not really. CF will continue to work just fine as long as you aren’t making any changes to the ESM. CF allows you to add new resources or modify existing resources as long as it isn’t the ESM which got replaced manually.</p><p>The best way to track such changes is thru <a href="https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html">AWS CloudTrail</a>. AWS documentation has enough information to get started on CloudTrail.</p><p>Let us prove it! Since we manually changed the batch size, as good citizens in dev ops world lets put this change in the code so that it is replicated in other environments and also to keep the current environment in sync (that’s where you will see the problem!).</p><p>Make this small change to the CDK stack and try to deploy using cdk deploy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*JEIvJ2xwiv4vaguylBbwAA.png" /></figure><p>It actually works! This is because there isn’t any change to the ESM. Why not, CF found the ESM to have the same definition as in the CF stack changeSet and it didn’t try to touch it.</p><p>Now let us change batchSize to 25. Now this is a real change and CF need to replace the <strong>old</strong> ESM with <strong>new</strong> ESM having batchSize of 25.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*UBzb8q2rdtMwc9-3jnca6w.png" /></figure><p>When you run cdk deploy with this change, the stack update will fail and it falls back to UPDATE_ROLLBACK_COMPLETE state. If you look into the ‘Events’ tab, you will notice error like this one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uGXoGoFHpPJ97p32DU0moQ.png" /></figure><p>I was in this situation where no one told me there was any change to EMS and there were no CloudTrail logs to detect the drift. I had no idea that ESM UUID is used by CF instead of ARN (like other AWS resources) and spent couple of hours starring at the CF template, parameters to find nothing looks wrong! :)</p><p>And the error message as above didn’t help me find what went wrong because I was adding an ‘on-failure’ destination instead of changing batchSize. It raised more questions if CF isn’t able to find on-failure resource ARN or Source ARN etc. Sorry for the brag, but I sat scratching my head until I decided to experiment by creating a simple stack like this article.</p><p>I hope this article helps to understand that some of AWS resources are UUID based and can be tricky to diagnose.</p><h3>How do we fix it?</h3><p>Your CF stack is rolled back and you can never make it run with your latest changes as is. There is only one solution (what I could come up with). Here are the steps:</p><ol><li>Download a copy of CF templates from ‘Template’ tab of the CF stack. This also applies to nested stacks (you may have to ignore the warnings)</li><li>Make a copy of the template and remove the ESM from it. Update batchSize=25. (I prefer to do it as part of cdk deploy so that it is part of pipeline deployment instead of one of manual change, but surely is a good shortcut.)</li><li>Upload both the templates in an S3 bucket in same region. This will avoid ‘upload’ time during stack update.</li><li>Update the CF stack with this new template by ‘Replace template’. <strong>(From this step, you need to be super quick as this will be blocking the workflow)</strong></li><li>Once the stack is complete, delete the ESMs that were manually added. (Note that step #3 will not delete it because it doesn’t know its existence)</li><li>Once the stack update is complete, repeat steps 2 and 3 using the template downloaded in step 1</li><li>(Optional, if batchSize is updated in step 2) Run cdk deploy using the latest changes you have in CDK code. It will successfully complete.</li></ol><p>It sure sounds like an ugly trick, but unfortunately this is the only way to get you CF back in sync with CDK and AWS resources. I hope it helps!</p><p>As a golden rule of using AWS CDK and CF,<em> “Never manually alter the resources of the stack”.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ccb3f7730a8" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/peculiar-aws-cloudformation-failure-8ccb3f7730a8">Peculiar AWS CloudFormation failure</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[User page access tracing using AWS Kinesis + Quicksight]]></title>
            <link>https://girishdeshpande.medium.com/user-page-access-tracing-using-aws-kinesis-quicksight-3fd7ced5f40e?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fd7ced5f40e</guid>
            <category><![CDATA[athena]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kinesis-firehose]]></category>
            <category><![CDATA[traceability]]></category>
            <category><![CDATA[s3]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Tue, 10 Aug 2021 06:29:49 GMT</pubDate>
            <atom:updated>2021-08-10T06:29:49.153Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the recent requirement in our platform was to provide ability to view administrator access pattern and viewed information related to clients.</p><p>The requirement was for an Insurance Administration platform to be 1+ compliant with HIPPA regulations.</p><p>This dataset is important to understand if any of insurance administrators viewed PII or PHI information about their client and leaked out the to the press. Or to identify such access trends and flag the administrators.</p><h4>Design</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RmLcWcetNRDY3IVelI5ntA.png" /></figure><p>This design is very typical for any of Kinesis firehose architecture. Please refer links at the end of blog for details.</p><h4>Challenge</h4><p>One of the challenge for us was the event sent to Kinesis was a Json payload of the format:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25257B%25250A%252520%252520%252520%252522user%252522%25253A%252522johnD%252522%25252C%25250A%252520%252520%252520%252522page%252522%25253A%252522NBS200S2%252522%25252C%25250A%252520%252520%252520%252522access_time%252522%25253A%252522Mon%252520Aug%25252009%25252014%25253A41%25253A18%252520PDT%2525202021%252522%25252C%25250A%252520%252520%252520%252522data%252522%25253A%25257B%25250A%252520%252520%252520%252520%252520%252520%252522ownerName%252522%25253A%2525222agstest%2525201update%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252522policyNumber%252522%25253A%252522COM%252522%25250A%252520%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525257B%2525250A%25252520%25252520%25252520%25252522user%25252522%2525253A%25252522johnD%25252522%2525252C%2525250A%25252520%25252520%25252520%25252522page%25252522%2525253A%25252522NBS200S2%25252522%2525252C%2525250A%25252520%25252520%25252520%25252522access_time%25252522%2525253A%25252522Mon%25252520Aug%2525252009%2525252014%2525253A41%2525253A18%25252520PDT%252525202021%25252522%2525252C%2525250A%25252520%25252520%25252520%25252522data%25252522%2525253A%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522ownerName%25252522%2525253A%252525222agstest%252525201update%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522policyNumber%25252522%2525253A%25252522COM%25252522%2525250A%25252520%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e407376fea6fbf6be85ef032c6c1309/href">https://medium.com/media/4e407376fea6fbf6be85ef032c6c1309/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25257B%25250A%252520%252520%252520%252522user%252522%25253A%252522johnD%252522%25252C%25250A%252520%252520%252520%252522page%252522%25253A%252522NBS200S2%252522%25252C%25250A%252520%252520%252520%252522access_time%252522%25253A%252522Mon%252520Aug%25252009%25252014%25253A41%25253A18%252520PDT%2525202021%252522%25252C%25250A%252520%252520%252520%252522data%252522%25253A%25257B%25250A%252520%252520%252520%252520%252520%252520%252522ownerName%252522%25253A%2525222agstest%2525201update%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252522commissionAmount%252522%25253A123.45%25252C%25250A%252520%252520%252520%252520%252520%252520%252522status%252522%252520%25253A%252520%252522Active%252522%25250A%252520%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525257B%2525250A%25252520%25252520%25252520%25252522user%25252522%2525253A%25252522johnD%25252522%2525252C%2525250A%25252520%25252520%25252520%25252522page%25252522%2525253A%25252522NBS200S2%25252522%2525252C%2525250A%25252520%25252520%25252520%25252522access_time%25252522%2525253A%25252522Mon%25252520Aug%2525252009%2525252014%2525253A41%2525253A18%25252520PDT%252525202021%25252522%2525252C%2525250A%25252520%25252520%25252520%25252522data%25252522%2525253A%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522ownerName%25252522%2525253A%252525222agstest%252525201update%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522commissionAmount%25252522%2525253A123.45%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522status%25252522%25252520%2525253A%25252520%25252522Active%25252522%2525250A%25252520%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8484d08123e5cb29b01a2864b195ab7/href">https://medium.com/media/f8484d08123e5cb29b01a2864b195ab7/href</a></iframe><p>Notice the structure of element ‘data’. This was designed to allow users to pick and choose the actual data they want to attach along with the page access details like ‘user’, ‘page’ and ‘access_time’.</p><h4>Solution</h4><p>I chose to use a flat schema structure for AWS Glue table definition. This allowed me to:</p><ol><li>Convert incoming json into parquet format and compress before storing into S3</li><li>Dynamic payload structure allowed to create Views in AWS Athena to map the reporting requirements.</li><li>Use AWS Quicksight SPICE storage to reduce heavy json parsing on Athena</li></ol><p>This solution allow us to abstracted data extraction and reporting requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rn3O5nMvpmxDpW6wvg6IWw.png" /><figcaption>AWS Glue Table schema used by Kinesis to convert into Parquet and store into S3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*5bvrDE2blf1CP-pihHLxrw.png" /><figcaption>AWS Kinesis Firehose stream setting</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*XOD63lki2PODyPTQ4HZukw.png" /><figcaption>AWS Athena View and Json extraction example</figcaption></figure><h4>Current challenge</h4><p>Parsing JSON at query time is flexible, but impacts wrt scale and cost.</p><p>One approach I am considering is to categorize the events at client level such that we may get N types of events mapping to N business objects and can have M attributes in ‘data’ payload for querying.</p><h4>Refer the links before for more details</h4><ul><li><a href="https://github.com/awsdocs/aws-doc-sdk-examples/blob/f4d97e8afc7ee0e78a7fa1b0fea68b7eb149abcd/javav2/example_code/firehose/src/main/java/com/example/firehose/PutRecord.java">aws-doc-sdk-examples/PutRecord.java at f4d97e8afc7ee0e78a7fa1b0fea68b7eb149abcd · awsdocs/aws-doc-sdk-examples</a></li><li><a href="https://aws.amazon.com/blogs/big-data/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight/">Analyze and visualize nested JSON data with Amazon Athena and Amazon QuickSight | Amazon Web Services</a></li><li><a href="https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html">JSON_EXTRACT_PATH_TEXT function</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fd7ced5f40e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python equivalent of Java’s Comparator<T>.compare(T,T)]]></title>
            <link>https://girishdeshpande.medium.com/python-equivalent-of-javas-comparator-t-compare-t-t-f0cf6071a2a3?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0cf6071a2a3</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[compare]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Wed, 04 Aug 2021 18:36:49 GMT</pubDate>
            <atom:updated>2021-08-04T18:36:49.980Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been coding in Java for over 10 years now. And I often admire the completeness of Java core packaging.</p><p>Recently I started working more on Python and found it great for building ETL tasks, standalone script jobs, automation clients etc. I know we can do a lot more than that, but those are my current use cases.</p><p>One of the requirement I recently had was, given an array of objects sort them using a custom comparison logic.</p><p>The few options I thought about were:</p><p>‘sorted(given_arr, key=compare_function)’</p><p>‘given_arr.sort(key=compare_function)’</p><p>But I quickly realized that the above methods are equivalent to Comparable&lt;T&gt;.compareTo(T&gt; in Java</p><p>And the solutions is <a href="https://docs.python.org/3/library/functools.html#functools.cmp_to_key">functools.cmp_to_key</a> function. Here is an example for solving leetcode #179 problem</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfrom%252520functools%252520import%252520cmp_to_key%25250Aclass%252520Solution%25253A%25250A%252520%252520%252520%252520def%252520largestNumber%2528self%25252C%252520nums%25253A%252520List%25255Bint%25255D%2529%252520-%25253E%252520str%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520def%252520compare%2528x%25252Cy%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520a%252520%252520%25253D%252520str%2528x%2529%252520%25252B%252520str%2528y%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520b%252520%25253D%252520str%2528y%2529%252520%25252B%252520str%2528x%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520return%252520int%2528b%2529%252520-%252520int%2528a%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520nums_sorted%252520%25253D%252520sorted%2528nums%25252C%252520key%25253Dcmp_to_key%2528compare%2529%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520nums_sorted%25255B0%25255D%252520%25253D%25253D%2525200%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520return%252520%25270%2527%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520return%252520%2527%2527.join%2528str%2528x%2529%252520for%252520x%252520in%252520nums_sorted%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfrom%25252520functools%25252520import%25252520cmp_to_key%2525250Aclass%25252520Solution%2525253A%2525250A%25252520%25252520%25252520%25252520def%25252520largestNumber%252528self%2525252C%25252520nums%2525253A%25252520List%2525255Bint%2525255D%252529%25252520-%2525253E%25252520str%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520def%25252520compare%252528x%2525252Cy%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520a%25252520%25252520%2525253D%25252520str%252528x%252529%25252520%2525252B%25252520str%252528y%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520b%25252520%2525253D%25252520str%252528y%252529%25252520%2525252B%25252520str%252528x%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520int%252528b%252529%25252520-%25252520int%252528a%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520nums_sorted%25252520%2525253D%25252520sorted%252528nums%2525252C%25252520key%2525253Dcmp_to_key%252528compare%252529%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520nums_sorted%2525255B0%2525255D%25252520%2525253D%2525253D%252525200%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520%2525270%252527%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520%252527%252527.join%252528str%252528x%252529%25252520for%25252520x%25252520in%25252520nums_sorted%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6d253a8882a8585202f7b516c62ebd4/href">https://medium.com/media/b6d253a8882a8585202f7b516c62ebd4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0cf6071a2a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python heapq min and mix heap sticky note and sample implementation]]></title>
            <link>https://girishdeshpande.medium.com/python-heapq-min-and-mix-heap-sticky-note-and-sample-implementation-69f199843c73?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/69f199843c73</guid>
            <category><![CDATA[priority-queue]]></category>
            <category><![CDATA[heapq]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Tue, 15 Jun 2021 21:26:39 GMT</pubDate>
            <atom:updated>2021-06-15T21:26:39.418Z</atom:updated>
            <content:encoded><![CDATA[<p>I am relatively new to python and more of a Java developer. But I love python for its simplicity.</p><p>AFAIK Python has min heap out of the box, but max heap methods are available but hidden. Please refer Python docs <a href="https://docs.python.org/3/library/heapq.html">here </a>.</p><p>In this article I wanted to document my learning about how to use heapq for max heap operation and the extract step needed. Also a sample code comparison with Java PriorityQueue</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252523%252520heapq%252520in%252520python%252520is%252520a%252520min%252520heap%25250A%252523%252520Procedure%252520to%252520create%252520a%252520min%252520heap%252520is%25253A%25250Aimport%252520heapq%25250A%25250An%252520%25253D%252520%25255B11%25252C%2525204%25252C%2525202%25252C%2525201%25252C%2525206%25252C%2525209%25252C%2525203%25255D%25250A%25250Aheapq.heapify%2528n%2529%25250Aprint%2528n%2529%252520%252520%252523%252520This%252520isn%2527t%252520sorted%25252C%252520remember%25252C%252520this%252520is%252520a%252520heap%252520ds%252520now%252520%2528except%252520for%252520the%252520first%252520element%2521%2521%252520%2528o%2528n%2529%2529%2529.%252520One%252520need%252520to%252520use%252520heap%252520methods%25250A%25250Aprint%2528heapq.heappop%2528n%2529%2529%252520%252520%252523%252520returns%2525201%25250Aheapq.heappush%2528n%25252C%2525201%2529%252520%252523%252520again%252520this%252520isn%2527t%252520sorted%25250Aprint%2528heapq.heappop%2528n%2529%2529%252520%252520%252523%252520returns%2525201%25250A%25250A%25250An.append%25281%2529%252520%252523%252520again%252520this%252520isn%2527t%252520sorted%25250Aprint%2528heapq.heappop%2528n%2529%2529%252520%252520%252523%252520returns%2525202%252520because%252520on%252520Line%25252015%252520we%252520didn%2527t%252520do%252520a%252520heappush%25250A%25250A%252523%252520How%252520to%252520implement%252520max%252520heap%25250Am%252520%25253D%252520%25255B11%25252C%2525204%25252C%2525202%25252C%2525201%25252C%2525206%25252C%2525209%25252C%2525203%25255D%25250Aheapq._heapify_max%2528m%2529%252520%252523%252520just%252520like%252520min%252520heap%25252C%252520this%252520will%252520bring%252520the%252520largest%252520element%252520to%252520the%252520top%252520of%252520heap%252520%2528rem%25252C%252520just%252520one%252520element%2529%25250Aprint%2528heapq._heappop_max%2528m%2529%2529%25250A%25250A%252523heapq.heappush%2528m%25252C%2525205%2529%252520%252523%252520Rem%252520this%252520is%252520a%252520min%252520heap%252520method%25250Am.append%25285%2529%252520%252523%252520similar%252520as%252520above%25250Aheapq._heapify_max%2528m%2529%252520%252523%252520Because%252520we%252520did%252520a%252520heappush%252520or%252520append%252520%2528which%252520is%252520for%252520min-heap%2529%25252C%252520we%252520need%252520to%252520heapify%252520again%25250Aprint%2528heapq._heappop_max%2528m%2529%2529%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252523%25252520heapq%25252520in%25252520python%25252520is%25252520a%25252520min%25252520heap%2525250A%25252523%25252520Procedure%25252520to%25252520create%25252520a%25252520min%25252520heap%25252520is%2525253A%2525250Aimport%25252520heapq%2525250A%2525250An%25252520%2525253D%25252520%2525255B11%2525252C%252525204%2525252C%252525202%2525252C%252525201%2525252C%252525206%2525252C%252525209%2525252C%252525203%2525255D%2525250A%2525250Aheapq.heapify%252528n%252529%2525250Aprint%252528n%252529%25252520%25252520%25252523%25252520This%25252520isn%252527t%25252520sorted%2525252C%25252520remember%2525252C%25252520this%25252520is%25252520a%25252520heap%25252520ds%25252520now%25252520%252528except%25252520for%25252520the%25252520first%25252520element%252521%252521%25252520%252528o%252528n%252529%252529%252529.%25252520One%25252520need%25252520to%25252520use%25252520heap%25252520methods%2525250A%2525250Aprint%252528heapq.heappop%252528n%252529%252529%25252520%25252520%25252523%25252520returns%252525201%2525250Aheapq.heappush%252528n%2525252C%252525201%252529%25252520%25252523%25252520again%25252520this%25252520isn%252527t%25252520sorted%2525250Aprint%252528heapq.heappop%252528n%252529%252529%25252520%25252520%25252523%25252520returns%252525201%2525250A%2525250A%2525250An.append%2525281%252529%25252520%25252523%25252520again%25252520this%25252520isn%252527t%25252520sorted%2525250Aprint%252528heapq.heappop%252528n%252529%252529%25252520%25252520%25252523%25252520returns%252525202%25252520because%25252520on%25252520Line%2525252015%25252520we%25252520didn%252527t%25252520do%25252520a%25252520heappush%2525250A%2525250A%25252523%25252520How%25252520to%25252520implement%25252520max%25252520heap%2525250Am%25252520%2525253D%25252520%2525255B11%2525252C%252525204%2525252C%252525202%2525252C%252525201%2525252C%252525206%2525252C%252525209%2525252C%252525203%2525255D%2525250Aheapq._heapify_max%252528m%252529%25252520%25252523%25252520just%25252520like%25252520min%25252520heap%2525252C%25252520this%25252520will%25252520bring%25252520the%25252520largest%25252520element%25252520to%25252520the%25252520top%25252520of%25252520heap%25252520%252528rem%2525252C%25252520just%25252520one%25252520element%252529%2525250Aprint%252528heapq._heappop_max%252528m%252529%252529%2525250A%2525250A%25252523heapq.heappush%252528m%2525252C%252525205%252529%25252520%25252523%25252520Rem%25252520this%25252520is%25252520a%25252520min%25252520heap%25252520method%2525250Am.append%2525285%252529%25252520%25252523%25252520similar%25252520as%25252520above%2525250Aheapq._heapify_max%252528m%252529%25252520%25252523%25252520Because%25252520we%25252520did%25252520a%25252520heappush%25252520or%25252520append%25252520%252528which%25252520is%25252520for%25252520min-heap%252529%2525252C%25252520we%25252520need%25252520to%25252520heapify%25252520again%2525250Aprint%252528heapq._heappop_max%252528m%252529%252529%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1abbfdd619a55061d0cfdc466565b45d/href">https://medium.com/media/1abbfdd619a55061d0cfdc466565b45d/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520java.util.Arrays%25253B%25250Aimport%252520java.util.PriorityQueue%25253B%25250A%25250Apublic%252520class%252520PQ%252520%25257B%25250A%25250A%252509%25252F%252A%252A%25250A%252509%252520%252A%252520%252540param%252520args%25250A%252509%252520%252A%25252F%25250A%252509public%252520static%252520void%252520main%2528String%25255B%25255D%252520args%2529%252520%25257B%25250A%252509%252509minHeap%2528%2529%25253B%25250A%252509%252509maxHeap%2528%2529%25253B%25250A%252509%25257D%25250A%252509%25250A%252509public%252520static%252520void%252520minHeap%2528%2529%252520%25257B%25250A%252509%252509PriorityQueue%25253CInteger%25253E%252520pq%252520%25253D%252520new%252520PriorityQueue%25253CInteger%25253E%2528%2529%25253B%252520%25252F%25252F%252520Min%252520heap%25250A%252509%252509pq.addAll%2528Arrays.asList%252811%25252C%2525204%25252C%2525202%25252C%2525201%25252C%2525206%25252C%2525209%25252C%2525203%2529%2529%25253B%25250A%252509%252509%25250A%252509%252509System.out.println%2528pq%2529%25253B%252520%25252F%25252F%252520Already%252520heapified%25250A%252509%252509System.out.println%2528pq.poll%2528%2529%2529%25253B%25250A%252509%252509%25250A%252509%252509pq.offer%25281%2529%25253B%252520%25252F%25252F%252520This%252520will%252520add%252520and%252520heapify%252520%25250A%252509%252509System.out.println%2528pq.poll%2528%2529%2529%25253B%25250A%252509%25257D%25250A%252509%25250A%252509public%252520static%252520void%252520maxHeap%2528%2529%252520%25257B%25250A%252509%252509PriorityQueue%25253CInteger%25253E%252520pq%252520%25253D%252520new%252520PriorityQueue%25253CInteger%25253E%2528%2528a%25252Cb%2529%252520-%25253E%252520b%252520-%252520a%2529%25253B%252520%25252F%25252F%252520Max%252520heap%25250A%252509%252509pq.addAll%2528Arrays.asList%252811%25252C%2525204%25252C%2525202%25252C%2525201%25252C%2525206%25252C%2525209%25252C%2525203%2529%2529%25253B%25250A%252509%252509%25250A%252509%252509System.out.println%2528pq%2529%25253B%252520%252520%25252F%25252F%252520Already%252520heapified%25250A%252509%252509%25250A%252509%252509System.out.println%2528pq.poll%2528%2529%2529%25253B%25250A%252509%252509%25250A%252509%252509pq.offer%2528100%2529%25253B%252520%25252F%25252F%252520This%252520will%252520add%252520and%252520heapify%252520%25250A%252509%252509%25250A%252509%252509System.out.println%2528pq.poll%2528%2529%2529%25253B%25250A%252509%25257D%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520java.util.Arrays%2525253B%2525250Aimport%25252520java.util.PriorityQueue%2525253B%2525250A%2525250Apublic%25252520class%25252520PQ%25252520%2525257B%2525250A%2525250A%25252509%2525252F%25252A%25252A%2525250A%25252509%25252520%25252A%25252520%25252540param%25252520args%2525250A%25252509%25252520%25252A%2525252F%2525250A%25252509public%25252520static%25252520void%25252520main%252528String%2525255B%2525255D%25252520args%252529%25252520%2525257B%2525250A%25252509%25252509minHeap%252528%252529%2525253B%2525250A%25252509%25252509maxHeap%252528%252529%2525253B%2525250A%25252509%2525257D%2525250A%25252509%2525250A%25252509public%25252520static%25252520void%25252520minHeap%252528%252529%25252520%2525257B%2525250A%25252509%25252509PriorityQueue%2525253CInteger%2525253E%25252520pq%25252520%2525253D%25252520new%25252520PriorityQueue%2525253CInteger%2525253E%252528%252529%2525253B%25252520%2525252F%2525252F%25252520Min%25252520heap%2525250A%25252509%25252509pq.addAll%252528Arrays.asList%25252811%2525252C%252525204%2525252C%252525202%2525252C%252525201%2525252C%252525206%2525252C%252525209%2525252C%252525203%252529%252529%2525253B%2525250A%25252509%25252509%2525250A%25252509%25252509System.out.println%252528pq%252529%2525253B%25252520%2525252F%2525252F%25252520Already%25252520heapified%2525250A%25252509%25252509System.out.println%252528pq.poll%252528%252529%252529%2525253B%2525250A%25252509%25252509%2525250A%25252509%25252509pq.offer%2525281%252529%2525253B%25252520%2525252F%2525252F%25252520This%25252520will%25252520add%25252520and%25252520heapify%25252520%2525250A%25252509%25252509System.out.println%252528pq.poll%252528%252529%252529%2525253B%2525250A%25252509%2525257D%2525250A%25252509%2525250A%25252509public%25252520static%25252520void%25252520maxHeap%252528%252529%25252520%2525257B%2525250A%25252509%25252509PriorityQueue%2525253CInteger%2525253E%25252520pq%25252520%2525253D%25252520new%25252520PriorityQueue%2525253CInteger%2525253E%252528%252528a%2525252Cb%252529%25252520-%2525253E%25252520b%25252520-%25252520a%252529%2525253B%25252520%2525252F%2525252F%25252520Max%25252520heap%2525250A%25252509%25252509pq.addAll%252528Arrays.asList%25252811%2525252C%252525204%2525252C%252525202%2525252C%252525201%2525252C%252525206%2525252C%252525209%2525252C%252525203%252529%252529%2525253B%2525250A%25252509%25252509%2525250A%25252509%25252509System.out.println%252528pq%252529%2525253B%25252520%25252520%2525252F%2525252F%25252520Already%25252520heapified%2525250A%25252509%25252509%2525250A%25252509%25252509System.out.println%252528pq.poll%252528%252529%252529%2525253B%2525250A%25252509%25252509%2525250A%25252509%25252509pq.offer%252528100%252529%2525253B%25252520%2525252F%2525252F%25252520This%25252520will%25252520add%25252520and%25252520heapify%25252520%2525250A%25252509%25252509%2525250A%25252509%25252509System.out.println%252528pq.poll%252528%252529%252529%2525253B%2525250A%25252509%2525257D%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/27768ae59c9212c88187fecd91ea93f4/href">https://medium.com/media/27768ae59c9212c88187fecd91ea93f4/href</a></iframe><p>My implementation (not original) of Min Heap (just for understand purpose)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520Heapo%25253A%25250A%252520%252520%252520%252520def%252520__init__%2528self%25252C%252520size%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520super%2528%2529.__init__%2528%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.N%252520%25253D%2525200%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.h%252520%25253D%252520%25255B0%25255D%25250A%252520%252520%252520%252520%25250A%252520%252520%252520%252520def%252520swap%2528self%25252C%252520i%25252C%252520j%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.h%25255Bi%25255D%25252C%252520self.h%25255Bj%25255D%252520%25253D%252520self.h%25255Bj%25255D%25252C%252520self.h%25255Bi%25255D%25250A%25250A%252520%252520%252520%252520def%252520sink%2528self%25252C%252520i%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520while%2525202%252520%252A%252520i%252520%25253C%25253D%252520self.N%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520l%252520%25253D%2525202%252Ai%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520if%252520l%252520%25253C%252520self.N%252520and%252520self.h%25255Bl%25255D%252520%25253E%252520self.h%25255Bl%25252B1%25255D%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520l%252520%25252B%25253D%2525201%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520if%252520self.h%25255Bi%25255D%252520%25253C%252520self.h%25255Bl%25255D%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520break%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520self.swap%2528i%25252Cl%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520i%252520%25253D%252520l%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520def%252520swim%2528self%25252C%252520i%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520i%252520%25253C%25253D%2525201%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520return%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520self.h%25255Bi%25255D%252520%25253C%252520self.h%25255Bi%25252F%25252F2%25255D%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520self.swap%2528i%25252C%252520i%252520%25252F%25252F2%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520self.swim%2528i%25252F%25252F2%2529%25250A%25250A%252520%252520%252520%252520def%252520add%2528self%25252C%252520item%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.h.append%2528item%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.N%252520%25252B%25253D%2525201%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.swim%2528self.N%2529%25250A%25250A%252520%252520%252520%252520def%252520pop%2528self%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520item%252520%25253D%252520self.h%25255B1%25255D%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.swap%25281%25252C%252520self.N%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.N%252520-%25253D%2525201%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.sink%25281%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520self.h.pop%2528%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520return%252520item%25250A%25250Aheap%252520%25253D%252520Heapo%252810%2529%25250Aheap.add%25284%2529%25250Aheap.add%25282%2529%25250Aheap.add%25285%2529%25250Aheap.add%25283%2529%25250Aheap.add%25281%2529%25250Aheap.add%252810%2529%25250Aheap.add%25289%2529%25250Aheap.add%25287%2529%25250Afor%252520_%252520in%252520range%2528heap.N%2529%25253A%25250A%252520%252520%252520%252520print%2528heap.pop%2528%2529%2529%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520Heapo%2525253A%2525250A%25252520%25252520%25252520%25252520def%25252520__init__%252528self%2525252C%25252520size%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520super%252528%252529.__init__%252528%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.N%25252520%2525253D%252525200%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.h%25252520%2525253D%25252520%2525255B0%2525255D%2525250A%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520def%25252520swap%252528self%2525252C%25252520i%2525252C%25252520j%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.h%2525255Bi%2525255D%2525252C%25252520self.h%2525255Bj%2525255D%25252520%2525253D%25252520self.h%2525255Bj%2525255D%2525252C%25252520self.h%2525255Bi%2525255D%2525250A%2525250A%25252520%25252520%25252520%25252520def%25252520sink%252528self%2525252C%25252520i%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520while%252525202%25252520%25252A%25252520i%25252520%2525253C%2525253D%25252520self.N%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520l%25252520%2525253D%252525202%25252Ai%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520l%25252520%2525253C%25252520self.N%25252520and%25252520self.h%2525255Bl%2525255D%25252520%2525253E%25252520self.h%2525255Bl%2525252B1%2525255D%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520l%25252520%2525252B%2525253D%252525201%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520self.h%2525255Bi%2525255D%25252520%2525253C%25252520self.h%2525255Bl%2525255D%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520break%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.swap%252528i%2525252Cl%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520i%25252520%2525253D%25252520l%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520def%25252520swim%252528self%2525252C%25252520i%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520i%25252520%2525253C%2525253D%252525201%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520self.h%2525255Bi%2525255D%25252520%2525253C%25252520self.h%2525255Bi%2525252F%2525252F2%2525255D%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.swap%252528i%2525252C%25252520i%25252520%2525252F%2525252F2%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.swim%252528i%2525252F%2525252F2%252529%2525250A%2525250A%25252520%25252520%25252520%25252520def%25252520add%252528self%2525252C%25252520item%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.h.append%252528item%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.N%25252520%2525252B%2525253D%252525201%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.swim%252528self.N%252529%2525250A%2525250A%25252520%25252520%25252520%25252520def%25252520pop%252528self%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520item%25252520%2525253D%25252520self.h%2525255B1%2525255D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.swap%2525281%2525252C%25252520self.N%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.N%25252520-%2525253D%252525201%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.sink%2525281%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520self.h.pop%252528%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520item%2525250A%2525250Aheap%25252520%2525253D%25252520Heapo%25252810%252529%2525250Aheap.add%2525284%252529%2525250Aheap.add%2525282%252529%2525250Aheap.add%2525285%252529%2525250Aheap.add%2525283%252529%2525250Aheap.add%2525281%252529%2525250Aheap.add%25252810%252529%2525250Aheap.add%2525289%252529%2525250Aheap.add%2525287%252529%2525250Afor%25252520_%25252520in%25252520range%252528heap.N%252529%2525253A%2525250A%25252520%25252520%25252520%25252520print%252528heap.pop%252528%252529%252529%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9584e896ff02df289f2111471153d690/href">https://medium.com/media/9584e896ff02df289f2111471153d690/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69f199843c73" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Calculate value of Pi from given number]]></title>
            <link>https://girishdeshpande.medium.com/calculate-value-of-pi-from-given-number-1d78b1b60ff0?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d78b1b60ff0</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[pi]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Thu, 27 May 2021 20:46:39 GMT</pubDate>
            <atom:updated>2021-05-27T20:46:39.645Z</atom:updated>
            <content:encoded><![CDATA[<p>The question and answer is well described by Joma in YT <a href="https://www.youtube.com/watch?v=pvimAM_SLic">https://www.youtube.com/watch?v=pvimAM_SLic</a></p><p>I ran a comparison test between Python and NodeJS version of the code. The difference was significant.</p><p>Why is the difference so big? That is for other day to figure out…</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dpie%252520%25253D%252520%2528n%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520under%252520%25253D%2525200%25250A%252520%252520%252520%252520total%252520%25253D%2525200%25250A%25250A%252520%252520%252520%252520for%2528let%252520i%252520%25253D%2525201%25253B%252520i%252520%25253C%25253D%252520n%25253B%252520i%25252B%25252B%2529%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520a%252520%25253D%252520Math.random%2528%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520b%252520%25253D%252520Math.random%2528%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520c%252520%25253D%252520Math.pow%2528a%25252C2%2529%252520%25252B%252520Math.pow%2528b%25252C%2525202%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%2528c%252520%25253C%2525201%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520under%25252B%25252B%25250A%252520%252520%252520%252520%252520%252520%252520%252520total%25252B%25252B%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520console.log%2528%252560Pi%25253A%252520%252524%25257B4%252520%252A%252520under%252520%25252F%252520total%25257D%252560%2529%25250A%25257D%252520%25250Aconsole.time%2528%2529%25250Apie%2528process.argv%25255B2%25255D%2529%25250Aconsole.timeEnd%2528%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dpie%25252520%2525253D%25252520%252528n%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520under%25252520%2525253D%252525200%2525250A%25252520%25252520%25252520%25252520total%25252520%2525253D%252525200%2525250A%2525250A%25252520%25252520%25252520%25252520for%252528let%25252520i%25252520%2525253D%252525201%2525253B%25252520i%25252520%2525253C%2525253D%25252520n%2525253B%25252520i%2525252B%2525252B%252529%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520a%25252520%2525253D%25252520Math.random%252528%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520b%25252520%2525253D%25252520Math.random%252528%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520c%25252520%2525253D%25252520Math.pow%252528a%2525252C2%252529%25252520%2525252B%25252520Math.pow%252528b%2525252C%252525202%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%252528c%25252520%2525253C%252525201%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520under%2525252B%2525252B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520total%2525252B%2525252B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520console.log%252528%25252560Pi%2525253A%25252520%25252524%2525257B4%25252520%25252A%25252520under%25252520%2525252F%25252520total%2525257D%25252560%252529%2525250A%2525257D%25252520%2525250Aconsole.time%252528%252529%2525250Apie%252528process.argv%2525255B2%2525255D%252529%2525250Aconsole.timeEnd%252528%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b3348cbf5e3bb12c98e277f813746c1/href">https://medium.com/media/4b3348cbf5e3bb12c98e277f813746c1/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520random%25250Aimport%252520timeit%25250Aimport%252520sys%25250Adef%252520pie%2528n%25253Aint%2529%25253A%25250A%252520%252520%252520%252520under%252520%25253D%2525200%25250A%252520%252520%252520%252520total%252520%25253D%2525200%25250A%252520%252520%252520%252520for%252520i%252520in%252520range%25281%25252C%252520n%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520a%252520%25253D%252520random.uniform%25280%25252C1%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520b%252520%25253D%252520random.uniform%25280%25252C1%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520c%252520%25253D%252520a%252520%252A%252A%2525202%252520%25252B%252520b%252520%252A%252A%2525202%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520c%252520%25253C%2525201%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520under%252520%25252B%25253D%2525201%25250A%252520%252520%252520%252520%252520%252520%252520%252520total%252520%25252B%25253D%2525201%25250A%25250A%252520%252520%252520%252520print%2528%2527Pi%25253A%252520%25257B%25257D%2527.format%25284%252520%252A%252520under%252520%25252F%252520total%2529%2529%25250A%25250At%252520%25253D%252520timeit.default_timer%2528%2529%252520%252520%25250Apie%2528int%2528sys.argv%25255B1%25255D%2529%2529%25250Ae%252520%25253D%252520timeit.default_timer%2528%2529%25250Aprint%2528%2527Time%252520taken%25253A%252520%25257B%25257D%2527.format%2528e-t%2529%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520random%2525250Aimport%25252520timeit%2525250Aimport%25252520sys%2525250Adef%25252520pie%252528n%2525253Aint%252529%2525253A%2525250A%25252520%25252520%25252520%25252520under%25252520%2525253D%252525200%2525250A%25252520%25252520%25252520%25252520total%25252520%2525253D%252525200%2525250A%25252520%25252520%25252520%25252520for%25252520i%25252520in%25252520range%2525281%2525252C%25252520n%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520a%25252520%2525253D%25252520random.uniform%2525280%2525252C1%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520b%25252520%2525253D%25252520random.uniform%2525280%2525252C1%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520c%25252520%2525253D%25252520a%25252520%25252A%25252A%252525202%25252520%2525252B%25252520b%25252520%25252A%25252A%252525202%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520c%25252520%2525253C%252525201%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520under%25252520%2525252B%2525253D%252525201%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520total%25252520%2525252B%2525253D%252525201%2525250A%2525250A%25252520%25252520%25252520%25252520print%252528%252527Pi%2525253A%25252520%2525257B%2525257D%252527.format%2525284%25252520%25252A%25252520under%25252520%2525252F%25252520total%252529%252529%2525250A%2525250At%25252520%2525253D%25252520timeit.default_timer%252528%252529%25252520%25252520%2525250Apie%252528int%252528sys.argv%2525255B1%2525255D%252529%252529%2525250Ae%25252520%2525253D%25252520timeit.default_timer%252528%252529%2525250Aprint%252528%252527Time%25252520taken%2525253A%25252520%2525257B%2525257D%252527.format%252528e-t%252529%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5b63d3a842525e41d8088c29ab370fd/href">https://medium.com/media/f5b63d3a842525e41d8088c29ab370fd/href</a></iframe><p>Python output:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DPS%252520C%25253A%25255CPie%25253E%252520python%252520.%25255Ccalcpy.py%25252010000%25250APi%25253A%2525203.1487148714871487%25250ATime%252520taken%25253A%2525200.007465100000000002%25250APS%252520C%25253A%25255CPie%25253E%252520python%252520.%25255Ccalcpy.py%2525201000000%25250APi%25253A%2525203.1395391395391394%25250ATime%252520taken%25253A%2525200.7450861%25250APS%252520C%25253A%25255CPie%25253E%252520python%252520.%25255Ccalcpy.py%252520100000000%25250APi%25253A%2525203.141786671417867%25250ATime%252520taken%25253A%25252076.9212521&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DPS%25252520C%2525253A%2525255CPie%2525253E%25252520python%25252520.%2525255Ccalcpy.py%2525252010000%2525250APi%2525253A%252525203.1487148714871487%2525250ATime%25252520taken%2525253A%252525200.007465100000000002%2525250APS%25252520C%2525253A%2525255CPie%2525253E%25252520python%25252520.%2525255Ccalcpy.py%252525201000000%2525250APi%2525253A%252525203.1395391395391394%2525250ATime%25252520taken%2525253A%252525200.7450861%2525250APS%25252520C%2525253A%2525255CPie%2525253E%25252520python%25252520.%2525255Ccalcpy.py%25252520100000000%2525250APi%2525253A%252525203.141786671417867%2525250ATime%25252520taken%2525253A%2525252076.9212521&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c5a50205a3858cd42f5ef5240fda58a/href">https://medium.com/media/0c5a50205a3858cd42f5ef5240fda58a/href</a></iframe><p>NodeJs output</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DPS%252520C%25253A%25255CPie%25253E%252520node%252520.%25255Cbasics.js%25252010000%25250APi%25253A%2525203.16%25250Adefault%25253A%25252010.167ms%25250APS%252520C%25253A%25255CPie%25253E%252520node%252520.%25255Cbasics.js%2525201000000%25250APi%25253A%2525203.141324%25250Adefault%25253A%25252057.324ms%25250APS%252520C%25253A%25255CPie%25253E%252520node%252520.%25255Cbasics.js%252520100000000%25250APi%25253A%2525203.14165664%25250Adefault%25253A%2525209827.864ms&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DPS%25252520C%2525253A%2525255CPie%2525253E%25252520node%25252520.%2525255Cbasics.js%2525252010000%2525250APi%2525253A%252525203.16%2525250Adefault%2525253A%2525252010.167ms%2525250APS%25252520C%2525253A%2525255CPie%2525253E%25252520node%25252520.%2525255Cbasics.js%252525201000000%2525250APi%2525253A%252525203.141324%2525250Adefault%2525253A%2525252057.324ms%2525250APS%25252520C%2525253A%2525255CPie%2525253E%25252520node%25252520.%2525255Cbasics.js%25252520100000000%2525250APi%2525253A%252525203.14165664%2525250Adefault%2525253A%252525209827.864ms&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1b0ff91019e75d60ac34715809d6b84/href">https://medium.com/media/f1b0ff91019e75d60ac34715809d6b84/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d78b1b60ff0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reporting Analytics using AWS Quicksight | Lambda CSV -> Parquet]]></title>
            <link>https://girishdeshpande.medium.com/reporting-analytics-using-aws-quicksight-lambda-csv-parquet-561ba77a45cc?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/561ba77a45cc</guid>
            <category><![CDATA[data-lake]]></category>
            <category><![CDATA[parquet]]></category>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[glue]]></category>
            <category><![CDATA[quicksight]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Wed, 26 May 2021 22:01:40 GMT</pubDate>
            <atom:updated>2021-05-26T22:01:40.033Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the story of building data late and new reporting platform using AWS Quicksight.</p><p>There are very good articles on AWS to build reporting platform. I won’t be repeating them here.</p><p>Ref: <a href="https://docs.aws.amazon.com/lake-formation/latest/dg/what-is-lake-formation.html">https://docs.aws.amazon.com/lake-formation/latest/dg/what-is-lake-formation.html</a></p><h3>Situation</h3><p>Our current ‘very expensive’ platform had limited flexibility and required subject matter expertise on the platform to build reporting.</p><h3>Task</h3><p>We were in search of new reporting and analytics platform. The platform was supposed to be highly flexible and scalable, cost effective, easy to develop and use data visualization.</p><h3>Action</h3><p>I zeroed on AWS <a href="https://aws.amazon.com/quicksight/"><em>Quicksight </em></a>given its cost model (Enterprise), flexibility and SPICE capacity approach was rightly mapping to our requirements.</p><p><strong>Challenges</strong>:</p><ol><li>ETL from various data sources</li></ol><p>One of the main challenge was to build the data lake using the data coming from on-premise data sources (such as SQL Server, Mainframe, Oracle DB, file systems, application APIs etc.) and external data sources (such as Workday, Azure SQL, Jira etc.)</p><p>I chose <a href="https://airflow.apache.org/">Apache Airflow</a> as an ETL orchestration platform. Mainly because we were already building ETL jobs in python and Airflow is super easy to maintain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lw5vs6vqYbIJb-uCGsORFQ.png" /></figure><p>2. Converting CSV to parquet was ideal scenario since we were going to use Athena for querying the data lake (we started with direct ingestion of CSVs in QS SPICE, but it quickly became unmanageable)</p><p>We separated data lake ingestion from generating Parquet formatted files (used for Athena queries). This was done mainly because the CSV files in S3 were shared with other applications in our Data lake eco-system.</p><p>I have configured Lambda trigger to S3 for converting CSV files to Parquet and place them in a separate S3 bucket used for Reporting and analytics</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/460891b5a287adae2b2e0414fb6c4380/href">https://medium.com/media/460891b5a287adae2b2e0414fb6c4380/href</a></iframe><p><strong>Why not Glue Jobs?</strong></p><p>Glue jobs basically do a transformation from CSV based table to Parquet. I don’t wanted to create CSV tables</p><p>Glue Jobs need to be added into Glue Workflow for trigger, where it has limit as how many jobs we can trigger for an event. This was a limitation as with Lambda we should create Parquets as they arrived.</p><p>Glue Jobs when converted from CSV to Parquet, doesn’t do a good job with replacing existing Parquet files thus corrupting the dataset.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=561ba77a45cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Apache Airflow on Docker | MS SQL Server ETL DAG]]></title>
            <link>https://girishdeshpande.medium.com/apache-airflow-on-docker-ms-sql-server-etl-dag-a8317da5cbcc?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8317da5cbcc</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[mssql]]></category>
            <category><![CDATA[installation]]></category>
            <category><![CDATA[airflow]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Tue, 06 Apr 2021 00:39:48 GMT</pubDate>
            <atom:updated>2021-04-06T00:39:48.076Z</atom:updated>
            <content:encoded><![CDATA[<p>I want to describe the workflow to setup <a href="https://airflow.apache.org/docs/apache-airflow">Apache Airflow</a> on Docker to run MS SQL based <a href="https://airflow.apache.org/docs/apache-airflow/stable/concepts.html?highlight=dag">DAGs</a>.</p><p>My search on web didn’t help me find one such document to give me all steps required to setup MS SQL DAGs. So, I am writing it up here. Please note that this isn’t a comprehensive setup document. But I will provide the links to other documents as possible</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lJzlaesHDfFNZe1v" /><figcaption>Photo by <a href="https://unsplash.com/@claybanks?utm_source=medium&amp;utm_medium=referral">Clay Banks</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Please follow the official <a href="https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html">doc </a>to setup Docker and Apache Airflow.</p><p>By now your installation is complete and you are able to login to Airflow and run sample DAGs.</p><figure><img alt="Docker containers after installation" src="https://cdn-images-1.medium.com/max/1024/1*78-AWyYNaOSFFycH70uP3w.png" /></figure><p>In order to connect and run SQL commands on MS SQL Server, you will have to install the provider: <a href="https://pypi.org/project/apache-airflow-providers-microsoft-mssql/">https://pypi.org/project/apache-airflow-providers-microsoft-mssql/</a></p><p>Please follow these steps to install pre-requisites and the provider.</p><p><em>I have executed the following commands on worker_1, scheduler_1 and webserver_1 containers.</em></p><p><strong>Connect to the Docker container as root user</strong></p><blockquote>docker exec -u root -ti my_airflow_container bash</blockquote><p><strong>Install GCC</strong></p><blockquote>sudo apt update<br>sudo apt install build-essential<br>sudo apt-get install manpages-dev (optional)</blockquote><p><strong>Install ODBC</strong></p><blockquote>sudo apt install unixodbc-dev (I am installing ‘dev’ version, please install according to your environment)</blockquote><p><strong>Install Apache ODBC provider</strong></p><blockquote>pip install apache-airflow-providers-odbc</blockquote><p><strong>Install Apache MS SQL provider</strong></p><blockquote>pip install apache-airflow-providers-microsoft-mssql</blockquote><p><strong>Test your setup</strong></p><p>Create a new connection for your MS SQL Database</p><figure><img alt="Please provide appropriate port number. Use 1433 as default, otherwise it doesn’t connect" src="https://cdn-images-1.medium.com/max/962/1*O-ELw6BuyXILQhfABTY-rg.png" /></figure><p>Create a simple DAG to use the connection and run a simple query</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc00979c9b4f821a96f1c9d865bf04b8/href">https://medium.com/media/fc00979c9b4f821a96f1c9d865bf04b8/href</a></iframe><p>Enable the DAG and trigger the DAG manually (schedule_interval=None)</p><p>Verify that the DAG has run successfully. Note that there won’t be any output from database since the MsSqlOperator doesn’t return data out of the box.</p><p>In order to further test that the connection is successful and you are able to retrieve data, please use MsSqlHook based task (refer task ‘t2’ in the code above).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8317da5cbcc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Apache Camel | Spring boot | Change Data source at runtime]]></title>
            <link>https://girishdeshpande.medium.com/apache-camel-spring-boot-change-data-source-at-runtime-68d633bb1f7c?source=rss-ea1e3051dcb0------2</link>
            <guid isPermaLink="false">https://medium.com/p/68d633bb1f7c</guid>
            <category><![CDATA[datasource]]></category>
            <category><![CDATA[runtime]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[apache-camel]]></category>
            <dc:creator><![CDATA[Girish Deshpande]]></dc:creator>
            <pubDate>Mon, 21 Sep 2020 16:04:03 GMT</pubDate>
            <atom:updated>2020-09-21T16:04:03.016Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Problem statement</strong></p><p>Given an <a href="https://camel.apache.org/">Apache Camel</a> Spring boot application, how do we dynamically change reference to data sources at run time?</p><p><strong>Background</strong></p><p>We were developing an Enterprise Integration Platform (EIP) using Apache Camel.</p><p>One of the main component of the EIP was a Data Access Layer interacting with MS SQL Server DB. It was used to run SQLs (Selects, Procs and Scripts) against various databases to perform ETL style operations.</p><p>The application needed ability to connect to multiple data sources as part of single request and/or ability to route the same request to different data sources based of input parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/0*Bh2Ax1t71jSt9dbF" /></figure><p><strong>Solution</strong></p><p>Spring’s ability to dynamically load DS beans and Apache Camel dynamic headers to route the requests to appropriate database.</p><ul><li>Created a configuration with details about all accessible databases and credentials. The credentials were stored encrypted at rest using an in-memory password, that can be set by administrators using HTTP POST API on the EIP. There are better ways to secure credentials and I am not discussing it here.</li><li>All HTTP requests for SQL services were required to set a header or parameter like environment=DB1 | environment=DB2 …</li><li>We built a PropertiesService that provided Datasource details for any given environment (again, there is RBAC in place to ensure the execution permissions are in place for requesting user.</li><li>DataSourceInitService ensured that it has created the Datasource bean on demand and loaded in context before the SQL Apache Camel route is called.</li></ul><p><strong>Snippets</strong></p><p>CamelRouteBuilder.java</p><pre>@Component(RouteBuilder.COMPONENT_NAME)<br>public class RouteBuilder extends BaseRouter {<br>        public static final String COMPONENT_NAME = &quot;&lt;&gt;_RouteBuilder&quot;;<br></pre><pre>        @Override<br>        public void configure() throws Exception {</pre><pre>                super.configure();<br>                <br>                rest(&quot;/servicePath/&quot;)<br>                        .get(&quot;/&lt;ServiceName&gt;&quot;)<br>                        .id(&quot;&lt;ComponentName&gt;_Exp&quot;)<br>                        .apiDocs(true)<br>                        .description(&quot;Example: /servicePath/&lt;ServiceName&gt;?environment=db1&quot;)<br>                        .bindingMode(RestBindingMode.auto).outType(ResponseBase.class)<br>                        .to(&quot;direct:&lt;ComponentName&gt;_Init&quot;);</pre><pre>                from(&quot;direct:&lt;ComponentName&gt;_Init&quot;).routeId(&quot;&lt;ComponentName&gt;_Init&quot;)<br>                    . &lt;do init, validate and preprocessing&gt;<br>					.process(RequestInitProcessor.COMPONENT_NAME) // enrich request<br>                    .to(&quot;direct:&lt;ComponentName&gt;_Sys&quot;) // get &lt;ServiceName&gt; data<br>                    .to(&quot;direct:&lt;ComponentName&gt;_HandleOutput&quot;); // ship it!</pre><pre>                from(&quot;direct:&lt;ComponentName&gt;_Sys&quot;).routeId(&quot;&lt;ComponentName&gt;_Sys&quot;)<br>                    .log(LoggingLevel.DEBUG,&quot;&lt;&gt; | headers = ${headers}&quot;)<br>                    .process(DataSourceInitProcessor.COMPONENT_NAME) // Ensure that the required datasource is loaded before calling SQL<br>                    .toD(&quot;sql:classpath:sql/&lt;&gt;/Get&lt;&gt;Data.sql?dataSource=${header.environment}&quot;);</pre><p>DataSourceInitProcessor.COMPONENT_NAME -&gt; DataSourceInitProcessor.java</p><pre>@Component(DataSourceInitProcessor.COMPONENT_NAME)<br>public class DataSourceInitProcessor implements Processor {</pre><pre>    @Autowired<br>    SpringDataSouceLoader dsLoader;</pre><pre>    public static final String COMPONENT_NAME = &quot;Common_DataSourceInitProcessor&quot;;</pre><pre>    @Autowired<br>    @Qualifier(PropertiesConstants.PROPERTIES_CACHE_MANAGER)<br>    CacheManager cacheManager; // We used EHCache to store the database credentials</pre><pre>    @Override<br>    public void process(Exchange exchange) throws Exception {<br>        // Read the properties from a common service and call the dsLoader<br>		.<br>		.<br>		.<br>        Map&lt;String, String&gt; dsProperties = (Map&lt;String, String&gt;) exchange.getIn()<br>                    .getHeader(PropertiesConstants.PROPERTIES_DS_VALUES);<br>        dsLoader.populateDs(dsProperties);</pre><p>SpringDataSouceLoader.java</p><pre>@Component<br>public class SpringDataSouceLoader {</pre><pre>...<br>private final Lock lock = new ReentrantLock();</pre><pre>    public void populateDs(Map&lt;String, String&gt; dsProperties) throws Exception {<br>        validateDSProperties(dsProperties);<br>        try {<br>            lock.lock();<br>            ConfigurableApplicationContext configContext = (ConfigurableApplicationContext) context;<br>            SingletonBeanRegistry beanRegistry = configContext.getBeanFactory();<br>            String name = dsProperties.get(DS_NAME);</pre><pre>            // final check<br>            if (beanRegistry.containsSingleton(name))<br>                return;</pre><pre>            DataSource dataSource = DataSourceBuilder.create()<br>                    .username(dsProperties.get(DATASOURCE_USERNAME))<br>                    .password(dsProperties.get(DATASOURCE_PASSWORD))<br>                    .url(https://medium.com/dsProperties.get(DATASOURCE_JDBC_URL))<br>                    .driverClassName(dsProperties.get(DATASOURCE_DRIVER_CLASS_NAME))<br>                    .build();</pre><pre>            beanRegistry.registerSingleton(name, dataSource);<br>        } finally {<br>            lock.unlock();<br>        }<br>    }</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68d633bb1f7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>