<?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>The Pythonist</title>
	<atom:link href="http://thepythonist.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://thepythonist.com</link>
	<description>While True: python()</description>
	<lastBuildDate>Sun, 06 Dec 2009 05:16:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python and Whitespace</title>
		<link>http://thepythonist.com/blog/python-and-whitespace/</link>
		<comments>http://thepythonist.com/blog/python-and-whitespace/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 00:28:57 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Editorials]]></category>
		<category><![CDATA[braces]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://thepythonist.com/?p=24</guid>
		<description><![CDATA[A friend of mine (who shall remain unnamed) recently blasted Python and the way it uses whitespace, saying that whitespace should not matter in code. He seems to feel that braces are a much better way of denoting a hierarchy, and frankly I think he&#8217;s insane for it. Firstly, how many people actually completely leave [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine (who shall remain unnamed) recently blasted Python and the way it uses whitespace, saying that whitespace should not matter in code. He seems to feel that braces are a much better way of denoting a hierarchy, and frankly I think he&#8217;s insane for it. Firstly, how many people actually completely leave out whitespace when writing in Java or C# for example? Most IDEs put it in for you, and reading the code would be a nightmare without it. Secondly, have you ever had a complicated series of conditional statements or loops or combination thereof (we&#8217;ll give you the benefit of the doubt and assume there wasn&#8217;t an easier, better way to write that block) in a language that uses braces? Did you have fun making all the braces balanced, struggling to find where that last one should go? If you did, you may be a masochist, or you may just like the look of braces, and who am I to judge you for that? If you didn&#8217;t, give Python a try and enter a blissful world where {} are only used for dictionaries (ok so they may have other uses that I just don&#8217;t recall or haven&#8217;t learned yet, but that&#8217;s not the point). Your code will be just a smidgen easier to read and you&#8217;ll never have to balance braces again! Now if only they could do something about parentheses and brackets with lists and tuples&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://thepythonist.com/blog/python-and-whitespace/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RegularExpressions + Python</title>
		<link>http://thepythonist.com/blog/regularexpressions-python/</link>
		<comments>http://thepythonist.com/blog/regularexpressions-python/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 05:19:58 +0000</pubDate>
		<dc:creator>Kenneth Reitz</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thepythonist.com/?p=22</guid>
		<description><![CDATA[Have you ever needed to parse through large amounts of text looking for a specific pattern? Patterns like “one capital letter followed by three numbers” or “dd/mm/yyyy”? This is known as Pattern Matching. Regular Expressions allow easy syntax for pattern matching, and is an invaluable skill to add to one’s toolkit, no matter what your [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever needed to parse through large amounts of text looking for a specific pattern? Patterns like “one capital letter followed by three numbers” or “dd/mm/yyyy”? This is known as Pattern Matching. Regular Expressions allow easy syntax for pattern matching, and is an invaluable skill to add to one’s toolkit, no matter what your area of expertise/practice is. Whether you’re writing a Compiler, Form Validator, Text Editor, Django Project, or Language Translator, Regular Expressions will always prove to be invaluable. Here is a very basic overview of some syntax: ‘\d’ represents a digit. ‘\s’ represents whitespace. ‘.’ represents any character. If you have worked with Python for very long, you are probably already familiar with the concept. Take a look at the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>“Rounded = <span style="color: #66cc66;">%</span>05d” <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">42</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><span id="more-22"></span><br />
This makes sure that the digit printed has 5 digits, and will automatically add 0’s to compensate. If you understand this concept, then you shouldn’t have a problem. Perl-style Regular Expressions are a very widely-accepted implementation, and Python has built in support for this mini-language! It’s easily accessible, so let’s get started. The included ‘re’ module will give us everything we need to get started:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span></pre></td></tr></table></div>

<p>Lets give our new module a try! It will enable you to do anything you could ever want with regular expressions. Here’s a quick example of some basic use.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
string0 = <span style="color: #483d8b;">'Kenneth Reitz is a cool guy!'</span>
regExp = r’kenneth<span style="color: black;">&#91;</span>- <span style="color: black;">&#93;</span><span style="color: #66cc66;">?</span>reitz’
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>regExp, string0, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span>:
   <span style="color: #ff7700;font-weight:bold;">print</span> “<span style="color: #008000;">True</span>” 
<span style="color: #ff7700;font-weight:bold;">else</span>: 
   <span style="color: #ff7700;font-weight:bold;">print</span> “<span style="color: #008000;">False</span>”</pre></td></tr></table></div>

<p>This script takes the string ‘Kenneth Reitz is a cool guy’, and searches for ‘kenneth reitz’ inside of it. If ‘kenneth reitz’ is found within string0 (re.match compares the expression with the string), the script will print “True”, if not, it will print “False”. Additional parameters can be passed to the re.match function when needed. Note the ‘re.IGNORECASE’ flag used here – This tells the function be case-insensitive. Once you master the regular expression syntax, you’ll realize how truly powerful they can be. The options become limitless and the usefulness becomes undeniable. Here’s another example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span> 
&nbsp;
string0 = <span style="color: #483d8b;">'10.03.1988'</span>
regExp = r<span style="color: #483d8b;">'^<span style="color: #000099; font-weight: bold;">\d</span><span style="color: #000099; font-weight: bold;">\d</span>[./]<span style="color: #000099; font-weight: bold;">\d</span><span style="color: #000099; font-weight: bold;">\d</span>[./]<span style="color: #000099; font-weight: bold;">\d</span><span style="color: #000099; font-weight: bold;">\d</span><span style="color: #000099; font-weight: bold;">\d</span><span style="color: #000099; font-weight: bold;">\d</span>?$'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>regExp, string0<span style="color: black;">&#41;</span>:
   <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'True'</span>
<span style="color: #ff7700;font-weight:bold;">else</span>: 
   <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'False/</span></pre></td></tr></table></div>

<p>When run, this script prints out “True”. If we were to change string0 to ‘10.03.88’, it would print “False”. Simple, isn’t it? Now, while a True/False return could be useful in certain applications (i.e. form validation), most of the time, we’re going to want to have a bit more information in order for our checks to be useful. We can tell Python to show us the data that matches our query. To do this, we’re going to have to break our expression up into different groups. In the date we have defined, there are three obvious groups we could separate this into: the day, month, and year. While defining a Regular Expression, you can use parentheses ‘()’ to define groups:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">regExp = r’^<span style="color: black;">&#40;</span>\d\d<span style="color: black;">&#41;</span>././$’</pre></td></tr></table></div>

<p>This separates our expression into 3 separate groups. Python also supports turning a Regular Expression string into an heavily-supported object with the re.compile() function. Once you define a string as a Regular Expression object, you can use the built in methods to preform powerful parsing. Now we can ask python what is in those groups:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> restring0 = ‘10.03.1988’
regExp = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>‘^<span style="color: black;">&#40;</span>\d\d<span style="color: black;">&#41;</span>././$’<span style="color: black;">&#41;</span>
regExpMatches = regExp.<span style="color: black;">match</span><span style="color: black;">&#40;</span>string0<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>regExp, string0<span style="color: black;">&#41;</span>:
   <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>“Day: <span style="color: #66cc66;">%</span>s\nMonth: <span style="color: #66cc66;">%</span>s\nYear: <span style="color: #66cc66;">%</span>s” <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>regExpMatches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>, \
     regExpMatches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>, regExpMatches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
   <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>“Invalid Date.”<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>When executed, this script parses through our validated date, breaks it down into groups, and prints the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="sh" style="font-family:monospace;">&gt; Day: 10
&gt; Month: 03
&gt; Year: 1988</pre></td></tr></table></div>

<p>The possibilities are limitless! Here’s a quick run-down of the re module’s functions, strait from the Python documentation for reference:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="sh" style="font-family:monospace;">   match: Match a regular expression pattern to the beginning of a string.
   search: Search a string for the presence of a pattern.
   sub: Substitute occurrences of a pattern found in a string
   subn: Same as sub, but also return the number of substitutions made.
   split: Split a string by the occurrences of a pattern.
   findall: Find all occurrences of a pattern in a string.
   compile: Compile a pattern into a RegexObject.
   purge: Clear the regular expression cache.
   escape: Backslash all non-alphanumerics in a string.</pre></td></tr></table></div>

<p>Remember, you can always type help(re) (after importing the re module) into the Python interpret to take a quick look at the module’s built-in documentation. Good luck and happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://thepythonist.com/blog/regularexpressions-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>QuickRange</title>
		<link>http://thepythonist.com/blog/quickrange/</link>
		<comments>http://thepythonist.com/blog/quickrange/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 01:51:51 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[generators]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[range]]></category>

		<guid isPermaLink="false">http://thepythonist.com/?p=12</guid>
		<description><![CDATA[We are all familiar with Python&#8217;s built-in range() function. Many of us use it all the time, especially in for loops, and especially if you come from a language that uses the

1
for &#40;i=0;i&#60;j;i++&#41;&#123;&#125;

 syntax, but few of us realize just how inefficient range() is until we try to use it with really large numbers. When [...]]]></description>
			<content:encoded><![CDATA[<p>We are all familiar with Python&#8217;s built-in range() function. Many of us use it all the time, especially in for loops, and especially if you come from a language that uses the</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>j<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> syntax, but few of us realize just how inefficient range() is until we try to use it with really large numbers. When you call range(x), the interpreter is actually creating a list of numbers from 0 to x-1. That&#8217;s fine for range(10), but what if you have to go to range(1000000000000000)? Go try it, let me know how long it takes to start running your loop. A better solution is to use a generator, as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> quickrange<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
   i=<span style="color: #ff4500;">0</span>
   <span style="color: #ff7700;font-weight:bold;">while</span> i<span style="color: #66cc66;">&lt;</span>x:
      <span style="color: #ff7700;font-weight:bold;">yield</span> i
      i+=<span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> quickrange<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:
   <span style="color: #ff7700;font-weight:bold;">print</span> i
<span style="color: #808080; font-style: italic;">#will print 0-9</span></pre></td></tr></table></div>

<p>The benefit of using a generator is that you don&#8217;t need to store an arbitrarily long list in memory (which can take up quite a lot of memory if your range is large enough). You also don&#8217;t have the delay in starting your loop while Python makes a list of x elements. The sample above is quite simple, of course, and doesn&#8217;t fully replicate the abilities of range(), but one could easily add some logic to handle start and (potentially negative) step values.</p>
]]></content:encoded>
			<wfw:commentRss>http://thepythonist.com/blog/quickrange/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FizzBuzz in Python</title>
		<link>http://thepythonist.com/blog/fizzbuzz-in-python/</link>
		<comments>http://thepythonist.com/blog/fizzbuzz-in-python/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 16:47:17 +0000</pubDate>
		<dc:creator>Kenneth Reitz</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thepythonist.com/?p=9</guid>
		<description><![CDATA[
1
2
3
4
5
6
7
8
9
10
11
12
def fizzbuzz&#40;&#41;:
   for i in range&#40;1,101&#41;:
     if not i % 15:
       print &#34;FizzBuzz&#34;
     elif not i % 3:
       print &#34;Fizz&#34;
     elif not i % 5:
     [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fizzbuzz<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
   <span style="color: #ff7700;font-weight:bold;">for</span> i <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;">1</span>,<span style="color: #ff4500;">101</span><span style="color: black;">&#41;</span>:
     <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> i <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">15</span>:
       <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;FizzBuzz&quot;</span>
     <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #ff7700;font-weight:bold;">not</span> i <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">3</span>:
       <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Fizz&quot;</span>
     <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #ff7700;font-weight:bold;">not</span> i <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">5</span>:
       <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Buzz&quot;</span>
     <span style="color: #ff7700;font-weight:bold;">else</span>: 
       <span style="color: #ff7700;font-weight:bold;">print</span> i
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
   fizzbuzz<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://thepythonist.com/blog/fizzbuzz-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expressions</title>
		<link>http://thepythonist.com/blog/regular-expressions/</link>
		<comments>http://thepythonist.com/blog/regular-expressions/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 21:37:30 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thepythonist.com/?p=4</guid>
		<description><![CDATA[I have a dangerous love affair with regular expressions. I am fascinated by them so much that I use them in the most bizarre and inappropriate places (just ask Ken Reitz). I have developed an addiction to regular expressions the likes of which simply cannot be healthy, both for myself and anyone who encounters my [...]]]></description>
			<content:encoded><![CDATA[<p>I have a dangerous love affair with regular expressions. I am fascinated by them so much that I use them in the most bizarre and inappropriate places (just ask Ken Reitz). I have developed an addiction to regular expressions the likes of which simply cannot be healthy, both for myself and anyone who encounters my code. Maybe I should consider rehab&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://thepythonist.com/blog/regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
