<?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 Kanishk Varshney on Medium]]></title>
        <description><![CDATA[Stories by Kanishk Varshney on Medium]]></description>
        <link>https://medium.com/@kanishkvarshney?source=rss-f03eab685dc5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SoC-X-P7bf2kTVlgvbW2tg.jpeg</url>
            <title>Stories by Kanishk Varshney on Medium</title>
            <link>https://medium.com/@kanishkvarshney?source=rss-f03eab685dc5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:39:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kanishkvarshney/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[Codyssey Unveiled: Embark on Your Journey to Coding Excellence]]></title>
            <link>https://medium.com/codyssey/codyssey-unveiled-embark-on-your-journey-to-coding-excellence-597a854ec51b?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/597a854ec51b</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Wed, 17 Jan 2024 16:00:47 GMT</pubDate>
            <atom:updated>2024-01-17T16:00:47.498Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NwgX-VsEzU5r-8YkzFhfZQ.png" /><figcaption>Codyssey: Your Coding Odyssey</figcaption></figure><p>Welcome aboard Codyssey, your Intellectual adventure into the heart of coding mastery! Whether you’re taking your first steps or already walking the expert’s path, this series promises a treasure trove of insights and tools to elevate your coding prowess. Together, we’ll delve into the vibrant world of programming, unravel the mysteries of competitive coding, and skillfully navigate through Python’s solutions to the first five Leetcode challenges. Buckle up and brace for an exhilarating journey into the code verse, where each line of code brings us closer to greatness! 🚀👨‍💻👩‍💻</p><h3>Competitive Programming: Unleashing Your Coding Prowess</h3><p>Competitive programming: the digital dojo where your brain muscles lock in epic battles with algorithmic beasts. Leetcode, Codeforces, Topcoder… these arenas are where code warriors clash, wielding data structures and algorithms like enchanted blades. But beyond the competitive thrill, the real treasure lies in the forge: sharpened problem-solving skills, laser-focused algorithmic thinking, and code that dances with efficiency. From mastering recursion to conquering dynamic programming, you’ll conquer diverse problem domains and emerge a coding samurai, ready to slay real-world challenges with elegance. So, polish your keyboard, sharpen your logic, and join the quest for coding mastery!</p><h4><strong>Strategies</strong>:</h4><ul><li><strong>Master the Foundations:</strong> Devour data structures and algorithms, your coding bedrock. Practice implementing them across languages, like Python. Conquer platforms like LeetCode.</li><li><strong>Sharpen Your Skills</strong>: Dive into practice problems, varying categories to expand your arsenal. Time yourself to boost efficiency.</li><li><strong>Learn from Every Solution:</strong> Analyze your code, seeking ways to optimize and evolve. Reflect on mistakes, then refactor for elegance.</li><li><strong>Test Yourself in Contests</strong>: Join coding battles like LeetCode contests, collaborate with fellow warriors, and rise from each challenge.</li><li><strong>Grow Together</strong>: Share your journey in online communities, contribute to open-source projects, and mentor others. Together, we code and conquer!</li></ul><h3>Launching Codyssey: A Power-Up for Your Competitive Programming Journey</h3><p>To complement and elevate your journey in competitive programming, we’re thrilled to introduce Codyssey — a dynamic initiative designed to synergize with the competitive coding landscape. Here’s how Codyssey is set to revolutionize your coding experience:</p><h4>Codyssey GitHub Repository: <a href="https://github.com/varskann/Codyssey">Explore Codyssey on GitHub</a></h4><ul><li>Dive into a repository teeming with real-world coding challenges and solutions.</li><li>Collaborate, share, and learn from a community passionate about coding excellence.</li></ul><h4>Codyssey Python Package: <a href="https://pypi.org/project/Codyssey/">Get Codyssey on PyPI</a></h4><ul><li>Access a suite of Python tools specifically crafted for competitive coding.</li><li>Enhance your problem-solving efficiency with our intuitive Python package.</li></ul><p>Don’t just read about it, experience it! Dive into the <a href="https://github.com/varskann/Codyssey">Codyssey Github repo</a> and start mastering your coding skills today.</p><h3>Embarking on Codyssey: Conquering the First Five Leetcode Challenges in Python</h3><p>Embark on Codyssey’s maiden journey: a thrilling adventure into Python programming, where we unravel the first five Leetcode challenges. This series is designed to build your skills, offering a hands-on approach to mastering key concepts and techniques. Together, we’ll turn each problem into a learning milestone, paving your path toward competitive programming mastery. Let the coding odyssey begin:</p><ol><li><a href="https://leetcode.com/problems/two-sum/description/"><strong>Two Sum</strong>:</a> Given an array of integer nums and an integer target, return indices of the two numbers such that they add up to the target.</li><li><a href="https://leetcode.com/problems/add-two-numbers/"><strong>Add Two Numbers</strong></a>: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse<strong> </strong>order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.</li><li><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/"><strong>Longest Substring</strong></a>: Given a string s, find the length of the longest <br>substring without repeating characters</li><li><a href="https://leetcode.com/problems/median-of-two-sorted-arrays/"><strong>Median of Sorted Arrays</strong>:</a> Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays</li><li><a href="https://leetcode.com/problems/longest-palindromic-substring/"><strong>Longest Palindromic Substring</strong>:</a> Given a string s, return the longest <br>palindromic substring in s.</li></ol><h3>Checkpoint</h3><p>Level up complete! You’ve just traversed the coding cosmos, cracked the secrets of competitive Python, and tamed the first five Leetcode beasts. But this is just the first checkpoint in your coding expedition. Remember, the path to coding mastery is paved with data structures &amp; algorithms, powered by practice, and fueled by a growth mindset. So, keep cranking out code, pushing boundaries, and basking in the glow of your digital creations. This is your invitation to code like a legend, and Codyssey is your ultimate guide to the code-o-sphere. Are you ready to level up again?</p><blockquote>In the realm of Python, where logic and art entwine, each line weaves a path to mastery, a dance of code divine.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=597a854ec51b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codyssey/codyssey-unveiled-embark-on-your-journey-to-coding-excellence-597a854ec51b">Codyssey Unveiled: Embark on Your Journey to Coding Excellence</a> was originally published in <a href="https://medium.com/codyssey">Codyssey</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Parallelization in Python]]></title>
            <link>https://kanishkvarshney.medium.com/parallelization-in-python-32d2add653f?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/32d2add653f</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[multiprocessing]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Sun, 27 Nov 2022 19:25:42 GMT</pubDate>
            <atom:updated>2022-11-28T04:21:13.529Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tcOEiLmL9R8sKGXj" /><figcaption>Photo by <a href="https://unsplash.com/@hiteshchoudhary?utm_source=medium&amp;utm_medium=referral">Hitesh Choudhary</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently, at my work, we were facing some slowdown in the production environment and I was put on the performance monitoring and optimization effort. I analyzed and benchmarked the algorithms, and figured that although the algorithms/libraries were optimized, the large number of inputs was becoming a bottleneck in the system.</p><p>Most of the code was either written as a simple <em>for loop</em>, or some services had a multiprocessing <em>map</em> method-based parallelization implementation. Ideally, this implementation should have taken worked well, but our processes were generating Image segmentation masks, and adding a blocking call wouldn’t yield many gains in the performance on a large dataset. When I explored the multiprocessing module in Python, I came across the following methods available at my disposal:</p><blockquote><em>apply()</em></blockquote><blockquote><em>apply_async()</em></blockquote><blockquote><em>map()</em></blockquote><blockquote><em>map_async()</em></blockquote><blockquote><em>imap()</em></blockquote><blockquote><em>imap_unordered()</em></blockquote><blockquote><em>starmap()</em></blockquote><blockquote><em>starmap_async()</em></blockquote><p>I skimmed through the <a href="https://docs.python.org/3/library/multiprocessing.html">official documentation</a> but found it a bit too verbose and overwhelming. So, after spending some time on the net and writing some basic code to understand the different scenarios and behaviors of these methods.</p><p>In this article, I have put together a brief, and simple to grasp summary with basic code snippets to understand and choose the best possible method for your use case. For a better understanding, I will be using the following sample functions and calling them 1000 times to benchmark various implementations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*QYl4ZOw8UhWOe13k1o4yMg.png" /><figcaption>Target Input Function | Single Input</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*BXbLyXHUfDIX4Diydfojhg.png" /><figcaption>Target Input Function | Multiple Inputs</figcaption></figure><blockquote><strong>Food for thought</strong>: Notice the 0.1s sleep inside the fucntion, what will happen if we remove this delay?</blockquote><p>A typical, non-parallel loop-based implementation, will look as follows and takes ~1min 42sec to finish</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WeBu3zn9MA08v3H4rPDi1Q.png" /><figcaption>Loop-based Implementation</figcaption></figure><p>We will use this as a baseline and see various multiprocessing variants on a 12-core CPU (# workers)</p><h4><strong>map()</strong></h4><p><em>map() </em>is the basic/standard parallel processing implementation for the job. It simply distributes the jobs amongst workers in a blocking manner.</p><blockquote><strong>Note</strong>: Map() method works only for Single Argument functions. For multi-argument support, see starmap()</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kafdkdTSVUcuFSdMYd7inQ.png" /><figcaption>Threadpool Map Implementation</figcaption></figure><p>Whoa! Run time reduced from 102 seconds to only 8.85 seconds (as expected ~102/12 cores). QED!!</p><h4><strong>map_async()</strong></h4><p><em>map_async() </em>is the <strong>non-blocking</strong> version of the aforementioned <em>map()</em> method. Instead of returning the results directly, this will return a &lt;multiprocessing.pool.MapResult &gt; object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zgF8jGLY0Z8Hw5qM6zZ0bQ.png" /><figcaption>Threadpool Map Async Implementation</figcaption></figure><p>You will have to fetch the result from the MapResult object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aro4qzxo0mF331lXorFPJw.png" /><figcaption>Threadpool Map Async Implementation: Wait</figcaption></figure><p>Fetching the results from the Map Async takes a similar time as Map. This is just a <strong><em>non-blocking</em></strong> version of the Threadpool Map and can be useful in places where you don’t have to wait for the entire process to complete before completing other tasks. Just trigger and Map Ahead.</p><h4><strong>starmap()</strong></h4><p>You would have noticed so far, and read above as well, <strong><em>map() </em></strong>method works with only single argument callables. If you want to parallelize the functions with multiple arguments, you will have to use <strong><em>starmap()</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jjh3NOWZuIojpBGBG3WTNg.png" /><figcaption>Threadpool StarMap Implementation</figcaption></figure><p>Whoa again! Same Speed up, but now with multiple arguments.</p><h4>starmap_async()</h4><p><em>starmap() </em>also comes with an asynchronous variant <strong><em>starmap_async()</em></strong><em>. </em>This works similarly to <em>map_async()</em>, but for multiple argument functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U12Mmw_7szzRVlDZFiJHpQ.png" /><figcaption>Threadpool StarMap Async Implementation</figcaption></figure><h4>apply()</h4><p><strong><em>apply()</em></strong> function is used to issue one-off tasks, and is called with the arguments list. This works similarly to calling a function directly and has been kept to maintain backward compatibility</p><blockquote>func(*args, **kwargs) is preferred over apply(f,args,kwargs)</blockquote><h4><strong>apply_async()</strong></h4><p>This is an asynchronous variant of <strong><em>apply()</em></strong> but accepts only a single argument. This is more suited for performing tasks in parallel or when you don’t need to wait for the output(e.g., function generating output files, etc.)</p><h4><strong>imap()</strong></h4><p>Most of you might be familiar with the concept of generators in python. <strong><em>imap()</em></strong>, lazier version of <strong><em>map()</em></strong>, does just that. Instead of returning the result or MapResult object, it returns IMapIterator object, an iterator to the results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XA2zkiSkgPVLoSg-SN-b_A.png" /><figcaption>Threadpool Imap Implementation</figcaption></figure><p>You can iterate over the results to get the final output</p><h4><strong>imap_unordered()</strong></h4><p>Sometimes the order of results doesn’t matter, and you would want to get the results in the order they get processed instead of the order in which the request is submitted. You can use <strong><em>imap_unordered() </em></strong>for such processes.</p><h4>Summary</h4><p>To re-iterate, the choice of function you would end up using will depend on your use case. You will have to consider multi-args, concurrency, blocking and ordering</p><p>You can also refer to the following explanations, to grasp the multiprocessing module:</p><ul><li><a href="https://superfastpython.com/multiprocessing-pool-python/">https://superfastpython.com/multiprocessing-pool-python/</a></li><li><a href="https://stackoverflow.com/questions/26520781/multiprocessing-pool-whats-the-difference-between-map-async-and-imap">https://stackoverflow.com/questions/26520781/multiprocessing-pool-whats-the-difference-between-map-async-and-imap</a></li></ul><p>Thanks for following through, hope this small read helps you understand Python’s multiprocessing module and utilize it properly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32d2add653f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Geospatial Image Co-registration: Part-1]]></title>
            <link>https://kanishkvarshney.medium.com/geospatial-image-co-registration-part-1-cfdc0f8f853d?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfdc0f8f853d</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[geospatial]]></category>
            <category><![CDATA[image-processing]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Wed, 11 May 2022 08:10:18 GMT</pubDate>
            <atom:updated>2022-05-11T08:10:36.156Z</atom:updated>
            <content:encoded><![CDATA[<p>While working with geospatial data, the most generic use case a data scientist runs into is comparing or overlaying the recent rasters over the old or different source rasters. But, as luck may have it, not always these two different rasters will align. Apart from the color intensity (either due to sensor difference, or time difference) between the two rasters, one also encounters the spatial shift between the two.</p><p>This shift can be introduced due to several factors, including but not limited to, Sensor/Camera angle, the height of the camera, new constructions, geometry change, time of day, the time gap between two rasters, etc.</p><p>Spatial shift (upwards) between two rasters using the same camera 1 year apart</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HTIrky01KcUuCBtov9tmoQ.png" /><figcaption>Road shifted upwards</figcaption></figure><p>To do any further analysis, e.g., depth map estimation, or change detection, it is advisable to first co-register the two rasters</p><p>Co-registering two rasters is a two-step process:</p><ol><li>Finding common points between two rasters, called <em>tie points</em></li><li>Calculate the Affine transformation on the target raster</li></ol><h3><strong>Finding tie-points</strong></h3><p>A tie point (TP) is a specific location that is recognizable visually in the overlap area between two or more images</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*bDDWAvQPP_bTFkWltVYLCA.png" /><figcaption>Tie points</figcaption></figure><p>Detecting reliable tie points is typically a manual process of selecting visibly matching points between the two rasters</p><p>The tie-points should be uniformly distributed across the whole raster</p><h3><strong>Correcting Shifts</strong></h3><p>Once you have the set of tie-points you can employ the following two strategies to correct the shifts:</p><ol><li><strong>Global Co-registration</strong>: Calculate the affine transform for the entire raster and apply homography correction based on the affine matrix</li><li><strong>Local Co-registration</strong>: Calculate the shifts in a sliding window manner.</li></ol><p>Once you have calculated the shifts, you can apply the affine transformation on the target rasters to correct the shifts. To achieve the aforementioned steps, you can use GDAL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83CdcKaFLbXPnOpfwYCATw.png" /><figcaption>Correcting the upward shift</figcaption></figure><h3><strong>Implementation</strong></h3><p>You can use the GDAL library to achieve the aforementioned tasks:</p><p>Calculate Spatial Shifts:</p><blockquote>gdal_translate -of GTiff -gcp pixel line easting northing “&lt;path_to_input_target&gt;” “&lt;path_to_intermittent_output&gt;”</blockquote><p>Apply Affine transform:</p><blockquote>gdalwarp -r near -tps -co COMPRESS=NONE -t_srs EPSG:4326 “&lt;path_to_intermittent_output&gt;” “&lt;path_to_final_coregistred_output&gt;”</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfdc0f8f853d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python Is Fun: Tricks of the Trade]]></title>
            <link>https://kanishkvarshney.medium.com/fun-with-python-88d599ac07e1?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/88d599ac07e1</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Tue, 13 Jul 2021 04:19:10 GMT</pubDate>
            <atom:updated>2021-07-29T17:34:40.055Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0sO3RXkzQRxIVcqP" /><figcaption>Photo by <a href="https://unsplash.com/@hiteshchoudhary?utm_source=medium&amp;utm_medium=referral">Hitesh Choudhary</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Indisputably, Python is the most used language in modern programming community. Its intelligibility, robustness, and versatility makes it a great choice for beginners as well as experts. If you follow <a href="https://pypl.github.io/PYPL.html">PYPL index</a>, as of July 2021, Python is way ahead in terms of popularity ( &gt;30% share from a plethora of languages). With over 1500 active contributors, the language provides some of the most interesting out-of-the-box tools for its community. In this blog, I will be introducing to three of my most favourite python tools that I frequently use in my development.</p><h4>Type-hinting</h4><p>For some of you this might be new, and many might be like what’s new about this. As the name suggests, it provides a C/C++ like type definitions for the python variables etc. The syntax looks something like following:</p><pre><strong>def</strong> greeting(name: str) -&gt; str:<br>    greet: str = &#39;Hello &#39;<br>    <strong>return</strong> greet + name</pre><p>And the fascinating part, you can use this to catch development time errors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XbQObJXoqUwvqQJdBM-qoQ.png" /></figure><p>You can further enhance the type-hinting by using <a href="https://docs.python.org/3/library/typing.html">typing</a> module to add more details to you code.</p><p>We just changed Python from being an interpreted language to compile language 😜 (just kidding, don’t kick me for it! But this does help in writing <em>better</em><strong> </strong>quality code)</p><h4>Walrus Operator</h4><p>Python3.8 came with a lot of new features, the most useful (and funny) one being the Walrus operator <strong>:=</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T-BXh0M8gJKz4SfNPNw_gg.png" /><figcaption>Walrus Non-Operator</figcaption></figure><p>Yes, your wild guess is correct. It is called <em>Walrus</em> because of this only (Walrist much? 🤷‍♂)</p><p>This is a new type of assignment expression that allows you to assign and return a value in the same expression</p><p>Assume your piece of code:</p><pre>def is_big_room(l, b):<br>  area = l*b<br>  if area &gt;= 10: <br>    big_room = True<br>  print(big_room)</pre><p>can now simple be written as:</p><pre>def is_big_room(l, b):<br>  area = l*b<br>  print(big_room := area &gt;= 10) #Assigns True to big_room on the fly</pre><p>If you are feeling a bit more geeky, go ahead and skim through the <a href="https://www.python.org/dev/peps/pep-0572/">PEP-0572</a></p><h4><strong>Dunder Methods</strong></h4><p>Saving the best for the last, Dunder or Magic methods. Dunder methods are special methods, preceded and succeeded by double underscores, that can override the functionality of build-in function or class methods.</p><blockquote>Fun fact: Everything in python is an object, so you can override the functionality of everything, in one way or another</blockquote><pre><strong>class </strong>Dunder:<br>    <strong>def </strong>__init__(self, name: str, age: int, height: float):<br>        self.name = name<br>        self.age = age<br>        self.height = height<br><br>    <strong>def </strong>__repr__(self):<br>        <strong>return f&quot;{</strong>self.name.capitalize()<strong>}, {</strong>self.age<strong>}&quot;<br><br>    def </strong>__len__(self):<br>        <strong>return </strong>self.height<br><br><br>dunder = Dunder(<strong>&quot;JohnDoe&quot;</strong>, 34, 178)<br>print(<strong>f&quot;Hi, I am {</strong>dunder<strong>}&quot;</strong>)<br>print(<strong>f&quot;My height?: {</strong>len(dunder)<strong>}&quot;</strong>)</pre><p>Or, you can override the default list behaviours:</p><pre><strong>class </strong>Liar(list):<br>    <strong>def </strong>__len__(self):<br>        <strong>return </strong>super().__len__() + 5<br><br><br>ll = Liar([1, 2, 3])<br>print(len(ll)) # prints 8</pre><p>Hopefully, these tools will help you enrich your code as well!</p><p>Thank you folks! That was my time, you all have been a wonderful audience!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88d599ac07e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python Documentation: Generating HTML using Sphinx]]></title>
            <link>https://kanishkvarshney.medium.com/python-documentation-generating-html-using-sphinx-a0d909f5e963?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0d909f5e963</guid>
            <category><![CDATA[documentation]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Fri, 18 Jun 2021 21:10:33 GMT</pubDate>
            <atom:updated>2021-06-18T21:10:33.791Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*S6xPqDfdj1Y1BrTm" /><figcaption>Photo by <a href="https://unsplash.com/@sigmund?utm_source=medium&amp;utm_medium=referral">Sigmund</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This is part2 of the Python Documentation series. If you need help getting started with writing the documentation, you can take a look at <a href="https://kanishkvarshney.medium.com/python-documentation-getting-started-141f077b1b92">part1</a></p><p>In this part, we will look at generating HTML files from the Python code docstrings. We will be using <a href="https://www.sphinx-doc.org/en/master/">Sphinx</a> for that. You can install sphinx via Pip</p><pre>pip install sphinx</pre><p>Sphinx typically works with ReStructured Text (.rst) files, but we can add support for Markdown(.md) files via <em>m2r2</em></p><pre>pip install m2r2</pre><p>And we are done! Just kidding, but all that remains is updating few files and running few commands 😌</p><blockquote><strong>Note:</strong><em> Before generating docs, make sure that your codebase is a </em><a href="https://www.python-course.eu/python3_packages.php"><em>package</em></a><em> (contains __init__.py at relevant locations)</em></blockquote><p>Create a folder to store your docs and quick start the sphinx-setup:</p><pre>mkdir docs &amp;&amp; cd docs</pre><pre><em>sphinx-quickstart -q -p &quot;&lt;Project&gt;&quot; -a &quot;&lt;Author&gt;&quot; -v &quot;&lt;Version&gt;&quot;</em>​</pre><p>You should see something similar to the following inside your <strong><em>docs</em></strong> directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Fm01Q4wAzpw2fjXdWfeDuw.png" /><figcaption>Sphinx-quickstart initial structure</figcaption></figure><p>Next, we need to update the conf.py (primary control / config file)</p><ul><li>Uncomment and Update the paths to make out python package visible</li></ul><pre><strong>import </strong>os<br><strong>import </strong>sys<br>sys.path.insert(0, os.path.abspath(<strong>&#39;.&#39;</strong>))<br>sys.path.insert(0, os.path.abspath(<strong>&#39;..&#39;</strong>))</pre><ul><li>Load sphinx extensions. You can read about the extensions in detail <a href="https://www.sphinx-doc.org/en/master/usage/extensions/index.html">here</a></li></ul><pre>extensions = [<br>    <strong>&#39;sphinx.ext.napoleon&#39;</strong>,      <em># Supports Google / Numpy docstring <br>    </em><strong>&#39;sphinx.ext.autodoc&#39;</strong>,       <em># Documentation from docstrings<br>    </em><strong>&#39;sphinx.ext.doctest&#39;</strong>,       <em># Test snippets in documentation<br>    </em><strong>&#39;sphinx.ext.todo&#39;</strong>,          <em># to-do syntax highlighting<br>    </em><strong>&#39;sphinx.ext.ifconfig&#39;</strong>,      <em># Content based configuration<br>    </em><strong>&#39;m2r2&#39;                      </strong><em># Markdown support <br></em>]</pre><ul><li>Add supported file extensions for documentation</li></ul><pre>source_suffix = [‘.rst’, ‘.md’]</pre><p>You can customise your conf.py upto any extent (changing theme, adding support for latex, etc.</p><p>Now, we are ready to generate our documentation. The final build commands looks for .rst/.md files to generate html. Either you can manually write the files or use <em>sphinx-apidoc to automatically do it for you.</em></p><pre><em>sphinx-apidoc -f -o . ../</em>​</pre><p>To add rst / md files manually (readme, summary, etc), you can take help from <a href="https://www.sphinx-doc.org/en/master/usage/quickstart.html">Sphinx</a> and <a href="https://docutils.sourceforge.io/rst.html">ReStructured text</a> syntax guides</p><p>You will see new .rst files, including a <em>modules.rst </em>inside your docs folder. We need to update this file inside our main <em>index.rst</em></p><pre>Welcome to Projects&#39;s documentation!<br>====================================<br><br>.. <strong>toctree::<br>   :maxdepth:</strong> 2<br>   <strong>:caption:</strong> Contents:<br>   <br>   modules</pre><p>To add .md file, you will need to follow the syntax (instead of simply using .rst file name)</p><pre>.. mdinclude:: ../README.md</pre><p>Finally, we can build our rst / md files</p><pre>make<em> </em>html<em>​</em></pre><p>If all goes well, you will have html files generated inside <em>docs/_build/html/</em>​</p><blockquote>You can ignore some of the warnings, if they look irrelevant.</blockquote><p>You can manually check the webpages by starting a python http server and redirect to the aforementioned directory in the browser (localhost:8000)</p><pre><em>python –m http.server</em>​</pre><p>You should be able to see some fancy website containing the documentation of your module.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0d909f5e963" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python Documentation: Getting Started]]></title>
            <link>https://kanishkvarshney.medium.com/python-documentation-getting-started-141f077b1b92?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/141f077b1b92</guid>
            <category><![CDATA[user-experience]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[pycharm]]></category>
            <category><![CDATA[documentation]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Sun, 13 Jun 2021 19:46:14 GMT</pubDate>
            <atom:updated>2021-06-13T19:46:14.874Z</atom:updated>
            <content:encoded><![CDATA[<p>Many of us Pythonistas have wished to publish our own modules on Pip for everyone else to download and use. While writing the code/logic as python script is straightforward, the real hassle is actually formatting your code for publishing, adding documentation and publishing to PyPi and Documentation servers.</p><p>In this will be a multi-series, I will try to collate all the tools / commands required enabling us to build and publish your packages. In first series, we will cover the documentation of the Python Modules: walking you through creating the documentation, generating HTML using Sphinx and hosting it at ReadTheDocs.</p><p>Focusing on Documentation first rather than the module itself might sound weird, but ‘ A good Python Module is unusable without a Great Documentation!’. So hold on tight…</p><h4>Creating the documentation</h4><p>We will be using PyCharm to create our documentation. PyCharm provides a good integration to auto-generate the Docstring template for our functions, etc.</p><p>First, we will need to setup our doc-string format. Goto PyCharm &gt; Preferences &gt; Tools &gt; Python Integrated Tools &gt; Docstrings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v6rKDdYaF65XMAWdfJDeCQ.png" /><figcaption>Setting up docstring format</figcaption></figure><p>I prefer Google Docstring format, you are free to choose anyone to your preference. You can get a brief idea about different formats <a href="https://realpython.com/documenting-python-code/#docstring-formats">here</a></p><p>Now, you can start documenting your code. To document your function add a new line below your function start and enter triple quotes (“““\n”””) and press enter. This will generate the template docstring for your function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*2DpHUW5M5kBPRUFkXNxiyQ.png" /><figcaption>Initialising docstring</figcaption></figure><p>All the docstrings should be added inside triple quotes and directly following the function / class / module start. Stick to this format, as in later parts we will be using these triple-quote docstrings to generate HTML / PDF documentation for our module</p><h4>Docstring guidelines</h4><p>Although you are free to write whatever and however you want to, but following some general guidelines will help you develop detailed and consistent documentation for your module:</p><ul><li>Brief one-liner description of the function</li><li>Detailed explanation of the algorithm</li><li>Usage syntax and class parameters for the classes</li><li>Data types of the function parameters</li><li>Exceptions and Return values</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bx09Ga5bdhEzxml7DkvXhg.png" /><figcaption>Python Docstring Guidelines</figcaption></figure><p>The documentation build tools follow ReStructured Text syntax. You can add more details, code-blocks, TODO comments, Hyperlinks, etc. in your docstrings using the RST syntax.</p><p>One can also add some basic function syntax test using <a href="https://docs.python.org/2/library/doctest.html">doctest</a></p><p>In next part, we will look at generating HTML files from our Docstrings using Sphinx</p><p>For more details and resources, you can go through following resources</p><ul><li><a href="https://thomas-cokelaer.info/tutorials/sphinx/rest_syntax.html">https://thomas-cokelaer.info/tutorials/sphinx/rest_syntax.html</a></li><li><a href="https://docs.python-guide.org/writing/documentation/">https://docs.python-guide.org/writing/documentation/</a></li><li><a href="https://www.python.org/dev/peps/pep-0257/">https://www.python.org/dev/peps/pep-0257/</a></li><li><a href="https://realpython.com/documenting-python-code/">https://realpython.com/documenting-python-code/</a></li><li><a href="https://docs.python.org/2/library/doctest.html">https://docs.python.org/2/library/doctest.html</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=141f077b1b92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hosting your Flask Web Application on GoDaddy]]></title>
            <link>https://kanishkvarshney.medium.com/hosting-your-flask-web-application-on-godaddy-5628a60e7151?source=rss-f03eab685dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/5628a60e7151</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[godaddy]]></category>
            <category><![CDATA[hosting]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Kanishk Varshney]]></dc:creator>
            <pubDate>Mon, 06 Jul 2020 13:34:58 GMT</pubDate>
            <atom:updated>2020-07-06T13:34:58.537Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hosting your Flask Website on GoDaddy</h3><h4>A step-by-step, and detailed, walkthrough to host your Flask(python) Website on GoDaddy</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*viRs4bf-N1LdFE3G" /><figcaption>Photo by <a href="https://unsplash.com/@halacious?utm_source=medium&amp;utm_medium=referral">Halacious</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Whenever someone wants to have their own website, they only think about funky animations and interactions. But once the exciting part of creating the web application in local environment is done, many of us have no clue how to make it available for the world to see. Since this isn’t the fancy work, there aren’t adequate comprehensive resources explaining the process. This blog will try to provide a step-by-step guide for this cumbersome but crucial task.</p><p>Although this tutorial is meant for <a href="https://flask.palletsprojects.com/en/1.1.x/">flask </a>web applications, the procedure should be similar for Django or any other python based web framework</p><blockquote>Since I have only tried with Flask, I can’t guarantee other python frameworks will behave similarly. Above assumption is due to the procedure I followed didn’t require any flask specific work.</blockquote><h4>Getting Started</h4><p>Make sure you have following things up and ready before we start with the hosting:</p><ol><li><strong>Flask Application: </strong>Sounds obvious, but make sure the application is working in your local environment. Use <a href="https://pip.pypa.io/en/stable/">Pip </a>as your package manager, since GoDaddy Hosting Space supports Pip installations by default. You can choose other package managers like Conda, Poetry, etc. but that will increase your manual workload. Create a <em>requirements.txt</em> list file of your python packages</li><li><strong>GoDaddy Domain Name: </strong>This is the URL people will be visiting, make sure you have it up and running. You can just try to visit the URL, and confirm that you aren’t running into Page Not Found error</li><li><strong>Hosting Space: </strong>This is like a linux box on the GoDaddy Servers where you will be putting your code for your domain name to access. You will need to purchase a hosting plan(this is, and you can use external hosting spaces which suits your budget, experience, requirements, etc.<em> This guide only talks about GoDaddy Hosting Space on GoDaddy Domain Name</em>. I have a Starter Linux pack:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*r2wfcUJXoPS4MOB3XwFtXQ.png" /><figcaption>GoDaddy Starter Hosting Plan</figcaption></figure><p>Once you are done, your <strong>My Products</strong> section should look something similar to the following and we can proceed with the hosting</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kwrML0lmm6y5RYorx4Q_NQ.png" /><figcaption>Domain name and Web Hosting space purchased</figcaption></figure><h3>Hosting the Application</h3><p>Hosting your website is a two step process:</p><ul><li>Syncing your code on the Hosting Machine</li><li>Creating a Web Application to Run your code</li></ul><h4>Syncing Code on Hosting Machine</h4><p>To sync the code you will need to login into your hosting machine via ssh(use <a href="https://www.putty.org/">Putty</a>, if you are on Windows). But before that you will have to <strong>enable SSH</strong> access for the server. You can do this by redirecting to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CVX2UFww86IqKrcW825meA.png" /><figcaption>Enabling SSH access on your machine</figcaption></figure><p>My Products &gt; Web Hosting &gt; Manage &gt; Settings &gt; Server &gt; SSH access &gt; Manage</p><p>This takes about 24–48 hours to take effect. Hence, have a good day or two ;)</p><p>One the SSH access in <strong>On</strong>, the IP address mentioned above will be your host address. You can find your credentials in the adjacent <strong>Access </strong>tab</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8x3bIgFPDaOHgy4iu8JLQw.png" /><figcaption>SSH login credentials</figcaption></figure><p><em>cPanel login </em>is your username and the folder path where you will be syncing your code once logged in</p><p>You might need to reset password and relogin on the dashboard for SSH to work</p><p>Remote machine supports Github and Python3 by default, if it isn’t you might need to contact customer care to reset your machine (and redo the above steps)</p><p>Clone your repository to <strong>/home/&lt;username/ </strong>or if you prefer you can manually sync your code here using FTP / file transfer tools.</p><p>You can also use cPanel Admin (black button as shown above) &gt; Files &gt; File Manager for syncing your code</p><h4>Creating the Web Application</h4><p>Once you are done with the code synving part, now it’s time to finally create the server application. For this you will need to goto:<br>cPanel admin &gt; Software &gt; Setup Pytho App</p><p>Click on <strong>Create Application</strong>,<strong> </strong>it will redirect you to the following page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n9n4bjrWU59_OoVVnWPUfQ.png" /></figure><ul><li>Select your python version from the drop down. If your version isn’t mentioned you can install it by downloading/transfering the python installer in the Host Machine and restart the machine.</li><li>Give Application root same name as your code folder</li><li>Application URL should pick your domain name by default</li><li>Application startup file, is the entry point for your application that you use to run application in your development environment</li><li>Application Entry Point, this file will be created by the cPanel. GoDaddy hosts python apps via wsgi, just put in an new python file name(you can just use wsgi.py).</li><li>Passenger log file path is optional, but recommended.</li><li>Under Configurations files, add your <em>requirements.txt </em>and trigger the <strong>Run pip install</strong>. This will install all the code dependencies.</li></ul><p>Now, click on the Create button near top right and Voila!!!</p><p>We are done and your website is out for the world to see. Have a good day ahead !!! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5628a60e7151" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>