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


Groups > comp.lang.python > #33145

Re: List comprehension for testing **params

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'true,': 0.04; 'arguments': 0.07; 'option,': 0.07; 'present,': 0.07; 'subject:skip:c 10': 0.07; 'testing,': 0.07; 'wrapped': 0.07; 'python': 0.09; 'exception:': 0.09; 'logic': 0.09; 'unexpected': 0.09; 'def': 0.10; 'empty.': 0.16; 'evaluates': 0.16; 'instead:': 0.16; 'msg):': 0.16; 'required):': 0.16; 'smtp,': 0.16; 'wrote:': 0.17; 'input': 0.18; 'define': 0.20; 'parameters': 0.20; 'explicit': 0.22; "i'd": 0.22; 'raise': 0.24; 'testing': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'checking': 0.27; 'message- id:@mail.gmail.com': 0.27; 'assert': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'keyword': 0.30; 'received:209.85.215.46': 0.30; 'generally': 0.32; '11,': 0.33; 'like:': 0.33; 'subject:List': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'nov': 0.35; 'requiring': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'method': 0.36; 'test': 0.36; 'being': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'you.': 0.61; 'first': 0.61; 'evaluate': 0.62; 'to,': 0.65; 'soon': 0.70; 'business': 0.70; 'state.': 0.71; "'for'": 0.84; 'login,': 0.84; 'required)': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=ODSQORmVlBhPmNbDYERJANvgWWchp9PHEwIezx8XxRs=; b=tvWkYkEKl/xtRHa98LNvWHBjgj+r5XZVl6KcOAdo9CU+pqIZWnmYF5s53L9x0qU3MX twRUCtEPS86pipyp93i08O+8aEYLqdoE9Kd+TX+2LiJ5x1ElFmvG4V7qmS+AxrlqKQ3k 4O+FR5oV7WgXzaBRrgMTw0PcjqeovsEMHQ+Q92EWIwNaey/YEtLUpkHLXxW10AQfUNbx K+J+2IWxHJ224NZJTGQV6BQ8ae06Lf09UYp+agILW+YFF8ZE+hTjsRNDb5SeQazB25fs YWlxNyXYlCrc/T6JhWPL8aY/cC6n6Dwmum0bNDUgNqJykPw6DmYvRqNOOp6Kw9bwM9QI wORg==
MIME-Version 1.0
In-Reply-To <50a0258f$0$21241$ba4acef3@reader.news.orange.fr>
References <50a0258f$0$21241$ba4acef3@reader.news.orange.fr>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 11 Nov 2012 15:56:52 -0700
Subject Re: List comprehension for testing **params
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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 <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.3563.1352674646.27098.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352674646 news.xs4all.nl 6891 [2001:888:2000:d::a6]:59427
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33145

Show key headers only | View raw


On Sun, Nov 11, 2012 at 3:24 PM, Cantabile <cantabile.03@wanadoo.fr> wrote:
> I'd like to do something like that instead of the 'for' loop in __init__:
>
> assert[key for key in required if key in params.keys()]

A list evaluates as true if it is not empty.  As long as at least one
of the required parameters is present, the list will not be empty and
will evaluate as true, and the assertion will pass.  Try this instead:

assert all(key in params.keys() for key in required)

By the way, using assertions for input checking is generally not
considered good practice.  As soon as Python is invoked with the -O
option, your input testing is tossed out the window.  Assertions are
good to use for internal logic testing, to avoid unexpected state.
For input checking, use an explicit if test and exception:

if any(key not in params.keys() for key in required):
    raise ValueError("Missing required keyword argument")

Additionally, if the arguments are required, then they probably have
no business being wrapped up in **params in the first place.  Why not
just define your method like:

def __init__(self, smtp, login, subject, from, to, msg):
    # ...

And then Python will do all the work of requiring them to be present for you.

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


Thread

List comprehension for testing **params Cantabile <cantabile.03@wanadoo.fr> - 2012-11-11 23:24 +0100
  Re: List comprehension for testing **params Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-11 15:56 -0700
  Re: List comprehension for testing **params Tim Chase <python.list@tim.thechases.com> - 2012-11-11 16:49 -0600
  Re: List comprehension for testing **params Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 23:18 +0000
    Re: List comprehension for testing **params Tim Chase <python.list@tim.thechases.com> - 2012-11-11 18:21 -0600
      Re: List comprehension for testing **params Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-12 07:32 +0000
  Re: List comprehension for testing **params Terry Reedy <tjreedy@udel.edu> - 2012-11-11 18:37 -0500
    Re: List comprehension for testing **params Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 23:41 +0000
  Re: List comprehension for testing **params Cantabile <cantabile.03@wanadoo.fr> - 2012-11-12 00:53 +0100
  Re: List comprehension for testing **params Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-12 11:48 +0100
    Re: List comprehension for testing **params Cantabile <cantabile.03@wanadoo.fr> - 2012-11-12 23:25 +0100

csiph-web