Chris's Wiki :: blog/python/LiftingPython2ToPython3 Commentshttps://utcc.utoronto.ca/~cks/space/blog/python/LiftingPython2ToPython3?atomcommentsDWiki2018-07-25T15:24:46ZRecent comments in Chris's Wiki :: blog/python/LiftingPython2ToPython3.By Aristotle Pagaltzis on /blog/python/LiftingPython2ToPython3tag:CSpace:blog/python/LiftingPython2ToPython3:4123dcc09c2d7fb46a1815f7a43d0a81ea15b2edAristotle Pagaltzishttp://plasmasturm.org/<div class="wikitext"><p>sapphirepaw:</p>
<ol><li>Key-based sorting <em>is</em> DSU – the key is what you would compute in the decoration phase – except that the wrapping and unwrapping is taken care of for you implicitly, without you having to write it out. Explicit DSU under these circumstances is just extra verbosity for no reason. So explicit DSU is obsolete in Python 3.<p>
</li>
<li>Since DSU and key-based sorting are the same thing, DSU is not a solution to any problem anyone has with key-based sorting (or vice versa).</li>
</ol>
<p>Clément:</p>
<p>If what you want is to know how to translate a comparator into a derivation of a value that <em>naturally</em> compares the same way as the comparator function would compare the original values, then <code>cmp_to_key</code> is less than useful.</p>
<p>The function it returns does <em>not</em> somehow derive such a value, it just puts each list element in a wrapper that attaches the comparator function to it. These values compare with each other like the comparator would compare the list elements… tautologically, because the comparator is still getting called every time to compare them.</p>
<p>So you pay the overhead for the implicit DSU in key-based sorting… just so you can waste it, and in fact you pay some extra <em>just</em> for the privilege of wasting it, in the form of overhead for reaching through the wrapper.</p>
<p>As the <code>cmp_to_key</code> docs say, it’s meant mostly as a transition aid for old code.</p>
</div>2018-07-25T15:24:46ZBy Twirrim on /blog/python/LiftingPython2ToPython3tag:CSpace:blog/python/LiftingPython2ToPython3:3c61baafe0b1d117bdb2723c4834f7a3aed03d16Twirrim<div class="wikitext"><p>I'm pretty much a fan of pylint. Catches / resolves so many things. That said, in tox.ini I've got it configured:</p>
<p>ignore = E501, E402, E722, E127, E128 # ignore line too long, imports not at top of file, ignore bare except, ignore 2x visual indentation complaints</p>
<p>That deals with about 95% of the annoying junk and just leaves the valuable stuff.</p>
<p>If you fancy a look, there's an interesting uncompromising code formatter for Python 3 called "Black" <a href="https://github.com/ambv/black">https://github.com/ambv/black</a>. It aims to be the equivalent of go-fmt. It makes some odd choices from time to time, some of which enter the territory of "blocker" for me, but some of our coding needs that trigger them are somewhat unusual.</p>
</div>2018-07-24T20:14:34ZBy Clément on /blog/python/LiftingPython2ToPython3tag:CSpace:blog/python/LiftingPython2ToPython3:7d79b69658f5bde5cfd0c7146c2aa8bc1e8135c7Clément<div class="wikitext"><blockquote><p>although there are still nuances that using sort keys can't easily capture.</p>
</blockquote>
<p>That's incorrect: any comparison function can be converted into a sort key, and there's even a standard library module that makes it trivial. From the docs: Use functools.cmp_to_key() to convert an old-style cmp function to a key function.</p>
</div>2018-07-24T17:37:26ZBy Chris Siebenmann on /blog/python/LiftingPython2ToPython3tag:CSpace:blog/python/LiftingPython2ToPython3:4ea67f75cdbe87872f1042fb28c8aab7e46553e7Chris Siebenmann<div class="wikitext"><p>I probably should have given an example of the bare <code>print</code> thing,
but you have it right. I had a few bits of code where things went
basically (in Python 2):</p>
<blockquote><pre>
print "..."
for ...:
print "more stuff"
...
print
</pre>
</blockquote>
<p>When I went over the whole thing, I basically did an editor search
for '<code>print </code>' (with the trailing space), which missed the last one.
In Python 3, 'print ...' is a syntax error, but the last line by itself
is just a more minimal version of '<code>_ = print</code>', which is silently not
a syntax error.</p>
<p>As far as keys versus comparison functions goes, my understanding is
that getting keys can be more efficient, especially if the key is just
a field or element (which it often is), because you can then reuse the
same key across multiple comparisons instead of starting from scratch
each time. I believe that my sorting cases here are relatively unusual.</p>
</div>2018-07-24T14:59:13ZBy sapphirepaw on /blog/python/LiftingPython2ToPython3tag:CSpace:blog/python/LiftingPython2ToPython3:23ff70656269e593d596444618f0b025c704f933sapphirepawhttps://keybase.io/sapphirepaw<div class="wikitext"><p>I was a bit confused about a "bare <code>print</code> statement" doing nothing, because <code>print()</code> prints the newline, then I finally realized you meant the former is an expression returning <code><built-in function print></code> without side effects.</p>
<p>For character encodings, it's my current model that Python will use some default encoding, and raise an exception if that doesn't work losslessly. Apparently that default depends on the system and environment, but if it's ASCII-compatible like UTF-8 or ISO-8859-1, then ASCII will work fine.</p>
<p>The final point I want to bring up is the peculiarly Pythonic "decorate-sort-undecorate" (aka DSU) idiom. It looks something like:</p>
<pre>
r = [t[-1] for t in
sorted((a.prio or sys.maxint, a.totbytes, a)
for a in athing)]
</pre>
<p>So working bottom up, we go over the thing to sort, make a tuple of <code>(k1, k2, value)</code> from each item, sort by that tuple, and finally convert the tuple back into the <code>value</code> alone. Making/removing the tuple is the decorate/undecorate part.</p>
<p>I think they missed the "there should be one obvious way to do it" thing there, since most other languages I use take the "pass a comparison function to sort" approach. And I assume that is more efficient than allocating tuples to fake it. But DSU is an option, I suppose.</p>
</div>2018-07-24T13:34:00Z