<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Simple substring counting script in Python</title> <atom:link href="https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Mon, 01 Jan 2024 17:12:20 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>By: Bogdan</title><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html#comment-103355</link> <dc:creator><![CDATA[Bogdan]]></dc:creator> <pubDate>Thu, 07 Jan 2010 16:25:46 +0000</pubDate> <guid
isPermaLink="false">http://www.bogdan.org.ua/blog/2006/06/21/simple-substring-counting-script-in-python.html#comment-103355</guid> <description><![CDATA[Nice, thanks.]]></description> <content:encoded><![CDATA[<p>Nice, thanks.</p> ]]></content:encoded> </item> <item><title>By: pete</title><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html#comment-103354</link> <dc:creator><![CDATA[pete]]></dc:creator> <pubDate>Thu, 07 Jan 2010 15:26:55 +0000</pubDate> <guid
isPermaLink="false">http://www.bogdan.org.ua/blog/2006/06/21/simple-substring-counting-script-in-python.html#comment-103354</guid> <description><![CDATA[&lt;pre&gt;&lt;code&gt;
def getSubStringPositionsList(str=None, subStr=None):
l=[]
posCount = 0
while str:
pos = str.find(subStr)
if pos != -1:
l.append(pos + posCount)
str = str[pos + len(subStr):]
posCount += pos + len(subStr)
else:
break
return l
def getSubStringCount(str=None, subStr=None):
return len(getSubStringPositionsList(str, subStr))
&lt;/code&gt;&lt;/pre&gt;]]></description> <content:encoded><![CDATA[<pre><code>
def getSubStringPositionsList(str=None, subStr=None):
    l=[]
    posCount = 0
    while str:
        pos = str.find(subStr)
        if pos != -1:
            l.append(pos + posCount)
            str = str[pos + len(subStr):]
            posCount += pos + len(subStr)
        else:
            break
    return l
def getSubStringCount(str=None, subStr=None):
    return len(getSubStringPositionsList(str, subStr))
</code></pre>]]></content:encoded> </item> <item><title>By: Bogdan</title><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html#comment-102552</link> <dc:creator><![CDATA[Bogdan]]></dc:creator> <pubDate>Mon, 12 Oct 2009 09:38:03 +0000</pubDate> <guid
isPermaLink="false">http://www.bogdan.org.ua/blog/2006/06/21/simple-substring-counting-script-in-python.html#comment-102552</guid> <description><![CDATA[Thanks for the contribution!
* please note, that the script listed is 3+ years old; something has definitely changed in Python since then
* your options parser is definitely less flexible (and less verbose to the end-user)
* your main loop is indeed several lines shorter, but mostly thanks to omitting open() and file.close() with while() - imported from __future__, as well as not using the &#039;--verbose&#039; option processing to dump extra data to the terminal
Overall, I find your submission definitely useful, but not actually as short and simple as you implied with &quot;That&#039;s not simple :)&quot; :)]]></description> <content:encoded><![CDATA[<p>Thanks for the contribution!</p><p>* please note, that the script listed is 3+ years old; something has definitely changed in Python since then<br
/> * your options parser is definitely less flexible (and less verbose to the end-user)<br
/> * your main loop is indeed several lines shorter, but mostly thanks to omitting open() and file.close() with while() &#8211; imported from __future__, as well as not using the &#8216;&#8211;verbose&#8217; option processing to dump extra data to the terminal</p><p>Overall, I find your submission definitely useful, but not actually as short and simple as you implied with &#8220;That&#8217;s not simple :)&#8221; <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p> ]]></content:encoded> </item> <item><title>By: spiderlama</title><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html#comment-102550</link> <dc:creator><![CDATA[spiderlama]]></dc:creator> <pubDate>Mon, 12 Oct 2009 04:56:08 +0000</pubDate> <guid
isPermaLink="false">http://www.bogdan.org.ua/blog/2006/06/21/simple-substring-counting-script-in-python.html#comment-102550</guid> <description><![CDATA[That&#039;s not simple :P
&lt;pre&gt;&lt;code&gt;
from __future__ import with_statement
import sys
if __name__ == &#039;__main__&#039;:
assert len(sys.argv) == 3, &#039;invalid arguments. usage: file str&#039;
filePath = sys.argv[1]
substr = sys.argv[2]
linesFound = 0
substrFound = 0
with open(filePath) as f:
for lineIndex, lineString in enumerate(f):
if lineString.find(substr) != -1:
linesFound += 1
substrFound += lineString.count(substr)
print filePath + &#039;:&#039; + str(lineIndex) + &#039;\t&#039; + \
lineString.rstrip(&#039;\r\n&#039;)
print &#039;Lines read from file:&#039;, lineIndex + 2
print &#039;Lines with substring found:&#039;, linesFound
print &#039;Total substrings detected:&#039;, substrFound
&lt;/code&gt;&lt;/pre&gt;]]></description> <content:encoded><![CDATA[<p>That&#8217;s not simple <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_razz.gif" alt=":P" class="wp-smiley" /></p><pre><code>
from __future__ import with_statement
import sys
if __name__ == '__main__':
    assert len(sys.argv) == 3, 'invalid arguments. usage: file str'
    filePath = sys.argv[1]
    substr = sys.argv[2]
    linesFound = 0
    substrFound = 0
    with open(filePath) as f:
        for lineIndex, lineString in enumerate(f):
            if lineString.find(substr) != -1:
                linesFound += 1
                substrFound += lineString.count(substr)
                print filePath + ':' + str(lineIndex) + '\t' + \
                    lineString.rstrip('\r\n')
        print 'Lines read from file:', lineIndex + 2
        print 'Lines with substring found:', linesFound
        print 'Total substrings detected:', substrFound
</code></pre>]]></content:encoded> </item> </channel> </rss>