<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
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/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Autarchy of the Private Cave &#187; Python</title> <atom:link href="https://bogdan.org.ua/categories/programming/python/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>Graphs in Python</title><link>https://bogdan.org.ua/2013/07/13/graphs-in-python.html</link> <comments>https://bogdan.org.ua/2013/07/13/graphs-in-python.html#comments</comments> <pubDate>Sat, 13 Jul 2013 18:06:26 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[graph]]></category> <category><![CDATA[graph-tool]]></category> <category><![CDATA[graphs]]></category> <category><![CDATA[igraph]]></category> <category><![CDATA[networkx]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1953</guid> <description><![CDATA[Sooner or later, everyone has to deal with graphs. Some people have to do programming with graphs, and a subset of those &#8211; do that in Python. NetworkX is a pure Python implementation, where anything can be nodes. Both nodes and edges have attributes. NetworkX supports directed graphs and multigraphs (where there are multiple edges [&#8230;]]]></description> <content:encoded><![CDATA[<p><img
src="http://bogdan.org.ua/wp-content/uploads/2013/07/directed-200x180.png" alt="directed graph" width="200" height="180" class="alignleft size-thumbnail wp-image-1956" />Sooner or later, everyone has to deal with <a
href="https://en.wikipedia.org/wiki/Graph_(mathematics)">graphs</a>. Some people have to do programming with graphs, and a subset of those &#8211; do that in Python.</p><p><a
href="http://networkx.github.com/" class="broken_link" rel="nofollow">NetworkX</a> is a pure Python implementation, where <em>anything</em> can be nodes. Both nodes and edges have attributes. NetworkX supports directed graphs and multigraphs (where there are multiple edges between nodes). It might be <a
href="http://projects.skewed.de/graph-tool/wiki/Performance" class="broken_link" rel="nofollow">slower than other implementations</a>, but you may even not notice that &#8211; especially when working with smaller graphs, or not applying computationally-intensive algorithms to your graphs.</p><p><a
href="http://projects.skewed.de/graph-tool/" class="broken_link" rel="nofollow">graph-tool</a> uses the Boost graph library (C++), so it should be really fast. It could be the only multi-threaded graph library for Python. It supports pickling the graphs, allows interactive graph drawing, and has well-illustrated documentation. If performance and efficiency are of utmost importance &#8211; could be the best choice.</p><p><a
href="http://igraph.sourceforge.net/">igraph</a> is also really fast &#8211; just like graph-tool when using 1 CPU; graph-tool only wins conclusively when it is run on multiple CPUs/cores. igraph has an R package bindings to C.</p><p><a
href="http://www.python.org/doc/essays/graphs.html" class="broken_link" rel="nofollow">Pure python</a> is also an option for really smaller cases.</p><p>Finally, there&#8217;s a discussion around <a
href="http://wiki.python.org/moin/PythonGraphApi">Python Graph API</a> to simplify the inter-changeability and inter-operability of various existing Python graph modules. It also has a list of some less-known Python graph libraries, so check it out.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2013%2F07%2F13%2Fgraphs-in-python.html&amp;linkname=Graphs%20in%20Python" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2013%2F07%2F13%2Fgraphs-in-python.html&amp;linkname=Graphs%20in%20Python" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2013%2F07%2F13%2Fgraphs-in-python.html&amp;linkname=Graphs%20in%20Python" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2013%2F07%2F13%2Fgraphs-in-python.html&amp;linkname=Graphs%20in%20Python" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2013%2F07%2F13%2Fgraphs-in-python.html&amp;linkname=Graphs%20in%20Python" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2013%2F07%2F13%2Fgraphs-in-python.html&#038;title=Graphs%20in%20Python" data-a2a-url="https://bogdan.org.ua/2013/07/13/graphs-in-python.html" data-a2a-title="Graphs in Python"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2013/07/13/graphs-in-python.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Beanstalkd and related tools for easy parallelizing and backgrounding</title><link>https://bogdan.org.ua/2012/02/18/beanstalkd-related-tools-for-easy-parallelizing-backgrounding.html</link> <comments>https://bogdan.org.ua/2012/02/18/beanstalkd-related-tools-for-easy-parallelizing-backgrounding.html#comments</comments> <pubDate>Sat, 18 Feb 2012 11:07:56 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[beanstalk]]></category> <category><![CDATA[beanstalkd]]></category> <category><![CDATA[queue]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1818</guid> <description><![CDATA[beanstalkd: a simple, fast work queue. Jack and the Beanstalkd: a web-app for basic work queue administration. beanstalkc: a simple beanstalkd client library for Python. queueit: a CLI interface tool which helps to integrate beanstalkd into shell scripts.]]></description> <content:encoded><![CDATA[<p><a
href="http://kr.github.com/beanstalkd/" class="broken_link" rel="nofollow">beanstalkd</a>: a simple, fast work queue.<br
/> <a
href="https://github.com/andreisavu/django-jack">Jack and the Beanstalkd</a>: a web-app for basic work queue administration.<br
/> <a
href="https://github.com/earl/beanstalkc">beanstalkc</a>: a simple beanstalkd client library for Python.<br
/> <a
href="https://github.com/chexov/queueit">queueit</a>: a CLI interface tool which helps to integrate beanstalkd into shell scripts.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2012%2F02%2F18%2Fbeanstalkd-related-tools-for-easy-parallelizing-backgrounding.html&amp;linkname=Beanstalkd%20and%20related%20tools%20for%20easy%20parallelizing%20and%20backgrounding" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2012%2F02%2F18%2Fbeanstalkd-related-tools-for-easy-parallelizing-backgrounding.html&amp;linkname=Beanstalkd%20and%20related%20tools%20for%20easy%20parallelizing%20and%20backgrounding" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2012%2F02%2F18%2Fbeanstalkd-related-tools-for-easy-parallelizing-backgrounding.html&amp;linkname=Beanstalkd%20and%20related%20tools%20for%20easy%20parallelizing%20and%20backgrounding" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2012%2F02%2F18%2Fbeanstalkd-related-tools-for-easy-parallelizing-backgrounding.html&amp;linkname=Beanstalkd%20and%20related%20tools%20for%20easy%20parallelizing%20and%20backgrounding" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2012%2F02%2F18%2Fbeanstalkd-related-tools-for-easy-parallelizing-backgrounding.html&amp;linkname=Beanstalkd%20and%20related%20tools%20for%20easy%20parallelizing%20and%20backgrounding" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2012%2F02%2F18%2Fbeanstalkd-related-tools-for-easy-parallelizing-backgrounding.html&#038;title=Beanstalkd%20and%20related%20tools%20for%20easy%20parallelizing%20and%20backgrounding" data-a2a-url="https://bogdan.org.ua/2012/02/18/beanstalkd-related-tools-for-easy-parallelizing-backgrounding.html" data-a2a-title="Beanstalkd and related tools for easy parallelizing and backgrounding"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2012/02/18/beanstalkd-related-tools-for-easy-parallelizing-backgrounding.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Python performance: set vs list</title><link>https://bogdan.org.ua/2011/08/15/python-performance-set-vs-list.html</link> <comments>https://bogdan.org.ua/2011/08/15/python-performance-set-vs-list.html#comments</comments> <pubDate>Mon, 15 Aug 2011 09:29:04 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Notepad]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[list]]></category> <category><![CDATA[membership]]></category> <category><![CDATA[performance]]></category> <category><![CDATA[set]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1673</guid> <description><![CDATA[Sometimes there is a need to be sure that no identifier is processed twice &#8211; for example, when parsing a file into a database, with file potentially containing duplicate records. An obvious solution is to properly wrap the DB insertion code into try&#8230;except block, and process duplicate primary ID exceptions. Another, sometimes more desired solution [&#8230;]]]></description> <content:encoded><![CDATA[<p>Sometimes there is a need to be sure that no identifier is processed twice &#8211; for example, when parsing a file into a database, with file potentially containing duplicate records. An obvious solution is to properly wrap the DB insertion code into try&#8230;except block, and process <em>duplicate primary ID</em> exceptions. Another, sometimes more desired solution is to maintain a set/list of processed IDs internally, and check against that list prior to attempting the insertion of anything. So is it a set or a list?</p><p>There are already quite a few internet resources discussing &#8220;python set vs list&#8221;, but probably the simplest while elegant way to test that is below.<br
/> <span
id="more-1673"></span><br
/> First, test the speed of adding/appending to a set or a list (here, I&#8217;m mimicking the real-life application, thus the test case has an optional loop):</p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="code" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$python -mtimeit -s 'myset = set()' 'for x in xrange(1000): myset.add(x)'</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">10000 loops, best of 3: 133 usec per loop</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$python -mtimeit -s 'tmp = list()' 'for x in xrange(1000): tmp.append(x)'</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">10000 loops, best of 3: 116 usec per loop</div></li></ol></div></div><p>As we can see, set and list are comparable in the speed of adding new items, with list being slightly (~12%) faster than set.</p><p>Now, the speed of membership testing: &#8216;x in tmp&#8217;. For this test, I&#8217;ve deliberately chosen an imbalance of True (1%) and False (99%) results for the test &#8211; again, mimicking the real problem I have at hand:</p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="code" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$python -mtimeit -s 'tmp = set()' -s 'for x in xrange(1000): tmp.add(x)' 'for x in xrange(100000): x in tmp'</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">100 loops, best of 3: 7.27 msec per loop</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$python -mtimeit -s 'tmp = list()' -s 'for x in xrange(1000): tmp.append(x)' 'for x in xrange(100000): x in tmp'</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">10 loops, best of 3: 2.12 sec per loop</div></li></ol></div></div><p>List is much slower for membership testing, while <a
href="http://en.wikipedia.org/wiki/Collection_(computing)#Sets">sets were designed to be fast for doing just that</a>.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F08%2F15%2Fpython-performance-set-vs-list.html&amp;linkname=Python%20performance%3A%20set%20vs%20list" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F08%2F15%2Fpython-performance-set-vs-list.html&amp;linkname=Python%20performance%3A%20set%20vs%20list" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F08%2F15%2Fpython-performance-set-vs-list.html&amp;linkname=Python%20performance%3A%20set%20vs%20list" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F08%2F15%2Fpython-performance-set-vs-list.html&amp;linkname=Python%20performance%3A%20set%20vs%20list" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F08%2F15%2Fpython-performance-set-vs-list.html&amp;linkname=Python%20performance%3A%20set%20vs%20list" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2011%2F08%2F15%2Fpython-performance-set-vs-list.html&#038;title=Python%20performance%3A%20set%20vs%20list" data-a2a-url="https://bogdan.org.ua/2011/08/15/python-performance-set-vs-list.html" data-a2a-title="Python performance: set vs list"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2011/08/15/python-performance-set-vs-list.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Using Python in a Java project</title><link>https://bogdan.org.ua/2011/05/17/using-python-in-a-java-project.html</link> <comments>https://bogdan.org.ua/2011/05/17/using-python-in-a-java-project.html#comments</comments> <pubDate>Tue, 17 May 2011 11:33:52 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Movies]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Groovy]]></category> <category><![CDATA[java]]></category> <category><![CDATA[Jython]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1589</guid> <description><![CDATA[If you are a Python zealot, and Java doesn&#8217;t feel right, but the project you are working on is a Java project &#8211; try Jython &#8211; Python for the Java platform, compile your python scripts into Java bytecode Groovy &#8211; not Python, but still a scripting language which compiles to jars]]></description> <content:encoded><![CDATA[<p>If you are a Python zealot, and Java <em>doesn&#8217;t feel right</em>, but the project you are working on is a Java project &#8211; try</p><ul><li><a
href="http://www.jython.org/">Jython</a> &#8211; Python for the Java platform, compile your python scripts into Java bytecode</li><li><a
href="http://groovy.codehaus.org/" class="broken_link" rel="nofollow">Groovy</a> &#8211; not Python, but still a scripting language which compiles to jars</li></ul><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F05%2F17%2Fusing-python-in-a-java-project.html&amp;linkname=Using%20Python%20in%20a%20Java%20project" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F05%2F17%2Fusing-python-in-a-java-project.html&amp;linkname=Using%20Python%20in%20a%20Java%20project" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F05%2F17%2Fusing-python-in-a-java-project.html&amp;linkname=Using%20Python%20in%20a%20Java%20project" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F05%2F17%2Fusing-python-in-a-java-project.html&amp;linkname=Using%20Python%20in%20a%20Java%20project" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F05%2F17%2Fusing-python-in-a-java-project.html&amp;linkname=Using%20Python%20in%20a%20Java%20project" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2011%2F05%2F17%2Fusing-python-in-a-java-project.html&#038;title=Using%20Python%20in%20a%20Java%20project" data-a2a-url="https://bogdan.org.ua/2011/05/17/using-python-in-a-java-project.html" data-a2a-title="Using Python in a Java project"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2011/05/17/using-python-in-a-java-project.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Introduction to Python for bioinformatics</title><link>https://bogdan.org.ua/2011/02/25/introduction-to-python-for-bioinformatics.html</link> <comments>https://bogdan.org.ua/2011/02/25/introduction-to-python-for-bioinformatics.html#comments</comments> <pubDate>Fri, 25 Feb 2011 12:03:55 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Bioinformatics]]></category> <category><![CDATA[Links]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Software]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1451</guid> <description><![CDATA[This overview presentation is two years old, but still a highly valuable resource: modules and tools mentioned are alive and useful. I think this is the second presentation by Giovanni I&#8217;m embedding (first one being about GNU/make for bioinformatics). Introduction to python for bioinformatics]]></description> <content:encoded><![CDATA[<p>This overview presentation is two years old, but still a highly valuable resource: modules and tools mentioned are alive and useful.<br
/> I think this is the second presentation by Giovanni I&#8217;m embedding (first one being about GNU/make for bioinformatics).</p><div
style="width:425px" id="__ss_1320208"><strong
style="display:block;margin:12px 0 4px"><a
href="http://www.slideshare.net/giovanni/introduction-to-python-for-bioinformatics" title="Introduction to python for bioinformatics">Introduction to python for bioinformatics</a></strong><object
id="__sse1320208" width="425" height="355"><param
name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introduction-090421044444-phpapp02&#038;stripped_title=introduction-to-python-for-bioinformatics&#038;userName=giovanni" /><param
name="allowFullScreen" value="true"/><param
name="allowScriptAccess" value="always"/><embed
name="__sse1320208" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introduction-090421044444-phpapp02&#038;stripped_title=introduction-to-python-for-bioinformatics&#038;userName=giovanni" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F25%2Fintroduction-to-python-for-bioinformatics.html&amp;linkname=Introduction%20to%20Python%20for%20bioinformatics" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F25%2Fintroduction-to-python-for-bioinformatics.html&amp;linkname=Introduction%20to%20Python%20for%20bioinformatics" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F25%2Fintroduction-to-python-for-bioinformatics.html&amp;linkname=Introduction%20to%20Python%20for%20bioinformatics" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F25%2Fintroduction-to-python-for-bioinformatics.html&amp;linkname=Introduction%20to%20Python%20for%20bioinformatics" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F25%2Fintroduction-to-python-for-bioinformatics.html&amp;linkname=Introduction%20to%20Python%20for%20bioinformatics" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F25%2Fintroduction-to-python-for-bioinformatics.html&#038;title=Introduction%20to%20Python%20for%20bioinformatics" data-a2a-url="https://bogdan.org.ua/2011/02/25/introduction-to-python-for-bioinformatics.html" data-a2a-title="Introduction to Python for bioinformatics"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2011/02/25/introduction-to-python-for-bioinformatics.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>How to easily install any PyPi/easy_install python module on Debian</title><link>https://bogdan.org.ua/2011/02/16/how-to-easily-install-any-pypi-easy_install-python-module-on-debian.html</link> <comments>https://bogdan.org.ua/2011/02/16/how-to-easily-install-any-pypi-easy_install-python-module-on-debian.html#comments</comments> <pubDate>Wed, 16 Feb 2011 17:22:20 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[*nix]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[Notepad]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Debian]]></category> <category><![CDATA[debianize]]></category> <category><![CDATA[easy_install]]></category> <category><![CDATA[pycassa]]></category> <category><![CDATA[PyPi]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1430</guid> <description><![CDATA[Imagine you need to install pycassa (which uses easy_install). Here are the 2 (at maximum) very simple steps to have it properly debianized and installed on your Debian/Ubuntu: if you don&#8217;t have the python-stdeb package: sudo aptitude install python-stdeb pypi-install pycassa That&#8217;s it. Refer to stdeb readme for more information. You will need that if [&#8230;]]]></description> <content:encoded><![CDATA[<p>Imagine you need to install <a
href="https://github.com/pycassa/pycassa">pycassa</a> (which uses easy_install). Here are the 2 (at maximum) very simple steps to have it properly debianized and installed on your Debian/Ubuntu:</p><ul><li>if you don&#8217;t have the python-stdeb package: <strong>sudo aptitude install python-stdeb</strong></li><li><strong>pypi-install pycassa</strong></li></ul><p>That&#8217;s it.</p><p>Refer to <a
href="https://github.com/astraw/stdeb#readme">stdeb readme</a> for more information. You will need that if there are dependencies &#8211; which might not be resolved automatically by stdeb.</p><p>Before stdeb, it wasn&#8217;t exactly trivial to <a
href="http://showmedo.com/videotutorials/video?name=linuxJensMakingDeb">make a .deb from python module</a>.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F16%2Fhow-to-easily-install-any-pypi-easy_install-python-module-on-debian.html&amp;linkname=How%20to%20easily%20install%20any%20PyPi%2Feasy_install%20python%20module%20on%20Debian" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F16%2Fhow-to-easily-install-any-pypi-easy_install-python-module-on-debian.html&amp;linkname=How%20to%20easily%20install%20any%20PyPi%2Feasy_install%20python%20module%20on%20Debian" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F16%2Fhow-to-easily-install-any-pypi-easy_install-python-module-on-debian.html&amp;linkname=How%20to%20easily%20install%20any%20PyPi%2Feasy_install%20python%20module%20on%20Debian" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F16%2Fhow-to-easily-install-any-pypi-easy_install-python-module-on-debian.html&amp;linkname=How%20to%20easily%20install%20any%20PyPi%2Feasy_install%20python%20module%20on%20Debian" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F16%2Fhow-to-easily-install-any-pypi-easy_install-python-module-on-debian.html&amp;linkname=How%20to%20easily%20install%20any%20PyPi%2Feasy_install%20python%20module%20on%20Debian" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2011%2F02%2F16%2Fhow-to-easily-install-any-pypi-easy_install-python-module-on-debian.html&#038;title=How%20to%20easily%20install%20any%20PyPi%2Feasy_install%20python%20module%20on%20Debian" data-a2a-url="https://bogdan.org.ua/2011/02/16/how-to-easily-install-any-pypi-easy_install-python-module-on-debian.html" data-a2a-title="How to easily install any PyPi/easy_install python module on Debian"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2011/02/16/how-to-easily-install-any-pypi-easy_install-python-module-on-debian.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>DreamPie: the ultimate Python shell</title><link>https://bogdan.org.ua/2010/08/11/dreampie-the-ultimate-python-shell.html</link> <comments>https://bogdan.org.ua/2010/08/11/dreampie-the-ultimate-python-shell.html#comments</comments> <pubDate>Wed, 11 Aug 2010 14:54:40 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1107</guid> <description><![CDATA[DreamPie: the Python shell you&#8217;ve always dreamt about! â€¢ Type your code in the lower pane of the window. To execute, press Ctrl+Enter. One-liners can be executed by simply pressing Enter; If you don&#8217;t want them executed, press Space and then Enter. â€¢ Use Ctrl+Up and Ctrl+Down to navigate between code segments you&#8217;ve already executed. [&#8230;]]]></description> <content:encoded><![CDATA[<p><a
href="http://dreampie.sourceforge.net/">DreamPie: the Python shell you&#8217;ve always dreamt about!</a></p><blockquote><p>â€¢ Type your code in the lower pane of the window. To execute, press Ctrl+Enter. One-liners can be executed by simply pressing Enter; If you don&#8217;t want them executed, press Space and then Enter.</p><p>â€¢ Use Ctrl+Up and Ctrl+Down to navigate between code segments you&#8217;ve already executed. You can write a few letters before pressing Ctrl+Up, and DreamPie will only search through code segments starting with those letters.</p><p>â€¢ Press Tab or Ctrl+Space to show a list of completions to the current expression. It will also complete file names!</p><p>â€¢ Your results are stored in variables named _0, _1, and so on.</p><p>â€¢ Type a function name and press the space key and DreamPie will automatically add parentheses for you!</p></blockquote><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F08%2F11%2Fdreampie-the-ultimate-python-shell.html&amp;linkname=DreamPie%3A%20the%20ultimate%20Python%20shell" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F08%2F11%2Fdreampie-the-ultimate-python-shell.html&amp;linkname=DreamPie%3A%20the%20ultimate%20Python%20shell" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F08%2F11%2Fdreampie-the-ultimate-python-shell.html&amp;linkname=DreamPie%3A%20the%20ultimate%20Python%20shell" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F08%2F11%2Fdreampie-the-ultimate-python-shell.html&amp;linkname=DreamPie%3A%20the%20ultimate%20Python%20shell" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F08%2F11%2Fdreampie-the-ultimate-python-shell.html&amp;linkname=DreamPie%3A%20the%20ultimate%20Python%20shell" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2010%2F08%2F11%2Fdreampie-the-ultimate-python-shell.html&#038;title=DreamPie%3A%20the%20ultimate%20Python%20shell" data-a2a-url="https://bogdan.org.ua/2010/08/11/dreampie-the-ultimate-python-shell.html" data-a2a-title="DreamPie: the ultimate Python shell"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2010/08/11/dreampie-the-ultimate-python-shell.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Good online Python book with code examples</title><link>https://bogdan.org.ua/2010/03/01/good-online-python-book-with-code-examples.html</link> <comments>https://bogdan.org.ua/2010/03/01/good-online-python-book-with-code-examples.html#comments</comments> <pubDate>Mon, 01 Mar 2010 18:03:11 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[book]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=1008</guid> <description><![CDATA[Building Skills in Python: A Programmer&#8217;s Introduction to Python by Steven F. Lott (Â© 2002, 2005, 2007, 2008 Steven F. Lott).]]></description> <content:encoded><![CDATA[<p><a
href="http://www.linuxtopia.org/online_books/programming_books/python_programming/index.html">Building Skills in Python: A Programmer&#8217;s Introduction to Python</a> by Steven F. Lott (Â© 2002, 2005, 2007, 2008 Steven F. Lott).</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F03%2F01%2Fgood-online-python-book-with-code-examples.html&amp;linkname=Good%20online%20Python%20book%20with%20code%20examples" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F03%2F01%2Fgood-online-python-book-with-code-examples.html&amp;linkname=Good%20online%20Python%20book%20with%20code%20examples" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F03%2F01%2Fgood-online-python-book-with-code-examples.html&amp;linkname=Good%20online%20Python%20book%20with%20code%20examples" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F03%2F01%2Fgood-online-python-book-with-code-examples.html&amp;linkname=Good%20online%20Python%20book%20with%20code%20examples" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2010%2F03%2F01%2Fgood-online-python-book-with-code-examples.html&amp;linkname=Good%20online%20Python%20book%20with%20code%20examples" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2010%2F03%2F01%2Fgood-online-python-book-with-code-examples.html&#038;title=Good%20online%20Python%20book%20with%20code%20examples" data-a2a-url="https://bogdan.org.ua/2010/03/01/good-online-python-book-with-code-examples.html" data-a2a-title="Good online Python book with code examples"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2010/03/01/good-online-python-book-with-code-examples.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Python: passing by value vs passing by reference</title><link>https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html</link> <comments>https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html#comments</comments> <pubDate>Mon, 11 Feb 2008 16:06:22 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[by reference]]></category> <category><![CDATA[by value]]></category> <category><![CDATA[function]]></category> <category><![CDATA[object]]></category> <category><![CDATA[scope]]></category> <category><![CDATA[variable]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html</guid> <description><![CDATA[Note: this a collection of scraps, describing how values (de)referencing works in Python, and describing when your variable is either a value or a reference. Primary source of knowledge for this post (also here). Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will [&#8230;]]]></description> <content:encoded><![CDATA[<p>Note: this a collection of scraps, describing how values (de)referencing works in Python, and describing when your variable is either a value or a reference. <a
href="http://www.testingreflections.com/node/view/5126" class="broken_link" rel="nofollow">Primary source of knowledge</a> for this post (also <a
href="http://bytes.com/topic/python/answers/37219-value-reference">here</a>).</p><p>Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference&#8230; what&#8217;s the deal?</p><p>It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable.  Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed.  Other objects, like lists and dictionaries are mutable, which means you can change the object in-place.  Therefore, altering an object inside a function/method will also change the original object outside.</p><p>Immutable variables &#8211; such as integers [<a
href="http://bytes.com/topic/python/answers/37219-value-reference">strings, numerics and tuples are immutables</a>] &#8211; are passed by value. That is, if your function accepts some integer argument, you are safe assuming that your function won&#8217;t be able to modify your integer. Mutable variables &#8211; such as dictionaries and lists &#8211; are passed by reference, and so if your function accepts mutable argument, it may modify the contents of that mutable variable outside the scope of the function.</p><p>When doing :<br
/> s = &#8220;Hello &#8221;<br
/> s += &#8220;World&#8221;<br
/> &#8230; you are not modifying the string object bound to s, but creating a new string object and binding it to s.</p><p>If using object&#8217;s methods within a called function, variable is considered &#8220;passed by reference&#8221; &#8211; it is modified out of the function&#8217;s scope. If using assignment on a mutable object, it is created a-new within the function, and global value isn&#8217;t modified.</p><p>When you call a function with an arg, a &#8220;local variable&#8221; is created, which references the object passed as the argument. (well&#8230; an entry with the formal parameter name as key and a reference to the object passed in is created in the &#8216;local&#8217; dict).</p><p>So, rebinding this local symbol does not impact the binding in the caller&#8217;s namespace &#8211; because the symbol lives in another namespace.</p><p>*But* &#8211; and if the object referenced is mutable of course &#8211; modifying the object in the function&#8230; well, just modifies the object, because it&#8217;s the *same* object that is bound to (&#8216;referenced by&#8217;, if you prefer) both symbols (the one in the caller&#8217;s namespace and the one in the function&#8217;s namespace). So yes, the object *is* modified when the function returns.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&amp;linkname=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F11%2Fpython-passing-by-value-vs-passing-by-reference.html&#038;title=Python%3A%20passing%20by%20value%20vs%20passing%20by%20reference" data-a2a-url="https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html" data-a2a-title="Python: passing by value vs passing by reference"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Useful Python looping techniques</title><link>https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</link> <comments>https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html#comments</comments> <pubDate>Wed, 26 Sep 2007 09:21:00 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[example]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[looping]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</guid> <description><![CDATA[These are all excerpts from the Python documentation. To synchronously and simultaneously loop over two sequences: questions = &#91;'name', 'quest', 'favourite colour'&#93; answers = &#91;'Lancelot', 'the holy grail', 'blue'&#93; &#160; for q, a in zip&#40;questions, answers&#41;: &#160; &#160; print 'What is your %s? &#160;It is %s.' % &#40;q, a&#41; To loop over a sequence with [&#8230;]]]></description> <content:encoded><![CDATA[<p>These are all excerpts from the Python documentation.</p><p>To synchronously and simultaneously loop over two sequences:</p><div
id="ig-sh-3" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">questions <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'name'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'quest'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'favourite colour'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">answers <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'Lancelot'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'the holy grail'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'blue'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> q<span
style="color: #66cc66;">,</span> a <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">zip</span><span
style="color: black;">&#40;</span>questions<span
style="color: #66cc66;">,</span> answers<span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">'What is your %s? &nbsp;It is %s.'</span> % <span
style="color: black;">&#40;</span>q<span
style="color: #66cc66;">,</span> a<span
style="color: black;">&#41;</span></div></li></ol></div></div><p>To loop over a sequence with both key and value:<br
/> <span
id="more-229"></span></p><div
id="ig-sh-4" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> i<span
style="color: #66cc66;">,</span> v <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">enumerate</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'tic'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'tac'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'toe'</span><span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> i<span
style="color: #66cc66;">,</span> v</div></li></ol></div></div><p>To loop over dictionary, again with key and value:</p><div
id="ig-sh-5" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">knights <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'gallahad'</span>: <span
style="color: #483d8b;">'the pure'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'robin'</span>: <span
style="color: #483d8b;">'the brave'</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> k<span
style="color: #66cc66;">,</span> v <span
style="color: #ff7700;font-weight:bold;">in</span> knights.<span
style="color: black;">iteritems</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k<span
style="color: #66cc66;">,</span> v</div></li></ol></div></div><p>To loop over some sorted sequence, but without modifying the original sequence (beware the use of set() in this example):</p><div
id="ig-sh-6" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">basket <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'apple'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'orange'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'apple'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'pear'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'orange'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'banana'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> f <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">sorted</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">set</span><span
style="color: black;">&#40;</span>basket<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> f</div></li></ol></div></div><p>For looping sorted dictionaries, see also <a
href="http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html">How to sort Python dict (dictionary)</a>.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&#038;title=Useful%20Python%20looping%20techniques" data-a2a-url="https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html" data-a2a-title="Useful Python looping techniques"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>How to sort Python dict (dictionary)</title><link>https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html</link> <comments>https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html#comments</comments> <pubDate>Wed, 26 Sep 2007 06:20:57 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[example]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[sorting]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html</guid> <description><![CDATA[Sample script (copypasted from Well House Consultants training course): click the PLAIN TEXT header for copy-pasteable version #!/usr/local/bin/python &#160; author = &#123;&#34;php&#34;:&#34;Rasmus Lerdorf&#34;,\ &#160; &#160; &#34;perl&#34;:&#34;Larry Wall&#34;,\ &#160; &#160; &#34;tcl&#34;:&#34;John Ousterhout&#34;,\ &#160; &#160; &#34;awk&#34;:&#34;Brian Kernighan&#34;,\ &#160; &#160; &#34;java&#34;:&#34;James Gosling&#34;,\ &#160; &#160; &#34;parrot&#34;:&#34;Simon Cozens&#34;,\ &#160; &#160; &#34;python&#34;:&#34;Guido van Rossum&#34;&#125; &#160; langs = author.keys&#40;&#41; langs.sort&#40;&#41; &#160; for [&#8230;]]]></description> <content:encoded><![CDATA[<p>Sample script (copypasted from <a
href="http://www.wellho.net/resources/ex.php4?item=y107/d3.py">Well House Consultants training course</a>):<br
/> <em>click the PLAIN TEXT header for copy-pasteable version</em></p><div
id="ig-sh-7" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #808080; font-style: italic;">#!/usr/local/bin/python</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">author <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">&quot;php&quot;</span>:<span
style="color: #483d8b;">&quot;Rasmus Lerdorf&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;perl&quot;</span>:<span
style="color: #483d8b;">&quot;Larry Wall&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;tcl&quot;</span>:<span
style="color: #483d8b;">&quot;John Ousterhout&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;awk&quot;</span>:<span
style="color: #483d8b;">&quot;Brian Kernighan&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;java&quot;</span>:<span
style="color: #483d8b;">&quot;James Gosling&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;parrot&quot;</span>:<span
style="color: #483d8b;">&quot;Simon Cozens&quot;</span><span
style="color: #66cc66;">,</span>\</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #483d8b;">&quot;python&quot;</span>:<span
style="color: #483d8b;">&quot;Guido van Rossum&quot;</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">langs <span
style="color: #66cc66;">=</span> author.<span
style="color: black;">keys</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">langs.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> language <span
style="color: #ff7700;font-weight:bold;">in</span> langs:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> language<span
style="color: #66cc66;">,</span><span
style="color: #483d8b;">&quot;is the child of&quot;</span><span
style="color: #66cc66;">,</span>author<span
style="color: black;">&#91;</span>language<span
style="color: black;">&#93;</span></div></li></ol></div></div><p>You can also define the Python <strong>ksort()</strong> function similar to that found in PHP:<br
/> <span
id="more-228"></span></p><div
id="ig-sh-8" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">def</span> ksort<span
style="color: black;">&#40;</span>d<span
style="color: #66cc66;">,</span> func <span
style="color: #66cc66;">=</span> <span
style="color: #008000;">None</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; keys <span
style="color: #66cc66;">=</span> d.<span
style="color: black;">keys</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; keys.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span>func<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> keys</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #808080; font-style: italic;"># example use, assume 'd' is a dictionary</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> k <span
style="color: #ff7700;font-weight:bold;">in</span> ksort<span
style="color: black;">&#40;</span>d<span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k<span
style="color: #66cc66;">,</span> d<span
style="color: black;">&#91;</span>k<span
style="color: black;">&#93;</span></div></li></ol></div></div><p>(example taken from <a
href="http://bytes.com/topic/python/answers/21704-sort-dictionary">TheScript forum</a>)</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&amp;linkname=How%20to%20sort%20Python%20dict%20%28dictionary%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fhow-to-sort-python-dict-dictionary.html&#038;title=How%20to%20sort%20Python%20dict%20%28dictionary%29" data-a2a-url="https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html" data-a2a-title="How to sort Python dict (dictionary)"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>MySQL &#8211; Python: good MySQLdb tutorial (examples)</title><link>https://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html</link> <comments>https://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html#comments</comments> <pubDate>Fri, 07 Sep 2007 12:32:25 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Notepad]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[examples]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[mysqldb]]></category> <category><![CDATA[tutorial]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html</guid> <description><![CDATA[Andy Dustman (used to blog at dustman.net) gave a presentation on Python and MySQL at the MySQL Users Conference 2005, Santa Clara, CA. The presentation is an excellent collection of examples for those who use the MySQLdb Python module. For the purpose of maintaining a personal archive of that excellent presentation, I created a PDF [&#8230;]]]></description> <content:encoded><![CDATA[<p>Andy Dustman (used to blog at dustman.net) gave a presentation on Python and MySQL at the MySQL Users Conference 2005, Santa Clara, CA. The presentation is an excellent collection of examples for those who use the MySQLdb Python module.<br
/> <span
id="more-220"></span><br
/> For the purpose of maintaining a personal archive of that excellent presentation, I created a <a
href="http://bogdan.org.ua/wp-content/uploads/2007/09/python-and-mysql.pdf">PDF version</a> of it. All copyrights belong to Andy Dustman.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fmysql-python-good-mysqldb-tutorial-examples.html&amp;linkname=MySQL%20%E2%80%93%20Python%3A%20good%20MySQLdb%20tutorial%20%28examples%29" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fmysql-python-good-mysqldb-tutorial-examples.html&amp;linkname=MySQL%20%E2%80%93%20Python%3A%20good%20MySQLdb%20tutorial%20%28examples%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fmysql-python-good-mysqldb-tutorial-examples.html&amp;linkname=MySQL%20%E2%80%93%20Python%3A%20good%20MySQLdb%20tutorial%20%28examples%29" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fmysql-python-good-mysqldb-tutorial-examples.html&amp;linkname=MySQL%20%E2%80%93%20Python%3A%20good%20MySQLdb%20tutorial%20%28examples%29" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fmysql-python-good-mysqldb-tutorial-examples.html&amp;linkname=MySQL%20%E2%80%93%20Python%3A%20good%20MySQLdb%20tutorial%20%28examples%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fmysql-python-good-mysqldb-tutorial-examples.html&#038;title=MySQL%20%E2%80%93%20Python%3A%20good%20MySQLdb%20tutorial%20%28examples%29" data-a2a-url="https://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html" data-a2a-title="MySQL – Python: good MySQLdb tutorial (examples)"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Date and time in Python scripts</title><link>https://bogdan.org.ua/2007/09/07/date-and-time-in-python-scripts.html</link> <comments>https://bogdan.org.ua/2007/09/07/date-and-time-in-python-scripts.html#comments</comments> <pubDate>Fri, 07 Sep 2007 12:07:45 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Notepad]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/07/date-and-time-in-python-scripts.html</guid> <description><![CDATA[Here&#8217;s an excellent description of various methods to handle date and time in Python scripts.]]></description> <content:encoded><![CDATA[<p>Here&#8217;s an <a
href="http://seehuhn.de/pages/pdate">excellent description</a> of various methods to handle date and time in Python scripts.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fdate-and-time-in-python-scripts.html&amp;linkname=Date%20and%20time%20in%20Python%20scripts" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fdate-and-time-in-python-scripts.html&amp;linkname=Date%20and%20time%20in%20Python%20scripts" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fdate-and-time-in-python-scripts.html&amp;linkname=Date%20and%20time%20in%20Python%20scripts" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fdate-and-time-in-python-scripts.html&amp;linkname=Date%20and%20time%20in%20Python%20scripts" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fdate-and-time-in-python-scripts.html&amp;linkname=Date%20and%20time%20in%20Python%20scripts" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F07%2Fdate-and-time-in-python-scripts.html&#038;title=Date%20and%20time%20in%20Python%20scripts" data-a2a-url="https://bogdan.org.ua/2007/09/07/date-and-time-in-python-scripts.html" data-a2a-title="Date and time in Python scripts"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/09/07/date-and-time-in-python-scripts.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Elegantly converting Python list into another list with changed items</title><link>https://bogdan.org.ua/2007/08/30/elegantly-converting-python-list-into-another-list-with-changed-items.html</link> <comments>https://bogdan.org.ua/2007/08/30/elegantly-converting-python-list-into-another-list-with-changed-items.html#comments</comments> <pubDate>Thu, 30 Aug 2007 09:06:50 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/30/elegantly-converting-python-list-into-another-list-with-changed-items.html</guid> <description><![CDATA[I just had a small problem with my Python script: 1. I have a list of tuples, called records_cache; each tuple looks like this: (note: for copy-pasting, click the PLAIN TEXT box headers) &#40;ensembl_transcript_id, ensembl_gene_id, chrom_start, chrom_end, utr_start, utr_end, sequence, strand&#41; 2. I need to INSERT only some of those values into MySQL table, using [&#8230;]]]></description> <content:encoded><![CDATA[<p>I just had a small problem with my Python script:</p><p>1. I have a list of tuples, called <strong>records_cache</strong>; each tuple looks like this:<br
/> (<em>note: for copy-pasting, click the PLAIN TEXT box headers</em>)</p><div
id="ig-sh-9" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: black;">&#40;</span>ensembl_transcript_id<span
style="color: #66cc66;">,</span> ensembl_gene_id<span
style="color: #66cc66;">,</span> chrom_start<span
style="color: #66cc66;">,</span> chrom_end<span
style="color: #66cc66;">,</span> utr_start<span
style="color: #66cc66;">,</span> utr_end<span
style="color: #66cc66;">,</span> sequence<span
style="color: #66cc66;">,</span> strand<span
style="color: black;">&#41;</span></div></li></ol></div></div><p>2. I need to INSERT only some of those values into MySQL table, using the MySQLdb executemany() method. The values I need are <strong>utr_start</strong>, <strong>utr_end</strong>, <strong>ensembl_gene_id</strong> (in exactly this order).</p><p>To do this, I need to create another list of tuples, but with shorter 3-item tuples. Let us call the new list <strong>genes_update</strong>.</p><p>Here is the elegant, though probably not the most efficient, solution:<br
/> <span
id="more-215"></span></p><div
id="ig-sh-10" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">genes_update <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: black;">&#40;</span>onetuple<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">4</span><span
style="color: black;">&#93;</span><span
style="color: #66cc66;">,</span>onetuple<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">5</span><span
style="color: black;">&#93;</span><span
style="color: #66cc66;">,</span>onetuple<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">1</span><span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span> <span
style="color: #ff7700;font-weight:bold;">for</span> onetuple <span
style="color: #ff7700;font-weight:bold;">in</span> records_cache<span
style="color: black;">&#93;</span><span
style="color: black;">&#93;</span></div></li></ol></div></div><p>Let&#8217;s see what is happening here:</p><ol><li>First, note the brackets around the r-expression. These brackets force the result to be a list.</li><li>Next, note the construct used, it is from the Python manual: <strong><em>list_element.method()</em> for <em>list_element</em> in <em>list</em></strong>. This construct iterates all items of the <em>list</em>, in our case &#8211; of the <em>records_cache</em> list. This construct also applies the <em>list_element.method()</em> to each list method.</li><li>Instead of applying some <em>list_element.method()</em>, I just use tuple indices to select the values I need, and then (using parenthesis) join those values into a tuple again.</li><li>So for each tuple in the <strong>records_cache</strong> list, another (shorter) tuple is returned, and those shorter tuples are joined into the new list, which is assigned to the <strong>genes_update</strong> list.</li></ol><p>After this conversion, I can happily write that list of tuples into MySQL table, using cursor.executemany():<br
/> (<em>note: error_log() is a small custom function for appending to script&#8217;s error.log file</em>)</p><div
id="ig-sh-11" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">try</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; cursor.<span
style="color: black;">executemany</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;&quot;&quot;UPDATE genes SET</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #483d8b;"> &nbsp; &nbsp; &nbsp; &nbsp;5utr_start = %s,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #483d8b;"> &nbsp; &nbsp; &nbsp; &nbsp;5utr_end = %s</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #483d8b;"> &nbsp; &nbsp; &nbsp; &nbsp;WHERE ensembl_gene_id = %s&quot;&quot;&quot;</span><span
style="color: #66cc66;">,</span> genes_update<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; db.<span
style="color: black;">commit</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">except</span> MySQLdb.<span
style="color: black;">Error</span><span
style="color: #66cc66;">,</span> <span
style="color: black;">&#40;</span><span
style="color: #dc143c;">errno</span><span
style="color: #66cc66;">,</span> strerror<span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; db.<span
style="color: black;">rollback</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; error_log<span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;Error %d: %s (%s)<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span> % <span
style="color: black;">&#40;</span><span
style="color: #dc143c;">errno</span><span
style="color: #66cc66;">,</span> strerror<span
style="color: #66cc66;">,</span> insertq<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">False</span> <span
style="color: #808080; font-style: italic;"># failed to update</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">True</span> <span
style="color: #808080; font-style: italic;"># updated</span></div></li></ol></div></div><p>Comments are welcome, especially on the efficiency improvement.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F30%2Felegantly-converting-python-list-into-another-list-with-changed-items.html&amp;linkname=Elegantly%20converting%20Python%20list%20into%20another%20list%20with%20changed%20items" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F30%2Felegantly-converting-python-list-into-another-list-with-changed-items.html&amp;linkname=Elegantly%20converting%20Python%20list%20into%20another%20list%20with%20changed%20items" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F30%2Felegantly-converting-python-list-into-another-list-with-changed-items.html&amp;linkname=Elegantly%20converting%20Python%20list%20into%20another%20list%20with%20changed%20items" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F30%2Felegantly-converting-python-list-into-another-list-with-changed-items.html&amp;linkname=Elegantly%20converting%20Python%20list%20into%20another%20list%20with%20changed%20items" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F30%2Felegantly-converting-python-list-into-another-list-with-changed-items.html&amp;linkname=Elegantly%20converting%20Python%20list%20into%20another%20list%20with%20changed%20items" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F30%2Felegantly-converting-python-list-into-another-list-with-changed-items.html&#038;title=Elegantly%20converting%20Python%20list%20into%20another%20list%20with%20changed%20items" data-a2a-url="https://bogdan.org.ua/2007/08/30/elegantly-converting-python-list-into-another-list-with-changed-items.html" data-a2a-title="Elegantly converting Python list into another list with changed items"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/08/30/elegantly-converting-python-list-into-another-list-with-changed-items.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Non-Programmer&#8217;s Tutorial for Python</title><link>https://bogdan.org.ua/2007/08/12/non-programmers-tutorial-for-python.html</link> <comments>https://bogdan.org.ua/2007/08/12/non-programmers-tutorial-for-python.html#comments</comments> <pubDate>Sun, 12 Aug 2007 19:11:33 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/non-programmers-tutorial-for-python.html</guid> <description><![CDATA[Non-Programmer&#8217;s Tutorial for Python]]></description> <content:encoded><![CDATA[<p><a
href="http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python">Non-Programmer&#8217;s Tutorial for Python</a></p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fnon-programmers-tutorial-for-python.html&amp;linkname=Non-Programmer%E2%80%99s%20Tutorial%20for%20Python" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fnon-programmers-tutorial-for-python.html&amp;linkname=Non-Programmer%E2%80%99s%20Tutorial%20for%20Python" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fnon-programmers-tutorial-for-python.html&amp;linkname=Non-Programmer%E2%80%99s%20Tutorial%20for%20Python" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fnon-programmers-tutorial-for-python.html&amp;linkname=Non-Programmer%E2%80%99s%20Tutorial%20for%20Python" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fnon-programmers-tutorial-for-python.html&amp;linkname=Non-Programmer%E2%80%99s%20Tutorial%20for%20Python" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fnon-programmers-tutorial-for-python.html&#038;title=Non-Programmer%E2%80%99s%20Tutorial%20for%20Python" data-a2a-url="https://bogdan.org.ua/2007/08/12/non-programmers-tutorial-for-python.html" data-a2a-title="Non-Programmer’s Tutorial for Python"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/08/12/non-programmers-tutorial-for-python.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html</link> <comments>https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comments</comments> <pubDate>Sun, 12 Aug 2007 11:33:57 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[directory]]></category> <category><![CDATA[files]]></category> <category><![CDATA[folder]]></category> <category><![CDATA[glob]]></category> <category><![CDATA[iterate]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html</guid> <description><![CDATA[To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet: import os import glob &#160; path = 'sequences/' for infile in glob.glob&#40; os.path.join&#40;path, '*.fasta'&#41; &#41;: &#160; &#160; print &#34;current file is: &#34; + infile If you do not [&#8230;]]]></description> <content:encoded><![CDATA[<p>To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet:<br
/> <span
id="more-200"></span></p><div
id="ig-sh-12" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">glob</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">path <span
style="color: #66cc66;">=</span> <span
style="color: #483d8b;">'sequences/'</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> infile <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #dc143c;">glob</span>.<span
style="color: #dc143c;">glob</span><span
style="color: black;">&#40;</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>path<span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'*.fasta'</span><span
style="color: black;">&#41;</span> <span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;current file is: &quot;</span> + infile</div></li></ol></div></div><p>If you do not need wildcards, then there is a simpler way to list all items in a directory:</p><div
id="ig-sh-13" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">path <span
style="color: #66cc66;">=</span> <span
style="color: #483d8b;">'sequences/'</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">listing <span
style="color: #66cc66;">=</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">listdir</span><span
style="color: black;">&#40;</span>path<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> infile <span
style="color: #ff7700;font-weight:bold;">in</span> listing:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;current file is: &quot;</span> + infile</div></li></ol></div></div><p><strong>print</strong> was promoted from a statement to a <a
href="http://www.harshj.com/2008/12/09/the-new-print-function-in-python-3/">function</a> in Python 3 (use <strong>print(infile)</strong> instead of <strong>print infile</strong>).</p><p>One should use &#8216;os.path.join()&#8217; part to make the script cross-platform-portable (different OS use different path separators, and hard-coding path separator would stop the script from executing under a different OS).</p><p><a
href="http://docs.python.org/library/glob.html">Python docs</a> mention that there is also iglob(), which is an iterator and thus working on directories with way too many files it will save memory by returning only single result per iteration, and not the whole list of files &#8211; as glob() does.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&#038;title=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" data-a2a-url="https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html" data-a2a-title="Python: iterate (and read) all files in a directory (folder)"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html/feed</wfw:commentRss> <slash:comments>39</slash:comments> </item> <item><title>Why Zope 3 is just great</title><link>https://bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html</link> <comments>https://bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html#comments</comments> <pubDate>Thu, 15 Mar 2007 17:30:58 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[CMS]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[zope]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html</guid> <description><![CDATA[Recently I learned about Zope, which is an &#8220;open source web application server&#8221;, primarily written in Python. Then Django and Turbogears were seen as web-development frameworks akin to Zope. Search revealed an interesting anti-Zope rant at Zope vs Django. Reading until the end, and then following the comments, I came across the comment by Holger [&#8230;]]]></description> <content:encoded><![CDATA[<p>Recently I learned about <a
href="http://bluebream.zope.org/about/index.html" class="broken_link" rel="nofollow">Zope</a>, which is an &#8220;open source web application server&#8221;, primarily written in Python.</p><p>Then Django and Turbogears were seen as web-development frameworks akin to Zope. Search revealed an interesting anti-Zope rant at <a
href="http://www.jrandolph.com/blog/2006/02/02/zope-vs-django-heres-some-gasoline-to-put-out-the-fire/" class="broken_link" rel="nofollow">Zope vs Django</a>. Reading until the end, and then following the comments, I came across the comment by <a
href="http://www.blogger.com/profile/13470705" class="broken_link" rel="nofollow">Holger Froebe</a>, which is a huge one (probably the longest comment I had ever seen), and represents a detailed explanation with examples of the reasons to use Zope 3. I found that comment to be a really good-written one, so if you are deciding on whether you should use Zope 3 or not, then read the comment <a
href="http://www.jrandolph.com/blog/2006/02/02/zope-vs-django-heres-some-gasoline-to-put-out-the-fire/" class="broken_link" rel="nofollow">here</a> (scroll down or just search for &#8220;Holger Froebe&#8221;).</p><p>For convenience and in order to preserve this worhty piece of work from vanishing, below is the 99%-exact copy-paste of that comment.<br
/> <span
id="more-143"></span></p><div
class="commentname">Holger Froebe&nbsp;February 8, 2006 5:32 pm</div><div
class="commenttext"><p>Hello Jason,</p><p>when I read your post, I smiled a little.<br
/> Because I was in a similar situation in January/February<br
/> 2005. Let me introduce: My name is Holger<br
/> Froebe, I work for the IT of a university hospital<br
/> here in Germany (does this count for enterprise</p><p>requirements? I hope so.) &#8211; sorry for my crude<br
/> english.</p><p>â€¦â€¦â€¦and now for something completely different â€¦â€¦â€¦.</p><p>We had some web-applications written in plone and<br
/> wanted to extend this stuff throughout the company<br
/> which failed via some of the reasons<br
/> (Integration of Oracle/User management) which you adressed<br
/> in your rant. The main goal of this extension project<br
/> was to ensure integrity of data stemming from</p><p>different sources (mainly Legacy systems, like SAP,<br
/> Oracle, stuff from File/FTP server, MySQL, SQLServer -<br
/> the whole spectrum <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>Why did we fail ?<br
/> First let me get one point clear: I think Plone has<br
/> its strenghts and merits and if you stay close to<br
/> the main street of its framework layout and its<br
/> original intention (see below), you can get very<br
/> satisfactory results &#8211; see Oxfam, Ebay-Developer Plattform,</p><p>Motorola and a lot of other impressive projects.</p><p>But if you look at its history, you see a pattern:<br
/> Plone started as a replacement for the User Interface of<br
/> the Content Management Framework (CMF) of Zope2. But<br
/> over the years more and more architectural<br
/> stuff slipped into the original Skin Package &#8211; which<br
/> suddenly became a framework of its own.<br
/> It started as one package &#8211; now<br
/> you have 13 (or so) Zope packages which constitutes Plone.</p><p>And this software stack got bigger with evâ€™ry release: There<br
/> is python, then comes zope, then plone, then put archetypes and<br
/> on top of it ArchetypesContentTypes. To add it, all this<br
/> stuff has strong inner dependencies.<br
/> So with all those dependencies<br
/> Plone slowly drifted away from its original goal (getting a more usable<br
/> + visually appealing UI for Zope-CMF) and now does a lot of stuff<br
/> which should be done better deep down<br
/> in the software stack &#8211; may it be a pure python library</p><p>or a standard Zope Component/Product.<br
/> And that &#8211; to my totally personal mind &#8211; was the reason we failed<br
/> with a complex enterprise scenario.<br
/> If youâ€™re interested in this point of view,<br
/> Iâ€™ll recommend you Chris Withers insightful talk<br
/> â€œPlone rocks my worldâ€ &#8211; <a
href="http://www.simplistix.co.uk/presentations" rel="nofollow" class="broken_link">http://www.simplistix.co.uk/presentations</a></p><p>â€¦â€¦â€¦and now for something completely different â€¦â€¦â€¦.</p><p>But &#8211; What was the solution ?</p><p>We tried different approaches, but stayed<br
/> closely in well-known Python territory and rounded up<br
/> the usual suspects: TurboGears, Django, Zope3, to name<br
/> the most prominent.</p><p>And the winner was â€¦ Zope3 (now its time<br
/> to put on my fireproof suit, right <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>We started to work with it in Spring 2005 -<br
/> and we never looked back.</p><p>Man, this is such an amazing piece of software !</p><p>For me itâ€™s like a piece of art <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /> <br
/> Everyday I work with it Iâ€™m getting more<br
/> enthusiastic about it.</p><p>Zope3 is build on some values which in my mind<br
/> really counts if you want to build an enterprise system:</p><ul><li>Quality</li><li>Dynamics + Extensibility</li><li>Flexibility</li><li>Reuse, reuse and reuse again <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></li><li>Clear and concise separation of concerns</li><li>Focus on core competencies<br
/> = Avoid the Not-invented-here-syndrom</p><ul><li>integrate proven solutions from outside your world</li></ul></li></ul><p>Zop3 is not the monolithic big framework like many<br
/> other appservers today, but a collection of loosely</p><p>coupled pieces where every piece has a clear and defined<br
/> responsibility and quality. There are a some<br
/> thousand tests to ensure the last one. You can use nearly every<br
/> of this little pieces (Zopies call them â€œcomponentsâ€)<br
/> outside Zope. This separation of concerns to the extreme<br
/> leads in a fast way to locate, isolate and remove errors -<br
/> that really saved some of my days in the last few months.</p><p>At the same time the Zope3-Guys build via ruthless refactoring a framework<br
/> which brings Zope-World closer to Python standard world</p><p>(stuff like WSGI, relational Database connectivity etc. &#8211; see<br
/> below).</p><p>Heck, you can even use Zope-Code for a client<br
/> app which doesnâ€™t know anything Persistency or ZODB.<br
/> Yes, itâ€™s true: You can write pure desktop applications<br
/> using Zope-Code without caring about ZODB &#8211; Example:<br
/> The CCPublisher2 rewrite of the CreativeCommons-Project,<br
/> see <a
href="http://svn.berlios.de/svnroot/repos/cctools/publisher/trunk/" rel="nofollow" class="broken_link">http://svn.berlios.de/svnroot/repos/cctools/publisher/trunk</a></p><p>or <a
href="http://wiki.python.org/moin/PyCon2006/Talks#48" rel="nofollow">http://wiki.python.org/moin/PyCon2006/Talks#48</a><br
/> Or you want to use a collaborative tool for groups with<br
/> instant + slick visual feeling &#8211; try Bebop which strongly<br
/> relies on Zope3-Technologies on server AND client side.</p><p>â€¦â€¦.and now for something completely different â€¦..</p><p>The Zope3-Team discussed very thoroughly: What are the<br
/> core competencies of Zope and what not ?<br
/> For example: Zope2 had its own webserver &#8211; ZServer.</p><p>But why write and maintain such a beast when there<br
/> is something better in Python-World ?<br
/> So the Zope3-Guys integrated Twisted and<br
/> got best of both worlds.</p><p>â€¦ and now for something completely different â€¦</p><p>Relational Databases and Zope3</p><p>You described very well in your rant the problems with Oracle.<br
/> This was nearly 100% identical to our experience,<br
/> so my smile from the beginning of this post</p><p>comes a little bit from the pain I left behind me.</p><p>With Zope3 there are two approaches:</p><p>a) Use cxOracleDA-adapter (http://svn.zope.org/cxoracleda/)<br
/> Looking at this source code I was amazed how easy and<br
/> straightforward it is to integrate a well-known and proven<br
/> python-Standard DB-Adapter into Zope3-world.<br
/> The same pattern again: Take a proven quality piece</p><p> of software from standard python world, put on a small wrapper<br
/> and &#8211; whoa &#8211; use it right away in Zope3.</p><p>Well, and then you could put sql-expressions into your templates<br
/> via sql-expressions (uhm, not really recommended <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /> or use<br
/> your zsql-scripts from Zope2.</p><p>But then we thought there should be a better solution,<br
/> which breathes the spirit of Zope3 â€¦ which led us to</p><p>b) an OR-Mapper-Solution</p><p>We tried SQLObject first, since it had a â€œnativeâ€ Zope3-Integration<br
/> via sqlos. But Oracle connectivity is not one of the main targets<br
/> of SQLObject, despite there is a Oracle-SQL-Object-0.6.1-branch<br
/> in the repository.</p><p>PyDo2 from the Skunkweb-Project looked really promising, but<br
/> we had to put the name of the table into the class-definition,</p><p> so this was not flexible enough. Despite Oracle-Connectivity<br
/> was OK.</p><p>So we ended up with the best (shameless marketing)<br
/> enterprise ORM in python-world: SQLAlchemy (http://www.sqlalchemy.org)<br
/> Yes, thereâ€™s no official download and the developers<br
/> say thereâ€™s only Version 0.9.1 &#8211; but itâ€™s pretty close to final 1.0</p><p> and it really suits our needs.</p><p>The great difference between SQLAlchemy and the rest of the bunch<br
/> is that other ORM-Mappers try to fit relational databases by all means<br
/> into Object-World. But a relational DB is no misled ObjectStore and<br
/> the whole analogy breaks more and more down with<br
/> &#8211; larger databases</p><p> &#8211; transactional aspects<br
/> &#8211; complex datasets/queries over many tables.</p><p>SQLAlchemy has the same philosophy as Z3 in<br
/> &#8211; extreme separation of concerns and<br
/> &#8211; integration of standard DB Adapters<br
/> &#8211; high quality of code + easy readability</p><p> (anybody said â€œpythonicâ€ <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /> <br
/> &#8211; good quality of documentation</p><p>SQLAlchemy treats a Class as a Class, a RDB as a RDB, and<br
/> (you guessed it, right?) a RDB Table as a<br
/> RDB Table and connects them via mappers, which can easily<br
/> enriched with some standard methods/queries provided by</p><p> the framework.<br
/> It makes heavy use of the new dynamics aspects enriching<br
/> Python with the last few releases in the 2.x-line.</p><p>With SQLAlchemy I could drop the lines of code of my sql-related<br
/> stuff somewhere around 20-30%. And I REALLY like the approach<br
/> of â€œWriting less codeâ€. Doing all my RDB-Stuff in python</p><p> is an extra-Bonus (OK, to be honest &#8211; some complex queries<br
/> survived, but I think this is only a matter of time, since<br
/> they vanish <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>So youâ€™re not alone in Zope3-Oracle-World. To<br
/> cite the glorious A. A. Milne â€œAnd then there were threeâ€<br
/> (and maybe even more, if someone answers this post <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>â€¦ and now for something completely different â€¦</p><p>LDAP-Integration</p><p>Zope3 is very concise about Authentication via<br
/> so-called PAUâ€™s &#8211; Pluggable Authentication Utilities.<br
/> You could easily plug together Authentication sources<br
/> with different Authentication methods &#8211; its as easy<br
/> as plug your lego-stones together.</p><p>Like in RDB-World: First you define an Adapter to your</p><p> Datasource &#8211; lets call it LDAP-Adapter, right -<br
/> which defines and holds the connection to your<br
/> external LDAP-Source (http://svn.zope.org/ldapadapter/).<br
/> That way, you could even use more than one LDAP-Source<br
/> in your Application.</p><p>Then you have another clearly defined LDAP-PAS (http://svn.zope.org/ldappas/).</p><p> which does the authentication against this Adapter.</p><p>And the whole beast (you guessed it again, right?) is a small,<br
/> well-defined wrapper around python-ldap. Plus<br
/> itâ€™s easy to read and fast to understand (my 2 cents).<br
/> Itâ€™s like dejavu all over again <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>Hint: I just played around with this LDAP-stuff and never<br
/> tested that in our production environment,</p><p> but I have great confidence from my previous experiences<br
/> with Zope3 that it should be working relatively seamless.</p><p>â€¦ and now for something completely different â€¦</p><p>ZCML</p><p>Well, everybody beats on ZCML, since its such an easy<br
/> target &#8211; â€œHey, itâ€™s XML &#8211; thatâ€™s bad. We donâ€™t want<br
/> to use XML (for whatever ideological reason), so</p><p> Zope3 must be something ill-constructedâ€</p><p>If you ask me about my feelings about ZCML, I would<br
/> not try to convince you it was made in heaven and tell you<br
/> that you are too blind to see the light <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>But &#8211; as often in life: truth lies somewhere in<br
/> between the extremes. My 2 cents:</p><ol><li><p>I share your feelings about not direct<br
/> debugging ZCML, despite the fact that Zope3.2 brings<br
/> very concise error-tracebacks.</p></li><li><p>The Zope3-Guys are aware of the problems users have with<br
/> ZCML. They try REALLY hard to bring as much ZCML back<br
/> to python as possible &#8211; see</p><p> <a
href="http://www.z3lab.org/sections/blogs/philipp-weitershausen/2005" rel="nofollow">http://www.z3lab.org/sections/blogs/philipp-weitershausen/2005</a><em>12</em>14_zcml-needs-to-do-less<br
/> for a thorough discussion from one of the core developers<br
/> of Zope3. Looking from Zope3.0 to Zope3.2 (the current release) some<br
/> stuff vanished from ZCML, so those guys do their homework<br
/> and will do it even more on the upcoming Zope3.3 release.</p></li><li><p>The best thing at the end (now Z3-Team will really beat me ;-):<br
/> You can write up and use<br
/> ALL (right, ALL) ZCML-directives right away as python-code, if you donâ€™t<br
/> XML.<br
/> And yes, this is even documented (call me old-fashioned,<br
/> but I read docs first). You may say, that there are such</p><p> a huge amount of READMEâ€™s and other .txt-stuff spread<br
/> over the whole Zope-Project, that its not easy to get into it.<br
/> But Zope3 provides you with a toolscript<br
/> called â€œstatic-apidocâ€ which gives you a clear, concise<br
/> overview of the whole documentation as a static website.</p><p>Now if you look at the README under zope/component or</p><p> at the static-apidoc unter â€œComponent Architectureâ€<br
/> you find methods like provideAdapter, provideUtility,<br
/> which do &#8211; surprise, surprise &#8211; the equivalent of<br
/> ZCML-alternatives. Or look at zope/component/site.py<br
/> or zope/configuration</p><p>You want to see this in action ? Look at this</p><p> 2-part-example of a simple Z3-object publishing system<br
/> without any piece of ZCML:</p><p>Part I: The Zope Component Architecture -<br
/> Interfaces, Adaptation, and Duck Typing<br
/> <a
href="http://griddlenoise.blogspot.com/2005/12/zope-component-architecture-interfaces.html" rel="nofollow">http://griddlenoise.blogspot.com/2005/12/zope-component-architecture-interfaces.html</a></p><p>Part II: The Zope Component Architecture -<br
/> One Way To Do It All</p><p> <a
href="http://griddlenoise.blogspot.com/2005/12/zope-component-architecture-one-way-to.html" rel="nofollow">http://griddlenoise.blogspot.com/2005/12/zope-component-architecture-one-way-to.html</a></p><p>Browsing through the docs, you can find the other<br
/> replacements in a straightforward way (or you debug<br
/> the xmlconfig-stuff from zope/configuration, which<br
/> gives you the corresponding callables for ZCML)</p><p>If thatâ€™s too tough and time-consuming &#8211; no problem,<br
/> ask on the Zope3-Users mailinglist. Those guys are</p><p> REALLY helpful to get you into Z3-world.</p></li></ol><p>â€¦ and now for something completely different ..</p><p>Kool-Aid and the magic world of interfaces and adapters</p><p>Speaking with developers about Zope3 you often hear<br
/> that interfaces and adapters are too much magic and<br
/> they have to drink so much Kool-Aid to understand them.<br
/> I wonâ€™t put this here into a lengthy pro-con-discussion</p><p>of these concepts, since Iâ€™m not really a core developer,<br
/> but more an application developer/maintainer.</p><p>But to tell you my story:</p><p>It took me a while to GET the main ideas/principles<br
/> behind this stuff &#8211; to be honest,<br
/> 2 days of intensive, dedicated work and 5 litres of H2O.<br
/> After one week of working with Zope3 I was more productive than<br
/> before. Plus I had learned a lot of new stuff about programming<br
/> in a quality way. Yes, dealing with Zope3 has made</p><p>me a better programmer &#8211; even if I never should do anymore project<br
/> with it ;-(</p><p>Plus it helped me to get my things done better + faster.</p><p>Well, thatâ€™s Kool-Aid I really like !</p><p>I wontâ€™t say this world of interfaces and adapters<br
/> is the easiest to understand. But again:<br
/> There are a good amount of play-around-with-it-tutorials/docs/books<br
/> around which take you into Zope3-World. Just give it a try !</p><p>Want some examples? Want some simple apps to play around with?</p><p>Well, it was never easier than with Zope3 &#8211; see yourself</p><p>Hereâ€™s some easy stuff which you can work through in less than 1 hour<br
/> (well, the last example takes you longer <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>a) â€œZope3 in 30 minutesâ€ &#8211; at<br
/> <a
href="http://zissue.berlios.de/z3/Zope3In30Minutes.html" rel="nofollow">http://zissue.berlios.de/z3/Zope3In30Minutes.html</a><br
/> showing you step by step how to build your first<br
/> simple Z3-application to collect your bookmarks on a server</p><p>b) Did I mention the magnificent Jeff Shell? His blog griddlenoise.blogspot.com is a rich and really insightful source for getting into Zope3 &#8211; plus itâ€™s really fun to read. In his archives you find this funky little thing <a
href="http://worldcookery.com/files/jeffshell-todo/" rel="nofollow" class="broken_link">http://worldcookery.com/files/jeffshell-todo/</a> where Jeff tells you how to build a Rails-like ToDo-Application in some simple steps. In every step he shows you what to do and why he thinks this implementation is carefully thought out in Zope3 and what is the reason they did it this and no other way.</p><p>c) Want a fresh new zope3-site without understandig â€œall the magicâ€<br
/> inside? Choose life &#8211; choose the z3 project starter</p><p> <a
href="http://old.zope.org/Members/adytumsolutions/" rel="nofollow">http://www.zope.org/Members/adytumsolutions/z3project</a><em>starter/z3project</em>starter_released<br
/> Answer a few simple questions and you have a project skeleton<br
/> to play around with without deeply understanding all this â€œkool-aidâ€ upfront.</p><p>d) Philipp von Weitershausens Website/Book about Zope3 -<br
/> <a
href="http://worldcookery.com" rel="nofollow">http://worldcookery.com</a></p><p>You will find more of this tutorials on Phillips website under<br
/> <a
href="http://worldcookery.com/Appetizers.html" rel="nofollow" class="broken_link">http://worldcookery.com/appetizers.</a></p><p>And this is not the end. New stuff is landing every day<br
/> in Zope3-space &#8211; like this little gem about events/notifications<br
/> and how they help you handle complex application<br
/> architectures:</p><p><a
href="http://remarkablysimple.blogspot.com/" rel="nofollow" class="broken_link">http://remarkablysimple.blogspot.com/</a></p><p>Or you look at <a
href="http://www.z3lab.org" rel="nofollow">www.z3lab.org</a> where you can get a peek</p><p>of the Zope3-ECM-Initiative and so on so on â€¦</p><p>So, my advice to you: Fire on feedster.com, type in Zope3 â€¦<br
/> and youâ€™ll find a lot more of this diamonds.</p><p>I made the experience that most of the complaints about<br
/> Kool-Aid come from developers who specialized in certain<br
/> frameworks/habits and now had difficulties to extend<br
/> their mindset since they had to leave known territory.<br
/> They struggled with Zope3, found some hurdles and then gave up,<br
/> since â€œthereâ€™s so much kool-aidâ€.</p><p>I was surprised to find out that absolute newbies to programming<br
/> get productive with Zope3 very fast and easily -<br
/> maybe since they are not fixed on certain stuff.</p><p>I mean, sometimes I donâ€™t get it: People want to write programs<br
/> for real complex enterprise scenarios, but at the same<br
/> time tell me itâ€™s too hard to spend a few hours to play with some toy examples<br
/> and read some docs and play with the marvellous python command prompt<br
/> trying to push their brain into a new direction.</p><p>Believe me: This whole interface-adapter-pattern definitely helps</p><p>you in bigger/complex projects evolving over time.<br
/> Remember Fred Brooks Mythical Man-Month, which made so many of us aware<br
/> that change of requirements is inherent in any software project -<br
/> even if the whole system is in production use.</p><p>Zope3 has not ALL, but a lot of REALLY GOOD answers<br
/> to this situation every developer faces from time to time <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>â€¦ and now for something completely different ..</p><p>Querying the ZODB from outside applications</p><p>Iâ€™m convinced that it should be easy and I know</p><p>that the guys in the Bebop-Project did some stuff<br
/> in this direction, But Iâ€™m no expert in that,<br
/> since relational databases<br
/> constitutes more of my work. If you want to<br
/> have a profound answer on this &#8211; push it<br
/> on the Zope3-Users-Mailinglist (sorry, that<br
/> wasnâ€™t a sufficient answer, right?). Those<br
/> helpful souls there will really show you<br
/> the best and easiest way to do it.</p><p>â€¦.. and now for something completely different â€¦.</p><p>Coming back to your Plone-dissatisfaction</p><p>I donâ€™t want to tell you that Plone is bad. Or Plone<br
/> sucks. Or stuff like that. For me itâ€™s the right tool<br
/> for the projects it was made of &#8211; usable portal solutions<br
/> for medium size. The same holds for Django which<br
/> is also OK, if I want to make a fast RDBMS-UI-App.</p><p>The good thing is: For different work tasks there<br
/> are different tools in my toolchest. Itâ€™s my</p><p>responsibility to choose the right one for<br
/> every new project, but it leaves me with a<br
/> warm safe feeling that the toolbelt is filled<br
/> with such good quality stuff.</p><p>And the good news is just around the corner:<br
/> Plone and Zope3-World are converging &#8211; approaching<br
/> each other with every day. Now what does that mean?</p><p>Since I worked with plone it was easy to find my<br
/> way in Zope3-World. Zope3 tried to learn from</p><p>the Zope2 AND the plone lessons and put a lot of<br
/> the best breed of Plone (which constituted at the<br
/> same time those hard-to-manage architectural overhead)<br
/> back to the core of the framework. Well, this<br
/> is not totally right, since there is no such thing<br
/> as a monolithic core of Zope3-framework &#8211; the greatest lesson<br
/> learned from the problems with complex Zope2-projects.<br
/> Which is the best news of all <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>Know Archetypes ? There is schema-driven content-types</p><p>with form generation (zope.formlib)</p><p>Know Skins and Layers ? Use them right away in Zope3</p><p>Know Portlets ? Generalized to Viewlets and managed via ViewletManagers.</p><p>RessourceRegistries? Now known as RessoureLibrary.</p><p>â€¦ and so on â€¦ and so on â€¦</p><p>But at the same time the Plone guys push their stuff<br
/> more and more towards the proven Z3-technologies -<br
/> and by handing over Z3 the framework responsibilities<br
/> the Plone community again can concentrate on being</p><p>the big shot at their homeground -<br
/> to provide you with the â€œMacOS of CMSesâ€<br
/> (well at least thatâ€™s what Limi told me <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p><p>I also appreciate the other web frameworks<br
/> in Python world &#8211; and Iâ€™m happy to see that<br
/> there will be a â€œWebFrameworkâ€-Track during<br
/> Europython this year where zopies, djangistas<br
/> and turbogearianos and all those funky-stuffistas<br
/> will get into fruitful</p><p>discussions about solutions. Iâ€™m really<br
/> looking forward to this meeting since<br
/> we can learn a lot from each other<br
/> if we leave our minds open for the NEW.</p><p>â€¦. and now for something completely different â€¦.</p><p>There is so much more to say about this marvellous<br
/> piece of software (like the integration<br
/> of other templating languages like meld or clarity<br
/> or the integration of standards like Java-like Portlet-Stuff or</p><p>WFMC &#8211; the Workflow-Coalition &#8211; and and and)<br
/> but let me come to an end, since itâ€™s really late<br
/> and I need some sleep:</p><p>I work for 20 years with software and applications.<br
/> Zope3 is one of the most professionall, mature<br
/> and qualitative outstanding frameworks I saw.</p><p>Itâ€™s really fun to work with, if you have a sense for<br
/> lasting quality solutions, if you want to be<br
/> proud of the stuff you created.</p><p>Thanx for your patience + Good night,</p><p>Holger @ Germany</p><p>PS: If youâ€™ve got any specific question about Zope3,<br
/> drop me a not at <a
href="mailto:booradley at web.de.">booradley at web.de.</a> Or visit some<br
/> of the links in my rant. Or subscribe<br
/> to the Zope3-User-Newsgroups and ask your questions.<br
/> This world is really full of possibilities <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p></div><div
class="commentname"> Holger Froebe&nbsp;February 8, 2006<br
/> 6:13 pm</p></div><div
class="commenttext"><p>Ah, and I forgot &#8211; the collaborative development<br
/> of my favourite Linux distro (Ubuntu)<br
/> is managed by a Zope3-Application -</p><p>see <a
href="https://launchpad.net/" rel="nofollow">http://launchpad.net</a> â€¦</p><p>or the shiny Z3-based Schooltool if you want<br
/> to manage ressources and calendars â€¦<br
/> see <a
href="http://www.schooltool.org" rel="nofollow" class="broken_link">http://www.schooltool.org</a></p><p>Zope3 is really smoking <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F03%2F15%2Fwhy-zope-3-is-just-great.html&amp;linkname=Why%20Zope%203%20is%20just%20great" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F03%2F15%2Fwhy-zope-3-is-just-great.html&amp;linkname=Why%20Zope%203%20is%20just%20great" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F03%2F15%2Fwhy-zope-3-is-just-great.html&amp;linkname=Why%20Zope%203%20is%20just%20great" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F03%2F15%2Fwhy-zope-3-is-just-great.html&amp;linkname=Why%20Zope%203%20is%20just%20great" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F03%2F15%2Fwhy-zope-3-is-just-great.html&amp;linkname=Why%20Zope%203%20is%20just%20great" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F03%2F15%2Fwhy-zope-3-is-just-great.html&#038;title=Why%20Zope%203%20is%20just%20great" data-a2a-url="https://bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html" data-a2a-title="Why Zope 3 is just great"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Simple substring counting script in Python</title><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html</link> <comments>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html#comments</comments> <pubDate>Wed, 21 Jun 2006 17:41:27 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[perl]]></category> <category><![CDATA[PHP]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/blog/2006/06/21/simple-substring-counting-script-in-python.html</guid> <description><![CDATA[Approximately a month ago I endeavoured to use Python as my main shell-scripting language. At that moment, I was already aware of multiple benefits you get when you use Python for scripting: source-level cross-platform scripting: your script will run anywhere, where Python compiles; expanding this statement &#8211; your script will run anywhere, where there is [&#8230;]]]></description> <content:encoded><![CDATA[<p>Approximately a month ago I endeavoured to use Python as my main shell-scripting language. At that moment, I was already aware of multiple benefits you get when you use Python for scripting:</p><ul><li>source-level cross-platform scripting: your script will run anywhere, where Python compiles; expanding this statement &#8211; your script will run anywhere, where there is a C compiler (needed to build Python itself)</li><li>high-level language: you can iterate all the lines in a text file with as little as one &#8216;for&#8217;-statement, for example (see the actual example below)</li><li>simple/minimalist syntax: no curly braces around blocks of statements, no semicolons after each and every line of code, etc. Python at a glance looks much more understandable, than, for example, Perl.</li><li>the power of C in a language-interpreting system</li><li>it is interpreted! This gives easyness of debugging: modify, execute, see the trouble &#8211; with no compile/link stages</li><li>and, despite being interpreted, it is fast!</li></ul><p>For the comparison (in speed, memory use, program size) with other computer programming languages, please see the &#8220;Computer Language Shootout Benchmarks&#8221;. Here I provide the link only to the <a
href="http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&amp;lang=python3&amp;lang2=perl&amp;data=u64" target="_blank" title="Python vs Perl comparison" class="broken_link" rel="nofollow">comparison of Python with Perl</a> and <a
href="http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&amp;lang=python3&amp;lang2=php&amp;data=u32" title="Python vs PHP" target="_blank" class="broken_link" rel="nofollow">comparison of Python with PHP</a> (which can also be used as shell-scripting language, albeit after some tinkering with settings and stuff)</p><p>Below is an example of the 2-minute script in Python, which counts the number of occurrences of some string in a file.</p><div
id="ig-sh-14" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #483d8b;">&quot;&quot;&quot;Read FILE and count number of occurences of SUBSTR.&quot;&quot;&quot;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">version <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0.01</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">sys</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">def</span> main<span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">from</span> <span
style="color: #dc143c;">optparse</span> <span
style="color: #ff7700;font-weight:bold;">import</span> OptionParser</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts <span
style="color: #66cc66;">=</span> OptionParser<span
style="color: black;">&#40;</span>usage<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;%prog [options] FILE SUBSTR&quot;</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; version<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;%prog &quot;</span> + <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>version<span
style="color: black;">&#41;</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; description<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;Read FILE and count number of occurences of SUBSTR.&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts.<span
style="color: black;">set_defaults</span><span
style="color: black;">&#40;</span>verbose<span
style="color: #66cc66;">=</span><span
style="color: #008000;">False</span><span
style="color: #66cc66;">,</span>flush<span
style="color: #66cc66;">=</span><span
style="color: #008000;">False</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts.<span
style="color: black;">add_option</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;-v&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">&quot;--verbose&quot;</span><span
style="color: #66cc66;">,</span> action<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;store_true&quot;</span><span
style="color: #66cc66;">,</span> dest<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;verbose&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">help</span><span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;Print every line containing substr [default: %default]&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts.<span
style="color: black;">add_option</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;-f&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">&quot;--flush&quot;</span><span
style="color: #66cc66;">,</span> action<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;store_true&quot;</span><span
style="color: #66cc66;">,</span> dest<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;flush&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">help</span><span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;When verbose, flush every line [default: %default]&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: black;">&#40;</span>options<span
style="color: #66cc66;">,</span> args<span
style="color: black;">&#41;</span> <span
style="color: #66cc66;">=</span> opts.<span
style="color: black;">parse_args</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span>args<span
style="color: black;">&#41;</span> <span
style="color: #66cc66;">!=</span> <span
style="color: #ff4500;">2</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Two arguments required for correct processing&quot;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; opts.<span
style="color: black;">print_help</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #dc143c;">sys</span>.<span
style="color: black;">exit</span><span
style="color: black;">&#40;</span><span
style="color: #ff4500;">2</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; infile <span
style="color: #66cc66;">=</span> args<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">0</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; substr <span
style="color: #66cc66;">=</span> args<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">1</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; lines_count <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; substr_count <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; lines_substr_count <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; msg <span
style="color: #66cc66;">=</span> <span
style="color: #483d8b;">&quot;&quot;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; f <span
style="color: #66cc66;">=</span> <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span>infile<span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'r'</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">for</span> line <span
style="color: #ff7700;font-weight:bold;">in</span> f:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; lines_count +<span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">1</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; found <span
style="color: #66cc66;">=</span> line.<span
style="color: black;">count</span><span
style="color: black;">&#40;</span>substr<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; substr_count +<span
style="color: #66cc66;">=</span> found</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> found <span
style="color: #66cc66;">&gt;</span> <span
style="color: #ff4500;">0</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; lines_substr_count +<span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">1</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; msg +<span
style="color: #66cc66;">=</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>found<span
style="color: black;">&#41;</span> + <span
style="color: #483d8b;">&quot;: &quot;</span> + line</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">elif</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: black;">&#40;</span><span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>found<span
style="color: black;">&#41;</span> + <span
style="color: #483d8b;">&quot;: &quot;</span> + line<span
style="color: black;">&#41;</span>.<span
style="color: black;">replace</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;n&quot;</span><span
style="color: #66cc66;">,</span><span
style="color: #483d8b;">&quot;&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> msg</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Lines read from file: &quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>lines_count<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Lines with substring found: &quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>lines_substr_count<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Total substrings detected: &quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>substr_count<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">if</span> __name__ <span
style="color: #66cc66;">==</span> <span
style="color: #483d8b;">&quot;__main__&quot;</span>: &nbsp;main<span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li></ol></div></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&#038;title=Simple%20substring%20counting%20script%20in%20Python" data-a2a-url="https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html" data-a2a-title="Simple substring counting script in Python"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> </channel> </rss>