Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ganesh Pal Newsgroups: comp.lang.python Subject: usage of try except for review. Date: Mon, 29 Feb 2016 13:43:29 +0530 Lines: 190 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de ZyF+vnOH6+qtPmGHNUvbXgi2TGT0XXjnBOq1he32ibAg== 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; 'modified': 0.05; 'sys': 0.05; '__name__': 0.07; 'except:': 0.07; 'main()': 0.07; 'skip:/ 10': 0.07; '%s\\n"': 0.09; 'cmd': 0.09; 'err:': 0.09; 'exited': 0.09; 'sake': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; "'__main__':": 0.16; '1):': 0.16; 'e))': 0.16; 'err,': 0.16; 'main():': 0.16; 'pdb': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:usage': 0.16; 'subprocess': 0.16; 'try:': 0.18; 'to:name:python-list@python.org': 0.20; '%s"': 0.22; 'errors': 0.23; 'import': 0.24; 'command': 0.26; 'linux': 0.26; 'error': 0.27; 'logging': 0.27; 'skip:# 10': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; '2.6': 0.27; 'function': 0.28; 'ret': 0.29; 'code:': 0.29; 'skip:[ 10': 0.31; 'included': 0.32; 'screen': 0.32; 'run': 0.33; 'displayed': 0.33; 'correctly': 0.34; 'file': 0.34; 'except': 0.34; 'running': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'false': 0.35; 'but': 0.36; 'needed': 0.36; 'received:209.85': 0.36; 'to:addr:python- list': 0.36; 'expect': 0.37; 'suggestion': 0.37; 'creation': 0.38; 'received:209': 0.38; 'log': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'your': 0.60; 'further': 0.62; 'subject:try': 0.84; 'improvement': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to :content-transfer-encoding; bh=v1+zjubcN4C8bXEmKG9GHLlyU3FN/BhdOl1duY62rNA=; b=h4NoR2vApY+NusIHjk//HMOo19W9epJMw+xh9TyhsFzIG9Z66++hzRMR22hCMxvNwo 8IaxhHledURCQkKX2mdYAD7SQGanp2AP4cu658/DponlSYA+J4nxd3NLe37sUN1d1CQQ xS353vuvs66VMucy4IgP3c2uO4rNxc/+yRF1SSdCL5iXF03KfS0DeYyngObA1UVDy2tj BNFUIPHkQ4XT4Vl0gwfOvHebz6ve1W7nvOb2LvHqIlrRIVhN4HMBVRDJAUIFVqsUOYeF 69SytH7PIDQhqYr+g4WS5oRwVcPueCqTi+qXMr9g97nJIynABzW59e9rhOeAZ99TJauJ /ZUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-transfer-encoding; bh=v1+zjubcN4C8bXEmKG9GHLlyU3FN/BhdOl1duY62rNA=; b=BBZ/kUDB8zCVDnBoNZIcyz0d8T0Ws7FSWruUA70Y6j41S8//zLXNoXYbJCeLNA5BxT 1TQ/J1gHvu16CEXSltIA/2+r9dwQNwoTqW1aMuyKgNtBaDSDSHDx2rekuv52kbVE5WZj eKTJnZnrz2MOgN0CwcKt082l3WBinxAskGoMmo39tYrBs6fQtpfeSYyJ0GZZcpvsHNxe SJA6I/w3mvz2VaVrTHD79JFF5dkCvcV/oV6GLganacc3+hqELVFoF35zEMGyBqn6uE2Q iijr8W0LEuVK0Mjtq0Llvmw5zOCmkDbmYBM1A4bHfcxX6Dapy6dIbkOil7XLb1t8ZbYx y5Jw== X-Gm-Message-State: AD7BkJL03NGixPxnvEbvCiRIs7Fx5bfx/S9y1UWyU7Oli9HkcVf+X04JzKb074tgn0ObJPpG0Rwan4q6GYqUxg== X-Received: by 10.112.54.201 with SMTP id l9mr5071872lbp.105.1456733609297; Mon, 29 Feb 2016 00:13:29 -0800 (PST) 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:103692 Iam on python 2.6 and Linux , need your suggestion on the usage of try and except in this program and Modified code: #!/usr/bin/env python """ """ import os import shlex import subprocess import sys import time import logging import run import pdb def run_cmd_and_verify(cmd, timeout=3D1000): try: out, err, ret =3D run(cmd, timeout=3Dtimeout) assert ret =3D=3D0,"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 """ pdb.set_trace() for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]: try: if not run_cmd_and_verify(cmd, timeout=3D3600): return False except: logging.error("Failure while running command %") logging.info("Setup and Creation ....Done !!!") # cmd =3D "run_scan" out, err, ret =3D run(cmd) for cmd in ["create_data.py -nfs ", "validate.py -30 "]: try: if not run_cmd_and_verify(cmd, timeout=3D3600): return False except: logging.error("") return False logging.info("Mount IS START.....Done !!!") def main(): if not run_test(): sys.exit("Exiting Main") if __name__ =3D=3D '__main__': main() Question 1: 1. Have I used try and expect block correctly ? , In my case I have the except block that's is not needed it just gives an message I have still included for the sake of try block try: if not run_cmd_and_verify(cmd, timeout=3D3600): return False except: logging.error("inside except") return False 2. If a failure=E2=80=99s are encountered the error by assert condition t= he errors are now displayed on the screen , how do I redirect it to log file using logging error def run_cmd_and_verify(cmd, timeout=3D1000): try: out, err, ret =3D run(cmd, timeout=3Dtimeout) assert ret =3D=3D0,"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 #script_10.py Failed to run mount /nfs got ERROR (ret 1): out: host-44-3 exited with status 1 err: host-44-3: mount_efs: on /nfs: efs is already mounted 3. my function def has 1000 but Iam using 3600 in the calling fnx etc , Time out value are overwritten ? 4. Any further improvement particularly on try -except ? Regards, Ganesh