Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ganesh Pal Newsgroups: comp.lang.python Subject: Re: how to make the below code look better Date: Thu, 3 Dec 2015 11:50:18 +0530 Lines: 65 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de lMbwjefRV8Y4yZpX1+g1yQIcARVzQmm6B5i48ktSAtcA== 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; 'true,': 0.04; 'exit': 0.07; 'rename': 0.07; 'subject:code': 0.07; 'cc:addr:python-list': 0.09; 'exception,': 0.09; 'exit.': 0.09; 'logic': 0.09; 'statements': 0.09; 'exception': 0.13; 'syntax': 0.13; 'wed,': 0.15; '(either': 0.16; '6:00': 0.16; 'clauses': 0.16; 'comma': 0.16; 'failed")': 0.16; 'fruit.': 0.16; 'handling.': 0.16; 'messy': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:make': 0.16; 'try/except': 0.16; 'wrote:': 0.16; "shouldn't": 0.18; 'skip': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'permission': 0.20; 'meant': 0.22; 'assuming': 0.22; 'latter': 0.22; 'terminate': 0.22; 'seems': 0.23; 'dec': 0.23; 'errors': 0.23; 'this:': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'skip:" 20': 0.26; 'error': 0.27; 'checking': 0.27; 'right.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'errors.': 0.27; 'correct': 0.28; 'fine': 0.28; '(maybe': 0.29; 'agreed.': 0.29; 'currently,': 0.29; 'indentation': 0.29; "i'm": 0.30; 'code': 0.30; 'certainly': 0.30; 'convention': 0.30; "i'd": 0.31; 'agreed': 0.31; 'probably': 0.31; "can't": 0.32; 'included': 0.32; 'statement': 0.32; 'avoiding': 0.33; 'errors,': 0.33; 'raising': 0.33; 'though.': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'easiest': 0.35; 'replace': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'thanks': 0.37; 'suggestion': 0.37; 'creation': 0.38; 'skip:v 20': 0.38; 'received:209': 0.38; 'log': 0.38; 'mean': 0.38; 'skip:o 20': 0.38; 'means': 0.39; 'data': 0.39; 'does': 0.39; 'subject:the': 0.39; 'rather': 0.39; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; 'side': 0.62; 'yes': 0.62; 'within': 0.64; 'worth': 0.67; "'not'": 0.84; 'all;': 0.84; 'away,': 0.84; 'conditions,': 0.84; 'style:': 0.84; 'subject:below': 0.84; 'can!': 0.91; 'carries': 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:to :cc:content-type; bh=ciG2GYvf5jZmUG2ep1nv4eaPsotXZwSCBN2gLwwQJMY=; b=NFChJ9UqPfvDXF26+G/sGW+AXynUDG370LaT1YAwEFfNuFLZesZjeFy1BZczI0tFYB vdn2mhFC9lxv7Nwz8rZfuXMe7VJYE+LS9RJStXqUgRi2syBOX+Mm1+0lIOGuabOsKSHr 2ZJoaX9JrvhDO5AGccWbupoZPHmuyvxdrJwF4IGcIHEu4l1O5HixnA2fHpOlCXYErDKi tNeD5LPO1m6gu7GQU422kK0R5tSxleF+myTS82cEsKGNWWp1T6aaN6y9r2wk133/Q2Dg IP0DRoPngtSk9HZ4qCblXcj7aHND9Srb3XU55Yoo0uLX6SHBtYPSR1RDLusac+dKOYIz LOOA== X-Received: by 10.112.247.103 with SMTP id yd7mr4924867lbc.46.1449123619036; Wed, 02 Dec 2015 22:20:19 -0800 (PST) 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: , Xref: csiph.com comp.lang.python:99931 On Wed, Dec 2, 2015 at 6:00 PM, Chris Angelico wrote: > If both the functions return true values, yes. You have an indentation > error there, but I'm assuming you meant to have the try/except > indented further. > Correct I had meant to have try/except indented further. >> 2. Can I have a if statement within if else ? , some how I feel its messy > > Certainly you can! However, most (maybe all) of your 'if' statements > are checking for error conditions, so the easiest solution is to > simply exit right away (either with sys.exit or by raising an > exception). Yes agreed , have included the suggestion >> 3. Any other suggestion ? please > > The first suggestion I'd make is to avoid the comma syntax for > exception handling. Replace "except Exception, e:" with "except > Exception as e:". That's a trivial change of syntax that shouldn't > affect your code at all; consider it low-hanging fruit. > ,Are we avoiding comma syntax because it's a bad convention or will it have any side effects? In fact I have used this comma syntax in most of my code . I will clean the code latter as it seems working fine > The second thing to look at is the way you're 'handling' those errors. > All you do is log the error and exit. So you can skip the except > clauses altogether, and just do this: > > if not os.path.ismount("/tmp"): > sys.exit("/tmp not mounted.") > if create_dataset() and check_permission(): > run_full_back_up() > run_partial_back_up() > if not validation_errors(): # CHECK ME > sys.exit("Validation failed") > compare_results() > > Agreed. > Check the logic of validation_errors, though. If it returns something > True, does that mean there were errors or there weren't? If it means > there were errors, then the 'not' seems to me to be wrong; but if it > means there weren't, then I would rename it "validation_successful" > rather than "validation_errors". > > Also worth checking: If you can't create the data set or the > permission check fails, what should it do? Should it terminate with an > error? Currently, it carries on and does the validation, which is > probably not right. If it ought to terminate straight away, you can > write the whole program in "fail and bail" style: > > if not create_dataset(): > sys.exit("Data set creation failed") > if not check_permission(): > sys.exit("Permission check failed") > Thanks for other suggestions also