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


Groups > comp.lang.python > #95278

Re: Is this a correct way to generate an exception when getting a wrong parameter

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!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; 'error:': 0.05; 'python3': 0.05; 'subject:getting': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:= 70': 0.10; 'exception': 0.13; 'argument': 0.15; 'argparse': 0.16; 'commandline': 0.16; 'function?': 0.16; 'parameter:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:exception': 0.16; 'subject:when': 0.16; 'wrote:': 0.16; 'later': 0.16; 'have:': 0.18; ';-)': 0.18; 'arguments': 0.22; 'parser': 0.22; 'suppose': 0.22; 'import': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'least': 0.27; 'correct': 0.28; 'cat': 0.29; 'way?': 0.29; 'raise': 0.29; 'skip:. 10': 0.32; 'handle': 0.34; 'add': 0.34; 'skip:p 30': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'why': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'more': 0.63; 'choose': 0.68; "'all'": 0.84; 'cecil': 0.84; 'westerhof': 0.84; 'subject:this': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Is this a correct way to generate an exception when getting a wrong parameter
Date Wed, 12 Aug 2015 11:33:04 +0200
Organization None
References <87h9o46flw.fsf@Equus.decebal.nl>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd80fc.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.105.1439371995.3627.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1439371995 news.xs4all.nl 2922 [2001:888:2000:d::a6]:45141
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95278

Show key headers only | View raw


Cecil Westerhof wrote:

> I have:
> ========================================================================
> accepted_params     = {
>     'pcpu',
>     'rss',
>     'size',
>     'time',
>     'vsize',
> }
> ========================================================================
> 
> Later I use:
> ========================================================================
> if (to_check != 'all') and not(to_check in accepted_params):
>     raise Exception('Used illegal parameter: {0}.\n'
>                     'Accepted ones: {1}'
>                     .format(to_check, sorted(accepted_params)))
> ========================================================================
> 
> When using 'all' I want to do the work for all accepted parameters.
> ;-)

Doesn't that make it an "accepted parameter"? Why not add it to the set?

> Is this a correct way to do this, or is there a better way?

I suppose you do this early in a function? Then at least choose a more 
specific exception (e. g. ValueError).

If this is about commandline arguments -- argparse can handle such 
restrictions:

$ cat demo.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--check", choices=["pcpu", "rss"], default="all")
print(parser.parse_args().check)
$ python3 demo.py
all
$ python3 demo.py --check rss
rss
$ python3 demo.py --check ssr
usage: demo.py [-h] [--check {pcpu,rss}]
demo.py: error: argument --check: invalid choice: 'ssr' (choose from 'pcpu', 
'rss')

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


Thread

Is this a correct way to generate an exception when getting a wrong parameter Cecil Westerhof <Cecil@decebal.nl> - 2015-08-12 11:06 +0200
  Re: Is this a correct way to generate an exception when getting a wrong parameter Chris Angelico <rosuav@gmail.com> - 2015-08-12 19:25 +1000
  Re: Is this a correct way to generate an exception when getting a wrong parameter Peter Otten <__peter__@web.de> - 2015-08-12 11:33 +0200
  Re: Is this a correct way to generate an exception when getting a wrong parameter Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 11:43 +0100
    Re: Is this a correct way to generate an exception when getting a wrong parameter Bernd Waterkamp <Bernd-Waterkamp@web.de> - 2015-08-12 18:42 +0200

csiph-web