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


Groups > comp.lang.python > #10545

Re: removing nested iffs

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; '"""': 0.07; 'defined.': 0.09; 'none:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:removing': 0.09; 'wrote:': 0.15; '"build': 0.16; 'nesting': 0.16; 'pairs:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'argument': 0.16; 'def': 0.16; 'elegant': 0.23; 'string': 0.26; 'function': 0.26; "i'm": 0.27; 'raise': 0.28; 'server': 0.29; 'yield': 0.29; 'from:addr:web.de': 0.30; 'skip:{ 20': 0.30; 'source': 0.32; 'break': 0.33; 'skip:" 20': 0.33; 'to:addr:python- list': 0.34; 'header:X-Complaints-To:1': 0.34; 'there': 0.34; 'received:org': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'format': 0.40
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: removing nested iffs
Date Fri, 29 Jul 2011 22:42:58 +0200
Organization None
References <CAAAhF4GeXr+vxNCcJeHm1LfvTciqzSTrp0f3onx020PJSGh4MA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a65b.dip.t-dialin.net
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.1632.1311972191.1164.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311972191 news.xs4all.nl 23871 [2001:888:2000:d::a6]:49441
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10545

Show key headers only | View raw


Josh Benner wrote:

> I'm writing a function to create a string that gets longer iff an argument
> is defined.  In there a more elegant way than nesting all those ifs?
> 
> def format_rsync_src_string(args, server="RSYNC"):
>     """ Format an rsync source directory string. """
>     if args.server is None:
>         raise CopyNightlyError("No rsync server provided.")
>     src = "{0}::".format(args.server)
>     if args.project not None:
>         src += "{0}/".format(args.project)
>         if args.version not None:
>                 src += "{0}/".format(args.version)
>                 if args.build not None:
>                     src += "Build {0}".format(args.build)
>     return src

How about 

def format_pairs(pairs):
    for template, value in pairs:
        if value is None:
            break
        yield template.format(value)

def format_rsync_src_string(args, server="RSYNC"):
    """ Format an rsync source directory string. """
    if args.server is None:
        raise CopyNightlyError("No rsync server provided.")

    return "".join(format_pairs([
        ("{0}::", args.server),
        ("{0}/", args.project),
        ("{0}/", args.version),
        ("Build {0}", args.build)]))

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


Thread

Re: removing nested iffs Peter Otten <__peter__@web.de> - 2011-07-29 22:42 +0200

csiph-web