Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'error:': 0.05; 'from:addr:yahoo.co.uk': 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; 'subject: \n ': 0.15; 'argparse': 0.16; 'commandline': 0.16; 'function?': 0.16; 'parameter:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:exception': 0.16; 'subject:when': 0.16; 'wrote:': 0.16; 'later': 0.16; 'have:': 0.18; ';-)': 0.18; 'language': 0.19; 'library': 0.20; 'arguments': 0.22; 'lawrence': 0.22; 'parser': 0.22; 'suppose': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'equivalent': 0.27; 'least': 0.27; 'correct': 0.28; 'cat': 0.29; 'way?': 0.29; 'raise': 0.29; 'language.': 0.32; 'skip:. 10': 0.32; 'handle': 0.34; 'add': 0.34; 'newer': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'why': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'charset:windows-1252': 0.62; 'more': 0.63; 'our': 0.64; 'believe': 0.66; 'choose': 0.68; "'all'": 0.84; 'cecil': 0.84; 'otten': 0.84; 'pythonistas,': 0.84; 'westerhof': 0.84; 'subject:this': 0.85 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Is this a correct way to generate an exception when getting a wrong parameter Date: Wed, 12 Aug 2015 11:43:44 +0100 References: <87h9o46flw.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-146-0-205.as13285.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439376239 news.xs4all.nl 2890 [2001:888:2000:d::a6]:58053 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95279 On 12/08/2015 10:33, Peter Otten wrote: > 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') > > The wonderful http://docopt.org/ makes this type of thing a piece of cake. I believe there's a newer library that's equivalent in functionality to docopt but I can never remember the name of it, anybody? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence