Re: usage of try except for review.
On Mon, 29 Feb 2016 07:13 pm, Ganesh Pal wrote: > 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) Do not use assert for error checking. http://import-that.dreamwidth.org/676.html Instead, you should write this: out, err, ret = run(cmd, timeout=timeout) if ret != 0: raise RuntimeError( "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() Do not use the debugger as part of production code. The debugger is for debugging. When you have debugged the section of code, remove the debugger calls. > for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]: > try: > if not run_cmd_and_verify(cmd, timeout=3600): > return False > except: >logging.error("Failure while running command %") > logging.info("Setup and Creation Done !!!") Do not use bare except clauses like this unless you know what you are doing. https://realpython.com/blog/python/the-most-diabolical-python-antipattern/ > 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("") > return False > logging.info("Mount IS START.Done !!!") > > def main(): > if not run_test(): > sys.exit("Exiting Main") > > > if __name__ == '__main__': > main() I would re-write this script as something like this: # Untested def run_cmd(cmd, timeout=1000): out, err, ret = run(cmd, timeout=timeout) if ret != 0: raise RuntimeError( "ERROR (ret %d): \nout: %s\nerr: %s\n" % (ret, out, err)) def run_test(): for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]: run_cmd(cmd, timeout=3600) logging.info("Setup and Creation Done !!!") cmd = "run_scan" out, err, ret = run(cmd) # Do you care about the result of the scan? Then you should log it. for cmd in ["create_data.py -nfs ", "validate.py -30 "]: run_cmd(cmd, timeout=3600) logging.info("Mount IS START.Done !!!") if __name__ == '__main__': try: run_test() except Exception as err: logging.error("run_test failed", exc_info=True) sys.exit(1) > 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 If the except block is not needed, then you should not use try. > 2. 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 Don't use assert like that. > 3. my function def has 1000 but Iam using 3600 in the calling fnx etc > , Time out value are overwritten ? I don't understand the question. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: usage of try except for review.
On Tue, 1 Mar 2016 04:38 am, Chris Angelico wrote: > On Tue, Mar 1, 2016 at 4:34 AM, Ganesh Palwrote: >> On Mon, Feb 29, 2016 at 10:10 PM, Dennis Lee Bieber >> wrote: >> >>> Ask yourself: Will my program still work if I remove all the >>> assert >>> statements. If the answer is "No", then you should not be using an >>> assert. >> >> You meant if the answer is "NO" then I should be using asset ? > > No, Dennis was correct. You should assume that "assert" can > potentially be replaced with "pass" and your program will continue to > work. http://import-that.dreamwidth.org/676.html -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: usage of try except for review.
> I have tried down the code to Read "I have tried down the code to " as I have trimmed down the code as below Ganesh -- https://mail.python.org/mailman/listinfo/python-list
Re: usage of try except for review.
> 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: usage of try except for review.
On Tue, Mar 1, 2016 at 4:34 AM, Ganesh Palwrote: > On Mon, Feb 29, 2016 at 10:10 PM, Dennis Lee Bieber > wrote: > >> Ask yourself: Will my program still work if I remove all the assert >> statements. If the answer is "No", then you should not be using an assert. > > You meant if the answer is "NO" then I should be using asset ? No, Dennis was correct. You should assume that "assert" can potentially be replaced with "pass" and your program will continue to work. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: usage of try except for review.
On Mon, Feb 29, 2016 at 10:10 PM, Dennis Lee Bieberwrote: > Ask yourself: Will my program still work if I remove all the assert > statements. If the answer is "No", then you should not be using an assert. You meant if the answer is "NO" then I should be using asset ? > Can your "run()" raise an exception? Since you never show it to us we > can't tell. And if it can, which ones? However, checking a return code is > not an exceptional condition -- that's expected logic. > The run api is like a wrapper to sub-process module it does a Popen run's the command and returns stdout ,err and ret. The only exception it can raise is a timeout error. >>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: > > >>except: >> logging.error("Some meaningful message") >>logging.info("Setup and Creation Done !!!") > > But you fall off and return Null on success... Iam using the try expect block to loop over the list do I have alternatives but would prefer the for loop because in the actual program there are 100 of command and putting them in a list is quite easy and running over with the same operation saves many lines of code , Cam I modify it something like a try except with pass in the except ? or any suggestions for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]: try: if not run_cmd_and_verify(cmd, timeout=3600): return False except: pass logging.info("Setup and Creation Done !!!") Regards, Ganesh -- https://mail.python.org/mailman/listinfo/python-list
Re: usage of try except for review.
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 -- https://mail.python.org/mailman/listinfo/python-list