Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103746

Re: usage of try except for review.

From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Subject Re: usage of try except for review.
Date 2016-02-29 23:27 +0530
Message-ID <mailman.33.1456768645.20602.python-list@python.org> (permalink)
References <CACT3xuWm56OE7LpR8kBLfWfkDoU18sPcN5DRiXjr=68+Kywo8Q@mail.gmail.com> <CACT3xuXA=xy8ovTQOS9Bz06P0R1FjQ=bN4-3Pxqf3Cu7wN1dXg@mail.gmail.com> <c5r8dbplpbr62lca859i5cs5dlskfdqeuu@4ax.com> <CACT3xuXC8ZxTd6E-giB9xEJvikQ76o62a5TKrZsQmqZgQtjLsw@mail.gmail.com> <CAPTjJmq9aHQN7EJoEcXU0nJpFdMSf3=0hcUYO+6xyGxTvv6JFw@mail.gmail.com>

Show all headers | View raw


> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: usage of try except for review. Ganesh Pal <ganesh1pal@gmail.com> - 2016-02-29 23:27 +0530

csiph-web