<?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 for /code/blog</title>
	<atom:link href="http://codeblog.dhananjaynene.com/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeblog.dhananjaynene.com</link>
	<description>Code, code and code</description>
	<lastBuildDate>Tue, 28 Jun 2011 21:03:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Vasudev Ram</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-129</link>
		<dc:creator>Vasudev Ram</dc:creator>
		<pubDate>Tue, 28 Jun 2011 21:03:45 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-129</guid>
		<description>You&#039;re welcome, guys.

Corbin&#039;s solutions look interesting.

Iterators and generators (and generator expressions, which exist since a somewhat recent Python version) are pretty powerful. A pretty good resource for the first two at least, is the chapter about them in the Python Cookbook, 2nd edition - the chapter is by Raymond Hettinger, a Python core developer, who is the main designer of those two language features, IIRC. I have the book and read that chapter a while ago. Still have to get my head around some of the capabilities of those two features - some of them are a little mind-twisting - but very useful, including for things like efficiency / performance. An example that comes to mind, off the top of my head, from that chapter, is one that uses either or both of iterators and generators to implement an efficient yet memory-saving fetch routine for an unknown (and potentially very large) number of  records from a database cursor in a client-server environment (Python client, DB server). The problem is that if you use the Python DBI method fetchone(), you&#039;re not making full / efficient use of the capabilities of the database driver and the network, particularly if the table is a lookup table with a small record size, since in that case many records may fit into even a single network packet, while if you use fetchall(), you may overflow your client memory. So he implements a fetchmany() that avoids both issues, with a user-settable number of records to fetch in each call to the iterator - same number in each call, though - you set the number while creating the iterator, IIRC. Then, by tweaking that number over multiple runs, I guess you could find an optimal number for your particular hardware / software / network environment. Neat idea.

