<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Intermediate Python: Pythonic file searches</title>
	<atom:link href="http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/feed/" rel="self" type="application/rss+xml" />
	<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/</link>
	<description>Random scribbling about programming, translation, and Japan</description>
	<lastBuildDate>Fri, 03 Feb 2012 09:05:20 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: wcyee</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-139</link>
		<dc:creator>wcyee</dc:creator>
		<pubDate>Sat, 16 Feb 2008 21:32:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-139</guid>
		<description>Ryan, thanks for answering my question. I had guessed, but didn&#039;t know, that the GC handled closing file handles. Also, the &quot;with&quot; syntax is very nice. I&#039;ll be using that a fair bit now. Thanks!</description>
		<content:encoded><![CDATA[<p>Ryan, thanks for answering my question. I had guessed, but didn&#8217;t know, that the GC handled closing file handles. Also, the &#8220;with&#8221; syntax is very nice. I&#8217;ll be using that a fair bit now. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-135</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Sat, 16 Feb 2008 09:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-135</guid>
		<description>@wcyee
Good question. When the call to open goes out of scope, the file handle will be closed upon the next garbage collection. Since GC is eager in CPython, that&#039;ll be very soon. If you&#039;re paranoid about these things, you can use the &quot;with&quot; syntax:
with open(filename) as f:
&#160;&#160;&#160;&#160;for num, line in enumerate(f):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pass

Here&#039;s a nice link that explains how file handles and scope work in CPython:
http://www.diveintopython.org/object_oriented_framework/instantiating_classes.html#fileinfo.scope</description>
		<content:encoded><![CDATA[<p>@wcyee<br />
Good question. When the call to open goes out of scope, the file handle will be closed upon the next garbage collection. Since GC is eager in CPython, that&#8217;ll be very soon. If you&#8217;re paranoid about these things, you can use the &#8220;with&#8221; syntax:<br />
with open(filename) as f:<br />
&nbsp;&nbsp;&nbsp;&nbsp;for num, line in enumerate(f):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass</p>
<p>Here&#8217;s a nice link that explains how file handles and scope work in CPython:<br />
<a href="http://www.diveintopython.org/object_oriented_framework/instantiating_classes.html#fileinfo.scope">http://www.diveintopython.org/object_oriented_framework/instantiating_classes.html#fileinfo.scope</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wcyee</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-134</link>
		<dc:creator>wcyee</dc:creator>
		<pubDate>Sat, 16 Feb 2008 09:12:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-134</guid>
		<description>I&#039;m just learning python and I see code like this often:

for line_num, line in enumerate(open(filename)):

My question is: don&#039;t you need the file handle to close the file? A lot of python code out there never takes that into account. Is there something I don&#039;t know? Or am I just unnecessarily paranoid about resource leaks.</description>
		<content:encoded><![CDATA[<p>I&#8217;m just learning python and I see code like this often:</p>
<p>for line_num, line in enumerate(open(filename)):</p>
<p>My question is: don&#8217;t you need the file handle to close the file? A lot of python code out there never takes that into account. Is there something I don&#8217;t know? Or am I just unnecessarily paranoid about resource leaks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-138</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Fri, 15 Feb 2008 06:25:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-138</guid>
		<description>@Pete Cable
Quite right, sorry about that Anonymous. Nice trick about not unpacking the tuples returned by enumerate, although indexing on result seems somehow kind of vulgar... :)</description>
		<content:encoded><![CDATA[<p>@Pete Cable<br />
Quite right, sorry about that Anonymous. Nice trick about not unpacking the tuples returned by enumerate, although indexing on result seems somehow kind of vulgar&#8230; <img src='http://ginstrom.com/scribbles/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pete Cable</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-137</link>
		<dc:creator>Pete Cable</dc:creator>
		<pubDate>Fri, 15 Feb 2008 05:17:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-137</guid>
		<description>@ryan
Anonymous&#039; list comprehension still gives you line numbers... he just returns the tuple from enumerate. However, the line numbers are zero-indexed instead of one-indexed like yours.</description>
		<content:encoded><![CDATA[<p>@ryan<br />
Anonymous&#8217; list comprehension still gives you line numbers&#8230; he just returns the tuple from enumerate. However, the line numbers are zero-indexed instead of one-indexed like yours.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-130</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Fri, 15 Feb 2008 02:10:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-130</guid>
		<description>@Ian
Yes, if the number of expected results is large. The enumerator is already a generator.

@Anonymous
True, but I&#039;m assuming that the line numbers are actually needed. (The function is adapted from an actual query of comp.lang.python that asked something similar)

@mackstann
You&#039;re right, I had totally forgotten about that. So the increment operation isn&#039;t contributing to the slowdown; just the line count :)</description>
		<content:encoded><![CDATA[<p>@Ian<br />
Yes, if the number of expected results is large. The enumerator is already a generator.</p>
<p>@Anonymous<br />
True, but I&#8217;m assuming that the line numbers are actually needed. (The function is adapted from an actual query of comp.lang.python that asked something similar)</p>
<p>@mackstann<br />
You&#8217;re right, I had totally forgotten about that. So the increment operation isn&#8217;t contributing to the slowdown; just the line count <img src='http://ginstrom.com/scribbles/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mackstann</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-136</link>
		<dc:creator>mackstann</dc:creator>
		<pubDate>Fri, 15 Feb 2008 01:33:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-136</guid>
		<description>Python generally caches numeric (and maybe string) constants, so it actually only creates the 1 integer object once.</description>
		<content:encoded><![CDATA[<p>Python generally caches numeric (and maybe string) constants, so it actually only creates the 1 integer object once.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-133</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Fri, 15 Feb 2008 00:52:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-133</guid>
		<description>Or, even shorter and easier to read - remove the line_num, line stuff and just focus on the result:

[result for result in enumerate(open(filename)) if to_find in result[1]]</description>
		<content:encoded><![CDATA[<p>Or, even shorter and easier to read &#8211; remove the line_num, line stuff and just focus on the result:</p>
<p>[result for result in enumerate(open(filename)) if to_find in result[1]]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashot</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-132</link>
		<dc:creator>ashot</dc:creator>
		<pubDate>Thu, 14 Feb 2008 23:00:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-132</guid>
		<description>One line, most pythonic:

 [(line_num,line) for line_num,line in enumerate(open(filename)) if to_find in line]</description>
		<content:encoded><![CDATA[<p>One line, most pythonic:</p>
<p> [(line_num,line) for line_num,line in enumerate(open(filename)) if to_find in line]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/comment-page-1/#comment-131</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Thu, 14 Feb 2008 21:52:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/#comment-131</guid>
		<description>If the file size is large you might find a generator approach useful.

def search(target, stream):
&#160;&#160;&#160;&#160;for line in stream:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if target in line:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;yield line

or

search = lambda target, stream: (line for line in stream if target in line)

for line in search(&quot;foo&quot;, open(&quot;bar&quot;)):
&#160;&#160;&#160;&#160;process(line)</description>
		<content:encoded><![CDATA[<p>If the file size is large you might find a generator approach useful.</p>
<p>def search(target, stream):<br />
&nbsp;&nbsp;&nbsp;&nbsp;for line in stream:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if target in line:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield line</p>
<p>or</p>
<p>search = lambda target, stream: (line for line in stream if target in line)</p>
<p>for line in search(&#8220;foo&#8221;, open(&#8220;bar&#8221;)):<br />
&nbsp;&nbsp;&nbsp;&nbsp;process(line)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

