<?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>Aaron Toponce &#187; Python</title> <atom:link href="http://pthree.org/category/python/feed/" rel="self" type="application/rss+xml" /><link>http://pthree.org</link> <description>Linux.  GNU.  Freedom.</description> <lastBuildDate>Wed, 01 Sep 2010 22:04:20 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1-alpha</generator> <item><title>Largest Palindromic Number In Python</title><link>http://pthree.org/2007/09/15/largest-palindromic-number-in-python/</link> <comments>http://pthree.org/2007/09/15/largest-palindromic-number-in-python/#comments</comments> <pubDate>Sat, 15 Sep 2007 20:53:39 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/09/15/largest-palindromic-number-in-python/</guid> <description><![CDATA[I found the absolute best way to learn the Python programming language, while at the same time, increasing deductive logic capacity and learning mathematics. The way is through Project Euler. Of course, you don&#8217;t have to use the Python language to complete the problems. You can use any language you like, or use pencil and [...]]]></description> <content:encoded><![CDATA[<p>I found the absolute best way to learn the Python programming language, while at the same time, increasing deductive logic capacity and learning mathematics.  The way is through <a
href="http://projecteuler.net">Project Euler</a>.  Of course, you don&#8217;t have to use the Python language to complete the problems.  You can use any language you like, or use pencil and paper if that suits your needs best.</p><p>At any event, I&#8217;m going through the problems one by one, and have completed the first 4. <a
href="http://projecteuler.net/index.php?section=problems&#038;id=4">The fourth problem</a> took a great deal of thinking to get right, but I nailed it out slowly, and actually came up with a pretty fast solution.  The problem is this:</p><blockquote><p>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.</p><p>Find the largest palindrome made from the product of two 3-digit numbers.</p></blockquote><p>After hacking a bit, I realized that the only way I knew how to solve this problem, was to brute force it.  Some things came to mind almost immediately:</p><ul><li>I should start with 999 * 999 and count down, rather than 100 * 100 and count up, seeing as though I&#8217;m looking for the largest palindromic number.</li><li>999 * 999 = 998001 iterations through every number, but I will be testing each one at least twice, so I only need to test half (999 * 999, 999 * 998, 999 * 997, &#8230; 999*100, 998 * 998, 998 * 997, &#8230; 998 * 100, 997 * 997, 997 * 996, &#8230;, etc)</li><li>There should a pattern with palindromic numbers to further increase my algorithm.</li></ul><p>Point 3 was the hardest for me to find, but I found it.  There is in fact a pattern with 6-digit palindromic numbers (technically speaking, any even-digit palindrome (I know that my result will be 6-digits, or at least I&#8217;m fairly confident)).  Consider the palindrome &#8220;abccba&#8221;:</p><pre>Let's look at the numeric column of each letter:

a(100,000) + b(10,000) + c(1,000) + c(100) + b(10) + a(1)

Adding similar variables (in algebraic notation):

100001a + 10010b + 1100c

The common denominator is 11:

11(9091a + 910b + 100c)

This means that each palindrome will be a multiple of 11.</pre><p>This means that I can start with 999 * 990 for my first test, 999 * 979 for my second, 999 * 968 for my third, and so on.  This will greatly reduce the number of iterations that I need to step through when finding this palindromic number.  It wasn&#8217;t long, however, that I realized this problem will need to be separated into two functions.  One to determine if the number is in fact a palindrome, returning either True or False, and the second function to iterate through all the possible product combinations, filling an array with nothing but palindromic numbers.  Here is my solution:</p><div
class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/></div></td><td><div
class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #808080; font-style: italic;">#!/usr/bin/env python</span><br
/> <span
style="color: #ff7700;font-weight:bold;">def</span> is_palin<span
style="color: black;">&#40;</span><span
style="color: #dc143c;">string</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; <span
style="color: #483d8b;">&quot;&quot;&quot;Returns true if a string is a palindrome&quot;&quot;&quot;</span><br
/> &nbsp; &nbsp; start, end = <span
style="color: #ff4500;">0</span>, <span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span><span
style="color: #dc143c;">string</span><span
style="color: black;">&#41;</span> - <span
style="color: #ff4500;">1</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">while</span> end <span
style="color: #66cc66;">&gt;</span> start:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: #dc143c;">string</span><span
style="color: black;">&#91;</span>start<span
style="color: black;">&#93;</span> <span
style="color: #66cc66;">!</span>= <span
style="color: #dc143c;">string</span><span
style="color: black;">&#91;</span>end<span
style="color: black;">&#93;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">False</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; start += <span
style="color: #ff4500;">1</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; end -= <span
style="color: #ff4500;">1</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">True</span><br
/> <br
/> <span
style="color: #ff7700;font-weight:bold;">def</span> palindrome<span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; <span
style="color: #483d8b;">&quot;&quot;&quot;Finds the largest palindrome that is the product of 2 3-digit numbers&quot;&quot;&quot;</span><br
/> &nbsp; &nbsp; num1 = <span
style="color: #ff4500;">999</span><br
/> &nbsp; &nbsp; result_arr = <span
style="color: black;">&#91;</span><span
style="color: black;">&#93;</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">while</span> num1 <span
style="color: #66cc66;">&gt;</span> <span
style="color: #ff4500;">100</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; num2 = <span
style="color: #ff4500;">990</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> num2 <span
style="color: #66cc66;">&gt;</span> num1:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num2 = num1 - <span
style="color: black;">&#40;</span>num1 <span
style="color: #66cc66;">%</span> <span
style="color: #ff4500;">11</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">while</span> num2 <span
style="color: #66cc66;">&gt;</span> <span
style="color: #ff4500;">109</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> is_palin<span
style="color: black;">&#40;</span><span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>num1 <span
style="color: #66cc66;">*</span> num2<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result_arr.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>num1 <span
style="color: #66cc66;">*</span> num2<span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num2 -= <span
style="color: #ff4500;">11</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; num1 -= <span
style="color: #ff4500;">1</span><br
/> &nbsp; &nbsp; result_arr.<span
style="color: black;">sort</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> result_arr<span
style="color: black;">&#91;</span><span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span>result_arr<span
style="color: black;">&#41;</span> - <span
style="color: #ff4500;">1</span><span
style="color: black;">&#93;</span><br
/> <br
/> palindrome<span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></td></tr></tbody></table></div><p>A couple comments about the code.  First, I&#8217;m turning the number into a string, then determining if that string is in fact a palindrome.  I look at the beginning and ending letter of the string.  If they match, I increase the beginning letter by 1 and decrease the ending letter by one, and test again if they match.  I continue to do this throughout the string until every letter was tested.  If they all passed, return True, otherwise, return False.  Second, to keep my algorithm only churning through half of the required iterations, I want to make sure that I start with the square, and work down from there.  For example:</p><pre>View with a monospace font, or this table won't make much sense (see the original post):

999*990    998*990    997*990    ...    990*990
999*979    998*979    997*979    ...    990*979    989*979    988*979    987*979    etc.
999*968    998*968    997*968    ...    990*968    989*968    988*968    987*968
  ...        ...        ...      ...      ...        ...        ...        ...
999*121    998*121    997*121    ...    990*121    989*121    988*121    987*121
999*110    998*110    997*110    ...    990*110    989*110    988*110    987*110</pre><p>The only aspect about my algorithm, was that I am not breaking out when I find the largest palindromic number.  The reason for this was simple- I got too caught up in churning through every 11 numbers, that I forgot to test when I found the largest.  However, after looking at the code, I figured that I was curious about all the palindromes that I have found, so I stored them in an array then sorted it ascending.  It is interesting to look through the array at each one, seeing the patterns throughout.</p><p>At any rate, on my machine, I benchmarked the code:</p><pre>aaron@kratos:~$ time python palindrome.py
906609

