<?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>simon.net.nz &#187; code</title> <atom:link href="http://simon.net.nz/articles/category/code/feed/" rel="self" type="application/rss+xml" /><link>http://simon.net.nz</link> <description>Dr. Simon J. Greenhill&#039;s website</description> <lastBuildDate>Mon, 26 Mar 2012 23:59:07 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=</generator> <item><title>python-nexus &#8211; a generic nexus (.nex, .trees) reader for python</title><link>http://simon.net.nz/articles/python-nexus-a-generic-nexus-nex-trees-reader-for-python/</link> <comments>http://simon.net.nz/articles/python-nexus-a-generic-nexus-nex-trees-reader-for-python/#comments</comments> <pubDate>Thu, 17 Dec 2009 01:12:17 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <guid
isPermaLink="false">http://simon.net.nz/?p=122</guid> <description><![CDATA[python-nexus - a generic nexus (.nex, .trees) reader for python © Simon J. Greenhill, 2009 Contact me if you have any problems. Download: You can download python-nexus from the bitbucket repository. Usage: Reading a Nexus PLAIN TEXT PYTHON: &#62;&#62;&#62; from nexus import NexusReader &#62;&#62;&#62; n = NexusReader&#40;&#41; &#62;&#62;&#62; n.read_file&#40;'example.nex'&#41; &#160; # or &#62;&#62;&#62; n = [...]]]></description> <content:encoded><![CDATA[<h2 class="center">python-nexus - a generic nexus (.nex, .trees) reader for python</h2><h3 class="center">© Simon J. Greenhill, 2009</h3><p><a
href="/contact/">Contact me if you have any problems.</a></p><h2>Download:</h2><p>You can download python-nexus from <a
href="http://bitbucket.org/simongreenhill/python-nexus/">the bitbucket repository</a>.</p><h2>Usage:</h2><h3>Reading a Nexus<br
/><h3><div
class="igBar"><span
id="lpython-3"><a
href="#" onclick="javascript:showPlainTxt('python-3'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-3"><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;">&gt;&gt;&gt; <span
style="color: #ff7700;font-weight:bold;">from</span> nexus <span
style="color: #ff7700;font-weight:bold;">import</span> NexusReader</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n = NexusReader<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;">&gt;&gt;&gt; n.<span
style="color: black;">read_file</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'example.nex'</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;</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: #808080; font-style: italic;"># or</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n = NexusReader<span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'example.nex'</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;">...</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: #808080; font-style: italic;"># display blocks found in data file</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n.<span
style="color: black;">blocks</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: black;">&#123;</span><span
style="color: #483d8b;">'data'</span>: &lt;nexusDataBlock: <span
style="color: #ff4500;color:#800000;">2</span> characters <span
style="color: #ff7700;font-weight:bold;">from</span> <span
style="color: #ff4500;color:#800000;">4</span> taxa&gt;<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: #808080; font-style: italic;">#`data` blocks</span></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;"># get the number of characters</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</span>.<span
style="color: black;">nchar</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff4500;color:#800000;">2</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: #808080; font-style: italic;"># get the number of taxa</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</span>.<span
style="color: black;">ntaxa</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #ff4500;color:#800000;">4</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: #808080; font-style: italic;"># get the format string</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</span>.<span
style="color: black;">format</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'datatype'</span>: <span
style="color: #483d8b;">'standard'</span>, <span
style="color: #483d8b;">'symbols'</span>: <span
style="color: #483d8b;">'01'</span>, <span
style="color: #483d8b;">'gap'</span>: <span
style="color: #483d8b;">'-'</span><span
style="color: black;">&#125;</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: #808080; font-style: italic;"># access the data matrix</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</span>.<span
style="color: black;">matrix</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'Simon'</span>: <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'01'</span><span
style="color: black;">&#93;</span>, <span
style="color: #483d8b;">'Louise'</span>: <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'11'</span><span
style="color: black;">&#93;</span>, <span
style="color: #483d8b;">'Betty'</span>: <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'10'</span><span
style="color: black;">&#93;</span>, <span
style="color: #483d8b;">'Harry'</span>: <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'00'</span><span
style="color: black;">&#93;</span><span
style="color: black;">&#125;</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: #808080; font-style: italic;"># access by taxon:</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</span>.<span
style="color: black;">matrix</span><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'Simon'</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;"><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'01'</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: #808080; font-style: italic;"># get list of taxa</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</span>.<span
style="color: black;">taxa</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'Betty'</span>, <span
style="color: #483d8b;">'Harry'</span>, <span
style="color: #483d8b;">'Louise'</span>, <span
style="color: #483d8b;">'Simon'</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: #808080; font-style: italic;"># loop over taxa and data</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;">&gt;&gt;&gt; <span
style="color: #ff7700;font-weight:bold;">for</span> taxon, characters <span
style="color: #ff7700;font-weight:bold;">in</span> n.<span
style="color: black;">data</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...</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;"># `tree` blocks</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;">&gt;&gt;&gt; n = NexusReader<span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'example.trees'</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;"><span
style="color: #808080; font-style: italic;"># get the number of trees</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n.<span
style="color: black;">trees</span>.<span
style="color: black;">ntrees</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: #ff4500;color:#800000;">3</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: #808080; font-style: italic;"># get a tree</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n.<span
style="color: black;">trees</span>.<span
style="color: black;">trees</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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;"><span
style="color: #483d8b;">'tree tree.0.1065.603220 = (((((((Chris:0.0668822155,Bruce:0.0173144449):0.0062091603,Tom:0.0523825242):0.0206190840,(Henry:0.0482653647,Timothy:0.0744964092):0.0183093750):0.0401805957,(Mark:0.0066961591,Simon:0.0755275882):0.0264078188):0.0536464636,((Fred:0.0428499135,Kevin:0.0734738565):0.0937536292,Roger:0.0538708492):0.0438297939):0.0453008384,(Michael:0.0953237112,Andrew:0.0654710419):0.0803079594):0.0630363263,David:0.0855948485);'</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: #808080; font-style: italic;"># loop over trees</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span
style="color: #ff7700;font-weight:bold;">for</span> tree <span
style="color: #ff7700;font-weight:bold;">in</span> n.<span
style="color: black;">trees</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;">...</div></li></ol></div></div></div><p></p><h3>Writing a Nexus File</h3><div
class="igBar"><span
id="lpython-4"><a
href="#" onclick="javascript:showPlainTxt('python-4'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-4"><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;">&gt;&gt;&gt; <span
style="color: #ff7700;font-weight:bold;">from</span> nexus <span
style="color: #ff7700;font-weight:bold;">import</span> NexusWriter</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n = NexusWriter<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: #808080; font-style: italic;"># Add a comment to appear in the header of the file</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;">&gt;&gt;&gt; n.<span
style="color: black;">add_comment</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">"I am a comment"</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;</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: #808080; font-style: italic;">#data are added by using the &quot;add&quot; function -</span></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;">#which takes 3 arguments, a taxon, a character name, and a value</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon1'</span>, <span
style="color: #483d8b;">'Character1'</span>, <span
style="color: #483d8b;">'A'</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;">&gt;&gt;&gt; n.<span
style="color: black;">data</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: black;">&#123;</span><span
style="color: #483d8b;">'Character1'</span>: <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'taxon1'</span>: <span
style="color: #483d8b;">'A'</span><span
style="color: black;">&#125;</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon2'</span>, <span
style="color: #483d8b;">'Character1'</span>, <span
style="color: #483d8b;">'C'</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon3'</span>, <span
style="color: #483d8b;">'Character1'</span>, <span
style="color: #483d8b;">'A'</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: #808080; font-style: italic;">#Characters and values can be strings or integers</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon1'</span>, <span
style="color: #ff4500;color:#800000;">2</span>, <span
style="color: #ff4500;color:#800000;">1</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon2'</span>, <span
style="color: #ff4500;color:#800000;">2</span>, <span
style="color: #ff4500;color:#800000;">2</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon3'</span>, <span
style="color: #ff4500;color:#800000;">2</span>, <span
style="color: #ff4500;color:#800000;">3</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;</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: #808080; font-style: italic;">#NexusWriter will interpolate missing entries (i.e. taxon2 in this case)</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon1'</span>, <span
style="color: #483d8b;">"Char3"</span>, <span
style="color: #483d8b;">'4'</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;">&gt;&gt;&gt; n.<span
style="color: black;">add</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'taxon3'</span>, <span
style="color: #483d8b;">"Char3"</span>, <span
style="color: #483d8b;">'4'</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;</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: #808080; font-style: italic;"># ... when you're ready, you can generate the nexus using `make_nexus` or `write_to_file`:</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; n.<span
style="color: black;">make_nexus</span><span
style="color: black;">&#40;</span>interleave=<span
style="color: #008000;">True</span>, charblock=<span
style="color: #008000;">True</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;">&gt;&gt;&gt; n.<span
style="color: black;">write_to_file</span><span
style="color: black;">&#40;</span>filename=<span
style="color: #483d8b;">"output.nex"</span>, interleave=<span
style="color: #008000;">True</span>, charblock=<span
style="color: #008000;">True</span><span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/python-nexus-a-generic-nexus-nex-trees-reader-for-python/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>r8spermute</title><link>http://simon.net.nz/articles/r8spermute/</link> <comments>http://simon.net.nz/articles/r8spermute/#comments</comments> <pubDate>Wed, 14 Oct 2009 02:09:35 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://simon.net.nz/?p=106</guid> <description><![CDATA[This is a small program to assess how robust divergence times are based on the calibrations given to the program r8s.]]></description> <content:encoded><![CDATA[<h2 class="center">r8spermute - Runs all permutations of r8s constraints.</h2><h3 class="center">© Simon J. Greenhill, 2009</h3><p>This is a small program to assess how robust divergence times are based on the calibrations given to the program r8s.</p><p>The program r8s by Mike Sanderson (<a
href="http://loco.biosci.arizona.edu/r8s/">http://loco.biosci.arizona.edu/r8s/</a>) [1, 2] performs a number of phylogenetic dating methods on a set of phylogenetic trees. In essence, you plug in a number of calibration points from known historical evidence and the program r8s takes your estimated trees, and "smoothes" the rates of change observed on those trees, using the calibrations to convert the branches into time.</p><p>However, it is often useful to explore the effect that certain calibration has on the various date estimates. For example, in the paper [3], I wanted to estimate the age at which a large language family originated. We had about 10 different calibration points based on historical evidence. Some of these calibrations were, however, more controversial than the others. So, I wrote this program to take a tree (or set of trees) and analyse them under all the combinations of these calibrations. This allowed us to assess the relative effects on the date estimates of the different calibrations.</p><p>If you find this program useful, please cite reference [3].</p><p><a
href="/contact/">Contact me if you have any problems.</a></p><h2>Download:</h2><p>You can download r8spermute from <a
href="http://bitbucket.org/simongreenhill/r8spermute/">the bitbucket repository</a> here: <a
href="http://bitbucket.org/simongreenhill/r8spermute/downloads/">download r8spermute</a></p><h2>Usage:</h2><p>You will need two things: 1) A set of trees, 2) A r8s command block. Examples of both of these are provided in the "examples" directory.</p><p>Your r8s block file will look something like this:</p><div
class="igBar"><span
id="lcode-5"><a
href="#" onclick="javascript:showPlainTxt('code-5'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-5"><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;">begin rates;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">blformat lengths=persite nsites=<span
style="color:#800000;color:#800000;">1000</span> ultrametric=no round=yes;</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;">collapse;</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;">mrca node1 Daisy Fiona;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">constrain taxon=node1 min_age=<span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">8</span> max_age=<span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">5</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;">mrca node2 Robert Tamara;</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;">constrain taxon=node2 min_age=<span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">1</span> max_age=<span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">3</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;">unfixage taxon=Simon;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">constrain taxon=Simon min_age=<span
style="color:#800000;color:#800000;">0</span>.<span
style="color:#800000;color:#800000;">7</span> max_age=<span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">2</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;">set num_restarts=<span
style="color:#800000;color:#800000;">5</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;">set smoothing=<span
style="color:#800000;color:#800000;">10</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;">divtime method=pl algorithm=tn;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">showage shownamed = yes;</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;">profile taxon=node1 parameter=age;</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;">profile taxon=node2 parameter=age;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe plot=chrono_description;</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;">end;</div></li></ol></div></div></div><p></p><p>... thus, we have 3 calibrations - node1, node2, and a constraint on the terminal taxon "Simon".</p><p>We can then run r8spermute like this:</p><div
class="igBar"><span
id="lcode-6"><a
href="#" onclick="javascript:showPlainTxt('code-6'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-6"><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;">r8spermute.<span
style="">py</span> example/example.<span
style="">trees</span> example/example.<span
style="">r8s</span></div></li></ol></div></div></div><p></p><p>This will start r8s running and you should then see some output like this:</p><div
class="igBar"><span
id="lcode-7"><a
href="#" onclick="javascript:showPlainTxt('code-7'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-7"><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;">Constraints found:&nbsp; <span
style="color:#800000;color:#800000;">3</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Constraints ignored:&nbsp; <span
style="color:#800000;color:#800000;">0</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;">Total Constraint Combinations:&nbsp; <span
style="color:#800000;color:#800000;">7</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;">Running analysis <span
style="color:#800000;color:#800000;">1</span> of <span
style="color:#800000;color:#800000;">7</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">0</span>.<span
style="color:#800000;color:#800000;">14</span>%<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Staging in:&nbsp; /var/folders/9Z/9Z3O2T2o2RmgRU+F75TSx++++TQ/-Tmp-/tmpXn1lw9</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;">Constraints:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">+&nbsp; constrain taxon=node1 min_age=<span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">8</span> max_age=<span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">5</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;">Logging in: example/example_100.<span
style="">log</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">r8s version <span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">71</span> <span
style="color:#006600; font-weight:bold;">&#40;</span>compiled May <span
style="color:#800000;color:#800000;">16</span> <span
style="color:#800000;color:#800000;">2006</span><span
style="color:#006600; font-weight:bold;">&#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:#006600; font-weight:bold;">&#91;</span>...<span
style="">reading</span> file /var/folders/9Z/9Z3O2T2o2RmgRU+F75TSx++++TQ/-Tmp-/tmpXn1lw9<span
style="color:#006600; font-weight:bold;">&#93;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Elapsed time: <span
style="color:#800000;color:#800000;">4</span>.28s <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">0</span>.<span
style="color:#800000;color:#800000;">07</span> minutes<span
style="color:#006600; font-weight:bold;">&#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;">Average run-time so far: <span
style="color:#800000;color:#800000;">4</span>.28s <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">0</span>.<span
style="color:#800000;color:#800000;">07</span> minutes<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Estimated run-time left: <span
style="color:#800000;color:#800000;">25</span>.68s <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">0</span>.<span
style="color:#800000;color:#800000;">43</span> minutes<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li></ol></div></div></div><p></p><p>...etc.</p><p>This tells us that we've got one constraint turned on (+  constrain taxon=node1 min_age=1.8 max_age=2.5;)' in this sub-analysis, as well as various other run-time statistics. ....and now we wait for this to finish. It could take some time..</p><p>When the analysis has finished, we use the program r8spermute_results.py to parse the results:</p><div
class="igBar"><span
id="lcode-8"><a
href="#" onclick="javascript:showPlainTxt('code-8'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-8"><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 r8spermute_results.<span
style="">py</span> example/</div></li></ol></div></div></div><p></p><p>This will loop over the log files and extract the relevant information. You probably want to output this information to a file, so run it like this:</p><div
class="igBar"><span
id="lcode-9"><a
href="#" onclick="javascript:showPlainTxt('code-9'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-9"><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 r8spermute_results.<span
style="">py</span> example/&amp;gt; results.<span
style="">txt</span></div></li></ol></div></div></div><p></p><p>Now, we can look at the results, which will look something like this:</p><div
class="igBar"><span
id="lcode-10"><a
href="#" onclick="javascript:showPlainTxt('code-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-10"><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;">node1 node2 Simon node1 node2</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;">0</span> <span
style="color:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">15</span>.<span
style="color:#800000;color:#800000;">22</span> <span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">84</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:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">54</span>.<span
style="color:#800000;color:#800000;">60</span> <span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">30</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;">0</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">49</span>.<span
style="color:#800000;color:#800000;">45</span> <span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">30</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:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">50</span> <span
style="color:#800000;color:#800000;">4</span>.<span
style="color:#800000;color:#800000;">38</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;">1</span> <span
style="color:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">50</span> <span
style="color:#800000;color:#800000;">4</span>.<span
style="color:#800000;color:#800000;">18</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:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">0</span> <span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">50</span> <span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">30</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;">1</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">1</span> <span
style="color:#800000;color:#800000;">2</span>.<span
style="color:#800000;color:#800000;">50</span> <span
style="color:#800000;color:#800000;">1</span>.<span
style="color:#800000;color:#800000;">30</span></div></li></ol></div></div></div><p></p><p>It's probably easiest to load this into a spreadsheet. Each row is an analysis. The first 3 columns (containing 1's and 0's) are our constraints and whether they're turned on (=1) or off (=0) in the analysis. The last two columns "node1" and "node2" are our estimated ages of those nodes, under the calibrations.</p><p>So, the first line shows that node1 is estimated to be 15.22 years old and node2 is 2.84 years old when only the calibration called "Simon" is used.</p><p>The second line shows that when only node2 is calibrated, the estimate for node1 and node2 is 54.60 and 1.30 respectively.</p><p>In contrast, the very last line shows that when all calibrations are turned on, then the age estimates are 2.50 and 1.30 respectively.</p><h2>References:</h2><p>[1] Sanderson, M. J. 1997. A nonparametric approach to estimating divergence<br
/> times in the absence of rate constancy. Mol. Biol. Evol. 14:1218-1231.</p><p>[2] Sanderson, M. J. 2002. Estimating absolute rates of molecular evolution and<br
/> divergence times: a penalized likelihood approach. Mol. Biol. Evol.<br
/> 19:101-109.</p><p>[3] Gray, R.D., Drummond, A.J., &amp; Greenhill, S.J. (2009) Language Phylogenies<br
/> Reveal Expansion Pulses and Pauses in Pacific Settlement. Science, 323:<br
/> 479-483.</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/r8spermute/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>genbank-download</title><link>http://simon.net.nz/articles/genbank-download/</link> <comments>http://simon.net.nz/articles/genbank-download/#comments</comments> <pubDate>Thu, 08 Oct 2009 00:42:42 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <guid
isPermaLink="false">http://simon.net.nz/?p=89</guid> <description><![CDATA[genbank-download is a small script to download nucleotide sequences from genbank using an accession number.]]></description> <content:encoded><![CDATA[<p>genbank-download is a small script to download nucleotide sequences from genbank using an accession number.</p><p><b>Usage:</b></p><blockquote><p> python genbankdownload.py [options] ACCESSION-NUMBER</p><blockquote><p><b>Example Usage</b></p><blockquote><p> python genbankdownload.py J01415<br
/> python genbankdownload.py J01415 > mysequence.xml<br
/> python genbankdownload.py -m fasta J01415 > mysequence.fasta</p></blockquote><p>The program source-code is available <a
href="http://bitbucket.org/simongreenhill/genbank-download/">here</a> or you can run:</p><blockquote><p> easy_install genbank-download</p><blockquote> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/genbank-download/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Simple webserver file alteration monitoring using integrit</title><link>http://simon.net.nz/articles/simple-webserver-file-alteration-monitoring-using-integrit/</link> <comments>http://simon.net.nz/articles/simple-webserver-file-alteration-monitoring-using-integrit/#comments</comments> <pubDate>Wed, 06 Jun 2007 06:06:32 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[security]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/simple-webserver-file-alteration-monitoring-using-integrit/</guid> <description><![CDATA[This shows us once again, that any software you run on your website needs to be kept up-to-date immediately, but what shocked me was that so many people out there running websites and are not watching them for file changes. They had no idea that their sites had been hacked until they went and looked for it.]]></description> <content:encoded><![CDATA[<h2>Intrusion detection?</h2><p>Over on <em><a
href="http://mezzoblue.com/archives/2007/06/05/unsettling/index.php">Mezzoblue</a></em>, Dave Shea found out that his website had been compromised subtly. The attacker had exploited some (as yet unknown) security hole and quietly modified his website to link to the standard spam sites.</p><p>Within a few hours there were tens of posts from people who'd checked <em>their</em> websites and found similar  modifications that had been sitting their un-noticed, with people pointing the finger either at old WordPress installations or a guesses that their hosting service had been compromised.</p><p>This shows us once again, that any software you run on your website needs to be kept up-to-date immediately, but what shocked me was that so many people out there running websites and are <strong>not watching them for file changes</strong>. They had <strong>no idea</strong> that their sites had been hacked until they went and looked for it.</p><p>So - in bold: <strong>Anyone running a website or webserver of any type needs to watch out for unexpected access and changes.</strong></p><p>The easiest way to do this is to use some intrusion detection software (IDS). This sounds complex, but it's actually quite easy to do. All these programs do is to monitor your files and warn you when they change. This would have immediately spotted this type of attack.</p><p>Because of this, I've decided to write up an easy guide to simple file alteration monitoring - here it is.</p><h2>Choose your weapon:</h2><p>There are plenty of intrusion detection/file modification apps out there - some of the better known ones include <a
href="http://www.cs.tut.fi/~rammer/aide.html">AIDE</a>, <a
href="http://www.la-samhna.de/samhain/">Samhain</a> and <a
href="http://www.tripwire.com/">Tripwire</a>. These are all very cool, and highly powerful, but are also quite complex and hard to install, especially on cheap shared hosting.</p><p>Therefore, my weapon of choice, for the last few months has been a lightweight and fast application called <a
href="http://integrit.sourceforge.net/">integrit</a>, so I'm going to tell you how to install it here.</p><p><strong>Before we start:</strong> Do make sure that you're not compromised right <em>now</em>, there's no point     running an IDS if you're already hacked. While you're at it, make sure everything's upgraded too.</p><h2>Step 1: Make a place to store integrit:</h2><p>Since you're on shared hosting, you can't install integrit properly into /usr, but you need to to put it somewhere anyway.</p><p>I decided to install it to a directory called "integrit" inside my home dir, so:</p><div
class="igBar"><span
id="lcode-11"><a
href="#" onclick="javascript:showPlainTxt('code-11'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-11"><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;">mkdir ~/integrit</div></li></ol></div></div></div><p></p><p>In the following commands, do remember to replace any mention of <em>~/integrit</em> with the directory you used.</p><h2>Step 2: Download and install integrit:</h2><p>The integrit webpage is at: <a
href="http://integrit.sourceforge.net/">http://integrit.sourceforge.net/</a>, so go  there and get the latest version (currently 4.1), or you could cut'n' paste this:</p><div
class="igBar"><span
id="lcode-12"><a
href="#" onclick="javascript:showPlainTxt('code-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-12"><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;">cd ~/integrit</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">wget http:<span
style="color:#FF9933; font-style:italic;">//optusnet.dl.sourceforge.net/sourceforge/integrit/integrit-4.1.tar.gz </span></div></li></ol></div></div></div><p></p><p>Once the integrit archive file is in your ~/integrit directory, we need to decompress it and install it:</p><div
class="igBar"><span
id="lcode-13"><a
href="#" onclick="javascript:showPlainTxt('code-13'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-13"><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;">cd ~/integrit</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">tar -zxvf integrit-<span
style="color:#800000;color:#800000;">4</span>.<span
style="color:#800000;color:#800000;">1</span>.<span
style="">tar</span>.<span
style="">gz</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;">cd integrit-<span
style="color:#800000;color:#800000;">4</span>.<span
style="color:#800000;color:#800000;">1</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;">./configure</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;">make</div></li></ol></div></div></div><p></p><p>Next we need to put the integrit binary somewhere where we can get it, here I've just dumped it into the ~/integrit<br
/> directory, but you could put it in ~/bin or something nicer if you want:</p><div
class="igBar"><span
id="lcode-14"><a
href="#" onclick="javascript:showPlainTxt('code-14'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-14"><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;">cp integrit ~/integrit</div></li></ol></div></div></div><p></p><h2>Step 3: Set up integrit:</h2><p>Look in the integrit-4.1/examples directory and make a config file from the example.</p><p>You need three things at least:</p><ol><li>The <em>known</em> file database - this is where the integrit database is stored.</li><li>The <em>current</em> file database - this is where integrit stores the modified info.</li><li>A <em>root</em> directory to monitor - this is the full path to the directory we want to watch.</li></ol><p>We can also tell integrit to <em>ignore</em> directories, by listing with an exclamation mark at the start of the line. We want to ignore the ~/integrit directory, and on dreamhost, we'll need to ignore the webserver log directory (because it changes a lot, and parts of it our user can't access which will cause errors).</p><p>All in all, it'll look something like this:</p><div
class="igBar"><span
id="lcode-15"><a
href="#" onclick="javascript:showPlainTxt('code-15'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-15"><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;"># database locations <span
style="color:#006600; font-weight:bold;">&#40;</span>FULL PATHS!<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">known=/home/simon/integrit/src_known.<span
style="">cdb</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;">current=/home/simon/integrit/src_current.<span
style="">cdb</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;"># What do we want to check <span
style="color:#006600; font-weight:bold;">&#40;</span>no trailing slash!<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">root=/home/simon</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;"># ignore the integrit dir:</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;">!/home/simon/integrit</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;"># ignore the webserver logs dir:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">!/home/simon/logs</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;"># oh, and the bash logfile</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;">!/home/simon/.<span
style="">bash_history</span></div></li></ol></div></div></div><p></p><p><em>Note:</em> You'll need to change "simon" to your user name, and "integrit" to where you installed integrit to in step 2.</p><h2>Step 4: Create integrit database</h2><p>We need to get integrit to store a list of the current files, and their vital statistics, so run this command:</p><div
class="igBar"><span
id="lcode-16"><a
href="#" onclick="javascript:showPlainTxt('code-16'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-16"><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;">~/integrit/integrit -C home.<span
style="">conf</span> -u</div></li></ol></div></div></div><p></p><p>...where "home.conf" is the integrit configuration file that you generated in step 3.</p><p>If all goes well, you'll see something like this:</p><div
class="igBar"><span
id="lcode-17"><a
href="#" onclick="javascript:showPlainTxt('code-17'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-17"><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;">integrit: ---- integrit, version <span
style="color:#800000;color:#800000;">4</span>.<span
style="color:#800000;color:#800000;">1</span> -----------------</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output : human-readable</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;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conf file : home.<span
style="">conf</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; known db : /home/simon/integrit/src_known.<span
style="">cdb</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;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current db : /home/simon/integrit/src_current.<span
style="">cdb</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root : /home/simon</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;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do check : no</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;do update : yes</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;">integrit: current-state db RMD160 --------------</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit: 3d6b135343a5031d357b5bb2d7d7dc39c7ab5646&nbsp; /home/simon8/integrit/src_current.<span
style="">cdb</span></div></li></ol></div></div></div><p></p><p>Once that's done, copy the newly created database to the known database location:</p><div
class="igBar"><span
id="lcode-18"><a
href="#" onclick="javascript:showPlainTxt('code-18'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-18"><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;">cp ~/integrit/src_current.<span
style="">cdb</span> ~/integrit/src_known.<span
style="">cdb</span></div></li></ol></div></div></div><p></p><h2>Step 5: Test that integrit's working:</h2><p>So lets make sure that integrit's working properly. To do this, we can add an empty file somewhere and see if integrit spots it.</p><div
class="igBar"><span
id="lcode-19"><a
href="#" onclick="javascript:showPlainTxt('code-19'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-19"><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;">cd ~</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">touch foo</div></li></ol></div></div></div><p></p><p>Now we can run integrit:</p><div
class="igBar"><span
id="lcode-20"><a
href="#" onclick="javascript:showPlainTxt('code-20'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-20"><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;">~/integrit/integrit -C ~/integrit/home.<span
style="">conf</span>&nbsp; -c</div></li></ol></div></div></div><p></p><p>If all goes well, you'll see something like this:</p><div
class="igBar"><span
id="lcode-21"><a
href="#" onclick="javascript:showPlainTxt('code-21'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-21"><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;">integrit: ---- integrit, version <span
style="color:#800000;color:#800000;">4</span>.<span
style="color:#800000;color:#800000;">1</span> -----------------</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output : human-readable</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;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conf file : /home/simon8/integrit/home.<span
style="">conf</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; known db : /home/simon8/integrit/src_known.<span
style="">cdb</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;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current db : /home/simon8/integrit/src_current.<span
style="">cdb</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root : /home/simon8</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;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do check : yes</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">integrit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;do update : no</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;">new:&nbsp; &nbsp; &nbsp;/home/simon8/foo&nbsp; &nbsp;p<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">664</span><span
style="color:#006600; font-weight:bold;">&#41;</span> t<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">100000</span><span
style="color:#006600; font-weight:bold;">&#41;</span> u<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">767504</span><span
style="color:#006600; font-weight:bold;">&#41;</span> g<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">203016</span><span
style="color:#006600; font-weight:bold;">&#41;</span> z<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">0</span><span
style="color:#006600; font-weight:bold;">&#41;</span> m<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">20070605</span>-<span
style="color:#800000;color:#800000;">162156</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">new:&nbsp; &nbsp; &nbsp;/home/simon8/foo&nbsp; &nbsp;s<span
style="color:#006600; font-weight:bold;">&#40;</span>9c1185a5c5e9fc54612808977ee8f548b2258d31<span
style="color:#006600; font-weight:bold;">&#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;">integrit: not doing update, so no check for missing files</div></li></ol></div></div></div><p></p><p>Notice how integrit's spotted the <em>foo</em> file that's not in the database? If one of the files has changed, you'll get much the same output, with "changed:" instead of "new:". So - remove the dummy file:</p><div
class="igBar"><span
id="lcode-22"><a
href="#" onclick="javascript:showPlainTxt('code-22'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-22"><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;">rm ~/foo</div></li></ol></div></div></div><p></p><h2>Step 7: Get integrit to run daily:</h2><p>Now, we want to set up a cron job, so that integrit is run automatically for us. First of all, we should make a quick little shell script to run integrit and email the results to us:</p><div
class="igBar"><span
id="lcode-23"><a
href="#" onclick="javascript:showPlainTxt('code-23'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-23"><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;">#!/bin/bash</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;">DATE=`/bin/date <span
style="color:#CC0000;">"+%F"</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;">/home/simon/integrit/integrit -C /home/simon/integrit/home.<span
style="">conf</span> -cu | /usr/bin/mutt -s <span
style="color:#CC0000;">"integrit - $DATE"</span> email@example.<span
style="">com</span></div></li></ol></div></div></div><p></p><p>Paste the above into a file called "run_integrit.sh" (a good place to put it would be in your ~/integrit directory), edit the paths to match your setup, and change the email address. Finally, make this file executable:</p><div
class="igBar"><span
id="lcode-24"><a
href="#" onclick="javascript:showPlainTxt('code-24'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-24"><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;">chmod +x ~/integrit/run_integrit.<span
style="">sh</span></div></li></ol></div></div></div><p></p><p>Now all we have to do is to add that to our crontab.</p><div
class="igBar"><span
id="lcode-25"><a
href="#" onclick="javascript:showPlainTxt('code-25'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-25"><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;">crontab -e</div></li></ol></div></div></div><p></p><p>...and add a line that looks something like this:</p><div
class="igBar"><span
id="lcode-26"><a
href="#" onclick="javascript:showPlainTxt('code-26'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-26"><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;"><span
style="color:#800000;color:#800000;">59</span>&nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">21</span>&nbsp; &nbsp; &nbsp; *&nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;/home/simon/integrit/run_integrit.<span
style="">sh</span></div></li></ol></div></div></div><p></p><p>This will run integrit at 21.59 every day, if you don't know what that means, then have a google for "crontab tutorial".</p><p>Save the file, and you're off.</p><h2>Updating the database after valid changes:</h2><p>When you've changed or added a file yourself, then you'll need to update your <em>known</em> database with these changes. To do this, just generate a <em>current</em> database, and copy it over the old one. The script I've got above will automatically generate a <em>current</em> one, so you can just use that version, or repeat Step 4.</p><h2>Final considerations:</h2><p><strong>Note</strong>: If you can, you should run integrit (that is BOTH the database files and the binary files) off a "safe" partition, that's not writable. Unfortunately, most of us on shared hosting don't have that privilege, so just be aware that if a really clever attacker does get you, then they're likely to disable or modify the IDS if they can.</p><p>A good way of dealing with this is to copy your known file database off the webserver and make sure that the one on the server matches this one every so often.</p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/simple-webserver-file-alteration-monitoring-using-integrit/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Query PubMed for citation information using a DOI and Python</title><link>http://simon.net.nz/articles/query-pubmed-for-citation-information-using-a-doi-and-python/</link> <comments>http://simon.net.nz/articles/query-pubmed-for-citation-information-using-a-doi-and-python/#comments</comments> <pubDate>Mon, 29 Jan 2007 14:01:44 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/query-pubmed-for-citation-information-using-a-doi-and-python/</guid> <description><![CDATA[Here's a simple little script to query PubMed for a Digitial Object Identifier (a DOI)]]></description> <content:encoded><![CDATA[<p>Here's a simple little script to query <a
href="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi">PubMed</a> for a <a
href="http://en.wikipedia.org/wiki/Digital_object_identifier">Digitial Object Identifier</a> (a DOI)</p><p>Usage is quite simple, find a DOI somewhere, e.g. <em>10.1038/nature02029</em> (for <a
href="http://language.psy.auckland.ac.nz/publications/index.php?pub=Gray_and_Atkinson2003Nature">this groundbreaking paper</a>), and run this:</p><div
class="igBar"><span
id="lcode-27"><a
href="#" onclick="javascript:showPlainTxt('code-27'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-27"><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;">lurch:~ python pythonquery.<span
style="">py</span> <span
style="color:#800000;color:#800000;">10</span>.<span
style="color:#800000;color:#800000;">1038</span>/nature02029</div></li></ol></div></div></div><p></p><p>... and via the magic of webservices and XML, and with a bit of luck, you'll get something like this back:</p><div
class="igBar"><span
id="lcode-28"><a
href="#" onclick="javascript:showPlainTxt('code-28'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-28"><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;">Language-tree divergence times support the Anatolian theory of Indo-European origin.</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="">Gray</span>, RD, Atkinson, QD</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Nature <span
style="color:#800000;color:#800000;">2003</span>, <span
style="color:#800000;color:#800000;">426</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#800000;color:#800000;">6965</span><span
style="color:#006600; font-weight:bold;">&#41;</span>:<span
style="color:#800000;color:#800000;">435</span>-<span
style="color:#800000;color:#800000;">9</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;">Languages, like genes, provide vital clues about human history. <span
style="">The</span> origin of</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;">the Indo-European language family is <span
style="color:#CC0000;">"the most intensively studied, yet still</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#CC0000;">most recalcitrant, problem of historical linguistics"</span>. <span
style="">Numerous</span> genetic studies</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;">of Indo-European origins have also produced inconclusive results. <span
style="">Here</span> we</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">analyse linguistic data using computational methods derived from evolutionary</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;">biology. <span
style="">We</span> test two theories of Indo-European origin: the <span
style="color:#CC0000;">'Kurgan expansion'</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">and the <span
style="color:#CC0000;">'Anatolian farming'</span> hypotheses. <span
style="">The</span> Kurgan theory centres on possible</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;">archaeological evidence for an expansion into Europe and the Near East by</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Kurgan horsemen beginning in the sixth millennium BP. <span
style="">In</span> contrast, the Anatolian</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;">theory claims that Indo-European languages expanded with the spread of</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">agriculture from Anatolia around <span
style="color:#800000;color:#800000;">8</span>,<span
style="color:#800000;color:#800000;">000</span>-<span
style="color:#800000;color:#800000;">9</span>,<span
style="color:#800000;color:#800000;">500</span> years bp. <span
style="">In</span> striking agreement</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;">with the Anatolian hypothesis, our analysis of a matrix of <span
style="color:#800000;color:#800000;">87</span> languages with</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;">2</span>,<span
style="color:#800000;color:#800000;">449</span> lexical items produced an estimated age range for the initial Indo-European</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;">divergence of between <span
style="color:#800000;color:#800000;">7</span>,<span
style="color:#800000;color:#800000;">800</span> and <span
style="color:#800000;color:#800000;">9</span>,<span
style="color:#800000;color:#800000;">800</span> years bp. <span
style="">These</span> results were robust to</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">changes in coding procedures, calibration points, rooting of the trees and priors</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;">in the bayesian analysis.</div></li></ol></div></div></div><p></p><h2>The Code:</h2><div
class="igBar"><span
id="lpython-29"><a
href="#" onclick="javascript:showPlainTxt('python-29'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-29"><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/bin/env python#&nbsp; &nbsp;Simple script to query pubmed for a DOI</span></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;">#&nbsp; &nbsp;(c) Simon Greenhill, 2007</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: #808080; font-style: italic;">#&nbsp; &nbsp;http://simon.net.nz/</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;">import</span> <span
style="color: #dc143c;">urllib</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;">from</span> <span
style="color: #dc143c;">xml</span>.<span
style="color: black;">dom</span> <span
style="color: #ff7700;font-weight:bold;">import</span> minidom</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;">def</span> get_citation_from_doi<span
style="color: black;">&#40;</span>query, <span
style="color: #dc143c;">email</span>=<span
style="color: #483d8b;">'YOUR EMAIL GOES HERE'</span>, tool=<span
style="color: #483d8b;">'SimonsPythonQuery'</span>, database=<span
style="color: #483d8b;">'pubmed'</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; params = <span
style="color: black;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #483d8b;">'db'</span>:database,</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; &nbsp; &nbsp; <span
style="color: #483d8b;">'tool'</span>:tool,</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #483d8b;">'email'</span>:<span
style="color: #dc143c;">email</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; &nbsp; &nbsp; <span
style="color: #483d8b;">'term'</span>:query,</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #483d8b;">'usehistory'</span>:<span
style="color: #483d8b;">'y'</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; &nbsp; &nbsp; <span
style="color: #483d8b;">'retmax'</span>:<span
style="color: #ff4500;color:#800000;">1</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: black;">&#125;</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;</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: #808080; font-style: italic;"># try to resolve the PubMed ID of the DOI</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; url = <span
style="color: #483d8b;">'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?'</span> + <span
style="color: #dc143c;">urllib</span>.<span
style="color: black;">urlencode</span><span
style="color: black;">&#40;</span>params<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; data = <span
style="color: #dc143c;">urllib</span>.<span
style="color: black;">urlopen</span><span
style="color: black;">&#40;</span>url<span
style="color: black;">&#41;</span>.<span
style="color: black;">read</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;</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: #808080; font-style: italic;"># parse XML output from PubMed...</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; xmldoc = minidom.<span
style="color: black;">parseString</span><span
style="color: black;">&#40;</span>data<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; ids = xmldoc.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Id'</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;</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: #808080; font-style: italic;"># nothing found, exit</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;">if</span> <span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span>ids<span
style="color: black;">&#41;</span> == <span
style="color: #ff4500;color:#800000;">0</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">raise</span> <span
style="color: #008000;">Exception</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">"DoiNotFound"</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;">&nbsp; &nbsp; <span
style="color: #808080; font-style: italic;"># get ID</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: #008000;">id</span> = ids<span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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;">&nbsp; &nbsp; <span
style="color: #808080; font-style: italic;"># remove unwanted parameters</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; params.<span
style="color: black;">pop</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'term'</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; params.<span
style="color: black;">pop</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'usehistory'</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; params.<span
style="color: black;">pop</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'retmax'</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; <span
style="color: #808080; font-style: italic;"># and add new ones...</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; params<span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'id'</span><span
style="color: black;">&#93;</span> = <span
style="color: #008000;">id</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;">&nbsp; &nbsp; params<span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'retmode'</span><span
style="color: black;">&#93;</span> = <span
style="color: #483d8b;">'xml'</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;">&nbsp; &nbsp; <span
style="color: #808080; font-style: italic;"># get citation info:</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; url = <span
style="color: #483d8b;">'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'</span> + <span
style="color: #dc143c;">urllib</span>.<span
style="color: black;">urlencode</span><span
style="color: black;">&#40;</span>params<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; data = <span
style="color: #dc143c;">urllib</span>.<span
style="color: black;">urlopen</span><span
style="color: black;">&#40;</span>url<span
style="color: black;">&#41;</span>.<span
style="color: black;">read</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;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> data</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;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">def</span> text_output<span
style="color: black;">&#40;</span><span
style="color: #dc143c;">xml</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; &nbsp; &nbsp; <span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"Makes a simple text output from the XML returned from efetch"</span><span
style="color: #483d8b;">""</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;">&nbsp; &nbsp; &nbsp; &nbsp; xmldoc = minidom.<span
style="color: black;">parseString</span><span
style="color: black;">&#40;</span><span
style="color: #dc143c;">xml</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;</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; &nbsp; &nbsp; title = xmldoc.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'ArticleTitle'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; title = title.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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;">&nbsp; &nbsp; &nbsp; &nbsp; abstract = xmldoc.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'AbstractText'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; abstract = abstract.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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;">&nbsp; &nbsp; &nbsp; &nbsp; authors = xmldoc.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'AuthorList'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; authors = authors.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Author'</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; &nbsp; &nbsp; authorlist = <span
style="color: black;">&#91;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">for</span> author <span
style="color: #ff7700;font-weight:bold;">in</span> authors:</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; &nbsp; &nbsp; &nbsp; &nbsp; LastName = author.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'LastName'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Initials = author.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Initials'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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; &nbsp; &nbsp; &nbsp; &nbsp; author = <span
style="color: #483d8b;">'%s, %s'</span> % <span
style="color: black;">&#40;</span>LastName, Initials<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; &nbsp; &nbsp; &nbsp; &nbsp; authorlist.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>author<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; &nbsp; &nbsp; &nbsp; &nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; journalinfo = xmldoc.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Journal'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; journal = journalinfo.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Title'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; journalinfo = journalinfo.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'JournalIssue'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; volume = journalinfo.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Volume'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; issue = journalinfo.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Issue'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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; &nbsp; &nbsp; year = journalinfo.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'Year'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #808080; font-style: italic;"># this is a bit odd?</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; pages = xmldoc.<span
style="color: black;">getElementsByTagName</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'MedlinePgn'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">childNodes</span><span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</span><span
style="color: black;">&#93;</span>.<span
style="color: black;">data</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;">&nbsp; &nbsp; &nbsp; &nbsp; output = <span
style="color: black;">&#91;</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; &nbsp; &nbsp; &nbsp; output.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>title<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; &nbsp; &nbsp; output.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">''</span><span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;">#empty line</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; &nbsp; &nbsp; output.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">', '</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>authorlist<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; &nbsp; &nbsp; output.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span> <span
style="color: #483d8b;">'%s %s, %s (%s):%s'</span> % <span
style="color: black;">&#40;</span>journal, year, volume, issue, pages<span
style="color: black;">&#41;</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; &nbsp; &nbsp; output.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">''</span><span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;">#empty line</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; output.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>abstract<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; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> output</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;">if</span> __name__ == <span
style="color: #483d8b;">'__main__'</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;">from</span> <span
style="color: #dc143c;">sys</span> <span
style="color: #ff7700;font-weight:bold;">import</span> argv, exit</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;">if</span> <span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span>argv<span
style="color: black;">&#41;</span> == <span
style="color: #ff4500;color:#800000;">1</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">'Usage: %s &lt;query&gt;'</span> % argv<span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">' e.g. %s 10.1038/ng1946'</span> % argv<span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">0</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; &nbsp; &nbsp; &nbsp; exit<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; &nbsp; &nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; citation = get_citation_from_doi<span
style="color: black;">&#40;</span>argv<span
style="color: black;">&#91;</span><span
style="color: #ff4500;color:#800000;">1</span><span
style="color: black;">&#93;</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; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">for</span> line <span
style="color: #ff7700;font-weight:bold;">in</span> text_output<span
style="color: black;">&#40;</span>citation<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; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> line</div></li></ol></div></div></div><p></p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/query-pubmed-for-citation-information-using-a-doi-and-python/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Simple FAQ application for Django</title><link>http://simon.net.nz/articles/simple-faq-application-for-django/</link> <comments>http://simon.net.nz/articles/simple-faq-application-for-django/#comments</comments> <pubDate>Sat, 30 Dec 2006 22:32:28 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[django]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/simple-faq-application-for-django/</guid> <description><![CDATA[This is an exceedingly simple FAQ application for Django, which I wrote for Henry.]]></description> <content:encoded><![CDATA[<p>This is an exceedingly simple FAQ application for <a
href="http://djangoproject.com">Django</a>, which I wrote for <a
href="http://henry.simon.net.nz/about/">Henry</a>.</p><p>Do what you want with it, &amp; let me know of any bugs etc.</p><h2>The Model</h2><p>To store the FAQs!</p><div
class="igBar"><span
id="lpython-30"><a
href="#" onclick="javascript:showPlainTxt('python-30'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-30"><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;">from</span> django.<span
style="color: black;">db</span> <span
style="color: #ff7700;font-weight:bold;">import</span> models</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;">from</span> django.<span
style="color: black;">conf</span> <span
style="color: #ff7700;font-weight:bold;">import</span> settings</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;"># Create your models here.</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;">class</span> Faq<span
style="color: black;">&#40;</span> models.<span
style="color: black;">Model</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;"><span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"Stores FAQS"</span><span
style="color: #483d8b;">""</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;">question = models.<span
style="color: black;">CharField</span><span
style="color: black;">&#40;</span>maxlength=<span
style="color: #ff4500;color:#800000;">255</span><span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;"># The Q</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">answer = models.<span
style="color: black;">TextField</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;"># the A</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;">post_date = models.<span
style="color: black;">DateTimeField</span><span
style="color: black;">&#40;</span>auto_now_add=<span
style="color: #008000;">True</span>, db_index=<span
style="color: #008000;">True</span><span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;"># when we answered it</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;">def</span> <span
style="color: #0000cd;">__str__</span><span
style="color: black;">&#40;</span> <span
style="color: #008000;">self</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;"><span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">self</span>.<span
style="color: black;">question</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;">class</span> Admin:</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;">search_fields = <span
style="color: black;">&#40;</span> <span
style="color: #483d8b;">'question'</span>, <span
style="color: #483d8b;">'answer'</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;">date_hierarchy = <span
style="color: #483d8b;">'post_date'</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;">list_display = <span
style="color: black;">&#40;</span> <span
style="color: #483d8b;">'question'</span>, <span
style="color: #483d8b;">'post_date'</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;">fields = <span
style="color: black;">&#40;</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">None</span>, <span
style="color: black;">&#123;</span> <span
style="color: #483d8b;">'fields'</span>: <span
style="color: black;">&#40;</span> <span
style="color: #483d8b;">'question'</span>, <span
style="color: #483d8b;">'answer'</span> <span
style="color: black;">&#41;</span><span
style="color: black;">&#125;</span><span
style="color: black;">&#41;</span>,<span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><h2>The View:</h2><p>To get the FAQs...</p><div
class="igBar"><span
id="lpython-31"><a
href="#" onclick="javascript:showPlainTxt('python-31'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-31"><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;">from</span> django.<span
style="color: black;">shortcuts</span> <span
style="color: #ff7700;font-weight:bold;">import</span> render_to_response</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;">from</span> django.<span
style="color: black;">template</span> <span
style="color: #ff7700;font-weight:bold;">import</span> RequestContext</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;"># Change &quot;myproject&quot; to your project name</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;">from</span> myproject.<span
style="color: black;">about</span>.<span
style="color: black;">models</span> <span
style="color: #ff7700;font-weight:bold;">import</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;">def</span> about<span
style="color: black;">&#40;</span>request<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;"><span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"Displays all the FAQs"</span><span
style="color: #483d8b;">""</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;">faqs = Faq.<span
style="color: black;">objects</span>.<span
style="color: black;">all</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;"><span
style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span
style="color: black;">&#40;</span> <span
style="color: #483d8b;">'about.html'</span>, <span
style="color: black;">&#123;</span>&nbsp; <span
style="color: #483d8b;">'faqs'</span>:faqs, <span
style="color: black;">&#125;</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;">context_instance=RequestContext<span
style="color: black;">&#40;</span>request<span
style="color: black;">&#41;</span> <span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><h2>The Template:</h2><p>...and finally, the template to bring it all together:</p><div
class="igBar"><span
id="lhtml-32"><a
href="#" onclick="javascript:showPlainTxt('html-32'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">HTML:</span><div
id="html-32"><div
class="html"><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;">{% extends &quot;base.html&quot; %}</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">{% load markup %}</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;">{% block content %}</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: #009900;"><a
href="http://december.com/html/4/element/h2.html"><span
style="color: #000000; font-weight: bold;">&lt;h2&gt;</span></a></span>Frequently Asked Questions:<span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/h2&gt;</span></span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">{% comment %}</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;">build a &quot;table of contents first...&quot;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">{% endcomment %}</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: #009900;"><a
href="http://december.com/html/4/element/ul.html"><span
style="color: #000000; font-weight: bold;">&lt;ul&gt;</span></a></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% for faq in faqs %}</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: #009900;"><a
href="http://december.com/html/4/element/li.html"><span
style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span><span
style="color: #009900;"><a
href="http://december.com/html/4/element/a.html"><span
style="color: #000000; font-weight: bold;">&lt;a</span></a> <span
style="color: #000066;">href</span>=<span
style="color: #ff0000;">"http://simon.net.nz/wp-admin/#%7B%7B%20faq.id%20%7D%7D"</span><span
style="color: #000000; font-weight: bold;">&gt;</span></a></span>{{ faq.question|escape }}<span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></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;">{% endfor %}<span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">{% comment %}</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;">now show the FAQs</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">{% endcomment %}</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;">{% for faq in faqs %}</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: #009900;"><a
href="http://december.com/html/4/element/a.html"><span
style="color: #000000; font-weight: bold;">&lt;a</span></a> <span
style="color: #000066;">title</span>=<span
style="color: #ff0000;">"{{ faq.id }}"</span> <span
style="color: #000066;">name</span>=<span
style="color: #ff0000;">"{{ faq.id }}"</span><span
style="color: #000000; font-weight: bold;">&gt;</span></a></span><span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #009900;"><a
href="http://december.com/html/4/element/h3.html"><span
style="color: #000000; font-weight: bold;">&lt;h3</span></a> <span
style="color: #000066;">class</span>=<span
style="color: #ff0000;">"q"</span><span
style="color: #000000; font-weight: bold;">&gt;</span></a></span> {{ faq.question|escape }}<span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/h3&gt;</span></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: #009900;"><a
href="http://december.com/html/4/element/p.html"><span
style="color: #000000; font-weight: bold;">&lt;p</span></a> <span
style="color: #000066;">class</span>=<span
style="color: #ff0000;">"faq"</span><span
style="color: #000000; font-weight: bold;">&gt;</span></a></span> {{ faq.answer|textile }}<span
style="color: #009900;"><span
style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; {% endfor %}</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;">{% endblock %}</div></li></ol></div></div></div><p></p><h2>The End:</h2><p>You couldn't get any simpler than that without using a textfile..</p><p><strong>Note:</strong> this requires textile - but you can easily change the filter in the template to whatever you want. You will need to have <em>django.contrib.markup</em> installed though.</p><p
class="download"> <a
href="http://simon.net.nz/files/djangofaqapplication.zip" id="p27">Download the Django FAQ Application</a></p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/simple-faq-application-for-django/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Django Database Export, v.00001</title><link>http://simon.net.nz/articles/django-database-export-v00001/</link> <comments>http://simon.net.nz/articles/django-database-export-v00001/#comments</comments> <pubDate>Sat, 04 Nov 2006 10:11:26 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[django]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/django-database-export-v00001/</guid> <description><![CDATA[Here's something I wrote to export database contents from a django application into the Django database API format.]]></description> <content:encoded><![CDATA[<p>Here's something I wrote to export database contents from <a
href="http://djangoproject.com">a django application</a> into the Django database API format.</p><p>It's certainly not going to do all the work for you - in particular you'll need to reorder things to make sure things that Django wants to be objects (i.e. Foreign Keys) are instantiated before the related object gets made.</p><p>Hope this helps someone somewhere.</p><div
class="igBar"><span
id="lpython-33"><a
href="#" onclick="javascript:showPlainTxt('python-33'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-33"><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/bin/env python</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"</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: #483d8b;">Script to export data from a Django database and generate</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">the appropriate django DB-API commands</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">Make sure you run it from the same directory as your settings.py</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">NOTE: it does NOT handle relationships yet, so, you'll need to take the output</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: #483d8b;">and hand edit it to make sure that anything that Django needs as an object,</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">gets instantiated first</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">(c) Simon Greenhill / dev@simon.net.nz / http://simon.net.nz</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">Version: 0.00001</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: #483d8b;">"</span><span
style="color: #483d8b;">""</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;">debug = <span
style="color: #008000;">False</span> <span
style="color: #808080; font-style: italic;"># just a debug flag to spit out what we're doing.</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;">import</span> <span
style="color: #dc143c;">sys</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;">from</span> django.<span
style="color: black;">core</span>.<span
style="color: black;">management</span> <span
style="color: #ff7700;font-weight:bold;">import</span> setup_environ</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;">try</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> settings</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;">except</span> <span
style="color: #008000;">ImportError</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;">print</span> <span
style="color: #483d8b;">"You don't appear to have a settings file in this directory!"</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;">print</span> <span
style="color: #483d8b;">"Please run this from inside a project directory"</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #dc143c;">sys</span>.<span
style="color: black;">exit</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;">setup_environ<span
style="color: black;">&#40;</span>settings<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;">from</span> django.<span
style="color: black;">db</span> <span
style="color: #ff7700;font-weight:bold;">import</span> models</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;"># do model imports</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;">print</span> <span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">###############################################################</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: #483d8b;">#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">#&nbsp; &nbsp;Model imports&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#</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: #483d8b;">#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">###############################################################</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: #483d8b;">"</span><span
style="color: #483d8b;">""</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> app <span
style="color: #ff7700;font-weight:bold;">in</span> models.<span
style="color: black;">get_apps</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;"><span
style="color: #ff7700;font-weight:bold;">for</span> model <span
style="color: #ff7700;font-weight:bold;">in</span> app.<span
style="color: black;">models</span>.<span
style="color: black;">get_models</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;"><span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">"from %s import %s"</span> % <span
style="color: black;">&#40;</span> app.__name__, model._meta.<span
style="color: black;">object_name</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;</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: #808080; font-style: italic;"># what apps we've done</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">seen = <span
style="color: black;">&#91;</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;"><span
style="color: #ff7700;font-weight:bold;">for</span> app <span
style="color: #ff7700;font-weight:bold;">in</span> models.<span
style="color: black;">get_apps</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;"><span
style="color: #ff7700;font-weight:bold;">for</span> model <span
style="color: #ff7700;font-weight:bold;">in</span> app.<span
style="color: black;">models</span>.<span
style="color: black;">get_models</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;"><span
style="color: #ff7700;font-weight:bold;">if</span> model <span
style="color: #ff7700;font-weight:bold;">not</span> <span
style="color: #ff7700;font-weight:bold;">in</span> seen:</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;">print</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;">print</span> <span
style="color: #483d8b;">"# %s.%s "</span> % <span
style="color: black;">&#40;</span>app.__name__, model._meta.<span
style="color: black;">object_name</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;"><span
style="color: #ff7700;font-weight:bold;">print</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;">if</span> debug:</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;">print</span> <span
style="color: #483d8b;">"#<span
style="color: #000099; font-weight: bold;">\t</span>"</span>, model</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> row <span
style="color: #ff7700;font-weight:bold;">in</span> model.<span
style="color: black;">objects</span>.<span
style="color: black;">iterator</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;</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;">if</span> debug:</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;">print</span> <span
style="color: #483d8b;">"#<span
style="color: #000099; font-weight: bold;">\t</span><span
style="color: #000099; font-weight: bold;">\t</span>"</span>,row</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;">attrs = <span
style="color: black;">&#91;</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;"><span
style="color: #ff7700;font-weight:bold;">for</span> col <span
style="color: #ff7700;font-weight:bold;">in</span> row._meta.<span
style="color: black;">fields</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">key = col.<span
style="color: black;">name</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;">value = <span
style="color: #008000;">getattr</span><span
style="color: black;">&#40;</span>row, key<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;"><span
style="color: #ff7700;font-weight:bold;">if</span> col.<span
style="color: black;">get_internal_type</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span> == <span
style="color: #483d8b;">'ForeignKey'</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: #808080; font-style: italic;">###print key, 'bork bork bork'</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;">pass</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: #808080; font-style: italic;"># if it's numeric, we don't need to quote it.</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;">if</span> <span
style="color: #008000;">isinstance</span><span
style="color: black;">&#40;</span>value,<span
style="color: #008000;">int</span><span
style="color: black;">&#41;</span> <span
style="color: #ff7700;font-weight:bold;">or</span> <span
style="color: #008000;">isinstance</span><span
style="color: black;">&#40;</span>value,<span
style="color: #008000;">float</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;"><span
style="color: #ff7700;font-weight:bold;">pass</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: #808080; font-style: italic;"># if it's a string...</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;">elif</span> <span
style="color: #008000;">isinstance</span><span
style="color: black;">&#40;</span>value,<span
style="color: #008000;">str</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;"><span
style="color: #808080; font-style: italic;"># ... and it's got a quote mark or new line in it, then triple-quote it.</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;">if</span> value.<span
style="color: black;">find</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'&quot;'</span><span
style="color: black;">&#41;</span>&amp;gt; <span
style="color: #ff4500;color:#800000;">0</span> <span
style="color: #ff7700;font-weight:bold;">or</span> value.<span
style="color: black;">find</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">"<span
style="color: #000099; font-weight: bold;">\n</span>"</span><span
style="color: black;">&#41;</span>&amp;gt; <span
style="color: #ff4500;color:#800000;">0</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;">value = <span
style="color: #483d8b;">'&quot;&quot;&quot;%s&quot;&quot;&quot;'</span> % value</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;"># ... otherwise single quote it.</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;">else</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">value = <span
style="color: #483d8b;">'&quot;%s&quot;'</span> % value</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: #808080; font-style: italic;"># don't know what it is, but single quote it</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;">else</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;">value = <span
style="color: #483d8b;">'&quot;%s&quot;'</span> % value</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;">attrs.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span> <span
style="color: #483d8b;">'%s=%s'</span> % <span
style="color: black;">&#40;</span> key, value <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;</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;">if</span> debug:</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;">print</span> <span
style="color: #483d8b;">"#<span
style="color: #000099; font-weight: bold;">\t</span><span
style="color: #000099; font-weight: bold;">\t</span><span
style="color: #000099; font-weight: bold;">\t</span>"</span>, key, <span
style="color: #008000;">getattr</span><span
style="color: black;">&#40;</span>row, key<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;">print</span> <span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"%s(%s).save()"</span><span
style="color: #483d8b;">""</span> % <span
style="color: black;">&#40;</span> model._meta.<span
style="color: black;">object_name</span>, <span
style="color: #483d8b;">', '</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>attrs<span
style="color: black;">&#41;</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;">seen.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>model<span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><p
class="highlight"> <strong>Update 19/01/2007:</strong> limodou has written  a <a
href="http://code.djangoproject.com/wiki/DbDumpScript">far superior export solution here</a>.</p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/django-database-export-v00001/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>mnemosyne &#8211; a personal information wiki</title><link>http://simon.net.nz/articles/mnemosyne/</link> <comments>http://simon.net.nz/articles/mnemosyne/#comments</comments> <pubDate>Thu, 19 Oct 2006 07:12:35 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[django]]></category> <category><![CDATA[mnemosyne]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/mnemosyne/</guid> <description><![CDATA[I've just released my personal wiki system mnemosyne to the public. It runs under Django, written in Python and I've released it under the BSD licence, check it out at http://mnemosyne.simon.net.nz]]></description> <content:encoded><![CDATA[<p>I've just released my personal wiki system <a
href="http://mnemosyne.simon.net.nz/">mnemosyne</a> to the public. It runs under Django, written in Python and I've released it under the BSD licence, check it out at <a
href="http://mnemosyne.simon.net.nz">mnemosyne.simon.net.nz</a></p><h3>Features:</h3><ul><li> internal and external linking</li><li> page versioning</li><li> edit history tracking</li><li> backlinks</li><li> users (sort of - in progress),</li><li> page tagging</li><li> bibtex handling</li><li> a pretentious name</li><li> 'sensible' file attachment handling</li><li> 'sensible' data storage</li><li> full search capabilities</li><li> swappable wiki parsers (textile, reStructured Text, plain text and<br
/> should be easy to add others)</li><li> macros for extra functionality</li></ul><p>This is the first major app. I've tackled in both python and Django, and I would love to hear comments / criticisms / suggestions. Be brutal!</p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/mnemosyne/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>benchmarking .htaccess performance</title><link>http://simon.net.nz/articles/benchmarking-htaccess-performance/</link> <comments>http://simon.net.nz/articles/benchmarking-htaccess-performance/#comments</comments> <pubDate>Sun, 27 Aug 2006 11:37:07 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[apache]]></category> <category><![CDATA[code]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/benchmarking-htaccess-performance/</guid> <description><![CDATA[One of the common tips to increasing Apache performance is to turn off the per-directory configuration files (aka .htaccess files) and merge them all into your main Apache server configuration file (httpd.conf).
Jeremy raised an interesting question about when the performance loss caused by using many htaccess files is offset by the ease of maintenance. He's arguing - and I agree - that it makes sense to keep the configuration locally inside .htaccess files, despite the performance loss as these are easier to maintain....]]></description> <content:encoded><![CDATA[<h2>Introduction:</h2><p>One of the common tips to increasing Apache performance is to turn off the per-directory configuration files (aka .htaccess files) and merge them all into your main Apache server configuration file (httpd.conf).</p><p>Jeremy raised <a
href="http://forums.devshed.com/apache-development-15/is-the-htaccess-performance-loss-acceptable-when-it-eases-maintenance-379487.html" title="is the htaccess performance loss acceptable when it eases maintenance">an interesting question</a> about when the performance loss caused by using many htaccess files is offset by the ease of maintenance. He's arguing - and I agree - that it makes sense to keep the configuration <em>locally</em> inside .htaccess files, despite the performance loss as these are easier to maintain.</p><p>It's fairly logical that the multiple .htaccess file route will be slower - for <strong>every</strong> node in the request URI, the webserver has to look for an .htaccess file and merge the rules found in every one. So, we're going to have to have a filesystem seek'n'read for every subdirectory.</p><p>However, is this a major issue? How much of a performance hit is there? Let's find out...</p><h2>Set-up:</h2><p>Ok. Let's make two docroots each with the same structure and files.</p><p>1) htdocs_access - the .htaccess version. This has one .htaccess file in the leaf directory.</p><p>2) htdocs_config - the httpd.conf version. This has the same rule as the above, but the rule is in the server-wide httpd.conf file and htaccess support is turned OFF (AllowOverride None).</p><p>Next, we need to get the .htaccess/httpd.conf files to do something ( mainly so we can see if Apache's merged them in ). So, we'll make a number of files in the last random directory (the leaf node), and give half of them the extension .foo, and the other half .bar. We'll then tell Apache to process the .bar's with PHP, and the .foo's as text. All files will have the same content:</p><div
class="igBar"><span
id="lphp-34"><a
href="#" onclick="javascript:showPlainTxt('php-34'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-34"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li></ol></div></div></div><p></p><p>Here's the (python) code I used to generate this structure:</p><div
class="igBar"><span
id="lpython-35"><a
href="#" onclick="javascript:showPlainTxt('python-35'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PYTHON:</span><div
id="python-35"><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/bin/env 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;"><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;"><span
style="color: #808080; font-style: italic;"># where we'll place the generated structure</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">staging = <span
style="color: #483d8b;">'/Users/simon/server'</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;">htdocs_access = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>staging, <span
style="color: #483d8b;">'htdocs_access'</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;">htdocs_config = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>staging, <span
style="color: #483d8b;">'htdocs_config'</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: #808080; font-style: italic;"># how deep to go!</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;">dir_depth = <span
style="color: #ff4500;color:#800000;">10</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: #808080; font-style: italic;"># how many files in the leaf node of the dir.</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">num_files = <span
style="color: #ff4500;color:#800000;">50</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: #808080; font-style: italic;"># what content to put in the files</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;">content = <span
style="color: #483d8b;">""</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: #808080; font-style: italic;"># the actual htaccess file</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">htaccess = <span
style="color: #483d8b;">""</span><span
style="color: #483d8b;">"</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: #483d8b;">AddHandler application/x-httpd-php .bar</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #483d8b;">"</span><span
style="color: #483d8b;">""</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: #808080; font-style: italic;"># make directory structure</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: #008000;">dir</span> = <span
style="color: #483d8b;">''</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;">for</span> dirnum <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">range</span><span
style="color: black;">&#40;</span> <span
style="color: #ff4500;color:#800000;">0</span>, dir_depth <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: #008000;">dir</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> <span
style="color: #008000;">dir</span>, <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span> dirnum <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;</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;">hta = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> htdocs_access, <span
style="color: #008000;">dir</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;">htc = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span> htdocs_config, <span
style="color: #008000;">dir</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;"><span
style="color: #dc143c;">os</span>.<span
style="color: black;">makedirs</span><span
style="color: black;">&#40;</span> hta <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;"><span
style="color: #dc143c;">os</span>.<span
style="color: black;">makedirs</span><span
style="color: black;">&#40;</span> htc <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: #808080; font-style: italic;"># make the files...</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> filenum <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">range</span><span
style="color: black;">&#40;</span> <span
style="color: #ff4500;color:#800000;">0</span>, num_files <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;"><span
style="color: #808080; font-style: italic;"># assign the file types - half .foo, and half .bar</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;">if</span> filenum % <span
style="color: #ff4500;color:#800000;">2</span> == <span
style="color: #ff4500;color:#800000;">0</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">filename = <span
style="color: #483d8b;">'%d.foo'</span> % filenum</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;">else</span>:</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">filename = <span
style="color: #483d8b;">'%d.bar'</span> % filenum</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;">f = <span
style="color: #008000;">open</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> hta, filename <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</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;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> content <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;">f.<span
style="color: black;">close</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;">f = <span
style="color: #008000;">open</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> htc, filename <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</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;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> content <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;">f.<span
style="color: black;">close</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: #808080; font-style: italic;"># now, add the .htaccess file inside the lead htdocs_access dir</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;">f = <span
style="color: #008000;">open</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> hta, <span
style="color: #483d8b;">'.htaccess'</span> <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</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;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> htaccess <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;">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: 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: #808080; font-style: italic;"># and we'll place it in the root of the htdocs_config dir as</span></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;"># httpd.conf to remind ourselves to add it to the httpd.conf file</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;">f = <span
style="color: #008000;">open</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> htdocs_config, <span
style="color: #483d8b;">'httpd.conf'</span> <span
style="color: black;">&#41;</span>, <span
style="color: #483d8b;">'w+'</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;">f.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span> htaccess <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;">f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li></ol></div></div></div><p></p><p>Here's what we end up with:</p><div
class="igBar"><span
id="lcode-36"><a
href="#" onclick="javascript:showPlainTxt('code-36'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-36"><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;"><span
style="color:#800000;color:#800000;">0</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;">1</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:#800000;color:#800000;">2</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;">3</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:#800000;color:#800000;">4</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;">5</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:#800000;color:#800000;">6</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;">7</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:#800000;color:#800000;">8</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;">9</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:#800000;color:#800000;">0</span>.<span
style="">foo</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;">1</span>.<span
style="">bar</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:#800000;color:#800000;">10</span>.<span
style="">foo</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;">11</span>.<span
style="">bar</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:#006600; font-weight:bold;">&#40;</span>...<span
style="">etc</span>...<span
style="color:#006600; font-weight:bold;">&#41;</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;">6</span>.<span
style="">foo</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:#800000;color:#800000;">7</span>.<span
style="">bar</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;">8</span>.<span
style="">foo</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:#800000;color:#800000;">9</span>.<span
style="">bar</span></div></li></ol></div></div></div><p></p><p>Where htdocs_access has a .htaccess file in 9/ and htdocs_config doesn't.</p><h2>Server Configuration:</h2><p>Here are the two httpd.conf files for the configurations:</p><h3>htdocs_config httpd.conf:</h3><div
class="igBar"><span
id="lcode-37"><a
href="#" onclick="javascript:showPlainTxt('code-37'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-37"><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;">### Section <span
style="color:#800000;color:#800000;">1</span>: Global Environment</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ServerRoot <span
style="color:#CC0000;">"/usr/local/apache2"</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;">PidFile logs/httpd.<span
style="">pid</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Timeout <span
style="color:#800000;color:#800000;">300</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;">KeepAlive On</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxKeepAliveRequests <span
style="color:#800000;color:#800000;">100</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;">KeepAliveTimeout <span
style="color:#800000;color:#800000;">15</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DirectoryIndex index.<span
style="">html</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;">AccessFileName .<span
style="">htaccess</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">HostnameLookups Off</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;"># fixes crashes on OSX??</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;">AcceptMutex fcntl</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;">StartServers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MinSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</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;">MaxSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxClients&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">100</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;">MaxRequestsPerChild&nbsp; <span
style="color:#800000;color:#800000;">10</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;">### Section <span
style="color:#800000;color:#800000;">2</span>: <span
style="color:#CC0000;">'Main'</span> server configuration</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">User nobody</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;">Group #-<span
style="color:#800000;color:#800000;">1</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;">DocumentRoot <span
style="color:#CC0000;">"/Users/simon/server/htdocs_config"</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;">LoadModule php5_module modules/libphp5.<span
style="">so</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;">Listen <span
style="color:#800000;color:#800000;">8111</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;">&lt;directory&gt;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Options Indexes FollowSymLinks</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;">AllowOverride None</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AddHandler application/x-httpd-php .<span
style="">bar</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;">&lt;/directory&gt;</div></li></ol></div></div></div><p></p><h3>htdocs_access httpd.conf:</h3><div
class="igBar"><span
id="lcode-38"><a
href="#" onclick="javascript:showPlainTxt('code-38'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-38"><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;">### Section <span
style="color:#800000;color:#800000;">1</span>: Global Environment</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ServerRoot <span
style="color:#CC0000;">"/usr/local/apache2"</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;">PidFile logs/httpd.<span
style="">pid</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Timeout <span
style="color:#800000;color:#800000;">300</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;">KeepAlive On</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxKeepAliveRequests <span
style="color:#800000;color:#800000;">100</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;">KeepAliveTimeout <span
style="color:#800000;color:#800000;">15</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DirectoryIndex index.<span
style="">html</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;">AccessFileName .<span
style="">htaccess</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">HostnameLookups Off</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;"># fixes crashes on OSX??</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;">AcceptMutex fcntl</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;">StartServers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MinSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</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;">MaxSpareServers&nbsp; &nbsp; &nbsp; <span
style="color:#800000;color:#800000;">5</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MaxClients&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color:#800000;color:#800000;">100</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;">MaxRequestsPerChild&nbsp; <span
style="color:#800000;color:#800000;">10</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;">### Section <span
style="color:#800000;color:#800000;">2</span>: <span
style="color:#CC0000;">'Main'</span> server configuration</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">User nobody</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;">Group #-<span
style="color:#800000;color:#800000;">1</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;">DocumentRoot <span
style="color:#CC0000;">"/Users/simon/server/htdocs_access"</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;">LoadModule php5_module modules/libphp5.<span
style="">so</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;">Listen <span
style="color:#800000;color:#800000;">8111</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;">&lt;directory&gt;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Options Indexes FollowSymLinks</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;">AllowOverride All</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/directory&gt;</div></li></ol></div></div></div><p></p><h2>Results:</h2><p>Benchmarking was done with "ab" the Apache Benchmark program, which was set to access one page 1,000 times with 10 concurrencies. Each configuration was benchmarked five times in random order (to minimise the effect of any running background processes etc).</p><table><tr><th></th><th
colspan="2">htdocs_config</th><th
colspan="2">htdocs_access</th></tr><tr><th>Test:</th><th>Time Taken (s):</th><th>Requests per Second:</th><th>Time Taken (s):</th><th>Requests per Second:</th></tr><tr><td>1</td><td>12.683213</td><td>78.84</td><td>13.21618</td><td>75.66</td></tr><tr><td>2</td><td>12.854491</td><td>77.79</td><td>13.574916</td><td>73.67</td></tr><tr><td>3</td><td>11.777676</td><td>84.91</td><td>13.163296</td><td>75.97</td></tr><tr><td>4</td><td>13.668398</td><td>73.16</td><td>12.26475</td><td>81.53</td></tr><tr><td>5</td><td>13.76753</td><td>76.47</td><td>13.264527</td><td>75.39</td></tr><tr><th>AVERAGE:</th><th>12.9</th><th>78.23</th><th>13.1</th><th>76.4</th></tr></table><p>So - we're looking at a difference of around 2.3% extra requests per second when htaccess files are disabled. This is really quite trivial, and should only be worried about when you're <em>really</em> loaded.</p><h2>Issues:</h2><p>There are a number of areas where this could be improved:</p><ul><li>Try different directory depths i.e. the more nested the directory is, the slower it should be under the .htaccess scenario. In contrast, if there's only 2 or 3 levels then it should be faster.</li><li>Have multiple .htaccess files in the intermediate nodes to see how Apache handles the merging of these files. Here we've just used one .htaccess file, and we should probably see further slowdowns if Apache has to merge some complicated rule sets.</li><li>Access different files - I just requested one file repeatedly, so we might be getting a lot of interference from any caching systems (harddrive, ram, php caches etc) that I forgot about. Additionally, requesting multiple URI's is a more realistic test case for a webserver.</li></ul> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/benchmarking-htaccess-performance/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Processing mod_security audit logs with logwatch</title><link>http://simon.net.nz/articles/processing-mod_security-audit-logs-with-logwatch/</link> <comments>http://simon.net.nz/articles/processing-mod_security-audit-logs-with-logwatch/#comments</comments> <pubDate>Sat, 26 Aug 2006 07:40:56 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[apache]]></category> <category><![CDATA[code]]></category> <category><![CDATA[mod_security]]></category> <category><![CDATA[perl]]></category> <guid
isPermaLink="false">http://simon.net.nz/articles/processing-mod_security-audit-logs-with-logwatch/</guid> <description><![CDATA[This is a quick and dirty LogWatch script to process mod_security audit logs. I had a google, and couldn't find any released scripts, so here's my one.]]></description> <content:encoded><![CDATA[<p>This is a quick and dirty <a
href="http://www.logwatch.org" title="LogWatch">LogWatch</a> script to process <a
href="http://www.modsecurity.org" title="mod_security">mod_security</a> audit logs. I had a google, and couldn't find any released scripts, so here's my one.</p><div
class="igBar"><span
id="lperl-39"><a
href="#" onclick="javascript:showPlainTxt('perl-39'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PERL:</span><div
id="perl-39"><div
class="perl"><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/bin/perl -w</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: #b1b100;">my</span> <span
style="color: #0000ff;">$Detail</span> = <span
style="color: #0000ff;">$ENV</span><span
style="color: #66cc66;">&#123;</span><span
style="color: #ff0000;">'LOGWATCH_DETAIL_LEVEL'</span><span
style="color: #66cc66;">&#125;</span> || <span
style="color: #cc66cc;color:#800000;">0</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: #b1b100;">while</span> <span
style="color: #66cc66;">&#40;</span><a
href="http://www.perldoc.com/perl5.6/pod/func/defined.html"><span
style="color: #000066;">defined</span></a><span
style="color: #66cc66;">&#40;</span><span
style="color: #0000ff;">$ThisLine</span> = <span
style="color: #009999;">&lt;stdin&gt;</span><span
style="color: #66cc66;">&#41;</span><span
style="color: #66cc66;">&#41;</span> <span
style="color: #66cc66;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.perldoc.com/perl5.6/pod/func/chomp.html"><span
style="color: #000066;">chomp</span></a><span
style="color: #66cc66;">&#40;</span><span
style="color: #0000ff;">$ThisLine</span><span
style="color: #66cc66;">&#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: #b1b100;">if</span> <span
style="color: #66cc66;">&#40;</span> <span
style="color: #0000ff;">$ThisLine</span> =~ <a
href="http://www.perldoc.com/perl5.6/pod/func/m.html"><span
style="color: #000066;">m</span></a>/^Request:/ <span
style="color: #66cc66;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #66cc66;">&#123;</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;"><a
href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span
style="color: #000066;">print</span></a> <span
style="color: #0000ff;">$ThisLine</span>.<span
style="color: #ff0000;">"<span
style="color: #000099; font-weight: bold;">\n</span>"</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #66cc66;">&#125;</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: #b1b100;">if</span><span
style="color: #66cc66;">&#40;</span> <span
style="color: #0000ff;">$ThisLine</span> =~ <a
href="http://www.perldoc.com/perl5.6/pod/func/m.html"><span
style="color: #000066;">m</span></a>/^mod_security-message/ <span
style="color: #66cc66;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color: #66cc66;">&#123;</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;"><a
href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span
style="color: #000066;">print</span></a> <span
style="color: #0000ff;">$ThisLine</span>.<span
style="color: #ff0000;">"<span
style="color: #000099; font-weight: bold;">\n</span>"</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span
style="color: #000066;">print</span></a> <span
style="color: #ff0000;">"<span
style="color: #000099; font-weight: bold;">\n</span>"</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: #66cc66;">&#125;</span>&lt;/stdin&gt;</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: #66cc66;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.perldoc.com/perl5.6/pod/func/exit.html"><span
style="color: #000066;">exit</span></a><span
style="color: #66cc66;">&#40;</span><span
style="color: #cc66cc;color:#800000;">0</span><span
style="color: #66cc66;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>On Debian, you should just be able to drop the above script into <em>/usr/share/logwatch/scripts/services</em> (chmod +x too!), and then add this to <em>/etc/logwatch/conf/logfiles/mod_security.conf</em>:</p><div
class="igBar"><span
id="lcode-40"><a
href="#" onclick="javascript:showPlainTxt('code-40'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">CODE:</span><div
id="code-40"><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;">########################################################</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#&nbsp; &nbsp;Define log file group for mod_security</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;">########################################################</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;"># Where the log files are - you will need to match this to your configuration</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"># Note that a relative path like this will be expanded automatically to the</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;"># standard log path <span
style="color:#006600; font-weight:bold;">&#40;</span>e.<span
style="">g</span>. /var/log/... <span
style="color:#006600; font-weight:bold;">&#41;</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;">LogFile = apache2/audit.<span
style="">log</span>.<span
style="color:#800000;color:#800000;">1</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LogFile = apache2/audit.<span
style="">log</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;"># If the archives are searched, here is one or more line</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:#006600; font-weight:bold;">&#40;</span>optionally containing wildcards<span
style="color:#006600; font-weight:bold;">&#41;</span> that tell where they are...</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"># Note: if these are gzipped, you need to end with a .<span
style="">gz</span> even if you use wildcards...</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="">Archive</span> = apache2<span
style="color:#008000;">/*audit.log.*.gz </span></div></li></ol></div></div></div><p></p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/processing-mod_security-audit-logs-with-logwatch/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
