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


Groups > comp.lang.python > #33148

Re: List comprehension for testing **params

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'args': 0.04; '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; 'positional': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'unexpected': 0.09; 'def': 0.10; 'empty.': 0.16; 'evaluates': 0.16; 'instead:': 0.16; 'msg):': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'required):': 0.16; 'smtp,': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'input': 0.18; 'define': 0.20; 'parameters': 0.20; 'bit': 0.21; 'explicit': 0.22; "i'd": 0.22; 'raise': 0.24; 'feature': 0.24; 'testing': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'header:X -Complaints-To:1': 0.28; '(since': 0.29; 'assert': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'keyword': 0.30; 'generally': 0.32; '11,': 0.33; 'like:': 0.33; 'subject:List': 0.33; 'to:addr:python- list': 0.33; 'list': 0.35; 'identified': 0.35; 'nov': 0.35; 'requiring': 0.35; 'pm,': 0.35; 'something': 0.35; 'received:org': 0.36; 'method': 0.36; 'test': 0.36; 'being': 0.37; 'why': 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; 'much)': 0.84; 'received:fios.verizon.net': 0.84; 'required)': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: List comprehension for testing **params
Date Sun, 11 Nov 2012 18:37:05 -0500
References <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <CALwzidnkFqk1+UOvCzbQNC_ootr+fJWCXE-j8WaSg2fQ-fsDiA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <CALwzidnkFqk1+UOvCzbQNC_ootr+fJWCXE-j8WaSg2fQ-fsDiA@mail.gmail.com>
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.3565.1352677051.27098.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352677051 news.xs4all.nl 6869 [2001:888:2000:d::a6]:41589
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33148

Show key headers only | View raw


On 11/11/2012 5:56 PM, Ian Kelly wrote:
> 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):

or if you want them to be identified by keyword only (since 7 positional 
args is a bit much)

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

(I forget when this feature was added)


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


-- 
Terry Jan Reedy

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