<?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 rnab on Medium]]></title>
        <description><![CDATA[Stories by rnab on Medium]]></description>
        <link>https://medium.com/@rnab?source=rss-aae4efc67977------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lrOXlxy9UMCmRS_3HJrzKg@2x.jpeg</url>
            <title>Stories by rnab on Medium</title>
            <link>https://medium.com/@rnab?source=rss-aae4efc67977------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:18:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rnab/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[Python Programming | The Standard Library | difflib]]></title>
            <link>https://medium.com/boring-tech/python-programming-the-standard-library-difflib-28ffaf5c1155?source=rss-aae4efc67977------2</link>
            <guid isPermaLink="false">https://medium.com/p/28ffaf5c1155</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[rnab]]></dc:creator>
            <pubDate>Fri, 18 Jan 2019 13:41:08 GMT</pubDate>
            <atom:updated>2019-01-19T06:00:59.981Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python Programming | difflib</h3><p>This module in the python standard library provides classes and functions for comparing sequences like strings, lists etc. In this article we will look into the basics of SequenceMatcher, get_close_matches and Differ.</p><p>SequenceMatcher a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980’s by Ratcliff and Obershelp under the hyperbolic name “gestalt pattern matching.” The idea is to find the longest contiguous matching subsequence that contains no “junk” elements; these “junk” elements are ones that are uninteresting in some sense, such as blank lines or whitespace. (Handling junk is an extension to the Ratcliff and Obershelp algorithm.) The same idea is then applied recursively to the pieces of the sequences to the left and to the right of the matching subsequence. This does not yield minimal edit sequences, but does tend to yield matches <em>that look right to people.</em></p><p>So, let’s see how to use it.</p><pre>from difflib import SequenceMatcher</pre><pre>str1 = &#39;abcd&#39;<br>str2 = &#39;abcde&#39;<br>seq = SequenceMatcher(a=str1, b=str2)<br>print(seq.ratio())</pre><p>The SequenceMatcher class accepts two pararmeters a and b and it compares the similarity of b to a and gives us a score or ratio of similarity. So the above code outputs 0.88888 ratio which means str2 is 80% similar to str1 .</p><p>The get_close_matches function gives us the top similar words from a list that is similar to a given string.</p><pre>from difflib import get_close_matches<br>word_list = [&#39;acdefgh&#39;, &#39;abcd&#39;,&#39;adef&#39;,&#39;cdea&#39;]<br>str1 = &#39;abcd&#39;<br>matches = get_close_matches(str1, word_list, n=2, cutoff=0.3)<br>print(matches)</pre><p>Here n is the number of top similar words we want in the output and cutoff is the minimum ratio value required for that word in order to classify it as similar. So this piece outputs [&#39;abcd&#39;, &#39;abcdefgh&#39;] , if we increase the cutoff to 0.7 it will only output [&#39;abcd&#39;] as that is the only word in the list that will give a similarity ratio of &gt;0.7. This function comes in very handy when making a quick ‘typo detection code’ , for example if we write ‘appl’ it can suggest did you mean ‘apple’.</p><p>The Differ class provides a human readable of the deltas in two sequences.</p><pre>from diffib import Differ<br>from pprint import pprint<br> <br>txt1 = &#39;&#39;&#39;<br>hello world.<br>we like python.&#39;&#39;&#39;.splitlines()</pre><pre>txt2 = &#39;&#39;&#39;<br>hello world.<br>we like python coding&#39;&#39;&#39;.splitlines()</pre><pre>dif = Differ()</pre><pre>df = list(dif.compare(txt1, txt2))</pre><pre>pprint(df)</pre><p>This gives us an output like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*RY4trn5pywd8MLv3JFXVKw.png" /><figcaption>output of Differ.compare()</figcaption></figure><p>Here we can see that it compares txt2 with txt1 and gives us a human readable structure showing what changed in txt2 from txt1.</p><p>As we can see here ‘hello world’ is same in both the sequences but the second sentence has changed and its showing that ‘coding’ is the change in the second sentence of both the strings. Here’s the video tutorial for this</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FrAXpKqCzwK8%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrAXpKqCzwK8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FrAXpKqCzwK8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/adfe975b56ed5687817e6e10494420a2/href">https://medium.com/media/adfe975b56ed5687817e6e10494420a2/href</a></iframe><p>There are lot more cool and complex functions in the module difflib , do check out <a href="https://docs.python.org/3/library/difflib.html">the official python documentation of this module</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28ffaf5c1155" width="1" height="1" alt=""><hr><p><a href="https://medium.com/boring-tech/python-programming-the-standard-library-difflib-28ffaf5c1155">Python Programming | The Standard Library | difflib</a> was originally published in <a href="https://medium.com/boring-tech">Boring Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python Programming | The Standard Library | cProfile]]></title>
            <link>https://medium.com/boring-tech/python-programming-the-standard-library-cprofile-3dab5b8240b1?source=rss-aae4efc67977------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dab5b8240b1</guid>
            <category><![CDATA[profiling]]></category>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python-standard-library]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[rnab]]></dc:creator>
            <pubDate>Mon, 14 Jan 2019 11:17:59 GMT</pubDate>
            <atom:updated>2019-01-19T06:01:24.653Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python Programming | cProfile</h3><p><strong>cProfile</strong> provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the <strong>pstats</strong> module.</p><p>It’s very easy to use and gives us an insight of what might be wrong or part of the code is taking most time, thus helping us a lot in optimizing.</p><p>Lets start by creating a dummy function called do_something and import cProfile</p><pre>import cProfile</pre><pre># a dummy function for demonstration<br>def do_something():<br>    li = []<br>    for i in range(100000):<br>        li.append(i)<br>    li.sort()<br>    return li</pre><p>Now, lets initialize the profiler…</p><pre>pr = cProfile.Profile()<br>pr.enable()<br>do_something()<br>pr.disable()<br>pr.print_stats()</pre><p>This will output us this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iSBhoIb6AuzEAV14_R7JSQ.png" /><figcaption>stats displayed by cProfile</figcaption></figure><p>Here we can see that it displays number of calls, total time taken and cumulative time of every function calls. We can also save this stats data in a file using pr.dump_stats(filename) instead of pr.print_stats() and can sort it and perform other statistical operations using pstats . Lets have a look at the whole code now.</p><pre>import cProfile, pstats</pre><pre>def do_something():</pre><pre>    li = []</pre><pre>    for i in range(100000):</pre><pre>        li.append(i)</pre><pre>    li.sort()</pre><pre>    return li</pre><pre>pr = cProfile.Profile()</pre><pre>pr.enable()</pre><pre>do_something()</pre><pre>pr.disable()</pre><pre>pr.dump_stats(&#39;prof_data&#39;)</pre><pre>ps = pstats.Stats(&#39;prof_data&#39;)</pre><pre>ps.sort_stats(pstats.SortKey.CUMULATIVE)</pre><pre>ps.print_stats()</pre><p>This save the stats data in prof_data file and also displays the stats ordered by cumulative time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sigwh-Q6rW5-h_s7OGCF_w.png" /><figcaption>stats displayed by pstats ordered by Cumulative time</figcaption></figure><p>Here is the video tutorial for this</p><p><a href="https://youtu.be/G8A4LVS0pHg">https://youtu.be/G8A4LVS0pHg</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dab5b8240b1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/boring-tech/python-programming-the-standard-library-cprofile-3dab5b8240b1">Python Programming | The Standard Library | cProfile</a> was originally published in <a href="https://medium.com/boring-tech">Boring Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>