usage of try except for review.

Ganesh Pal ganesh1pal at gmail.com
Mon Feb 29 12:57:18 EST 2016


> 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


More information about the Python-list mailing list