<?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>Hackerdude &#187; Python</title>
	<atom:link href="http://www.hackerdude.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hackerdude.com</link>
	<description>Software Development Blog by David Martinez</description>
	<lastBuildDate>Tue, 06 Apr 2010 05:02:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Retrieving random lines from a file</title>
		<link>http://www.hackerdude.com/2005/06/20/retrieving-random-lines-from-a-file/</link>
		<comments>http://www.hackerdude.com/2005/06/20/retrieving-random-lines-from-a-file/#comments</comments>
		<pubDate>Mon, 20 Jun 2005 21:38:10 +0000</pubDate>
		<dc:creator>David Martinez</dc:creator>
				<category><![CDATA[CBBTR]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.hackerdude.com/2005/06/20/retrieving-random-lines-from-a-file/</guid>
		<description><![CDATA[A little class to retrieve a random line from a file. import linecache, random """ This class will retrieve a random line from a file. """ class RandomRetriever: """Retrieve a random line from a file. It caches the file, so it runs fast when used again""" def __init__(self, fileName=None): self.fileName = fileName self.lineCount = self.countLine() [...]]]></description>
			<content:encoded><![CDATA[<p>A little class to retrieve a random line from a file.<br />
<span id="more-317"></span></p>
<p><code lang="python"><br />
import linecache, random</p>
<p>"""<br />
This class will retrieve a random line from a file.</p>
<p>"""<br />
class RandomRetriever:<br />
    """Retrieve a random line from a file.<br />
    It caches the file, so it runs fast when used again"""<br />
    def __init__(self, fileName=None):<br />
        self.fileName = fileName<br />
        self.lineCount = self.countLine()</p>
<p>    def countLine(self):<br />
        lines = 0<br />
        try:<br />
            fileLines = open(self.fileName, 'r')<br />
        except IOError:<br />
            print "Cannot open "+self.fileName+" for reading"<br />
        for line in fileLines:<br />
            lines += 1<br />
        return lines</p>
<p>    """Retrieves a random item from the file in this class.</p>
<p>    return A random line from the text file.<br />
    """<br />
    def randomItem(self):<br />
        lineNumber = random.randint(1,self.lineCount)<br />
        return linecache.getline(self.fileName, lineNumber).rstrip()</p>
<p></code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackerdude.com/2005/06/20/retrieving-random-lines-from-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
