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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'params': 0.07; 'subject:getting': 0.07; 'cc:addr:python-list': 0.09; 'indication': 0.09; 'parameter.': 0.09; 'valueerror': 0.09; 'skip:= 70': 0.10; 'wed,': 0.15; 'subject: \n ': 0.15; 'exception;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parameter:': 0.16; 'reasonable.': 0.16; 'received:mail-ig0-x22a.google.com': 0.16; 'subject:exception': 0.16; 'subject:when': 0.16; 'wrote:': 0.16; 'later': 0.16; 'else,': 0.18; 'have:': 0.18; ';-)': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'parameter': 0.22; 'seems': 0.23; 'slightly': 0.23; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'way?': 0.29; 'raise': 0.29; "i'd": 0.31; 'table': 0.32; 'skip:. 10': 0.32; 'maybe': 0.33; 'source': 0.33; 'received:google.com': 0.35; 'clear': 0.35; 'maps': 0.35; 'possible,': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'depends': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'things': 0.38; 'rather': 0.39; 'elsewhere': 0.66; 'obvious': 0.76; 'truth': 0.79; "'all'": 0.84; 'cecil': 0.84; 'chrisa': 0.84; 'clarity.': 0.84; 'westerhof': 0.84; 'subject:this': 0.85; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=3mhvAeK4yBfndqcxJsGeSzyWJMY/Dbj/dswXO8HJJzg=; b=a4qKdKHJmv7Jo5V8KV+ybgMUmCWLtzrIKpxr8Fy9tn/ca5cAKjIQxzO2j3ryfjSOxi LGz2AsechdGeQPWNxtZ4jwas+e6nMVsDjRhOAtaOGpKXdru+I4WH4qpj6UQSrdcV8eoc HRRkRzj2uAUYJs6tHShYxqDIb5/DkSr7ro+6osgw/6Lb/Bhhj3BTaMZzajW7qxCfJu53 8IQcCLmDLN5EwfGtdlwWXsc+Yeywpgs7T/jEiu8Y6wkyUNgiheKfKQx3D/PezHVTk5m2 PtQt6LxZW2XRD51X/WDmGIzFkud69mjIeOREsqYRgKFX5y4VE3xBCPucJlhf2ZkVV+EU w5SQ== MIME-Version: 1.0 X-Received: by 10.50.221.107 with SMTP id qd11mr22979170igc.13.1439371556425; Wed, 12 Aug 2015 02:25:56 -0700 (PDT) In-Reply-To: <87h9o46flw.fsf@Equus.decebal.nl> References: <87h9o46flw.fsf@Equus.decebal.nl> Date: Wed, 12 Aug 2015 19:25:56 +1000 Subject: Re: Is this a correct way to generate an exception when getting a wrong parameter From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439371564 news.xs4all.nl 2882 [2001:888:2000:d::a6]:42400 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95277 On Wed, Aug 12, 2015 at 7:06 PM, 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. > ;-) > > Is this a correct way to do this, or is there a better way? There are a couple of things I'd do slightly differently, one of which is to raise ValueError rather than Exception; but mostly, yes, that seems reasonable. I'd also use "x not in y" rather than "not (x in y)", for obvious reasons of clarity. But it kinda depends on what those params are going to be used for. Maybe you don't need to check at all - maybe something elsewhere will have a table that maps a parameter to something else, and absence from that table is a clear indication that it's an invalid parameter. If at all possible, have a Single Source Of Truth (SSOT), from which everything else derives. ChrisA