And speaking of the Python Cookbook, I heard somewhere that the 3rd edition is in the pipeline. Looking forward to it ...</description>
		<content:encoded><![CDATA[<p>You&#8217;re welcome, guys.</p>
<p>Corbin&#8217;s solutions look interesting.</p>
<p>Iterators and generators (and generator expressions, which exist since a somewhat recent Python version) are pretty powerful. A pretty good resource for the first two at least, is the chapter about them in the Python Cookbook, 2nd edition &#8211; the chapter is by Raymond Hettinger, a Python core developer, who is the main designer of those two language features, IIRC. I have the book and read that chapter a while ago. Still have to get my head around some of the capabilities of those two features &#8211; some of them are a little mind-twisting &#8211; but very useful, including for things like efficiency / performance. An example that comes to mind, off the top of my head, from that chapter, is one that uses either or both of iterators and generators to implement an efficient yet memory-saving fetch routine for an unknown (and potentially very large) number of  records from a database cursor in a client-server environment (Python client, DB server). The problem is that if you use the Python DBI method fetchone(), you&#8217;re not making full / efficient use of the capabilities of the database driver and the network, particularly if the table is a lookup table with a small record size, since in that case many records may fit into even a single network packet, while if you use fetchall(), you may overflow your client memory. So he implements a fetchmany() that avoids both issues, with a user-settable number of records to fetch in each call to the iterator &#8211; same number in each call, though &#8211; you set the number while creating the iterator, IIRC. Then, by tweaking that number over multiple runs, I guess you could find an optimal number for your particular hardware / software / network environment. Neat idea.</p>
<p>And speaking of the Python Cookbook, I heard somewhere that the 3rd edition is in the pipeline. Looking forward to it &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by 10 Smalltalk One Liners to Impress Your Friends &#124; Varvello On XP, Simplicity, Smalltalk, Agile, Programming</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-120</link>
		<dc:creator>10 Smalltalk One Liners to Impress Your Friends &#124; Varvello On XP, Simplicity, Smalltalk, Agile, Programming</dc:creator>
		<pubDate>Thu, 09 Jun 2011 21:42:28 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-120</guid>
		<description>[...] &#160; &#160; &#160; &#160; &#160; &#160;Python, [...]</description>
		<content:encoded><![CDATA[<p>[...] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Python, [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Parag Shah</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-114</link>
		<dc:creator>Parag Shah</dc:creator>
		<pubDate>Sat, 04 Jun 2011 12:01:30 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-114</guid>
		<description>Thanks Vasudev.</description>
		<content:encoded><![CDATA[<p>Thanks Vasudev.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Dhananjay Nene</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-113</link>
		<dc:creator>Dhananjay Nene</dc:creator>
		<pubDate>Sat, 04 Jun 2011 09:31:27 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-113</guid>
		<description>excellent! I would use tuple() instead of any() to retain all the found statuses.</description>
		<content:encoded><![CDATA[<p>excellent! I would use tuple() instead of any() to retain all the found statuses.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Corbin</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-112</link>
		<dc:creator>Corbin</dc:creator>
		<pubDate>Sat, 04 Jun 2011 09:07:18 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-112</guid>
		<description>You might be interested in my answers as well.

For (3), any() is a far more elegant solution:

&lt;code lang=&quot;python&quot;&gt;
  wordlist= [&quot;scala&quot;, &quot;akka&quot;, &quot;play framework&quot;, &quot;sbt&quot;, &quot;typesafe&quot;]
  tweet = &quot;This is an example tweet talking about scala and sbt.&quot;
  any(word in tweet for word in wordList)
&lt;/code&gt;

Another approach to (10) is to use some itertools:

&lt;code lang=&quot;python&quot;&gt;
  g = lambda l=[2]: ifilter(lambda x: (all(x % i for i in l) and (l.append(x) or True)), count(3))
&lt;/code&gt;

I answered all of them here: http://news.ycombinator.com/item?id=2610879</description>
		<content:encoded><![CDATA[<p>You might be interested in my answers as well.</p>
<p>For (3), any() is a far more elegant solution:</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; wordlist<span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;scala&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;akka&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;play framework&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;sbt&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;typesafe&quot;</span><span style="color: black;">&#93;</span><br />
&nbsp; tweet <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;This is an example tweet talking about scala and sbt.&quot;</span><br />
&nbsp; <span style="color: #008000;">any</span><span style="color: black;">&#40;</span>word <span style="color: #ff7700;font-weight:bold;">in</span> tweet <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> wordList<span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>Another approach to (10) is to use some itertools:</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; g <span style="color: #66cc66;">=</span> <span style="color: #ff7700;font-weight:bold;">lambda</span> l<span style="color: #66cc66;">=</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>: ifilter<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: black;">&#40;</span><span style="color: #008000;">all</span><span style="color: black;">&#40;</span>x % i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> l<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span>l.<span style="color: black;">append</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">True</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> count<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>I answered all of them here: <a href="http://news.ycombinator.com/item?id=2610879" rel="nofollow">http://news.ycombinator.com/item?id=2610879</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Dhananjay Nene</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-111</link>
		<dc:creator>Dhananjay Nene</dc:creator>
		<pubDate>Sat, 04 Jun 2011 03:28:15 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-111</guid>
		<description>range always creates a list. From the documentation 

range([start], stop[, step])¶

    This is a versatile function to create lists containing arithmetic progressions</description>
		<content:encoded><![CDATA[<p>range always creates a list. From the documentation </p>
<p>range([start], stop[, step])¶</p>
<p>    This is a versatile function to create lists containing arithmetic progressions</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Dhananjay Nene</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-110</link>
		<dc:creator>Dhananjay Nene</dc:creator>
		<pubDate>Sat, 04 Jun 2011 02:54:28 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-110</guid>
		<description>This needs to be corrected, as Vasudev points out later, range returns a list.</description>
		<content:encoded><![CDATA[<p>This needs to be corrected, as Vasudev points out later, range returns a list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Dhananjay Nene</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-109</link>
		<dc:creator>Dhananjay Nene</dc:creator>
		<pubDate>Sat, 04 Jun 2011 02:53:02 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-109</guid>
		<description>Interesting, I always thought range returns a generator. 

I later recollected range returns a list whereas it is xrange which returns a generator (or somthing rather similar in terms of a memory optimised way).

Thanks.</description>
		<content:encoded><![CDATA[<p>Interesting, I always thought range returns a generator. </p>
<p>I later recollected range returns a list whereas it is xrange which returns a generator (or somthing rather similar in terms of a memory optimised way).</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Vasudev Ram</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-108</link>
		<dc:creator>Vasudev Ram</dc:creator>
		<pubDate>Fri, 03 Jun 2011 21:12:27 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-108</guid>
		<description>Parag: the expression;

list(range(1,1001))

is redundant, because range() itself returns a list:

&gt;&gt;&gt;print type(range(1, 1001))
</description>
		<content:encoded><![CDATA[<p>Parag: the expression;</p>
<p>list(range(1,1001))</p>
<p>is redundant, because range() itself returns a list:</p>
<p>&gt;&gt;&gt;print type(range(1, 1001))</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on 10 Python one liners to impress your friends by Parag Shah</title>
		<link>http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/comment-page-1/#comment-104</link>
		<dc:creator>Parag Shah</dc:creator>
		<pubDate>Fri, 03 Jun 2011 16:45:41 +0000</pubDate>
		<guid isPermaLink="false">http://codeblog.dhananjaynene.com/?p=126#comment-104</guid>
		<description>Interesting... so the range function generates a list from operands when it is not asked to create a range for a specific list?</description>
		<content:encoded><![CDATA[<p>Interesting&#8230; so the range function generates a list from operands when it is not asked to create a range for a specific list?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

