Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61248

Re: One liners

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <torriem+gmail@torriefamily.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'programmer': 0.03; 'else:': 0.03; 'operator': 0.03; 'languages,': 0.04; 'base.': 0.05; 'abuse': 0.07; 'suppose': 0.07; 'git': 0.09; 'iterate': 0.09; 'shame': 0.09; 'def': 0.12; 'anyway': 0.14; "wouldn't": 0.14; 'background,': 0.16; 'bisect': 0.16; 'c/c++': 0.16; 'complaining': 0.16; 'dictionary.': 0.16; 'examples?': 0.16; 'expressions,': 0.16; 'expressions.': 0.16; 'fails.': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'itervalues': 0.16; 'limiting.': 0.16; 'nevertheless': 0.16; 'only)': 0.16; 'readable': 0.16; 'recently.': 0.16; 'roy': 0.16; 'targets': 0.16; 'targets:': 0.16; 'ternary': 0.16; ':-)': 0.16; 'language': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'slightly': 0.19; 'seems': 0.21; 'example': 0.22; 'header:User-Agent:1': 0.23; 'him.': 0.24; 'this:': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'rest': 0.29; 'compared': 0.30; 'returned': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'about.': 0.31; "d'aprano": 0.31; 'keys': 0.31; 'lot.': 0.31; 'python).': 0.31; 'remotely': 0.31; 'steven': 0.31; 'probably': 0.32; 'skip:c 30': 0.32; 'quite': 0.32; 'maybe': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'agree': 0.35; 'skip:- 50': 0.35; 'common': 0.35; 'skip:s 30': 0.35; 'anybody': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'fun,': 0.36; 'set.': 0.36; 'skip:s 60': 0.36; 'done': 0.36; 'list': 0.37; 'message-id:@gmail.com': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'little': 0.38; 'does': 0.39; 'though,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'expression': 0.60; 'warm': 0.60; 'identify': 0.61; 'took': 0.61; 'matter': 0.61; "you're": 0.61; 'first': 0.61; 'making': 0.63; 'skip:n 10': 0.64; 'map': 0.64; 'stand': 0.64; 'more': 0.64; 'love': 0.65; 'relatively': 0.65; 'here': 0.66; 'smith': 0.68; 'subject:One': 0.74; 'unusual': 0.74; 'saw': 0.77; 'article': 0.77; 'lack': 0.78; '(none': 0.84; 'clearer': 0.84; 'concept.': 0.84; 'confusing': 0.84; 'distracted': 0.84; 'returns.': 0.84; 'sets,': 0.84; 'do:': 0.91; 'hand,': 0.93
X-Virus-Scanned amavisd-new at torriefamily.org
Date Sat, 07 Dec 2013 09:56:04 -0700
From Michael Torrie <torriem@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12
MIME-Version 1.0
To python-list@python.org
Subject Re: One liners
References <CAGGBd_o1TS02cosi+q9YeN5rnWJxe5mU7xtCTuTMZGO0ntF5TA@mail.gmail.com> <52A26763.3070803@gmail.com> <CAGGBd_rsP3rU6X30W_ocF8vNrPrLBrivT+nCv-1htgehmPZWrg@mail.gmail.com> <mailman.3678.1386375653.18130.python-list@python.org> <52a287cb$0$30003$c3e8da3$5496439d@news.astraweb.com> <roy-10E325.22270006122013@news.panix.com>
In-Reply-To <roy-10E325.22270006122013@news.panix.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3703.1386435393.18130.python-list@python.org> (permalink)
Lines 95
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386435393 news.xs4all.nl 2954 [2001:888:2000:d::a6]:52712
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61248

Show key headers only | View raw


