<?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>learning python &#187; rss reader</title>
	<atom:link href="http://www.learningpython.com/category/python/rss-reader/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningpython.com</link>
	<description>one man's journey into python...</description>
	<lastBuildDate>Mon, 26 Apr 2010 01:21:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>RSS reader &#8211; Part Four &#8211; Integrating with the GUI</title>
		<link>http://www.learningpython.com/2006/03/05/rss-reader-part-four-integrating-with-the-gui/</link>
		<comments>http://www.learningpython.com/2006/03/05/rss-reader-part-four-integrating-with-the-gui/#comments</comments>
		<pubDate>Sun, 05 Mar 2006 19:25:50 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[gui]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>
		<category><![CDATA[tkinter]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=17</guid>
		<description><![CDATA[
			
				
			
		
So far our RSS reader has been completely command-line, which is functional but not as nice as we&#8217;d really like to have it.  So what we are going to do is integrate the GUI that I created in my two Tkinter GUI tutorials into our RSS application.
If you look at our GUI application you [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F03%2F05%2Frss-reader-part-four-integrating-with-the-gui%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F03%2F05%2Frss-reader-part-four-integrating-with-the-gui%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>So far our <a href="http://www.learningpython.com/category/python/rss-reader/">RSS reader</a> has been completely command-line, which is functional but not as nice as we&#8217;d really like to have it.  So what we are going to do is integrate the GUI that I created in my two <a href="http://www.learningpython.com/category/python/tkinter/">Tkinter GUI tutorials</a> into our RSS application.</p>
<p>If you look at our GUI application you can see that at this point it actually has the basic look that we&#8217;ll want, what it doesn&#8217;t have is the functionality attached to to GUI.  So the first thing that we are going to do is let the user add an RSS site when the press the Add button.  We are going to pop up a dialog and let them enter the name of the RSS site and the address of the RSS feed.</p>
<p>In order to do that we are going to have to create a simple dialog.  Fortunately Tkinter has a nice simple dialog interface that makes creating simple pop-up dialogs quite easy.  It&#8217;s not as robust as you might like, but if you really wanted to do something very complicated you could simply create another window in the same way that we created our main window.  Since we don&#8217;t need anything fancy we&#8221;ll use the Simple dialog.</p>
<p>To start we must import the simple dialog into our program, so the following will have to be added to the top of the code:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">tkSimpleDialog</span></pre></div></div>
<p>Now we could create create our dialog class in a totally new python file but since it&#8217;s going to be pretty simple I decided to keep all together in our one GUI file.  The first step in understanding the simple dialog is to read <a href="http://www.pythonware.com/library/tkinter/introduction/dialog-windows.htm">PythonWare&#8217;s introduction to the dialog window</a> it will explain all that you need to know to create a simple dialog.</p>
<p>Basically what you need to do is create a class whose parent is the simple dialog:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSItemDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">tkSimpleDialog</span><span class="hl-code">.</span><span class="hl-identifier">Dialog</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Then you have to create a body function, this is the function that the tkSimpleDialog assumes will be used to create the body of the dialog.  The definition of the body function is as follows:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">body</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">master</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Pretty simple so far right?  There are a few other important functions that you need to know about to create a simple dialog, like when to process the data after the Ok button is pressed:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-builtin">apply</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p><span id="more-17"></span><br />
When the cancel or Ok button is pressed:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">cancel</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-default">:
</span><span class="hl-reserved">def </span><span class="hl-identifier">ok</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>You can read this <a href="http://epydoc.sourceforge.net/stdlib/public/tkSimpleDialog.Dialog-class.html">documentation</a> on sourceforge is you need to know more.  For our simple dialog we only care about the body and apply functions.  The only other feature that I wanted to add to this dialog was a simple way to set the default values of the RSS name and RSS Feed URL.</p>
<p>That way it would be easy to use this dialog to add and edit RSS sites.  To do so you have add and __init__ function to the class, it is defined as:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">parent</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-identifier">RSSName</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">, </span><span class="hl-identifier">RSSUrl</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Where self, parent, and title are the parameters expected by tkSimpleDialog, title being the dialogs title, and RSSName and RSSUrl our new parameters.  So here is the RSSDialog&#8217;s code in it&#8217;s entirety, if you are familiar with tkInter at all it should be pretty simple, if you have any questions ask them and I will try to answer them:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSItemDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">tkSimpleDialog</span><span class="hl-code">.</span><span class="hl-identifier">Dialog</span><span class="hl-brackets">)</span><span class="hl-default">:

    </span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">parent</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-identifier">RSSName</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">, </span><span class="hl-identifier">RSSUrl</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Init, override default for default params</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSName</span><span class="hl-default"> = </span><span class="hl-identifier">RSSName
        self</span><span class="hl-default">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-default"> = </span><span class="hl-identifier">RSSUrl
        self</span><span class="hl-default">.</span><span class="hl-identifier">Result</span><span class="hl-default"> = </span><span class="hl-reserved">False </span><span class="hl-comment">#Default = Cancel
        </span><span class="hl-identifier">tkSimpleDialog</span><span class="hl-default">.</span><span class="hl-identifier">Dialog</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">parent</span><span class="hl-code">,</span><span class="hl-identifier">title</span><span class="hl-brackets">)
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">body</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">master</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is where you create the Body of the dlg</span><span class="hl-quotes">&quot;&quot;&quot;
 
        </span><span class="hl-identifier">lbText</span><span class="hl-default"> = </span><span class="hl-identifier">Label</span><span class="hl-brackets">(</span><span class="hl-identifier">master</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">lbText</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">padx</span><span class="hl-code">=</span><span class="hl-number">5</span><span class="hl-code">, </span><span class="hl-identifier">pady</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-identifier">sticky</span><span class="hl-code">=</span><span class="hl-identifier">W</span><span class="hl-brackets">)
        </span><span class="hl-identifier">lbText</span><span class="hl-default"> = </span><span class="hl-identifier">Label</span><span class="hl-brackets">(</span><span class="hl-identifier">master</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">URL:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">lbText</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-identifier">padx</span><span class="hl-code">=</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-identifier">sticky</span><span class="hl-code">=</span><span class="hl-identifier">W</span><span class="hl-brackets">)

        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSName</span><span class="hl-default"> = </span><span class="hl-identifier">Entry</span><span class="hl-brackets">(</span><span class="hl-identifier">master</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSName</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">RSSName</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSUrl</span><span class="hl-default"> = </span><span class="hl-identifier">Entry</span><span class="hl-brackets">(</span><span class="hl-identifier">master</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSUrl</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)

        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSName</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">column</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSUrl</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-identifier">column</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-brackets">)
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Return the widget that will get focus</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">eRSSName

    </span><span class="hl-reserved">def </span><span class="hl-builtin">apply</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">They have selected OK</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">Result</span><span class="hl-default"> = </span><span class="hl-reserved">True
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSName</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">eRSSName</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">())
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">eRSSUrl</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">())</span></pre></div></div>
<p><a href="http://www.learningpython.com/images/RSSDialog.png"><img style="margin: 0pt 10px 10px 0pt; cursor: pointer;" src="http://www.learningpython.com/images/small_RSSDialog.png" alt="Python Dialog" border="0" /></a></p>
<p>Now we need to do a few things, we need to save a list of RSS sites in our GUIFramework class, this will correspond to the list of items in the Listbox. To do so we add the following code to the __init__ function:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstSites</span><span class="hl-default"> = </span><span class="hl-brackets">[]</span></pre></div></div>
<p>This is a list of RSS sites that the user has added to our application.  We will also need a new class to contain all of the RSSSite information, the class is pretty simple:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSSite</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">An RSS Site</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">Name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">, </span><span class="hl-identifier">Url</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">Name</span><span class="hl-default"> = </span><span class="hl-identifier">Name
        self</span><span class="hl-default">.</span><span class="hl-identifier">Url</span><span class="hl-default"> = </span><span class="hl-identifier">Url</span></pre></div></div>
<p>Then we have to create a function that will correspond to when the Add button is pressed.  This dialog will show the RSSItemDialog and then if Ok is press add the resulting RSSSite to the RSS Site Listbox (<em>lbSites</em>) and store the site in the <em>lstSites</em> list.  The code to accomplish this is as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">AddRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Add an RSSItem</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-identifier">RSSDlg</span><span class="hl-default"> = </span><span class="hl-identifier">RSSItemDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">master</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">Add new Item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">Result</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">newSite</span><span class="hl-default"> = </span><span class="hl-identifier">RSSSite</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">RSSName</span><span class="hl-code">
                           , </span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-identifier">END</span><span class="hl-code">, </span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">RSSName</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">select_clear</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-identifier">END</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">select_set</span><span class="hl-brackets">(</span><span class="hl-identifier">END</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstSites</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">newSite</span><span class="hl-brackets">)</span></pre></div></div>
<p>So we create the new dialog, and then if the user clicks Ok (Result == True) we create a new site.  Then we add the sites name to the end of the lbSites listbox, clear any current selection, and then select what we just added.  After that we simply add the RSS site to our site list.  So the position of the RSS Sites name in the <em>lbSites</em> ListBox corresponds to the RSSSite&#8217;s position in the <em>lstSites</em> list.  It&#8217;s basically like a simple mapping, I would rather store all of the information in the Listbox but I couldn&#8217;t find anyway to do that. </p>
<p>We will use that same basic formula to create the EditRSSItem function, but instead of creating a new RSSSite we need to edit the selected RSSSite:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">EditRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Edit an RSSItem</span><span class="hl-quotes">&quot;&quot;&quot;  
    &quot;&quot;&quot;</span><span class="hl-string">Get the Current selection</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">lstCurrSel</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">curselection</span><span class="hl-brackets">()</span><span class="hl-default">;
               
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">selected_site</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstSites</span><span class="hl-brackets">[</span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">])]
        </span><span class="hl-identifier">RSSDlg</span><span class="hl-default"> = </span><span class="hl-identifier">RSSItemDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
                                ,</span><span class="hl-quotes">&quot;</span><span class="hl-string">Edit</span><span class="hl-quotes">&quot;</span><span class="hl-code">
                                , </span><span class="hl-identifier">selected_site</span><span class="hl-code">.</span><span class="hl-identifier">Name</span><span class="hl-code">
                                , </span><span class="hl-identifier">selected_site</span><span class="hl-code">.</span><span class="hl-identifier">Url</span><span class="hl-brackets">)
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">Result</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Update the ListBox</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">delete</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">)
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-code">, </span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">RSSName</span><span class="hl-brackets">)
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">select_clear</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-identifier">END</span><span class="hl-brackets">)
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">select_set</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">)
            
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Save the values in the list</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstSites</span><span class="hl-brackets">[</span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">])]</span><span class="hl-default"> =  </span><span class="hl-identifier">RSSSite</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">RSSName</span><span class="hl-code">
                           , </span><span class="hl-identifier">RSSDlg</span><span class="hl-code">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)</span></pre></div></div>
