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


Groups > comp.lang.python > #12258

Re: how to format long if conditions

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'else:': 0.03; 'instance': 0.05; 'suppose': 0.05; 'pep': 0.07; 'suggestions.': 0.07; 'function:': 0.09; 'def': 0.15; '\xc2\xa0if': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'rewrite': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'formatting': 0.23; 'helper': 0.23; 'suggests': 0.23; 'code': 0.25; "i'm": 0.27; 'all,': 0.28; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'example': 0.30; 'subject:format': 0.30; 'thanks': 0.30; 'class': 0.30; 'version': 0.32; 'wondering': 0.33; "i've": 0.34; '...': 0.34; 'do?': 0.34; 'statements': 0.37; 'thread': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'could': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'define': 0.39; 'believe': 0.65; 'details': 0.65; 'benefit': 0.66; 'as:': 0.70; 'august': 0.70; 'skip:\xc2 10': 0.74; 'exercise.': 0.84; 'hybrid': 0.84; 'other):': 0.84; 'right)': 0.84; 'received:209.85.218.46': 0.91; 'received:mail- yi0-f46.google.com': 0.91; 'subject:long': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=NS4RJC62jgCyuSLiHB8FzzYztln4uakERrOAVto/+54=; b=qdi/qfTKYoR1+Y0n4NMhZuGBHPbIQMc/kwWnlF6uy1p3iiDC0COBq7Dip/zA+h3vm3 6SHZ32C4kXGA5nvu447XZYgqjaZ05CjB7vkNM7wQgTbe7bgi4P7QwqoUiMHKfs/FN8KC Fme+oRnlywD2U6alA/Tc6pfPFb8a7NkBv0V9k=
MIME-Version 1.0
In-Reply-To <4e589b9b$0$29981$c3e8da3$5496439d@news.astraweb.com>
References <mailman.457.1314428909.27778.python-list@python.org> <4e589b9b$0$29981$c3e8da3$5496439d@news.astraweb.com>
Date Sat, 27 Aug 2011 11:24:33 +0100
Subject Re: how to format long if conditions
From Arnaud Delobelle <arnodel@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.459.1314440676.27778.python-list@python.org> (permalink)
Lines 118
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314440676 news.xs4all.nl 2464 [2001:888:2000:d::a6]:40468
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12258

Show key headers only | View raw


On 27 August 2011 08:24, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Arnaud Delobelle wrote:
>
>> Hi all,
>>
>> I'm wondering what advice you have about formatting if statements with
>> long conditions (I always format my code to <80 colums)
>>
>> Here's an example taken from something I'm writing at the moment and
>> how I've formatted it:
>>
>>
>>         if (isinstance(left, PyCompare) and isinstance(right, PyCompare)
>>                 and left.complist[-1] is right.complist[0]):
>>             py_and = PyCompare(left.complist + right.complist[1:])
>>         else:
>>             py_and = PyBooleanAnd(left, right)
>>
>> What would you do?
>
> I believe that PEP 8 now suggests something like this:
>
>        if (
>                isinstance(left, PyCompare) and isinstance(right, PyCompare)
>                and left.complist[-1] is right.complist[0]):
>            )
>            py_and = PyCompare(left.complist + right.complist[1:]
>        else:
>            py_and = PyBooleanAnd(left, right)
>
>
> I consider that hideous and would prefer to write this:
>
>
>        if (isinstance(left, PyCompare) and isinstance(right, PyCompare)
>            and left.complist[-1] is right.complist[0]):
>            py_and = PyCompare(left.complist + right.complist[1:]
>        else:
>            py_and = PyBooleanAnd(left, right)
>
>
> Or even this:
>
>        tmp = (
>            isinstance(left, PyCompare) and isinstance(right, PyCompare)
>            and left.complist[-1] is right.complist[0])
>            )
>        if tmp:
>            py_and = PyCompare(left.complist + right.complist[1:]
>        else:
>            py_and = PyBooleanAnd(left, right)
>
>
> But perhaps the best solution is to define a helper function:
>
> def is_next(left, right):
>    """Returns True if right is the next PyCompare to left."""
>    return (isinstance(left, PyCompare) and isinstance(right, PyCompare)
>        and left.complist[-1] is right.complist[0])
>    # PEP 8 version left as an exercise.
>
>
> # later...
>        if is_next(left, right):
>            py_and = PyCompare(left.complist + right.complist[1:]
>        else:
>            py_and = PyBooleanAnd(left, right)
>

Thanks Steven and Hans for you suggestions.  For this particular
instance I've decided to go for a hybrid approach:

* Add two methods to PyCompare:

class PyCompare(PyExpr):
    ...
    def extends(self, other):
        if not isinstance(other, PyCompare):
            return False
        else:
            return self.complist[0] == other.complist[-1]
    def chain(self, other):
        return PyCompare(self.complist + other.complist[1:])

* Rewrite the if as:

        if isinstance(right, PyCompare) and right.extends(left):
            py_and = left.chain(right)
        else:
            py_and = PyBooleanAnd(left, right)


The additional benefit is to hide the implementation details of
PyCompare (I suppose this could illustrate the thread on when to
create functions).

-- 
Arnaud

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


Thread

how to format long if conditions Arnaud Delobelle <arnodel@gmail.com> - 2011-08-27 08:08 +0100
  Re: how to format long if conditions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 17:24 +1000
    Re: how to format long if conditions Arnaud Delobelle <arnodel@gmail.com> - 2011-08-27 11:24 +0100
    Re: how to format long if conditions Ben Finney <ben+python@benfinney.id.au> - 2011-08-27 22:04 +1000
  Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 09:50 +0200
    Re: how to format long if conditions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 19:05 +1000
      Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 12:51 +0200
    Re: how to format long if conditions "Colin J. Williams" <cjw@ncf.ca> - 2011-08-27 11:16 -0400
      Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 17:53 +0200
        Re: how to format long if conditions "Colin J. Williams" <cjw@ncf.ca> - 2011-08-27 17:25 -0400
  Re: how to format long if conditions Roy Smith <roy@panix.com> - 2011-08-27 12:39 -0400

csiph-web