On 12/06/2013 08:27 PM, Roy Smith wrote:
> In article <52a287cb$0$30003$c3e8da3$5496439d@news.astraweb.com>,
>  Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> 
>> The ternary if is slightly unusual and unfamiliar
> 
> It's only unusual an unfamiliar if you're not used to using it :-)  
> Coming from a C/C++ background, I always found the lack of a ternary 
> expression rather limiting.  There was much rejoicing in these parts 
> when it was added to the language relatively recently.  I use them a lot.
> 
> On the other hand, I found list comprehensions to be mind-bogglingly 
> confusing when I first saw them (read: slightly unusual and unfamiliar).  
> It took me a long time to warm up to the concept.  Now I love them.
> 
>> As for readability, I accept that ternary if is unusual compared to other 
>> languages, but it's still quite readable in small doses. If you start 
>> chaining them:
>>
>> result = a if condition else b if flag else c if predicate else d 
>>
>> you probably shouldn't.
> 
> That I agree with (and it's just as true in C as it is in Python).
> 
> Just for fun, I took a look through the Songza code base.  66 kloc of 
> non-whitespace Python.  I found 192 ternary expressions.  Here's a few 
> of the more bizarre ones (none of which I consider remotely readable):
> 
> --------------------------------------------------
> extracols = sorted(set.union(*(set(t.data.keys()) for t in tracks))) if 
> tracks else []

This is a generator expressions, and ternary ifs are common and often
needed in generator expressions.

> --------------------------------------------------
> c2s = compids2songs(set(targets.keys()) | 
> set.union(*map(set,targets.itervalues())),self.docmap,self.logger) if 
> targets else {}

I suspect the ternary distracted you on this one.  The ternary here is
needed because if targets is None the expression fails.  This part
anyway is a common idiom.

The rest is basically making a set (list of unique items only) of the
combined keys and values from the "targets" dictionary.  Now I'm not
sure why the programmer needs do this, but nevertheless that's what it's
doing.  set.union is used because that can iterate over a list of sets,
which is what the map returns.  I suppose they could have done this, but
it wouldn't be much clearer unless you knew what sets, map and
itervalues do:

if targets:
    c2s = compids2songs(
            set(targets.keys()) |
              set.union(*map(set,targets.itervalues())),
            self.docmap,
            self.logger )
else:
   c2s = {}

In any case the ternary operator isn't really the part you were
complaining about.  Personally if I needed to do this particular
operation a lot (combine keys and values into a set), I'd write a
function that returned the set.  Still can't avoid the ternary, though,
unless you made compids2songs a little smarter (and we don't know what
compids2songs does with an empty set):

def dict_keys_and_values_set (some_dict):
   return set(some_dict.keys()) |
            set.union(*map(set,some_dict.itervalues()))

c2s = compids2songs( dict_keys_and_values_set(targets) ) if targets else {}

or I suppose you could o this:

c2s = {}
if targets: c2s = compids2songs( dict_keys_and_values_set(targets) )

Just a matter of taste.

> --------------------------------------------------
> code = 2 if (pmp3,paac)==(mmp3,maac) else 3 if any(x is None for x in 
> (pmp3,paac,mmp3,maac)) else 4
> --------------------------------------------------

This one probably could stand to be reworked for sure!  A standard if
block would be much clearer.  Definitely an example of a programmer
thinking he was clever... maybe a git bisect could identify the author
and we can shame him.

> Anybody else have some fun ternary abuse examples?

Only the last one seems to be problematic to me.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: One liners Michael Torrie <torriem@gmail.com> - 2013-12-06 17:20 -0700
  Re: One liners Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-07 02:28 +0000
    Re: One liners Chris Angelico <rosuav@gmail.com> - 2013-12-07 13:40 +1100
    Re: One liners Roy Smith <roy@panix.com> - 2013-12-06 22:27 -0500
      Re: One liners Chris Angelico <rosuav@gmail.com> - 2013-12-07 15:06 +1100
      Re: One liners Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-07 11:27 +0000
        Re: One liners Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-12-07 14:41 +0200
          Re: One liners Rotwang <sg552@hotmail.co.uk> - 2013-12-07 16:13 +0000
            Re: One liners Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-07 16:25 +0000
              Re: One liners Rotwang <sg552@hotmail.co.uk> - 2013-12-08 02:23 +0000
            Re: One liners Michael Torrie <torriem@gmail.com> - 2013-12-07 09:26 -0700
            Re: One liners Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-12-07 19:49 +0200
            Re: One liners Terry Reedy <tjreedy@udel.edu> - 2013-12-07 16:02 -0500
      Re: One liners Michael Torrie <torriem@gmail.com> - 2013-12-07 09:56 -0700
        Re: One liners rusi <rustompmody@gmail.com> - 2013-12-07 09:10 -0800
      Re: One liners Michael Torrie <torriem@gmail.com> - 2013-12-07 10:02 -0700

csiph-web