<p>The only confusing code that you might see is this <em>int(lstCurrSel[0])</em>, which is basically converting the first value in the selection list to an integer.  We need to do this because list in python expect integers for indices.  So we get the current selected index in the ListBox and then use that to get the RSSSite from our list.  Then the rest of the process is almost identical to the AddRSSItem function.</p>
<p>Now we need to hook the two functions up to our buttons:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">btnAdd</span><span class="hl-default"> = </span><span class="hl-identifier">Button</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Add</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">AddRSSItem</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">btnEdit</span><span class="hl-default"> = </span><span class="hl-identifier">Button</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Edit</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">EditRSSItem</span><span class="hl-brackets">)</span></pre></div></div>
<p>You can probably guess what the next step in the equation is, we need to make it so that our View button displays all of the RSS items in the <em>lbRSSItems</em> ListBox.  To do this we are going to need to bring in the RSSGenerator.py code that we created in part three.  Instead of taking all the code from that file and then adding it to our current file (which we could do) we are simple going to move that file into the same directory as our current python file (mine is called GUI.py) then we have to import or include the RSSGenerator.py classes:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">RSSGenerator </span><span class="hl-reserved">import</span><span class="hl-default"> *</span></pre></div></div>
<p>Then we have to create a ViewRSSItems function, which is very similar to our EditRSSItem function except that it will fill the <em>lbRSSItems</em> ListBox with all of the current RSS items available on the feed:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">ViewRSSItems</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Get the Current selection</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">lstCurrSel</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbSites</span><span class="hl-default">.</span><span class="hl-identifier">curselection</span><span class="hl-brackets">()</span><span class="hl-default">;
               
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">selected_site</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstSites</span><span class="hl-brackets">[</span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">])]
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Empty the ListBox</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbRSSItems</span><span class="hl-default">.</span><span class="hl-identifier">delete</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">END</span><span class="hl-brackets">)
        </span><span class="hl-identifier">rss_reader</span><span class="hl-default"> = </span><span class="hl-identifier">RSSReader</span><span class="hl-brackets">(</span><span class="hl-identifier">selected_site</span><span class="hl-code">.</span><span class="hl-identifier">Url</span><span class="hl-brackets">)
        </span><span class="hl-reserved">for </span><span class="hl-identifier">rss_item </span><span class="hl-reserved">in </span><span class="hl-identifier">rss_reader</span><span class="hl-default">.</span><span class="hl-identifier">GetItems</span><span class="hl-brackets">()</span><span class="hl-default">:
            </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">rss_item</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbRSSItems</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-identifier">END</span><span class="hl-code">,</span><span class="hl-identifier">rss_item</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-brackets">)
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstItems</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">rss_item</span><span class="hl-brackets">)</span></pre></div></div>
<p>So we get the current selection, then we create our RSSReader based upon the RSSSites Url.  Then we loop through the RSSItems adding each to our ListBox and to our list of the items (<em>lstItems</em>) that we have added in out GUIFramework&#8217;s __init__ function:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstItems</span><span class="hl-default"> = </span><span class="hl-brackets">[]</span></pre></div></div>
<p><a href="http://www.learningpython.com/images/RSSReader01.png"><img style="margin: 0pt 10px 10px 0pt; cursor: pointer;" src="http://www.learningpython.com/images/small_RSSReader01.png" alt="Python RSS Reader" border="0" /></a></p>
<p>As you can see the RSS Reader is actually starting to take shape, now all we have to do is show the RSS Items description in the text widget when we click the Set Text button.  Given all the ListBox processing that we have done recently  the way that this works should come as no surprise:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">SetStoryText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the Story text, called form the btnSetText</span><span class="hl-quotes">&quot;&quot;&quot; 
    &quot;&quot;&quot;</span><span class="hl-string">Get the Current selection</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">lstCurrSel</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbRSSItems</span><span class="hl-default">.</span><span class="hl-identifier">curselection</span><span class="hl-brackets">()</span><span class="hl-default">;
               
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">rss_item</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lstItems</span><span class="hl-brackets">[</span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCurrSel</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">])]
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the Text Widgets text</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">txtItem</span><span class="hl-default">.</span><span class="hl-identifier">config</span><span class="hl-brackets">(</span><span class="hl-identifier">state</span><span class="hl-code">=</span><span class="hl-identifier">NORMAL</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">txtItem</span><span class="hl-default">.</span><span class="hl-identifier">delete</span><span class="hl-brackets">(</span><span class="hl-number">1.0</span><span class="hl-code">,</span><span class="hl-identifier">END</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">txtItem</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-identifier">INSERT</span><span class="hl-code">, </span><span class="hl-identifier">rss_item</span><span class="hl-code">.</span><span class="hl-identifier">description</span><span class="hl-code"> + </span><span class="hl-quotes">&quot;</span><span class="hl-special">\r\n\r\n</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">txtItem</span><span class="hl-default">.</span><span class="hl-identifier">insert</span><span class="hl-brackets">(</span><span class="hl-identifier">INSERT</span><span class="hl-code">, </span><span class="hl-identifier">rss_item</span><span class="hl-code">.</span><span class="hl-identifier">link</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">a</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">txtItem</span><span class="hl-default">.</span><span class="hl-identifier">config</span><span class="hl-brackets">(</span><span class="hl-identifier">state</span><span class="hl-code">=</span><span class="hl-identifier">DISABLED</span><span class="hl-brackets">)</span></pre></div></div>
<p><a href="http://www.learningpython.com/images/RSSReader02.png"><img style="margin: 0pt 10px 10px 0pt; cursor: pointer; float: left" src="http://www.learningpython.com/images/small_RSSReader02.png" alt="Python RSS Reader" border="0" /></a>Whew that&#8217;s a lot of code this time, but none of it is really that new or that difficult if you have been following along with the RSS Reader so far.  You might notice that I have not gone into great detail describing all of the code.  The reason for that is because for the most part I&#8217;ve already gone over the code in detail in a previous post, or the code is sufficiently similar to code I have gone over that I don&#8217;t think that it needs a great deal of description.  But if you have any questions about any of the code that I have shown above feel free to ask a question and I will be happy to explain it in more detail.</p>
<p>The next step in the RSS Read will be to save and load the RSSSites into a file so that we don&#8217;t have to waste our time typing them in every time we start the program, but I&#8217;ll leave that for another day.</p>
<p>You can download all the code used in this post here: <a href="http://www.learningpython.com/sources/RSSPart4.zip">RSSPart4.zip</a></p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/03/05/rss-reader-part-four-integrating-with-the-gui/&title=RSS reader - Part Four - Integrating with the GUI&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2006/03/05/rss-reader-part-four-integrating-with-the-gui/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>RSS reader &#8211; Part Three &#8211; Generator Class</title>
		<link>http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/</link>
		<comments>http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/#comments</comments>
		<pubDate>Tue, 31 Jan 2006 04:51:01 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=13</guid>
		<description><![CDATA[
			
				
			
		
Please remember to read part one and part two.
Classes and Generators
All right, now that we have split our RSS reader up into functions, we&#8217;re going to go one step further and put our code into a class.
We&#8217;re also going to do something a bit more advanced with our class and create a generator.  The [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F30%2Frss-reader-part-three-generator-class%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F30%2Frss-reader-part-three-generator-class%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Please remember to read <a href="http://www.learningpython.com/2006/01/14/rss-reader-part-one/">part one</a> and <a href="http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/">part two</a>.</p>
<h3>Classes and Generators</h3>
<p>All right, now that we have split our RSS reader up into <a href="http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/">functions</a>, we&#8217;re going to go one step further and put our code into a <a href="http://www.learningpython.com/2006/01/28/classes/">class</a>.</p>
<p>We&#8217;re also going to do something a bit more advanced with our class and create a generator.  The reason that we are going to do this is because python has such nice iteration handling and because in the future we&#8217;ll probably want to handle each RSS item individually rather then simply dumping it out to the terminal.</p>
<p><a href="http://www.python.org/doc/2.4.2/tut/node11.html#SECTION00111000000000000000000">Generators</a> are basically a type of <a href="http://www.python.org/doc/2.4.2/tut/node11.html#SECTION0011900000000000000000">iterator</a>, except their syntax is slightly different.  In fact anything you can accomplish in a generator you can accomplish in a standard iterator. </p>
<p>Here is an example generator that yields all the factors of the specified number:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">factors </span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-identifier">count</span><span class="hl-default"> = </span><span class="hl-number">1</span><span class="hl-default">;
	</span><span class="hl-reserved">while </span><span class="hl-identifier">count</span><span class="hl-default"> &lt; = </span><span class="hl-identifier">num</span><span class="hl-default">/</span><span class="hl-number">2</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span class="hl-code"> % </span><span class="hl-identifier">count</span><span class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">yield </span><span class="hl-identifier">count
		count</span><span class="hl-default"> = </span><span class="hl-identifier">count</span><span class="hl-default"> + </span><span class="hl-number">1
	</span><span class="hl-reserved">yield </span><span class="hl-identifier">num</span></pre></div></div>
<p><span id="more-13"></span></p>
<p>The special statements that make this function a generator are those two yield statements.  (The second yield statement is simply to return the number itself, since a number is always a factor of itself.)  The yield statement basically returns the data to the caller and the next time the function is iterated on, the function continues exactly where it left off.</p>
<p>You call the generator in the following way:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">for </span><span class="hl-identifier">res </span><span class="hl-reserved">in </span><span class="hl-identifier">factors</span><span class="hl-brackets">(</span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-identifier">res</span></pre></div></div>
<p>With the following results:</p>
<p><code><br />
1<br />
2<br />
3<br />
5<br />
6<br />
10<br />
15<br />
30</p>
<h3>Code</h3>
<p>As you can see having something like this would be very useful in our RSS reader.  In order to accomplish this the first new addition to the code that we created in <a href="http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/">part two</a>  is the <em>RSSItem</em> class.  The <em>RSSItem</em> class basically represents one RSS item.  For the time being it is going to contain only the title and the description.  The nice thing about having this in a separate class is that we can add new members at any time without affecting existing code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSItem</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is an RSS item, it contain all the RSS info like Tile and Description</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-identifier">title
		self</span><span class="hl-default">.</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">description</span></pre></div></div>
<p>You'll notice that the RSSItem class has no functions besides the <em>__init__</em> which takes two optional parameters the title and the description, and then sets two data members equal to those values.</p>
<p>The next thing that we are going to define is out main class, we'll call it <em>RSSReader</em> for lack of a better name:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSReader</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This class is an RSS reader, it should have a better docstring</span><span class="hl-quotes">&quot;&quot;&quot;</span></pre></div></div>
<p>Here is the <em>__init__</em> function, which takes one parameter, the <em>RSSUrl</em>.  The <em>__init__</em> function also does some "pre-processing" and gets the XML document:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the class</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-default"> = </span><span class="hl-identifier">RSSUrl</span><span class="hl-default">;
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting XML Document!</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>The RSSUrl class has two member variables, <em>RSSUrl</em> and <em>xmldoc</em>.  You'll also notice that the <em>__init__</em> function calls  a member function of <em>RSSReader</em> called <em>GetXMLDocument</em>, which does exactly what it says:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function reads in a RSS URL and then</span><span class="hl-quotes">&quot;&quot;&quot;
	&quot;&quot;&quot;</span><span class="hl-string">returns the XML document on success</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
	</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-reserved">None
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
	</span><span class="hl-reserved">else</span><span class="hl-default">	:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting URL</span><span class="hl-quotes">&quot;
	</span><span class="hl-reserved">return </span><span class="hl-identifier">xmldoc</span></pre></div></div>
<p>The next function that I'm going to show you is the generator, and how simply it actually is:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetItems</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Generator to get items</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">All right we have an item</span><span class="hl-quotes">&quot;&quot;&quot;
				</span><span class="hl-identifier">rss_item</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
				</span><span class="hl-reserved">yield </span><span class="hl-identifier">rss_item</span></pre></div></div>
<p>You'll see that all this generator does is iterate through all of the nodes in the XML document until it comes to a node named "Item".  Once a node named "item" is encountered a member function called <em>CreateRSSItem</em> (Which creates an RSSItem) is called and then that RSSItem is yielded to the caller.</p>
<p><em>CreateRSSItem</em> is another simply function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create an RSS item and return it</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-reserved">return </span><span class="hl-identifier">RSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-brackets">)</span></pre></div></div>
<p>All this function does is call the member function <em>GetChildText</em> for the "title" and "description" values.  <em>GetChildText</em> basically searches through the passed xml node's children searching for a match to the second parameter, and if one is found  that item's text is returned.</p>
<p>Then <em>CreateRSS</em> item call the constructor of the <em>RSSItem</em> class and returns the created class: </code><code>return RSSItem(title,description)</code></p>
<p>There are only two more functions left, both of which are (as you might have guessed) very simply.  This is the beauty of using functions, you get to break up the code into manageable and reusable tasks.  The two functions that are left are <em>GetChildText</em> and <em>GetItemText</em>.</p>
<p>Where <em>GetChildText</em> searched through childNodes for a matching item, <em>GetItemText</em> simply returns an XML node&#8217;s text:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">xml_node</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Get the text from an xml item</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">Node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">text</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
	</span><span class="hl-reserved">return </span><span class="hl-identifier">text

</span><span class="hl-reserved">def </span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">xml_node</span><span class="hl-code">, </span><span class="hl-identifier">child_name</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Get a child node from the xml node</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">xml_node</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error GetChildNode: No xml_node</span><span class="hl-quotes">&quot;
		</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code">==</span><span class="hl-identifier">child_name</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Return Nothing</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<p>All that&#8217;s left is to show how we create an instance of the RSSReader class, and then use the generator to iterate through all of the <em>RSSItems</em>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">rss_reader</span><span class="hl-default"> = </span><span class="hl-identifier">RSSReader</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)
	</span><span class="hl-reserved">for </span><span class="hl-identifier">rss_item </span><span class="hl-reserved">in </span><span class="hl-identifier">rss_reader</span><span class="hl-default">.</span><span class="hl-identifier">GetItems</span><span class="hl-brackets">()</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">rss_item</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">title
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">description
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<p>Here is the code in its entirety, it call also be downloaded from here:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">urllib2
</span><span class="hl-reserved">from </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom </span><span class="hl-reserved">import </span><span class="hl-identifier">minidom</span><span class="hl-default">, </span><span class="hl-identifier">Node

</span><span class="hl-reserved">class </span><span class="hl-identifier">RSSItem</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is an RSS item, it contain all the RSS info like Tile and Description</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-identifier">title
		self</span><span class="hl-default">.</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">description

</span><span class="hl-reserved">class </span><span class="hl-identifier">RSSReader</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This class is an RSS reader, it should have a better docstring</span><span class="hl-quotes">&quot;&quot;&quot;
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the class</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-default"> = </span><span class="hl-identifier">RSSUrl</span><span class="hl-default">;
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting XML Document!</span><span class="hl-quotes">&quot;
		
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function reads in a RSS URL and then</span><span class="hl-quotes">&quot;&quot;&quot;
		&quot;&quot;&quot;</span><span class="hl-string">returns the XML documentn on success</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
		</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-reserved">None
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
		</span><span class="hl-reserved">else</span><span class="hl-default">	:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting URL</span><span class="hl-quotes">&quot;
		</span><span class="hl-reserved">return </span><span class="hl-identifier">xmldoc
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">xml_node</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Get the text from an xml item</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">Node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">text</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
		</span><span class="hl-reserved">return </span><span class="hl-identifier">text
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">xml_node</span><span class="hl-code">, </span><span class="hl-identifier">child_name</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Get a child node from the xml node</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">xml_node</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error GetChildNode: No xml_node</span><span class="hl-quotes">&quot;
			</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code">==</span><span class="hl-identifier">child_name</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Return Nothing</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create an RSS item and return it</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-reserved">return </span><span class="hl-identifier">RSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-brackets">)
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetItems</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Generator to get items</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
				</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
					</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Allright we have an item</span><span class="hl-quotes">&quot;&quot;&quot;
					</span><span class="hl-identifier">rss_item</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
					</span><span class="hl-reserved">yield </span><span class="hl-identifier">rss_item
					
</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">rss_reader</span><span class="hl-default"> = </span><span class="hl-identifier">RSSReader</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)
	</span><span class="hl-reserved">for </span><span class="hl-identifier">rss_item </span><span class="hl-reserved">in </span><span class="hl-identifier">rss_reader</span><span class="hl-default">.</span><span class="hl-identifier">GetItems</span><span class="hl-brackets">()</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">rss_item</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">title
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">description
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/&title=RSS reader - Part Three - Generator Class&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>RSS reader &#8211; Part Two (and Functions)</title>
		<link>http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/</link>
		<comments>http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/#comments</comments>
		<pubDate>Tue, 24 Jan 2006 22:55:34 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=10</guid>
		<description><![CDATA[
			
				
			
		
This post is my continuation of my Python based RSS reader that I wrote in part one.  As I said the code written in part one is not something that you would ever really want to use or maintain since it wasn&#8217;t broken up in to functions properly.  So, in this part we&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F24%2Frss-reader-part-two-and-functions%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F24%2Frss-reader-part-two-and-functions%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>This post is my continuation of my Python based RSS reader that I wrote in <a href="http://www.learningpython.com/2006/01/14/rss-reader-part-one/">part one</a>.  As I said the code written in part one is not something that you would ever really want to use or maintain since it wasn&#8217;t broken up in to functions properly.  So, in this part we&#8217;re going to work on breaking the old script up into functions.</p>
<h3>Functions</h3>
<p>Functions are defined in python using the <em>def</em> keyword.  So if I wanted to define a function called &#8220;count&#8221; that counts from 1 to a certain number I would do so like so:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-identifier">nNum</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Where <em>count</em> is the name of the function and <em>nNum</em> is a parameter that is being passed into the function.  If I wanted to call the function I would do so like this:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-brackets">)</span></pre></div></div>
<p><span id="more-10"></span><br />
Here is the entire code for the count function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-identifier">nNum</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-number">1
    </span><span class="hl-reserved">while </span><span class="hl-identifier">x</span><span class="hl-default"> &amp;</span><span class="hl-identifier">lt</span><span class="hl-default">: </span><span class="hl-identifier">nNum</span><span class="hl-default"> :
        </span><span class="hl-reserved">print </span><span class="hl-identifier">x
        x</span><span class="hl-default"> = </span><span class="hl-identifier">x</span><span class="hl-default"> +</span><span class="hl-number">1</span></pre></div></div>
<p>Another interesting feature that python uses are <a href="http://www.python.org/doc/2.4.2/tut/node6.html#SECTION006760000000000000000">Documentation Strings</a>, these are basically comments that help document the code for you and can also be read by documentation tools like <a href="http://lfw.org/python/pydoc.html">pyDoc</a> to create formatted documentation.  Since this is becoming a standard it&#8217;s a good way for other people, and you, to be able to easily see what your program does.</p>
<p>So for the count function we might add something like this:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-identifier">nNum</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function prints out the numbers from 1 to nNum</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-number">1
    </span><span class="hl-reserved">while </span><span class="hl-identifier">x</span><span class="hl-default"> &amp;</span><span class="hl-identifier">lt</span><span class="hl-default">: </span><span class="hl-identifier">nNum</span><span class="hl-default"> :
        </span><span class="hl-reserved">print </span><span class="hl-identifier">x
        x</span><span class="hl-default"> = </span><span class="hl-identifier">x</span><span class="hl-default"> +</span><span class="hl-number">1</span></pre></div></div>
<h3>The Code</h3>
<p>We will be reusing the code that was already written in Part One for this section.</p>
<p>Now what we need to do is break up the code that we used in <a href="http://www.learningpython.com/2006/01/14/rss-reader-part-one/">part one</a> so that it uses functions.  The first thing that we are going to do is instead of always using the same location for our RSS we are going to write a function that takes an RSS url as it parameter and then attempts to retrieve it&#8217;s RSS information.  We&#8217;ll call that function <em>GetRSS</em>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetRSS</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSurl</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function attempts to get the RSS info using RSSurl as the RSS url</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSurl</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">We have retrieve the RSS url properly, now let's parse it up</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Loop through all children of the main document</span><span class="hl-quotes">&quot;&quot;&quot;
			</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
				</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:	
					</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">If we have found an item print out the title 
					and description</span><span class="hl-quotes">&quot;&quot;&quot;
					</span><span class="hl-identifier">PrintNodeItems</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">, </span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">])
		</span><span class="hl-reserved">else</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error parsing url into xml</span><span class="hl-quotes">&quot;
	</span><span class="hl-reserved">else</span><span class="hl-default">:
        	</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error! Getting URL</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>You&#8217;ll notice that <em>GetRSS</em> is very similar to the majority of code that we had in Part One.  Basically it gets the RSS XML from a specific URL, then it loops through all of the child nodes in the XML looking for <em>item</em> nodes.  Once an node with the name of &#8220;item” is found the following new code is called:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">PrintNodeItems</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">, </span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">])</span></pre></div></div>
<p>This line calls another new function in the code called <em>PrintNodeItems</em>, passing it our <em>item</em> node and a list of items:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">PrintNodeItems</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlNode</span><span class="hl-code">, </span><span class="hl-identifier">items</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function prints out all children of XmlNode found in items</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">XmlNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-reserved">if </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeName </span><span class="hl-reserved">in </span><span class="hl-identifier">items</span><span class="hl-default">:
			</span><span class="hl-identifier">PrintNodesText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)</span></pre></div></div>
