Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ganesh Pal Newsgroups: comp.lang.python Subject: Re: usage of try except for review. Date: Mon, 29 Feb 2016 23:27:18 +0530 Lines: 75 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de k+xase0hDHH4DbPGZSMBOQX60MMqE5DTn9Wl8UIl6vVg== Return-Path: 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; '"""': 0.05; 'sys': 0.05; '__name__': 0.07; 'correct.': 0.07; 'except:': 0.07; 'main()': 0.07; 'skip:/ 10': 0.07; 'cc:addr:python-list': 0.09; '%s\\n"': 0.09; 'cmd': 0.09; 'exception,': 0.09; 'python': 0.10; 'assume': 0.11; 'exception': 0.13; 'def': 0.13; '"assert"': 0.16; "'__main__':": 0.16; 'conditional': 0.16; 'e))': 0.16; 'err,': 0.16; 'failed")': 0.16; 'false:': 0.16; 'i.e': 0.16; 'main():': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:usage': 0.16; 'subprocess': 0.16; 'basically': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '%s"': 0.22; 'exceptions': 0.22; 'pass': 0.22; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'logging': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'ret': 0.29; 'raise': 0.29; 'work.': 0.30; 'code': 0.30; 'run': 0.33; 'point': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'false': 0.35; 'replaced': 0.35; 'should': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'thanks': 0.37; 'creation': 0.38; 'no,': 0.38; 'version': 0.38; 'received:209': 0.38; 'your': 0.60; 'potentially': 0.67; 'subject:try': 0.84; 'dennis': 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; bh=013Vz+Z4j5nDY0FcZq5EftYwRWSIBVKW4NZm1Gj/izk=; b=wy7v+VKazTUpetlOzWKNUWTagGXQ36/foYDYrVDvM65QRi1JrAZFqCooAHpyMtPCaD QN4PwZ3a/FCFqml23Lf8tl9t+8azkDeA7BwUURaXzHKwjXea5CmSyWL7RU3IeiV7VXLm bz+YacmnER2X2+Wcw1zd1j/lhXZ8X8G52GTh5ESQBVAt1iJkaids90FJK0R77J5Z0/zh WiDIKt6jd/R+rAYtbPxXd5VtHtGXpUHvIkM0jJVvagOLSJ9sN8NGa4nSMIktyzYQvsu+ +AKsTlckLiHVgWAi1tFFzSKsv6zlxRWfEMyY3+i277X0kOx2wcs7A91NkzVcq3Z8ap+n lvQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=013Vz+Z4j5nDY0FcZq5EftYwRWSIBVKW4NZm1Gj/izk=; b=VTza7bzGbGPyAvphAHu2u4IfVgnxGBN3vZu7R9rC1kfxchXYP1bM/tREp2I2dhGfCP mLxo/m1rfMqRF+94Z2Pk3G3x/SEkgCWY6kB7uvNpy4QjrLrUrFR/VQFt6wYJ/i4HT1lx abR7uyHlgPeY92On0SvdhKZCS2pFSJEl8UMeahTJ+8ZkQWUtFun2O+QxPJkeijcY2cPU t8VX470cn1fxhKxv+gKWVCBAVo0cAcd16vfH6jVm/vAvJ13wrQht2e50KsdOG72XFEYe vFGTZ5KiICtp+p5JZAolFwrgxWSHtA8em2l76ELOIUE7IZFTDsmRggrxUp8V6VWKcpBz NPWQ== X-Gm-Message-State: AD7BkJIHq16LcJDV0o7t3aIwR1MX643RWgqb0wWIkMw0rBQO7S54o3enEJrNb1JU7a+Pgs5n/rK5cglQOQ2XDw== X-Received: by 10.112.223.37 with SMTP id qr5mr6103956lbc.19.1456768638239; Mon, 29 Feb 2016 09:57:18 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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:103746 > No, Dennis was correct. You should assume that "assert" can > potentially be replaced with "pass" and your program will continue to > work. Thanks Chris for clarifying Dennis point of view , >> try: >> if not run_cmd_and_verify(cmd, timeout=3600): > > Since your version of rcav() always trapped exceptions internally, this > call will never raise an exception, so using a try: block is meaningless >> return False > > And your conditional basically comes down to: > > if False: > return False: What would be the alternative for try expect in the FOR loop section of the program , will this work fine i.e expect with pass , or any better alternative for this ? I have tried down the code to #!/usr/bin/env python """ """ import os import shlex import subprocess import sys import time import logging from utils import run def run_cmd_and_verify(cmd, timeout=1000): try: out, err, ret = run(cmd, timeout=timeout) assert ret ==0,"ERROR (ret %d): " \ " \nout: %s\nerr: %s\n" % (ret, out, err) except Exception as e: logging.error("Failed to run %s got %s" % (cmd, e)) return False return True def run_test(): """ Mount """ # For Loop section of the program for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]: try: if not run_cmd_and_verify(cmd, timeout=3600): logging.error("mount Failed") return False except: pass logging.info("Setup and Creation ....Done !!!") def main(): if not run_test(): sys.exit("Exiting Main") if __name__ == '__main__': main() Regards, Ganesh