real    0m0.067s
user    0m0.068s
sys     0m0.000s</pre><p>Nice and lean.  0.067 seconds isn&#8217;t bad for a first attempt, I think.  Thoughts?  Comments?</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/09/15/largest-palindromic-number-in-python/feed/</wfw:commentRss> <slash:comments>12</slash:comments> </item> <item><title>Prime Numbers In Python</title><link>http://pthree.org/2007/09/05/prime-numbers-in-python/</link> <comments>http://pthree.org/2007/09/05/prime-numbers-in-python/#comments</comments> <pubDate>Wed, 05 Sep 2007 21:41:41 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/09/05/prime-numbers-in-python/</guid> <description><![CDATA[The following program returns True if a number is prime or False otherwise. I am proud of this code, as it implements dead code. Upon the first positive condition in the if statement nested in the while loop, the program terminates, ignoring any further code following. As far as I can tell, this is the [...]]]></description> <content:encoded><![CDATA[<p>The following program returns True if a number is prime or False otherwise.  I am proud of this code, as it implements <i>dead code</i>.  Upon the first positive condition in the if statement nested in the while loop, the program terminates, ignoring any further code following.  As far as I can tell, this is the most efficient way (not necessarily the fastest) to test if a number is prime or not. <strike>This means that this function has an order of O(n-2) (it doesn&#8217;t test against 1 and itself) as a worst case scenario, and O(n/m) where m is the first factor of n as a best case.</strike> As elementary as it is, I think it&#8217;s pretty slick.  All handled in 9 <i>very readable</i> lines of code.</p><p>UPDATE:  Thanks to some observant readers, I have come to the realization that I only need to loop to the square root of n, and not n, as the square root of n is the largest factor.  This brings the order to O(sqrt(n)-2) at the worst case scenario, and O(1) as the best case.</p><div
class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/></div></td><td><div
class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #ff7700;font-weight:bold;">def</span> is_prime<span
style="color: black;">&#40;</span>n<span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">math</span><br
/> &nbsp; &nbsp; n = <span
style="color: #008000;">abs</span><span
style="color: black;">&#40;</span>n<span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; i = <span
style="color: #ff4500;">2</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">while</span> i <span
style="color: #66cc66;">&lt;</span>= <span
style="color: #dc143c;">math</span>.<span
style="color: black;">sqrt</span><span
style="color: black;">&#40;</span>n<span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> n <span
style="color: #66cc66;">%</span> i == <span
style="color: #ff4500;">0</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">False</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; i += <span
style="color: #ff4500;">1</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> <span
style="color: #008000;">True</span></div></td></tr></tbody></table></div> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/09/05/prime-numbers-in-python/feed/</wfw:commentRss> <slash:comments>25</slash:comments> </item> <item><title>Recursion In Python</title><link>http://pthree.org/2007/08/09/recursion-in-python/</link> <comments>http://pthree.org/2007/08/09/recursion-in-python/#comments</comments> <pubDate>Thu, 09 Aug 2007 12:58:59 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/08/09/recursion-in-python/</guid> <description><![CDATA[I took my Perl script that I wrote some time ago, and rewrote it in Python. The exercise of the initial Perl script, was to get a better handle on the language using recursion, and to inventory my Ogg Vorbis collection of music. The same reasons were used for this Python version. This script traverses [...]]]></description> <content:encoded><![CDATA[<p>I took <a
href="http://www.pthree.org/2007/01/18/simple-recursion-in-perl/">my Perl script that I wrote some time ago</a>, and rewrote it in Python.  The exercise of the initial Perl script, was to get a better handle on the language using recursion, and to inventory my Ogg Vorbis collection of music.  The same reasons were used for this Python version.  This script traverses a directory tree, full of Ogg Vorbis files, printing out to a file in indented form, the contents, and counting the number of Ogg Vorbis files it comes across.  You&#8217;ll notice the lack of comments, as I find Python code much more readable than Perl code.  It should be fairly obvious what this script is doing.</p><div
class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/></div></td><td><div
class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #808080; font-style: italic;">#!/usr/bin/env python</span><br
/> <span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">dircache</span>, <span
style="color: #dc143c;">os</span><br
/> counter = <span
style="color: #ff4500;">0</span><br
/> <br
/> <span
style="color: #ff7700;font-weight:bold;">def</span> PrintFiles<span
style="color: black;">&#40;</span>indent<span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">global</span> counter<br
/> &nbsp; &nbsp; thisDir = <span
style="color: #dc143c;">os</span>.<span
style="color: black;">getcwd</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">for</span> <span
style="color: #008000;">file</span> <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #dc143c;">dircache</span>.<span
style="color: black;">listdir</span><span
style="color: black;">&#40;</span>thisDir<span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: black;">&#40;</span><span
style="color: #008000;">file</span>.<span
style="color: black;">endswith</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'ogg'</span><span
style="color: black;">&#41;</span> <span
style="color: #ff7700;font-weight:bold;">or</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">isdir</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">file</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> <span
style="color: #008000;">file</span>.<span
style="color: black;">startswith</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'.'</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: #008000;">file</span>.<span
style="color: black;">endswith</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'ogg'</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += <span
style="color: #ff4500;">1</span><br
/> &nbsp; &nbsp; <br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oggFile.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'%s%s<span
style="color: #000099; font-weight: bold;">\n</span>'</span> <span
style="color: #66cc66;">%</span><span
style="color: black;">&#40;</span>indent, <span
style="color: #008000;">file</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span><br
/> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">isdir</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">file</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #dc143c;">os</span>.<span
style="color: black;">chdir</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">file</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrintFiles<span
style="color: black;">&#40;</span>indent + <span
style="color: #483d8b;">' &nbsp;'</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #dc143c;">os</span>.<span
style="color: black;">chdir</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'../'</span><span
style="color: black;">&#41;</span><br
/> <br
/> <span
style="color: #ff7700;font-weight:bold;">try</span>:<br
/> &nbsp; &nbsp; oggFile = <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'oggfiles.txt'</span>, <span
style="color: #483d8b;">'w'</span><span
style="color: black;">&#41;</span><br
/> <span
style="color: #ff7700;font-weight:bold;">except</span> <span
style="color: #008000;">IOError</span>, e:<br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Unable to open 'oggfiles.txt' for writing: &quot;</span>, e<br
/> <span
style="color: #ff7700;font-weight:bold;">else</span>: &nbsp; &nbsp;<br
/> &nbsp; &nbsp; PrintFiles<span
style="color: black;">&#40;</span><span
style="color: #483d8b;">''</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; oggFile.<span
style="color: black;">write</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'<span
style="color: #000099; font-weight: bold;">\n</span>Current number of ogg files: %d<span
style="color: #000099; font-weight: bold;">\n</span><span
style="color: #000099; font-weight: bold;">\n</span>'</span> <span
style="color: #66cc66;">%</span><span
style="color: black;">&#40;</span>counter<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; oggFile.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></td></tr></tbody></table></div> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/08/09/recursion-in-python/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>29 And Holding</title><link>http://pthree.org/2007/06/09/29-and-holding/</link> <comments>http://pthree.org/2007/06/09/29-and-holding/#comments</comments> <pubDate>Sat, 09 Jun 2007 13:31:42 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/06/09/29-and-holding/</guid> <description><![CDATA[123456789101112from datetime import date birth_day = date&#40;1977, 6, 9&#41; right_now = date.today&#40;&#41; if &#40;right_now.month &#62;= birth_day.month&#41; &#38; &#40;right_now.day &#62;= birth_day.day&#41;: &#160; &#160; age = right_now.year - birth_day.year else: &#160; &#160; age = right_now.year - birth_day.year - 1 if age &#62;= 30: &#160; &#160; age = 29]]></description> <content:encoded><![CDATA[<div
class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/></div></td><td><div
class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #ff7700;font-weight:bold;">from</span> <span
style="color: #dc143c;">datetime</span> <span
style="color: #ff7700;font-weight:bold;">import</span> date<br
/> <br
/> birth_day = date<span
style="color: black;">&#40;</span><span
style="color: #ff4500;">1977</span>, <span
style="color: #ff4500;">6</span>, <span
style="color: #ff4500;">9</span><span
style="color: black;">&#41;</span><br
/> right_now = date.<span
style="color: black;">today</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> <br
/> <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: black;">&#40;</span>right_now.<span
style="color: black;">month</span> <span
style="color: #66cc66;">&gt;</span>= birth_day.<span
style="color: black;">month</span><span
style="color: black;">&#41;</span> <span
style="color: #66cc66;">&amp;</span> <span
style="color: black;">&#40;</span>right_now.<span
style="color: black;">day</span> <span
style="color: #66cc66;">&gt;</span>= birth_day.<span
style="color: black;">day</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; age = right_now.<span
style="color: black;">year</span> - birth_day.<span
style="color: black;">year</span><br
/> <span
style="color: #ff7700;font-weight:bold;">else</span>:<br
/> &nbsp; &nbsp; age = right_now.<span
style="color: black;">year</span> - birth_day.<span
style="color: black;">year</span> - <span
style="color: #ff4500;">1</span><br
/> <br
/> <span
style="color: #ff7700;font-weight:bold;">if</span> age <span
style="color: #66cc66;">&gt;</span>= <span
style="color: #ff4500;">30</span>:<br
/> &nbsp; &nbsp; age = <span
style="color: #ff4500;">29</span></div></td></tr></tbody></table></div> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/06/09/29-and-holding/feed/</wfw:commentRss> <slash:comments>20</slash:comments> </item> <item><title>Poll: First Programming Language?</title><link>http://pthree.org/2007/03/13/poll-first-programming-language/</link> <comments>http://pthree.org/2007/03/13/poll-first-programming-language/#comments</comments> <pubDate>Tue, 13 Mar 2007 16:47:19 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Ruby]]></category> <category><![CDATA[Scripting]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/03/13/poll-first-programming-language/</guid> <description><![CDATA[My coworker asked me this question, and began asking a few others in a couple IRC channels. So, I thought I&#8217;d extend it to my blog, and the planets that I syndicate. I added as many choices as I could hoping to see a wide array of votes. I added a few newer languages for [...]]]></description> <content:encoded><![CDATA[<p>My coworker asked me this question, and began asking a few others in a couple IRC channels.  So, I thought I&#8217;d extend it to my blog, and the planets that I syndicate.  I added as many choices as I could hoping to see a wide array of votes.  I added a few newer languages for the younger audience.</p><p>For me, my first language that I sat down and learned in a formal setting was Java.  However, I had TI, Casio, and HP calculators, as well as an Atari 800 that I would fiddle with, and program.  So, Basic was really the first language that I toyed with.  I just never sat down, and took the time to &#8220;learn&#8221; it.</p><p>So, for all the programmers, coders, script kiddies and developers out there, what is the 1st programming language that you learned?  If applicable, leave a comment specifying the system that you learned the language on.</p><div><div
class='democracy'> <strong
class="poll-question">What was the 1st programming language that you learned?</strong><div
class='dem-results'><form
action='http://pthree.org/wp-content/plugins/democracy/democracy.php' onsubmit='return dem_Vote(this)'><ul><li> <input
type='radio' id='dem-choice-19' value='19' name='dem_poll_4' /> <label
for='dem-choice-19'>Basic</label></li><li> <input
type='radio' id='dem-choice-20' value='20' name='dem_poll_4' /> <label
for='dem-choice-20'>Pascal</label></li><li> <input
type='radio' id='dem-choice-21' value='21' name='dem_poll_4' /> <label
for='dem-choice-21'>C</label></li><li> <input
type='radio' id='dem-choice-22' value='22' name='dem_poll_4' /> <label
for='dem-choice-22'>Perl</label></li><li> <input
type='radio' id='dem-choice-23' value='23' name='dem_poll_4' /> <label
for='dem-choice-23'>Lisp</label></li><li> <input
type='radio' id='dem-choice-24' value='24' name='dem_poll_4' /> <label
for='dem-choice-24'>Fortran</label></li><li> <input
type='radio' id='dem-choice-25' value='25' name='dem_poll_4' /> <label
for='dem-choice-25'>Assembly</label></li><li> <input
type='radio' id='dem-choice-26' value='26' name='dem_poll_4' /> <label
for='dem-choice-26'>PHP</label></li><li> <input
type='radio' id='dem-choice-27' value='27' name='dem_poll_4' /> <label
for='dem-choice-27'>ASP / ASP.NET</label></li><li> <input
type='radio' id='dem-choice-28' value='28' name='dem_poll_4' /> <label
for='dem-choice-28'>C++</label></li><li> <input
type='radio' id='dem-choice-29' value='29' name='dem_poll_4' /> <label
for='dem-choice-29'>Python</label></li><li> <input
type='radio' id='dem-choice-30' value='30' name='dem_poll_4' /> <label
for='dem-choice-30'>Java</label></li><li> <input
type='radio' id='dem-choice-31' value='31' name='dem_poll_4' /> <label
for='dem-choice-31'>sh / csh / bash</label></li><li> <input
type='radio' id='dem-choice-32' value='32' name='dem_poll_4' /> <label
for='dem-choice-32'>Ruby</label></li><li> <input
type='radio' id='dem-choice-33' value='33' name='dem_poll_4' /> <label
for='dem-choice-33'>Other</label></li></ul> <input
type='hidden' name='dem_poll_id' value='4' /> <input
type='hidden' name='dem_action' value='vote' /> <input
type='submit' class='dem-vote-button' value='Vote' /> <a
href='/category/python/feed/?dem_action=view&amp;dem_poll_id=4' onclick='return dem_getVotes("http://pthree.org/wp-content/plugins/democracy/democracy.php?dem_action=view&amp;dem_poll_id=4", this)' rel='nofollow' class='dem-vote-link'>View Results</a></form></div></div></div> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/03/13/poll-first-programming-language/feed/</wfw:commentRss> <slash:comments>46</slash:comments> </item> <item><title>Some Spam Karma Automation in Python</title><link>http://pthree.org/2007/03/07/some-spam-karma-automation-in-python/</link> <comments>http://pthree.org/2007/03/07/some-spam-karma-automation-in-python/#comments</comments> <pubDate>Thu, 08 Mar 2007 01:58:18 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/03/07/some-spam-karma-automation-in-python/</guid> <description><![CDATA[Being a WordPress blogger, unfortunately, I have to wade through tons and tons of comment spam. As of current, I have caught 18,119 comment spams since the inception of this blog in September 2005. Thats more than 1,000 spams per month! That&#8217;s some serious spam. Problem is, though, that some of it is getting through [...]]]></description> <content:encoded><![CDATA[<p>Being a WordPress blogger, unfortunately, I have to wade through tons and tons of comment spam.  As of current, I have caught 18,119 comment spams since the inception of this blog in September 2005.  Thats more than 1,000 spams per month!  That&#8217;s some serious spam.</p><p>Problem is, though, that some of it is getting through the <a
href="http://unknowngenius.com/blog/wordpress/spam-karma/">Spam Karma 2</a> filters and tests (the single best spam protection for WordPress that you can get).  As such, I have had to strengthen the grip on some of the rules.  Unfortunately, though, this causes some legit comments to get flagged as spam.  If you have fallen victim to this, please contact me, with the date and the post of the comment, and I&#8217;ll retrieve it from the database.</p><p>Well, I can&#8217;t have this happening.  If it does, I need to catch it.  So, in an effort to learn Python better (seeing as though my job requires it), I set out to write a script that emails me the results of each days spam at the end of the day.  This proved to be a bit tricky, but thanks to the wonderful Python language, it was a lot of fun, and not too bad.  After the script is written, I throw it in a cron job that runs each night before the beginning of the new day.</p><p>First, let me mention that I know some of this code isn&#8217;t as efficient as it could be (if you&#8217;re an uber-Python hacker, please be gentle).  The problem is, I am a complete n00b to Python.  As such, I am sure that there are much better ways to achieve the results that I am looking for.  However, given the results, it runs quickly, and I&#8217;m proud of it.  At any event, <a
href="http://pthree.org/mysql/2007-03-07.csv">here is a sample output</a> and here is the code:</p><div
class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/>29<br
/>30<br
/>31<br
/>32<br
/>33<br
/>34<br
/>35<br
/>36<br
/>37<br
/>38<br
/>39<br
/>40<br
/>41<br
/>42<br
/>43<br
/>44<br
/>45<br
/>46<br
/>47<br
/>48<br
/>49<br
/>50<br
/>51<br
/>52<br
/>53<br
/>54<br
/>55<br
/>56<br
/>57<br
/>58<br
/>59<br
/>60<br
/>61<br
/>62<br
/>63<br
/>64<br
/>65<br
/>66<br
/>67<br
/></div></td><td><div
class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #808080; font-style: italic;">#!/usr/bin/python</span><br
/> <span
style="color: #808080; font-style: italic;">#</span><br
/> <span
style="color: #808080; font-style: italic;"># Script details:</span><br
/> <span
style="color: #808080; font-style: italic;"># Connects to your wordpress database, runs a query, returns the result, prints to a file, and emails you a notification</span><br
/> <span
style="color: #808080; font-style: italic;"># Spam Karma 2 provides the ability to email you spam comments under a certain karma threshold. &nbsp;Unfortunately, the</span><br
/> <span
style="color: #808080; font-style: italic;"># email sent is not in a good format, and the email can be fairly large in size. &nbsp;This script returns the comment spams</span><br
/> <span
style="color: #808080; font-style: italic;"># into a comma-separated file left on the server, and just emails you a notification to review the file.</span><br
/> <span
style="color: #808080; font-style: italic;">#</span><br
/> <span
style="color: #808080; font-style: italic;"># To take the best advantage of this script, place in a directory on your server away from the web document root, then</span><br
/> <span
style="color: #808080; font-style: italic;"># place into cron to run at the end of each day. &nbsp;The cron syntax:</span><br
/> <span
style="color: #808080; font-style: italic;">#</span><br
/> <span
style="color: #808080; font-style: italic;"># 59 23 * * * ~/sk_mysql.py</span><br
/> <span
style="color: #808080; font-style: italic;">#</span><br
/> <span
style="color: #808080; font-style: italic;"># Author: Aaron Toponce</span><br
/> <span
style="color: #808080; font-style: italic;"># Version: 0.3</span><br
/> <span
style="color: #808080; font-style: italic;"># License: GPL v. 2</span><br
/> <br
/> <span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">time</span>, MySQLdb, <span
style="color: #dc143c;">smtplib</span>, <span
style="color: #dc143c;">re</span>, <span
style="color: #dc143c;">csv</span><br
/> <span
style="color: #ff7700;font-weight:bold;">from</span> <span
style="color: #dc143c;">datetime</span> <span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">datetime</span><br
/> <br
/> <span
style="color: #808080; font-style: italic;"># Function declarations</span><br
/> <span
style="color: #ff7700;font-weight:bold;">def</span> strip_tags<span
style="color: black;">&#40;</span>value<span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; value = <span
style="color: #dc143c;">re</span>.<span
style="color: black;">sub</span><span
style="color: black;">&#40;</span>r<span
style="color: #483d8b;">'&lt; [^&gt;]*?&gt;'</span>, <span
style="color: #483d8b;">''</span>, value<span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;"># Returns the given HTML with all tags stripped</span><br
/> &nbsp; &nbsp; value = <span
style="color: #dc143c;">re</span>.<span
style="color: black;">sub</span><span
style="color: black;">&#40;</span>r<span
style="color: #483d8b;">'<span
style="color: #000099; font-weight: bold;">\[</span>[^<span
style="color: #000099; font-weight: bold;">\]</span>]*?<span
style="color: #000099; font-weight: bold;">\]</span>'</span>, <span
style="color: #483d8b;">''</span>, value<span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;"># Returns the given phpBB with all tags stripped</span><br
/> &nbsp; &nbsp; value = <span
style="color: #dc143c;">re</span>.<span
style="color: black;">sub</span><span
style="color: black;">&#40;</span>r<span
style="color: #483d8b;">'[<span
style="color: #000099; font-weight: bold;">\r</span><span
style="color: #000099; font-weight: bold;">\n</span>$]*?<span
style="color: #000099; font-weight: bold;">\n</span>'</span>,<span
style="color: #483d8b;">''</span>,value<span
style="color: black;">&#41;</span> <span
style="color: #808080; font-style: italic;"># Returns the given text with no newlines</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span> value<br
/> <br
/> <span
style="color: #808080; font-style: italic;"># Database connection and query</span><br
/> Con = MySQLdb.<span
style="color: black;">Connect</span><span
style="color: black;">&#40;</span>host=<span
style="color: #483d8b;">'127.0.0.1'</span>, port=<span
style="color: #ff4500;">3306</span>, <span
style="color: #dc143c;">user</span>=<span
style="color: #483d8b;">'root'</span>, passwd=<span
style="color: #483d8b;">'cs3210!'</span>, db=<span
style="color: #483d8b;">'wordpress'</span><span
style="color: black;">&#41;</span><br
/> Cursor = Con.<span
style="color: black;">cursor</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> sql = <span
style="color: #483d8b;">'SELECT c.comment_ID, s.karma, c.comment_author, c.comment_author_email, c.comment_date, c.comment_content FROM wp_comments c, wp_sk2_spams s WHERE c.comment_ID = s.comment_ID AND c.comment_date like <span
style="color: #000099; font-weight: bold;">\'</span>%%%s%%<span
style="color: #000099; font-weight: bold;">\'</span> AND c.comment_approved = <span
style="color: #000099; font-weight: bold;">\'</span>spam<span
style="color: #000099; font-weight: bold;">\'</span> AND s.karma &gt; -50 ORDER BY c.comment_ID'</span> &nbsp;<span
style="color: #66cc66;">%</span> <span
style="color: #dc143c;">time</span>.<span
style="color: black;">strftime</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'%Y-%m-%d'</span><span
style="color: black;">&#41;</span><br
/> Cursor.<span
style="color: black;">execute</span><span
style="color: black;">&#40;</span>sql<span
style="color: black;">&#41;</span><br
/> Results = Cursor.<span
style="color: black;">fetchall</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> Con.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> <br
/> <span
style="color: #808080; font-style: italic;"># Creating a modifiable list</span><br
/> NewList1 = <span
style="color: black;">&#91;</span><span
style="color: black;">&#93;</span><br
/> <span
style="color: #ff7700;font-weight:bold;">for</span> i <span
style="color: #ff7700;font-weight:bold;">in</span> Results:<br
/> &nbsp; &nbsp; NewList2 = <span
style="color: black;">&#91;</span><span
style="color: black;">&#93;</span><br
/> &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">for</span> j <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">range</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span>i<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> j == <span
style="color: #ff4500;">5</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NewList2.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>strip_tags<span
style="color: black;">&#40;</span>i<span
style="color: black;">&#91;</span>j<span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">elif</span> j == <span
style="color: #ff4500;">4</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NewList2.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>i<span
style="color: black;">&#91;</span>j<span
style="color: black;">&#93;</span>.<span
style="color: black;">strftime</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'%Y-%m-%d %H:%M:%S'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">else</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NewList2.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>i<span
style="color: black;">&#91;</span>j<span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span><br
/> &nbsp; &nbsp; NewList1.<span
style="color: black;">append</span><span
style="color: black;">&#40;</span>NewList2<span
style="color: black;">&#41;</span><br
/> <br
/> <span
style="color: #808080; font-style: italic;"># Creating a .csv file for mailing</span><br
/> now = <span
style="color: #dc143c;">datetime</span>.<span
style="color: black;">now</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span><br
/> outfile = <span
style="color: #483d8b;">'/var/www/pthree/mysql/%s.csv'</span> <span
style="color: #66cc66;">%</span> now.<span
style="color: black;">strftime</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'%Y-%m-%d'</span><span
style="color: black;">&#41;</span><br
/> output = <span
style="color: #dc143c;">csv</span>.<span
style="color: black;">writer</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span>outfile, <span
style="color: #483d8b;">'w'</span><span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span><br
/> <span
style="color: #ff7700;font-weight:bold;">for</span> item <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #dc143c;">csv</span>.<span
style="color: black;">reader</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'id,karma,author,email,date,content'</span><span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span>:<br
/> &nbsp; &nbsp; output.<span
style="color: black;">writerow</span><span
style="color: black;">&#40;</span>item<span
style="color: black;">&#41;</span><br
/> <span
style="color: #ff7700;font-weight:bold;">for</span> item <span
style="color: #ff7700;font-weight:bold;">in</span> NewList1:<br
/> &nbsp; &nbsp; output.<span
style="color: black;">writerow</span><span
style="color: black;">&#40;</span>item<span
style="color: black;">&#41;</span><br
/> <br
/> <span
style="color: #808080; font-style: italic;"># Email to the user</span><br
/> server = <span
style="color: #dc143c;">smtplib</span>.<span
style="color: black;">SMTP</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'localhost'</span><span
style="color: black;">&#41;</span><br
/> sender = <span
style="color: #483d8b;">'root@localhost'</span><br
/> reply_to = <span
style="color: #483d8b;">'root@localhost'</span><br
/> recipient = <span
style="color: #483d8b;">'aaron@localhost'</span><br
/> subject = <span
style="color: #483d8b;">'[Pthree.org] MySQL wordpress spam comments results'</span><br
/> headers = <span
style="color: #483d8b;">'From: %s<span
style="color: #000099; font-weight: bold;">\r</span><span
style="color: #000099; font-weight: bold;">\n</span>Reply-To: %s<span
style="color: #000099; font-weight: bold;">\r</span><span
style="color: #000099; font-weight: bold;">\n</span>To: %s<span
style="color: #000099; font-weight: bold;">\r</span><span
style="color: #000099; font-weight: bold;">\n</span>Subject: %s<span
style="color: #000099; font-weight: bold;">\r</span><span
style="color: #000099; font-weight: bold;">\n</span><span
style="color: #000099; font-weight: bold;">\r</span><span
style="color: #000099; font-weight: bold;">\n</span>'</span> <span
style="color: #66cc66;">%</span> <span
style="color: black;">&#40;</span>sender, reply_to, recipient, subject<span
style="color: black;">&#41;</span><br
/> text = <span
style="color: #483d8b;">'&quot;http://www.pthree.org/mysql/%s.csv&quot; is ready for review.'</span> <span
style="color: #66cc66;">%</span> now.<span
style="color: black;">strftime</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">'%Y-%m-%d'</span><span
style="color: black;">&#41;</span><br
/> message = headers + text<br
/> server.<span
style="color: black;">sendmail</span><span
style="color: black;">&#40;</span>sender, recipient, message<span
style="color: black;">&#41;</span></div></td></tr></tbody></table></div><p>UPDATE (11-Mar-2007): Realizing that Python has a CSV module, I added that into the code, and removed what I was typing up by hand.  First it removed 6 lines of code, plus, it seems to execute a bit quicker.  Cheers!</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/03/07/some-spam-karma-automation-in-python/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Python and the Horrendous Tab Character</title><link>http://pthree.org/2007/01/31/python-and-the-horrendous-tab-character/</link> <comments>http://pthree.org/2007/01/31/python-and-the-horrendous-tab-character/#comments</comments> <pubDate>Thu, 01 Feb 2007 05:08:17 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2007/01/31/python-and-the-horrendous-tab-character/</guid> <description><![CDATA[This thread was on reddit about a month or so ago, but I didn&#8217;t stumble on it until just recently. Basically, the thread talks about the dreaded tab character, and it&#8217;s effect on Python code using different editors. I have pasted the items I find most interesting. It&#8217;s amazing the philosophy changes that one can [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://mail.python.org/pipermail/python-list/2003-January/183758.html">This thread</a> was on reddit about a month or so ago, but I didn&#8217;t stumble on it until just recently.  Basically, the thread talks about the dreaded tab character, and it&#8217;s effect on Python code using different editors.  I have pasted the items I find most interesting.</p><p>It&#8217;s amazing the philosophy changes that one can have when stumbling upon something of this nature.  Moral of the story?  Don&#8217;t use the tab character when editing Python code.  Have your editor fill the tab with spaces to keep everything uniform.</p><blockquote><p>You know, the more I see the debate about spaces and tabs, the more puzzled I become that anyone still wants to use tabs. They *only* work nicely, IME, when a source file has only one author making changes, with only one editor. Give the file to another author (who uses a different editor) and boom! Nasty stuff happens, about 80-85% of the time.</p><p>&#8230; (snip) &#8230;</p><p>I cringe every time I see a tab character in source code that I have to edit, because I know indentation issues are going to be ugly. I have to guess what tabstop settings the original author used so that I can exactly mimic his view of the code, otherwise my own additions will look fine on my screen but ugly on his. And nobody ever seems to document what tabstop settings they use! A simple comment in the header of the file would make a lot of difference&#8230;</p><p>&#8230; (snip) &#8230;</p><p>I don&#8217;t like tab characters. I wish they would go away and that everyone would stop using them. They cause me far too many headaches, and I have never found a good use for them. Every time I have to edit a source file with tabs, I have to explicitly think about indentation levels&#8230;</p><p>&#8230; (snip) &#8230;</p><p>Lest anyone get me wrong, I am in no way opposed to using the Tab *key* on your keyboard to indent lines. That&#8217;s a great use for it. My Vim has the &#8220;expandtab&#8221;, &#8220;smarttab&#8221;, and &#8220;autoindent&#8221; options turned on, which<br
/> makes my Tab key practically into a Do-What-I-Mean key. And it always inserts spaces (ASCII 0&#215;20), not tabs (ASCII 0&#215;09). The Tab key is good. But tab characters are bad.</p><p>Tab characters have no place in modern source code. Their use is considered harmful and should be strictly avoided.</p></blockquote> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2007/01/31/python-and-the-horrendous-tab-character/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>&#8220;We&#8217;re Going In A New Direction&#8221;</title><link>http://pthree.org/2006/12/16/were-going-in-a-new-direction/</link> <comments>http://pthree.org/2006/12/16/were-going-in-a-new-direction/#comments</comments> <pubDate>Sat, 16 Dec 2006 18:30:06 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2006/12/16/were-going-in-a-new-direction/</guid> <description><![CDATA[Looking back at the archives of this blog, it&#8217;s overall theme and vibrancy has changed throughout the months, and honestly, I don&#8217;t like where it&#8217;s landed. So, this blog is taking a new turn and revisiting it&#8217;s roots. The initial reason this blog was put up, back in September 2005, was to provide an archive [...]]]></description> <content:encoded><![CDATA[<p>Looking back at the archives of this blog, it&#8217;s overall theme and vibrancy has changed throughout the months, and honestly, I don&#8217;t like where it&#8217;s landed.  So, this blog is taking a new turn and revisiting it&#8217;s roots.</p><p>The initial reason this blog was put up, back in September 2005, was to provide an archive to <strong>P</strong>erl, <strong>P</strong>ython and <strong>P</strong>HP (thus, the &#8220;Pthree&#8221;).   It was to take a look at those languages from a scripting point of view.  Being fairly new in all 3, I thought this would provide great tutorials to those languages for those who are also new to those languages.  More importantly, it was to keep me working with those languages to improve my programming prowess.  Well, it took a different direction, and it didn&#8217;t take long either.</p><p>I&#8217;d like to go back to what this blog was initially designed for, and provide my experience in looking at those three languages.  However, I admit that I am intimidated by the expertise of those out in the &#8220;real world&#8221;.  I fear that my experiences will be ridiculed by those of greater experience and knowledge.  However, I&#8217;m going to take a step in the dark, and brave the journey.  Starting tomorrow, the overall content of the blog will be in respects to Perl, Python and PHP with touches of Ruby and Bash all on GNU/Linux systems, primarily Ubuntu.  Of course, comments are always open, and I encourage those of higher knowledge to help me identify what to fix or improve with my code.  I plan to have a post up per day at a minimum.</p><p>This means that I&#8217;ll need a good syntax highlighter for my blog.  I have been using <a
href="http://qbnz.com/highlighter/">Geshi</a>, and like it so far, but it needs a little tweaking.  If anyone knows of a solid syntax highlighter, which cover the above languages, for the WordPress platform, I&#8217;m all ears.</p><p>I hope you enjoy the direction this blog takes.</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2006/12/16/were-going-in-a-new-direction/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Python Fibonacci Sequence</title><link>http://pthree.org/2006/07/20/python-fibonacci-sequence/</link> <comments>http://pthree.org/2006/07/20/python-fibonacci-sequence/#comments</comments> <pubDate>Fri, 21 Jul 2006 03:33:05 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2006/07/20/python-fibonacci-sequence/</guid> <description><![CDATA[So, in case you were wondering, Pthree stands for Perl, Python and PHP. I know, I know. I haven&#8217;t posted hardly anything at all. So I make a feeble attempt at starting now. Everything that I have been doing at work is supposed to be written in Python, but instead, I have been writing everything [...]]]></description> <content:encoded><![CDATA[<p>So, in case you were wondering, Pthree stands for <u>P</u>erl, <u>P</u>ython and <u>P</u>HP.  I know, I know.  I haven&#8217;t posted hardly anything at all.  So I make a feeble attempt at starting now.</p><p>Everything that I have been doing at work is supposed to be written in Python, but instead, I have been writing everything in Bash.  Well, tonight, I turn over a new leaf.  It&#8217;s time to convert many of Bash scripts into Python.</p><p>Problem.</p><p>I don&#8217;t know Python.</p><p>So, I have decided to start getting serious.  I have literally jumped leaps and bounds these past few months learning Bash, and while I am far from perfect, I think I should begin applying that energy to Python.  So, here we go.</p><p>First, I went to http://doc.python.org, and began reading.  It wasn&#8217;t long, but I stumbled upon writing a Fibonacci sequence in Python.  I thought, sure, why not?</p><div
class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/></div></td><td><div
class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">a,b=<span
style="color: #ff4500;">0</span>,<span
style="color: #ff4500;">1</span><br
/> <span
style="color: #ff7700;font-weight:bold;">while</span> b <span
style="color: #66cc66;">&lt;</span> <span
style="color: #ff4500;">10</span>:<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> b<br
/> &nbsp; &nbsp; &nbsp; &nbsp; a, b = b, a+b</div></td></tr></tbody></table></div><p>Not bad.  Only 4 simple lines, and already, I feel like I am getting the hang of the language.  The only problem, is, if I don&#8217;t have any exercises to run through, then I won&#8217;t have any motivation to learn the language, and if I spend time learning the language, then by the time it will be time to convert everything over, the window will have passed, and I will feel like I wasted time.</p><p>Anyway, I am enjoying myself, and I guess that is what is most important.  Maybe in the next post, I&#8217;ll post a solution in Python to the traveling salesman problem. <img
src='http://pthree.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </code></p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2006/07/20/python-fibonacci-sequence/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Gmail-notify as an Example of the Power of Open Source Software</title><link>http://pthree.org/2006/04/08/gmail-notify-as-an-example-of-the-power-of-open-source-software/</link> <comments>http://pthree.org/2006/04/08/gmail-notify-as-an-example-of-the-power-of-open-source-software/#comments</comments> <pubDate>Sun, 09 Apr 2006 01:13:50 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[OSS]]></category> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2006/04/08/gmail-notify-as-an-example-of-the-power-of-open-source-software/</guid> <description><![CDATA[The example I am about to expense is why Open Source Software (OSS) is so powerful. A friend of mine was reading my blog, and cheking out my Firefox extension list (that I still need to update badly). He noticed that I use the gmail notifier extension for Firefox rather than the gmail-notify package for [...]]]></description> <content:encoded><![CDATA[<p>The example I am about to expense is why Open Source Software (OSS) is so powerful.</p><p>A friend of mine was reading my blog, and cheking out my Firefox extension list (that I still need to update badly).  He noticed that I use the gmail notifier extension for Firefox rather than the gmail-notify package for Ubuntu that sits in the notification area.  I hadn&#8217;t tried the gmail-notify package, so I thought I would give it a try.  What the heck.</p><blockquote><p>sudo apt-get install gmail-notify</p></blockquote><div
align="left">After installing and launching, the first thing I noticed was how horrible the icon was.  Take a look:</div><p
align="center"><img
width="64" height="64" alt="gmail-notify-icon1.png" id="image112" src="http://www.pthree.org/wp-content/uploads/2006/04/gmail-notify-icon1.png" /></p><p
align="left">Ouch.  Whomever designed that icon needed a little help.  Anyway, the first thing on the agenda was either to redesign the icon, or find one the open commons, and use that. So, I needed to know where the application was looking for the images, so I could make the necessary changes.  This was as easy as doing a simple:</p><blockquote><p
align="left">whereis gmail-notify</p></blockquote><p
align="left">I was lead to the /usr/lib/gmail-notify directory where the python files are stored for the application.  Looking at  GmailConfig.py, I found the icons were stored in /usr/share/apps/gmail-notify.  Duh.  I should&#8217;ve looked there first.  Upon discovery of the directory, there were 6 images that needed to be changed.  Four red and two blue.</p><p
align="left">Now to change the icons.  Redesigning the current icon made it look worse, so I went on the hunt to find a Gmail icon that was much more professional looking.  Unfortunately, all &#8220;official&#8221; Gmail icons are copyrighted and I don&#8217;t know if they can be used freely for this purpose or not.  However, for the meantime, I took the Gmail logo, grabbed the &#8220;M&#8221; and worked with it as necessary.  I&#8217;ll fire off an email to Gmail and see if this is okay.  If it is, I&#8217;ll post a tar.gz for download.</p><p
align="left">So, in Gimp, I copied the size and name of each image just as it is in the /usr/share/apps/gmail-notify directory.  I made all the changes as necessary.  Then, the big test.  I shutdown the application, made a backup of the current images, and copied my new images into the directory, then fired up the application.  Much better.  Here&#8217;s the same icon, but more professional looking:</p><p
align="center"><img
width="64" height="64" alt="gmail-notify-icon.png" id="image113" src="http://www.pthree.org/wp-content/uploads/2006/04/gmail-notify-icon.png" /></p><p
align="left">How&#8217;s that?  Better?  I think so.  Shadow and all.  The only thing left was to change the icon that the launcher in the menu looks at.  That was easy by just pointing it from /usr/share/pixmaps to /usr/share/apps/gmail-notify.</p><p
align="left">Why is this so cool?  Because this is the exact reason OSS is so powerful.  Proprietary applications may or may not give you this ability, but OSS always does.  Think of it, for a second.  I didn&#8217;t like the way an application looked, and I decided to do something about it.  I didn&#8217;t do any coding, any compiling, hacking or tweaking.  All I did was change the images.  Now, the application looks the way I want it to look.  Try, just try, doing that with, say, Microsoft Office.  Good luck.</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2006/04/08/gmail-notify-as-an-example-of-the-power-of-open-source-software/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Sudoku Mania</title><link>http://pthree.org/2006/03/05/sudoku-mania/</link> <comments>http://pthree.org/2006/03/05/sudoku-mania/#comments</comments> <pubDate>Sun, 05 Mar 2006 16:29:00 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2006/03/05/sudoku-mania/</guid> <description><![CDATA[My wife and I were in Barnes and Noble last night. Of course, the first place I go is the magazine rack to pick up the latest Linux Format issue, then over to the prgramming section to get a new computer book. I was completely torn between a number of different computer books, but finally [...]]]></description> <content:encoded><![CDATA[<p><img
hspace="15" align="right" alt="SudokuBan Screenshot" src="http://www.pthree.org/wp-content/uploads/2006/03/SudokuBan.png" />My wife and I were in Barnes and Noble last night.  Of course, the first place I go is the magazine rack to pick up the latest <a
target="_blank" title="Linux Format Magazine" href="http://www.linuxformat.co.uk">Linux Format</a> issue, then over to the prgramming section to get a new computer book.  I was completely torn between a number of different computer books, but finally settled on <span
style="font-style: italic">Wicked Cool Shell Scripts</span> by Dave Taylor.  You just can&#8217;t get enough of shell scripting, even if it is in csh and not bash.</p><p>Anyway, while browsing around, I couldn&#8217;t help but notice Sudoku books everywhere!  Gads, they were on tables, end-caps, at the cash register- literally everywhere.  All different kinds of books too.  In fact, while at the cash register, paying for our books, Barnes and Noble was even selling a Sudoku game!</p><p>Now mind you, I am not foreign to the game by any means of the word.  I have Sudoku books at home, and a page-a-day Sudoku calendar at work.  Now you can&#8217;t open a newspaper without seeing Sudoku puzzles.   Which is fine I guess.  For me, I got into this game about 7-8 months ago, before all the craze and hype.  But here&#8217;s what&#8217;s funny:  I open my newly purchased Linux Format magazine, and what is on page 98, but a review of 3 Sudoku solvers for Linux.  It&#8217;s a global takeover.  Resistance is futile.</p><p>So, I read the article.  Linux Format was offering 500 to whomever could program the best Sudoku solvers, with 300 going to 1st place, 150 to 2nd place, and 50 to 3rd place.  Heck, if I would&#8217;ve read the article last month, or at least paid attention to it, I could&#8217;ve submitted a solver in Python.  Apparently, the 1st place winner <a
target="_blank" title="SudoduBan" href="http://sourceforge.net/projects/sudokuban/">SudokuBan</a>, beat me to it.  Because it is written in Python, it is completely platform independant.</p><p>Well, I went ahead, and downloaded SudokuBan, and began playing.  It is a very polished program!  Looking at the code (yes, it is OSS), it&#8217;s clean and can be easily maintained, even if it isn&#8217;t commented (which is a bit bothersome).  Launching the program just means firing up the Python interpreter, and off you go!  I made a shortcut in my games menu.  The command for launching the program is</p><div
class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/></div></td><td><div
class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python sudokuban.py</div></td></tr></tbody></table></div><p>.</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2006/03/05/sudoku-mania/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Spellbound, The Workforce and Firefox Market Share</title><link>http://pthree.org/2006/01/05/spellbound-the-workforce-and-firefox-market-share/</link> <comments>http://pthree.org/2006/01/05/spellbound-the-workforce-and-firefox-market-share/#comments</comments> <pubDate>Thu, 05 Jan 2006 14:17:29 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Firefox]]></category> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org/2006/01/05/spellbound-the-workforce-and-firefox-market-share/</guid> <description><![CDATA[Being a serious Firefox junkie, I was testing out a couple new extensions when a fellow coworker came by my desk and asked me what I was doing.&#160; He then asked what extensions I find useful, at which point I began showing off Spellbound.&#160; Immediately, he jumped out of his seat and couldn&#8217;t believe his [...]]]></description> <content:encoded><![CDATA[<p>Being a serious Firefox junkie, I was testing out a couple new extensions when a fellow coworker came by my desk and asked me what I was doing.&nbsp; He then asked what extensions I find useful, at which point I began showing off Spellbound.&nbsp; Immediately, he jumped out of his seat and couldn&#8217;t believe his eyes.&nbsp; He then asked if he could use my browser for a second, and navigated to an intranet site that he had been developing.</p><p>His intranet site is a Python powered application that pulls in large amounts of text for the data processing department to clean up.&nbsp; Being in market research, and talking with thousands of people on the phone through our surveys, we ask people how they feel about certain topics, at which point, they tell the interviewer as he/she types in their response.&nbsp; We end up with long text files of responses which include grammar, punctuation and spelling problems, not to mention foul language and slang.&nbsp; It is their job to clean up this text as best they can, removing the foul language, correcting the grammar, punctuation and fixing the spelling.&nbsp; Well, with this extension, the spelling aspect just got a whole lot easier.</p><p>It wasn&#8217;t long before the IT manager wanted to see me and wanted to know about the extension and how it worked.&nbsp; Once I showed it off, he was convinced, because for the past several months, he had been trying to program his own spell check program to implement into this Python application, and without much luck.&nbsp; Why reinvent the wheel?&nbsp; A tool has already been constructed, that works wonderfully, and virtually bug-free.&nbsp; Needless to say, the spell check program has now been abandoned for the new extension.</p><p>Which brings me to my next point.&nbsp; Now that this extension has been exposed, the workplace, specifically the Data Processing department, is very interested in the browser.&nbsp; In fact, they will need the browser if they want to make their jobs much less painful.&nbsp; So I spent an hour or two showing off the extension to the DP department, downloading and installing Firefox and the extension on their computers.&nbsp; Now, except for maybe one or two people, our entire workplace has been converted to Firefox.&nbsp; That may only be approximately 30 people, but, it&#8217;s about 20 more than before, and that brings the Firefox market share all that much closer to beating out Internet Explorer.</p><p>It then came to me, that it is the workplace that is the ultimate factor in determining whether or not Firefox will in fact beat out IE.&nbsp; I would dare say, although I have nothing to back this up, that the workplace makes up more than 60% of Internet activity, and if that is the case, then targeting corporations and businesses would be more successful for Firefox, than targeting individual users.&nbsp; With Firefox at the 10% mark, and growing, I would think that this would be the direction to go.&nbsp; Also, by converting the employees at work means converting them at home, which poses as a double threat.&nbsp; Lastly, the ultimate success of any service or product is word-of-mouth.&nbsp; When you like a movie you just saw, what do you do?&nbsp; You tell your friends about it, and they go and see it.&nbsp; It has been the secret weapon of anything successful.&nbsp; Word-of-mouth beats out direct advertising 10 to 1.</p><p>Go Firefox!</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2006/01/05/spellbound-the-workforce-and-firefox-market-share/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Python File Handling: Part I</title><link>http://pthree.org/2005/11/09/perl-file-handling-part-i/</link> <comments>http://pthree.org/2005/11/09/perl-file-handling-part-i/#comments</comments> <pubDate>Thu, 10 Nov 2005 05:28:00 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org:8080/2005/11/09/perl-file-handling-part-i/</guid> <description><![CDATA[Because learning a new language takes a little bit of time, I haven&#8217;t had much to blog about here. I would like to blog more often, but I feel that I need to get a hang of the language before I can venture too far into blogging about it. I woudn&#8217;t have much to blog [...]]]></description> <content:encoded><![CDATA[<p>Because learning a new language takes a little bit of time, I haven&#8217;t had much to blog about here.  I would like to blog more often, but I feel that I need to get a hang of the language before I can venture too far into blogging about it.  I woudn&#8217;t have much to blog about, and would just be wasting your time as a reader.  However, I do also need to remember that while the focus is Perl, Python and PHP, I <em>can</em> also blog about Linux news and other geek related stuff.  I guess it is just habit to put it on my <a
href="http://www.toponcefamily.com/blogs/aaron">other blog</a>.</p><p>So, where to begin?  How about file handling?  Remember, I am working on converting QuickBasic files at work into Python files for all of our data manipulation and setup.  This is something that I have taken on myself, and can only do when things are slow at work.  This is one of those last priorities things when everuthing else is out of the way.  So far, all I have been able to accomplish is successfully open a text file for reading, create a text file for writing, copy data from the read-only file, and paste it to the newly created file.  Here is what I have so far:</p><pre>
fout = open('sample.fin', 'w')
fin = open('sample.txt', 'r')

while 1:
    line = fin.readline()   # Read line by line
    if not line:            # If EOF reached
        break               # exit program
    fout.writelines(line)   # write each line to new file
</pre><p>As you can see, it is all to plain, and not much is going on.  The while loop will continue processing until we reach the EOF marker.  When we do, exit the loop, thus also exiting the program.  Not very exciting.  Certainly, nothing advanced.  I do find it odd, however, that most programming books wait until the middle chapters to cover file handling.  Just an observation.</p><p>While this Python program does nothing but copy one file into another, it will grow as I get more time to work on it, and learn more about the language.  Eventually, it will be advanced enough to replace the current QuickBasic program that we currently use.  The QB program opens a file, looks at a certain row and column in that file, copies a certain length of text and pastes it to a specific location in the newly created file.  While this sounds easy, in the QB program, there are actually several variables to take into account, and the text locations have to be exact.  Also, the QB program is generated source code from another fairly large QB program, that will also be converted.  Converting the larger QB program will take a great deal of time, due to the size of the source, and figuring out what everything does exactly.</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2005/11/09/perl-file-handling-part-i/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Number Arrays in Python</title><link>http://pthree.org/2005/10/17/number-arrays-in-python/</link> <comments>http://pthree.org/2005/10/17/number-arrays-in-python/#comments</comments> <pubDate>Tue, 18 Oct 2005 01:30:45 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org:8080/2005/10/17/number-arrays-in-python/</guid> <description><![CDATA[Being new to Python and completely discovering the language has been quite the experience. I find it realy cool that it is whitespace dependant, and that indenting matters. This helps reduce alot of the &#8220;chatter&#8221; that is common in C/C++, Java, Perl, etc. This week, I decided, among other things, to write a game in [...]]]></description> <content:encoded><![CDATA[<p>Being new to Python and completely discovering the language has been quite the experience.  I find it realy cool that it is whitespace dependant, and that indenting <em>matters</em>.  This helps reduce alot of the &#8220;chatter&#8221; that is common in C/C++, Java, Perl, etc.</p><p>This week, I decided, among other things, to write a game in Python.  A game called <a
href="http://www.sudoku.com">Su Doku</a>.  I got hooked on it over the weekend, and I have done almost 100 puzzles in the past few days.  So I thought it would be cool to program the game.  Heck, what an exercise, eh?</p><p>First things first, write the pseudocode.  I figure the steps for creating such a game would be as follows:</p><ul><li>Only the numbers 1 through 9 are possible</li><li>Create the &#8220;rules&#8221; of the puzzle in that no number can be in the same row, column or 3&#215;3 quadrant</li><li>Build the 9&#215;9 array using the &#8220;rules&#8221; established</li><li>Create &#8220;patterns&#8221; using 25-30 of the numbers that make up the solution</li><li>Show only the pattern of numbers to the user</li><li>Check the users solution against the 9&#215;9 array solution for accuracy</li></ul><p>Fairly straight forward.  I am sure that there are probably some steps missing, and no doubt it can get more detailed, but the skeleton is there.  So, I begin setting up the array using the standard list array in the Python language.  It comes as a surprise to me, however, that Python treats every element in the list array as strings.  If any integer, long or float arithmetic is done on that element, the element is converted to a number, and the manipulation is done &#8220;under the hood&#8221;.  In other words, speed is compromised, and there is no numeric array built into the Python core.</p><p>Luckily, for Python developers, the language supports modules, much like the import statement in Java, so numeric arrays are supported.  However, I did find it a little frustrating that it is not supported in the core.  Although the math will not be complex, and in my situation, small, speed won&#8217;t be an issue.  But still, it&#8217;s the principle that matters.</p><p>Ugh.</p> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2005/10/17/number-arrays-in-python/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>One Solid Python IDE</title><link>http://pthree.org/2005/10/13/one-solid-python-ide/</link> <comments>http://pthree.org/2005/10/13/one-solid-python-ide/#comments</comments> <pubDate>Fri, 14 Oct 2005 04:48:00 +0000</pubDate> <dc:creator>Aaron</dc:creator> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.pthree.org:8080/?p=13</guid> <description><![CDATA[A house contractor utilizes the best tools of his trade. As does a plumber, carpenter, mechanic and technician. So should developers. It is easy as a developer to get caught into using simple tools to get the job done. For example, when editing a web page, a simple text editor gets the job done just [...]]]></description> <content:encoded><![CDATA[<p>A house contractor utilizes the best tools of his trade.  As does a plumber, carpenter, mechanic and technician.  So should developers.</p><p>It is easy as a developer to get caught into using simple tools to get the job done.  For example, when editing a web page, a simple text editor gets the job done just as good as any IDE.  It might just take a little longer to accomplish the task.  Sure, you don&#8217;t have all of your project files at your fingertips.  You don&#8217;t have intelligent word completion.  You don&#8217;t have compiling and debugging tools.  But, those can get in the way when you only need to edit a few lines of code.  Why spend all the time bringing up the debugger, intelligent completion and project files, which could take a long time depending on the size of your project, when you can make your edit just as fast?</p><p>However, with that said, software development and programming just would not be where it is today had it not been for integrated deveopment environments (IDEs).  IDEs make development as painless as possible.  They provide run-time environments, compilers, debuggers, project information and much much more.  IDEs just make coding pleasurable.</p><p>Okay.  That&#8217;s great and all, but what about Python?  There are great IDEs for C/C++, Java, BASIC, and even for web aps.  But is there really anything solid out there for Python?  Don&#8217;t you either have to create an executable script, or parse everything through the interpreter?  For those of you who code in Python, know this isn&#8217;t the case.  Welcome <a
href="http://www.stani.be/python/spe">Stani&#8217;s Python Editor (SPE)</a>.</p><p>Just looking at SPE, without even getting into the code, it looks good.  It is cross platform compatible (Linux, Mac, Windows), clean, loads quite fast and it&#8217;s just plain sexy.  Digging into the code, we find managing projects, a compiler and debugger, command line interpreter, intelligent word completion, project building, etc.  Pretty much everything we established what makes a good IDE.  And it&#8217;s not even a first release!  And, it&#8217;s free.  Free as in beer, free as in freedom.  Free.</p><p>Sweet.</p><div
style="text-align:center;"><a
href="http://www.pthree.org:8080/wp-content/SPE_Linux.png"><img
border="0" src='http://www.pthree.org:8080/wp-content/SPE_Linux_Thumb.png' alt='Stani\&#39;s Python Editor' /></a></div> ]]></content:encoded> <wfw:commentRss>http://pthree.org/2005/10/13/one-solid-python-ide/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)

Served from: pthree.org @ 2010-09-03 00:11:21 -->