<p><em>PrintNodeItems</em> is a simple function that loops through all of the <em>childNodes</em> found in <em>XmlNode</em> and check to see if the childNodes <em>nodeName</em> is in the list <em>items</em>.  If it is in the list, then the function <em>PrintNodesText</em> is called.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">PrintNodesText</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlNode</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function prints out an XML Nodes text nodes.</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">XmlNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">Node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">text</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Noe print out the text</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">text</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-identifier">text
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<p><em>PrintNodeItems</em> is another simple function that basically loops through an XmlNode&#8217;s children and prints out all <em>TEXT_NODES</em>.  You&#8217;ll notice that this code is identical to the code that we used to print out the &#8220;title&#8221; and &#8220;description&#8221; nodes in Part One.  The difference this time is that instead of duplicating code in two spots we simple create one function that gets called twice.</p>
<p>Now we have the a simple <em>main()</em> function that calls <em>GetRSS</em>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">main</span><span class="hl-brackets">()</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Main Function</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">GetRSS</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></pre></div></div>
<p>The final step is a method for getting the ball rolling, for starting the execution.  So far we&#8217;ve simply defined functions that perform tasks when called but how do we call the first function?</p>
<p>The answer is a simple check at the bottom of your python script to determine if the script was launched directly as a standalone script (which is all that we have been doing so far when we call out scripts via the command line):</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">main</span><span class="hl-brackets">()</span></pre></div></div>
<p>This is basically a way for the Python script to know if it has been launched directly, if it has not been launched directly and is been instantiated in a different manner __name__ will not equal &#8220;__main__&#8221; and the main function will not be called automatically.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/&title=RSS reader - Part Two (and Functions)&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RSS reader &#8211; Part One</title>
		<link>http://www.learningpython.com/2006/01/14/rss-reader-part-one/</link>
		<comments>http://www.learningpython.com/2006/01/14/rss-reader-part-one/#comments</comments>
		<pubDate>Sat, 14 Jan 2006 19:12:03 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=9</guid>
		<description><![CDATA[
			
				
			
		
All right so I&#8217;ve already figured out how to write an executable script that writes out &#8220;Hello World!&#8221; to the command line, now I need to figure out how to do something interesting.
As a result I was surfing the Internet and reading some Python documentation, trying to come up with something to do but nothing [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F14%2Frss-reader-part-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F14%2Frss-reader-part-one%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>All right so I&#8217;ve already figured out how to <a href="http://www.learningpython.com/2006/01/10/writing-my-first-python-script/">write an executable script</a> that writes out &#8220;Hello World!&#8221; to the command line, now I need to figure out how to do something interesting.</p>
<p>As a result I was surfing the Internet and reading some Python documentation, trying to come up with something to do but nothing seemed interesting enough or easy enough for me to do until I visited one of my favorite websites: <a href="http://slashdot.org/">slashdot.org</a>.</p>
<p>It was there that I noticed that RSS icon that <a href="http://www.mozilla.com/firefox/">Firefox</a> always shows me whenever I come across a website with an RSS feed (like this one.)</p>
<p>So I thought to myself, &#8216;Hmm I wonder how hard it would be to create a simple RSS reader in Python?&#8217;  Read on to discover the results.<span id="more-9"></span></p>
<p>First I had to look at Slashdots RSS XML to know what it looks like and what my code should be looking for.  If you copy and paste: <a href="http://rss.slashdot.org/Slashdot/slashdot">http://rss.slashdot.org/Slashdot/slashdot</a> into your web browser you will be able to take a look at the XML format.  The format is basically as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-brackets">&lt;</span><span class="hl-code"> ?</span><span class="hl-reserved">xml </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot; </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;
&lt;</span><span class="hl-reserved">rdf </span><span class="hl-var">:RDF</span><span class="hl-brackets">&gt;
	
	&lt;</span><span class="hl-reserved">item </span><span class="hl-var">rdf:about</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://someulr</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Title</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Link</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Description</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;
		
	&lt;/</span><span class="hl-reserved">item</span><span class="hl-brackets">&gt;
	&lt;</span><span class="hl-reserved">item </span><span class="hl-var">rdf:about</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://someulr</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Title</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Link</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Description</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;
		
	&lt;/</span><span class="hl-reserved">item</span><span class="hl-brackets">&gt;
&lt;/</span><span class="hl-reserved">rdf</span><span class="hl-brackets">&gt;</span></pre></div></div>
<p>For this simple RSS reader I&#8217;m only going to pay attention to each &lt;item&gt; and ignore all other tags.  In each &lt;item&gt; I&#8217;m going to want to display the &lt;title&gt; and the &lt;description&gt;.  That way when we run this program well see the title and descriptions of all the items on Slashdot&#8217;s main page.</p>
<p>Now that I knew what I wanted to do I fired up my trusty copy of gedit and began searching the web for information.  By far the best site I&#8217;ve found for Python based information is the source, <a href="http://www.python.org/">python.org</a>.  There is tons of documentation and information on this site, everything a new programmer needs.  There may be sites out there that are better, but I haven&#8217;t found them yet.</p>
<p>Now that I knew what I was going to do, I needed to find some libraries to help me with my task, since there was no way I was going to do all of this myself.  Fortunately for me python has many <a href="http://docs.python.org/lib/lib.html">built-in libraries</a> that make doing complicated things a lot easier.</p>
<p>The first thing I needed to do was create a blank shell of a script based on my first executable python script.  I called my new file pythonRSS.py and edited it so that it only contained the following:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python</span></pre></div></div>
<p>After that I needed to include (sorry for the c++ terminology) the libraries that I needed to make my RSS reader:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">urllib2
</span><span class="hl-reserved">from </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom </span><span class="hl-reserved">import </span><span class="hl-identifier">minidom</span><span class="hl-default">, </span><span class="hl-identifier">Node</span></pre></div></div>
<p>The first line gives me access to the library <a href="http://docs.python.org/lib/module-urllib2.html">urllib2</a>, and the second line imports the minidom and Node submodules from the<a href="http://www.python.org/doc/current/lib/module-xml.dom.html"> xml.dom</a> library and makes them available without the package prefix.  Had I just used the following line:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom</span></pre></div></div>
<p>I would have had to reference the Node and minidom submodules using the package prefix: <code>xml.dom.Node</code>, instead I am able to reference them directly as <code>Node</code></p>
<p>Now to get down to the programming business, the first piece of the puzzle that I  needed was slashdot&#8217;s RSS XML, that&#8217;s pretty easy to get using the ulrlib2 library.  We download the RSS feed using the <a href="http://www.python.org/doc/current/lib/module-urllib.html">urlopen</a> function:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></pre></div></div>
<p>This gives us a &#8220;file-like&#8221; object stored in url_info, if we wanted to run through the XML returned line-by-line we could actually use the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">for </span><span class="hl-identifier">lines </span><span class="hl-reserved">in </span><span class="hl-identifier">url_info</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-identifier">lines</span></pre></div></div>
<p>But since we have a fancy XML library at our disposal manually running through the file wouldn&#8217;t make much sense.  </p>
<p>Now that we have the RSS XML we are going to pass it to our <a href="http://www.python.org/doc/current/lib/module-xml.dom.minidom.html">minidom</a> object and get it to parse it into a document:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)</span></pre></div></div>
<p>The parse function parses up the XML into the Document Object Model, or DOM (as in minidom or xml.dom), and returns the document to us.  For more information on this please see the <a href="http://www.w3.org/TR/REC-DOM-Level-1/">Document Object Model specification</a>.</p>
<p>Now that we have the document, were going to get the root node (&lt;rdf :RDF&gt;) and then loop through all of its children nodes looking for &lt;item&gt; nodes.  This is actually really simply and intuitive:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">rootNode</span><span class="hl-default"> = </span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement
</span><span class="hl-reserved">for </span><span class="hl-identifier">node </span><span class="hl-reserved">in </span><span class="hl-identifier">rootNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>The above is something that I am definitely learning to love about python, the ability to iterate through things easily.  Instead of having to redo the same iteration code for difference classes and different types (like I have to in C++) python makes this easy and intuitive using for loops or while loops.</p>
<p>Once we&#8217;ve found an &lt;item&gt; node, were just going to do that exact same thing we did above except this time we are going to look for &lt;title&gt; and &lt;description&gt; nodes:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">title</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
		if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-identifier">title

	if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">description</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
		if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">description</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-identifier">description</span><span class="hl-default"> + </span><span class="hl-quotes">&quot;</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>The above code is basically just a variation of the code that we used to get the iterate through all the children of the root node.  The only difference in this case is what we do when we find them, which is to iterate through all of their child nodes that are text nodes and then store that value for printing.  This is the only part of the script that felt a bit weird to me, shouldn&#8217;t it be more like <code>node.Text</code> or something like that?  Either way it works and that is basically the end of the RSS reader.  Just feed it into python or make it executabe and that&#8217;s it:</p>
<p><code>$ python pythonRSS.py</code></p>
<p>All-in-all this code didn&#8217;t take that long to write, a few google searches and I was on my way.  Truthfully witting this blog post took longer to write then the code did.</p>
<p>Please keep in mind that there are many things wrong with this code, the most obvious is that it make no use of functions to simplify the code.  But this is only part one of my RSS reader and with time I will fix up the code.</p>
<p>Here is the code in its entirety with some comments thrown in, alternatively you could also <a href="http://www.learningpython.com/sources/pythonRSS.txt">download the code</a> as  a text file:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">urllib2
</span><span class="hl-reserved">from </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom </span><span class="hl-reserved">import </span><span class="hl-identifier">minidom</span><span class="hl-default">, </span><span class="hl-identifier">Node

</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Get the XML </span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> We have the RSS XML lets try to parse it up </span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">We have the Doc, get the root node</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">rootNode</span><span class="hl-default"> = </span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Iterate the child nodes </span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">node </span><span class="hl-reserved">in </span><span class="hl-identifier">rootNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> We only care about &quot;item&quot; entries</span><span class="hl-quotes">&quot;&quot;&quot;
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Now iterate through all of the &lt;item&gt;'s children </span><span class="hl-quotes">&quot;&quot;&quot;
				</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
					</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Loop through the title Text nodes to get
						the actual title</span><span class="hl-quotes">&quot;&quot;&quot;
						</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
						</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
							</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
								</span><span class="hl-identifier">title</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Now print the title if we have one </span><span class="hl-quotes">&quot;&quot;&quot;
						</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
							</span><span class="hl-reserved">print </span><span class="hl-identifier">title

					if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Loop through the description Text nodes to get
						the actual description</span><span class="hl-quotes">&quot;&quot;&quot;
						</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
						</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
							</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
								</span><span class="hl-identifier">description</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Now print the title if we have one.
						Add a blank with </span><span class="hl-special">\n</span><span class="hl-string"> so that it looks better </span><span class="hl-quotes">&quot;&quot;&quot;
						</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">description</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
							</span><span class="hl-reserved">print </span><span class="hl-identifier">description</span><span class="hl-default"> + </span><span class="hl-quotes">&quot;</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;
	</span><span class="hl-reserved">else</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error getting XML document!</span><span class="hl-quotes">&quot;
</span><span class="hl-reserved">else</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error! Getting URL</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>Links that helped me get through this post:</p>
<ul>
<li><a href="href="http://www.rexx.com/~dkuhlman/pyxmlfaq.html">Pyxml FAQ</a>
</li>
<li><a href="http://pyxml.sourceforge.net/topics/howto/xml-howto.html">Python XML howto</a></li>
<li><a href="http://docs.python.org/">Python Documentation</a></li>
<li><a href="http://www.google.com">http://www.google.com</a></li>
<li><a href="http://www.chroder.com/archives/2005/04/16/wordpress-code-highlight-plugin/">WordPress Code Highlight Plugin</a></li>
</ul>
<p></item></p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/14/rss-reader-part-one/&title=RSS reader - Part One&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2006/01/14/rss-reader-part-one/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
