<?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>chys&#039;s random notes &#187; Python</title>
	<atom:link href="http://en.chys.info/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A problem with pipes in Python 3</title>
		<link>http://en.chys.info/2011/11/a-problem-with-pipes-in-python-3/</link>
		<comments>http://en.chys.info/2011/11/a-problem-with-pipes-in-python-3/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 09:00:30 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=901</guid>
		<description><![CDATA[The most disturbing change from Python 2 to 3 definitely is not the print() function; nor that some functions which used to return lists now return iterators; nor the removal of __cmp__; but the transition to Unicode. I&#8217;m completely supportive of the transition per se, but I&#8217;m disappointed that they&#8217;re trying to compel us to [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/08/cmp-should-be-reintroduced-to-python/' rel='bookmark' title='__cmp__ should be reintroduced to Python'>__cmp__ should be reintroduced to Python</a></li>
<li><a href='http://en.chys.info/2008/10/another-vmware-problem/' rel='bookmark' title='Another VMware problem'>Another VMware problem</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The most disturbing <a href="http://docs.python.org/release/3.0.1/whatsnew/3.0.html">change from Python 2 to 3</a> definitely is not the <code>print()</code> function; nor that some functions which used to return lists now return iterators; nor the <a href="/2009/08/cmp-should-be-reintroduced-to-python/">removal of <code>__cmp__</code></a>; but the transition to Unicode.</p>
<p>I&#8217;m completely supportive of the transition <em>per se</em>, but I&#8217;m disappointed that they&#8217;re trying to compel us to use Unicode by dropping useful functionalities for byte-streams/8-byte strings. For example, <code>bytes</code> has no <a href="http://www.python.org/dev/peps/pep-3101/"><code>format</code></a> or <a href="http://docs.python.org/release/2.5.2/lib/typesseq-strings.html"><code>%</code></a> in Python 3.</p>
<p>I have some code like this:</p>
<blockquote><pre>
proc = <a href="http://docs.python.org/release/3.1.3/library/subprocess.html">subprocess</a>.Popen((....), stdout=subprocess.PIPE)
for line in proc.stdout:
    ...
</pre>
</blockquote>
<p>I found that, on Linux, this code snippet is almost 10 times slower in Python 3 than in Python 2. Then I <a href="http://en.wikipedia.org/wiki/Strace">strace</a>&#8216;d the code and found Python 3 is passing length 1 to <a href="http://linux.die.net/man/2/read"><code>read</code></a>, incurring thousands of times more system calls than Python 2. Are you kidding me? I was forced to use something like <code>proc.stdout.read(...)</code>.</p>
<p>I understand this is not the direct result of the transition to Unicode, but it is somehow related.</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/08/cmp-should-be-reintroduced-to-python/' rel='bookmark' title='__cmp__ should be reintroduced to Python'>__cmp__ should be reintroduced to Python</a></li>
<li><a href='http://en.chys.info/2008/10/another-vmware-problem/' rel='bookmark' title='Another VMware problem'>Another VMware problem</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2011/11/a-problem-with-pipes-in-python-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integer division</title>
		<link>http://en.chys.info/2011/05/integer-division/</link>
		<comments>http://en.chys.info/2011/05/integer-division/#comments</comments>
		<pubDate>Wed, 25 May 2011 07:31:46 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=890</guid>
		<description><![CDATA[C89 and C++98 say the result of an integer division where the divisor and/or dividend is negative is implementation defined. This reflects that early hardware implemented integer divisions differently. According to C89/C++98, we may have either (-3)/2 == -1 (round toward zero) or (-3)/2 == -2 (round toward negative infinity). It appears round toward zero [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>C89 and C++98 say the result of an integer division where the divisor and/or dividend is negative is <a href="http://gcc.gnu.org/onlinedocs/gcc-3.3.6/cpp/Implementation_002ddefined-behavior.html">implementation defined</a>. This reflects that early hardware implemented integer divisions differently.</p>
<p>According to C89/C++98, we may have either <code>(-3)/2 == -1</code> (round toward zero) or <code>(-3)/2 == -2</code> (round toward negative infinity).</p>
<p>It appears <em>round toward zero</em> has become the overwhelming <em>de facto</em> standard now, adopted by both hardware and software vendors. Now both C and C++ explicitly require round toward zero in their new standards (C99 and <a href="http://en.wikipedia.org/wiki/C++0x">C++2011</a>*).</p>
<p>Division of negative integers has always been a complicated problem. Fortran mandated the same round-toward-zero mode much earlier than C/C++; so did Java. Python, on the other hand, has required round toward -∞ (i.e. <code>(-3)//2 == -2</code>) from its beginning. Everybody, nevertheless, agrees that <code>a/b*b + a%b == a</code> should always hold.</p>
<p>* C++0x has yet to be officially approved. Hopefully it will be approved within this year and known as C++2011. I&#8217;m using this name prematurely.</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2011/05/integer-division/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Python sort trick</title>
		<link>http://en.chys.info/2009/10/a-python-sort-trick/</link>
		<comments>http://en.chys.info/2009/10/a-python-sort-trick/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 16:50:33 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=683</guid>
		<description><![CDATA[&#62;&#62;&#62; a = [12,14,133,130,176,25,54,79,127] &#62;&#62;&#62; b = sorted(range(len(a)),key=a.__getitem__) &#62;&#62;&#62; b [0, 1, 5, 6, 7, 8, 3, 2, 4] &#62;&#62;&#62; map(a.__getitem__,b) [12, 14, 25, 54, 79, 127, 130, 133, 176] b is the so called sort index. Reference http://www.newsmth.net/bbscon.php?bid=284&#038;id=59819 No related posts.<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<blockquote><pre>&gt;&gt;&gt; a = [12,14,133,130,176,25,54,79,127]
&gt;&gt;&gt; b = sorted(range(len(a)),key=a.__getitem__)
&gt;&gt;&gt; b
[0, 1, 5, 6, 7, 8, 3, 2, 4]
&gt;&gt;&gt; map(a.__getitem__,b)
[12, 14, 25, 54, 79, 127, 130, 133, 176]
</pre>
</blockquote>
<p><code>b</code> is the so called sort index.</p>
<p><strong>Reference</strong><br />
<a href="http://www.newsmth.net/bbscon.php?bid=284&#038;id=59819">http://www.newsmth.net/bbscon.php?bid=284&#038;id=59819</a></p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/10/a-python-sort-trick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>__cmp__ should be reintroduced to Python</title>
		<link>http://en.chys.info/2009/08/cmp-should-be-reintroduced-to-python/</link>
		<comments>http://en.chys.info/2009/08/cmp-should-be-reintroduced-to-python/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 03:02:02 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=636</guid>
		<description><![CDATA[A lot of people think this decision to drop __cmp__ from Python sucks. (1 2) The cmp argument is also dropped from list.sort and sorted. Some people explain that some programmers used to use cmp when it is more convenient and efficient to use key. By dropping cmp, poor programmers are forced to use key. [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2011/11/a-problem-with-pipes-in-python-3/' rel='bookmark' title='A problem with pipes in Python 3'>A problem with pipes in Python 3</a></li>
<li><a href='http://en.chys.info/2009/10/a-python-sort-trick/' rel='bookmark' title='A Python sort trick'>A Python sort trick</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>A lot of people think this decision to drop <code>__cmp__</code> from Python sucks. (<a href="http://mail.python.org/pipermail/python-3000/2007-October/010920.html">1</a> <a href="http://oakwinter.com/code/porting-setuptools-to-py3k/">2</a>)</p>
<p>The <code>cmp</code> argument is also dropped from <code>list.sort</code> and <code><a href="http://docs.python.org/library/functions.html#sorted">sorted</a></code>. <a href="http://bytes.com/topic/python/answers/844614-python-3-sorting-comparison-function#post3382493">Some people</a> explain that some programmers used to use <code>cmp</code> when it is more convenient and efficient to use <code>key</code>. By dropping <code>cmp</code>, poor programmers are forced to use <code>key</code>. On the other hand, almost all comparators found in practice are actually based on some kind of key function.</p>
<p>Well, theoretically this probably is true: <code>something.sort(key=locale.strxfrm)</code> is better than <code>something.sort(cmp=locale.strcoll)</code>.</p>
<p>BUT, what can I do if I only have a third-party comparator?</p>
<p>I have a Python script where I use <code>vercmp</code>, provided in the source of Gentoo Portage, to sort a list of Gentoo packages. No corresponding <code>verkey</code> is provided. So now, in Python 3, I have to either write a <code>verkey</code> myself, or use the following ugly and inefficient workaround:</p>
<pre>class VerKey:
    def __init__(self,str):
        self.str = str
    def __lt__(self,other):
        return vercmp(self.str, other.str) < 0

versions.sort (key = VerKey)
</pre>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2011/11/a-problem-with-pipes-in-python-3/' rel='bookmark' title='A problem with pipes in Python 3'>A problem with pipes in Python 3</a></li>
<li><a href='http://en.chys.info/2009/10/a-python-sort-trick/' rel='bookmark' title='A Python sort trick'>A Python sort trick</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/08/cmp-should-be-reintroduced-to-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

