usage of try except for review.

Ganesh Pal ganesh1pal at gmail.com
Mon Feb 29 05:03:18 EST 2016


Iam really sorry , I will have to resend my question again , because
the earlier post had mistakes and formatting was bad , so apologies
for top posting  will try to avoid such mistakes in future.


Iam on python 2.6 and Linux , need your suggestion on the usage of try
and except in this program

#!/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=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

    """
    pdb.set_trace()

    for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]:
        try:
            if not run_cmd_and_verify(cmd, timeout=3600):
                return False
        except:
           logging.error("Some meaningful message")
    logging.info("Setup and Creation ....Done !!!")
    #
    cmd = "run_scan"
    out, err, ret = run(cmd)

    for cmd in ["create_data.py -nfs ",
                "validate.py -30 "]:
        try:
            if not run_cmd_and_verify(cmd, timeout=3600):
               return False
        except:
            logging.error("some meaningful message")
            return False
    logging.info("Mount IS START.....Done !!!")

def main():

    if not run_test():
        sys.exit("Exiting Main")

if __name__ == '__main__':
    main()

Question 1:

(a)  Have I used try and expect block correctly  ?  in run_test()  I
have  expect  block which displays some error message  instead of pass
, is it fine ?

 (b)  Have I returned True and  False correctly , looks fine for me

    try:
        if not run_cmd_and_verify(cmd, timeout=3600):
                return False
    except:
           logging.error("inside except")
           return False

Question 2.

(a) If a failure’s are encountered  the error by assert condition the
errors are now displayed on the screen , how do I redirect it to log
file using logging error
     because the moment assert ret==0 becomes true the program exits

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

#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


More information about the Python-list mailing list