<?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 CavePython &#187;</title> <atom:link href="http://bogdan.org.ua/tags/python/feed" rel="self" type="application/rss+xml" /><link>http://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Fri, 03 Feb 2012 22:51:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Python performance: set vs list</title><link>http://bogdan.org.ua/2011/08/15/python-performance-set-vs-list.html</link> <comments>http://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>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 - 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...except block, and process duplicate primary ID exceptions. Another, sometimes more desired solution [...]]]></description> <content:encoded><![CDATA[<p>Sometimes there is a need to be sure that no identifier is processed twice - 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...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 "python set vs list", 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'm mimicking the real-life application, thus the test case has an optional loop):</p><div
class="igBar"><span
id="lcode-3"><a
href="#" onclick="javascript:showPlainTxt('code-3'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-3"><div
class="code"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$python -mtimeit -s <span
style="color:#CC0000;">'myset = set()'</span> <span
style="color:#CC0000;">'for x in xrange(1000): myset.add(x)'</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">10000</span> loops, best of <span
style="color:#800000;color:#800000;">3</span>: <span
style="color:#800000;color:#800000;">133</span> usec per loop</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$python -mtimeit -s <span
style="color:#CC0000;">'tmp = list()'</span> <span
style="color:#CC0000;">'for x in xrange(1000): tmp.append(x)'</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">10000</span> loops, best of <span
style="color:#800000;color:#800000;">3</span>: <span
style="color:#800000;color:#800000;">116</span> usec per loop</div></li></ol></div></div></div><p></p><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: 'x in tmp'. For this test, I've deliberately chosen an imbalance of True (1%) and False (99%) results for the test - again, mimicking the real problem I have at hand:</p><div
class="igBar"><span
id="lcode-4"><a
href="#" onclick="javascript:showPlainTxt('code-4'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-4"><div
class="code"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$python -mtimeit -s <span
style="color:#CC0000;">'tmp = set()'</span> -s <span
style="color:#CC0000;">'for x in xrange(1000): tmp.add(x)'</span> <span
style="color:#CC0000;">'for x in xrange(100000): x in tmp'</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">100</span> loops, best of <span
style="color:#800000;color:#800000;">3</span>: <span
style="color:#800000;color:#800000;">7</span>.<span
style="color:#800000;color:#800000;">27</span> msec per loop</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$python -mtimeit -s <span
style="color:#CC0000;">'tmp = list()'</span> -s <span
style="color:#CC0000;">'for x in xrange(1000): tmp.append(x)'</span> <span
style="color:#CC0000;">'for x in xrange(100000): x in tmp'</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#800000;color:#800000;">10</span> loops, best of <span
style="color:#800000;color:#800000;">3</span>: <span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">12</span> sec per loop</div></li></ol></div></div></div><p></p><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> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2011/08/15/python-performance-set-vs-list.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Using Python in a Java project</title><link>http://bogdan.org.ua/2011/05/17/using-python-in-a-java-project.html</link> <comments>http://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>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't feel right, but the project you are working on is a Java project - try Jython - Python for the Java platform, compile your python scripts into Java bytecode Groovy - 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't feel right</em>, but the project you are working on is a Java project - try</p><ul><li><a
href="http://www.jython.org/">Jython</a> - Python for the Java platform, compile your python scripts into Java bytecode</li><li><a
href="http://groovy.codehaus.org/">Groovy</a> - not Python, but still a scripting language which compiles to jars</li></ul> ]]></content:encoded> <wfw:commentRss>http://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>http://bogdan.org.ua/2011/02/25/introduction-to-python-for-bioinformatics.html</link> <comments>http://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>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'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'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> ]]></content:encoded> <wfw:commentRss>http://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>http://bogdan.org.ua/2011/02/16/how-to-easily-install-any-pypi-easy_install-python-module-on-debian.html</link> <comments>http://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>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't have the python-stdeb package: sudo aptitude install python-stdeb pypi-install pycassa That's it. Refer to stdeb readme for more information. You will need that if [...]]]></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't have the python-stdeb package: <strong>sudo aptitude install python-stdeb</strong></li><li><strong>pypi-install pycassa</strong></li></ul><p>That'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 - which might not be resolved automatically by stdeb.</p><p>Before stdeb, it wasn't exactly trivial to <a
href="http://showmedo.com/videotutorials/video?name=linuxJensMakingDeb">make a .deb from python module</a>.</p> ]]></content:encoded> <wfw:commentRss>http://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>Good online Python book with code examples</title><link>http://bogdan.org.ua/2010/03/01/good-online-python-book-with-code-examples.html</link> <comments>http://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>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'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's Introduction to Python</a> by Steven F. Lott (© 2002, 2005, 2007, 2008 Steven F. Lott).</p> ]]></content:encoded> <wfw:commentRss>http://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>http://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html</link> <comments>http://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>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 [...]]]></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">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... what'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 - such as integers [<a
href="http://bytes.com/topic/python/answers/37219-value-reference">strings, numerics and tuples are immutables</a>] - are passed by value. That is, if your function accepts some integer argument, you are safe assuming that your function won't be able to modify your integer. Mutable variables - such as dictionaries and lists - 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 = "Hello "<br
/> s += "World"<br
/> ... 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's methods within a called function, variable is considered "passed by reference" - it is modified out of the function's scope. If using assignment on a mutable object, it is created a-new within the function, and global value isn't modified.</p><p>When you call a function with an arg, a "local variable" is created, which references the object passed as the argument. (well... an entry with the formal parameter name as key and a reference to the object passed in is created in the 'local' dict).</p><p>So, rebinding this local symbol does not impact the binding in the caller's namespace - because the symbol lives in another namespace.</p><p>*But* - and if the object referenced is mutable of course - modifying the object in the function... well, just modifies the object, because it's the *same* object that is bound to ('referenced by', if you prefer) both symbols (the one in the caller's namespace and the one in the function's namespace). So yes, the object *is* modified when the function returns.</p> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Useful Python looping techniques</title><link>http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</link> <comments>http://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>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: PLAIN TEXT PYTHON: 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 [...]]]></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
class="igBar"><span
id="lpython-9"><a
href="#" onclick="javascript:showPlainTxt('python-9'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-9"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">questions = <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'name'</span>, <span
style="color: #483d8b;">'quest'</span>, <span
style="color: #483d8b;">'favourite colour'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">answers = <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'Lancelot'</span>, <span
style="color: #483d8b;">'the holy grail'</span>, <span
style="color: #483d8b;">'blue'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> q, a <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">zip</span><span
style="color: black;">&#40;</span>questions, answers<span
style="color: black;">&#41;</span>:</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&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, a<span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><p>To loop over a sequence with both key and value:<br
/> <span
id="more-229"></span></p><div
class="igBar"><span
id="lpython-10"><a
href="#" onclick="javascript:showPlainTxt('python-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-10"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> i, 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: #483d8b;">'tac'</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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> i, v</div></li></ol></div></div></div><p></p><p>To loop over dictionary, again with key and value:</p><div
class="igBar"><span
id="lpython-11"><a
href="#" onclick="javascript:showPlainTxt('python-11'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-11"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">knights = <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'gallahad'</span>: <span
style="color: #483d8b;">'the pure'</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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">for</span> k, 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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k, v</div></li></ol></div></div></div><p></p><p>To loop over some sorted sequence, but without modifying the original sequence (beware the use of set() in this example):</p><div
class="igBar"><span
id="lpython-12"><a
href="#" onclick="javascript:showPlainTxt('python-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-12"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">basket = <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'apple'</span>, <span
style="color: #483d8b;">'orange'</span>, <span
style="color: #483d8b;">'apple'</span>, <span
style="color: #483d8b;">'pear'</span>, <span
style="color: #483d8b;">'orange'</span>, <span
style="color: #483d8b;">'banana'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> f</div></li></ol></div></div></div><p></p><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> ]]></content:encoded> <wfw:commentRss>http://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>http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html</link> <comments>http://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>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 PLAIN TEXT PYTHON: #!/usr/local/bin/python &#160; author = &#123;"php":"Rasmus Lerdorf",\ &#160; &#160; "perl":"Larry Wall",\ &#160; &#160; "tcl":"John Ousterhout",\ &#160; &#160; "awk":"Brian Kernighan",\ &#160; &#160; "java":"James Gosling",\ &#160; &#160; "parrot":"Simon Cozens",\ &#160; &#160; "python":"Guido van Rossum"&#125; &#160; langs = author.keys&#40;&#41; [...]]]></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
class="igBar"><span
id="lpython-15"><a
href="#" onclick="javascript:showPlainTxt('python-15'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-15"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;">#!/usr/local/bin/python</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">author = <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">"php"</span>:<span
style="color: #483d8b;">"Rasmus Lerdorf"</span>,\</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"perl"</span>:<span
style="color: #483d8b;">"Larry Wall"</span>,\</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"tcl"</span>:<span
style="color: #483d8b;">"John Ousterhout"</span>,\</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"awk"</span>:<span
style="color: #483d8b;">"Brian Kernighan"</span>,\</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"java"</span>:<span
style="color: #483d8b;">"James Gosling"</span>,\</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"parrot"</span>:<span
style="color: #483d8b;">"Simon Cozens"</span>,\</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #483d8b;">"python"</span>:<span
style="color: #483d8b;">"Guido van Rossum"</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">langs = 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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">langs.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> language,<span
style="color: #483d8b;">"is the child of"</span>,author<span
style="color: black;">&#91;</span>language<span
style="color: black;">&#93;</span></div></li></ol></div></div></div><p></p><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
class="igBar"><span
id="lpython-16"><a
href="#" onclick="javascript:showPlainTxt('python-16'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-16"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">def</span> ksort<span
style="color: black;">&#40;</span>d, func = <span
style="color: #008000;">None</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; keys = d.<span
style="color: black;">keys</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> keys</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #808080; font-style: italic;"># example use, assume 'd' is a dictionary</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k, d<span
style="color: black;">&#91;</span>k<span
style="color: black;">&#93;</span></div></li></ol></div></div></div><p> (example taken from <a
href="http://bytes.com/topic/python/answers/21704-sort-dictionary">TheScript forum</a>)</p> ]]></content:encoded> <wfw:commentRss>http://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>http://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html</link> <comments>http://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>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 [...]]]></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> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2007/09/07/mysql-python-good-mysqldb-tutorial-examples.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html</link> <comments>http://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>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: PLAIN TEXT PYTHON: import os import glob &#160; path = 'sequences/' for infile in glob.glob&#40; os.path.join&#40;path, '*.fasta'&#41; &#41;: &#160; &#160; print "current file is: " + infile If [...]]]></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
class="igBar"><span
id="lpython-19"><a
href="#" onclick="javascript:showPlainTxt('python-19'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-19"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">glob</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">path = <span
style="color: #483d8b;">'sequences/'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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: #483d8b;">'*.fasta'</span><span
style="color: black;">&#41;</span> <span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">"current file is: "</span> + infile</div></li></ol></div></div></div><p></p><p>If you do not need wildcards, then there is a simpler way to list all items in a directory:</p><div
class="igBar"><span
id="lpython-20"><a
href="#" onclick="javascript:showPlainTxt('python-20'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-20"><div
class="python"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">path = <span
style="color: #483d8b;">'sequences/'</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">listing = <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-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">"current file is: "</span> + infile</div></li></ol></div></div></div><p></p><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 'os.path.join()' 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 - as glob() does.</p> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html/feed</wfw:commentRss> <slash:comments>18</slash:comments> </item> <item><title>Why Zope 3 is just great</title><link>http://bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html</link> <comments>http://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>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 "open source web application server", 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 [...]]]></description> <content:encoded><![CDATA[<p>Recently I learned about <a
href="http://bluebream.zope.org/about/index.html">Zope</a>, which is an "open source web application server", 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/">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">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/">here</a> (scroll down or just search for "Holger Froebe").</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.) - 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='http://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 - 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 - which<br
/> suddenly became a framework of its own.<br
/> It started as one package - 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 - may it be a pure python library</p><p>or a standard Zope Component/Product.<br
/> And that - to my totally personal mind - 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” - <a
href="http://www.simplistix.co.uk/presentations" rel="nofollow">http://www.simplistix.co.uk/presentations</a></p><p>………and now for something completely different ……….</p><p>But - 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='http://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='http://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='http://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. - 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 - Example:<br
/> The CCPublisher2 rewrite of the CreativeCommons-Project,<br
/> see <a
href="http://svn.berlios.de/svnroot/repos/cctools/publisher/trunk/" rel="nofollow">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 - 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 - 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 - whoa - 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='http://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 - 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
/> - larger databases</p><p> - transactional aspects<br
/> - complex datasets/queries over many tables.</p><p>SQLAlchemy has the same philosophy as Z3 in<br
/> - extreme separation of concerns and<br
/> - integration of standard DB Adapters<br
/> - high quality of code + easy readability</p><p> (anybody said “pythonic” <img
src='http://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> <br
/> - 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 - some complex queries<br
/> survived, but I think this is only a matter of time, since<br
/> they vanish <img
src='http://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='http://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 - Pluggable Authentication Utilities.<br
/> You could easily plug together Authentication sources<br
/> with different Authentication methods - 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 - 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='http://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 - “Hey, it’s XML - 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='http://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p><p>But - 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 - 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 <img
src='http://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> :<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 - surprise, surprise - 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 - 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 - 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 - 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 - 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='http://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p><p>a) “Zope3 in 30 minutes” - 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 - 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">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 - 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">http://worldcookery.com/appetizers.</a></p><p>And this is not the end. New stuff is landing every day<br
/> in Zope3-space - 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">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='http://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 - 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 - 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 - 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 - the greatest lesson<br
/> learned from the problems with complex Zope2-projects.<br
/> Which is the best news of all <img
src='http://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='http://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 - 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 - the Workflow-Coalition - 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='http://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 - 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">http://www.schooltool.org</a></p><p>Zope3 is really smoking <img
src='http://bogdan.org.ua/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p></div> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2007/03/15/why-zope-3-is-just-great.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
