Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: usage of try except for review. Date: Mon, 29 Feb 2016 11:40:00 -0500 Organization: IISS Elusive Unicorn Lines: 100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de vqbv+NJxqxLS5NHogHSHMwKCWFwE5ck21IuQACHMd52g== 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; 'sys': 0.05; 'caller': 0.07; 'except:': 0.07; 'false,': 0.07; 'null,': 0.07; 'see.': 0.07; '%s\\n"': 0.09; 'cmd': 0.09; 'exception,': 0.09; 'logic': 0.09; 'meaningful': 0.09; 'message-id:@4ax.com': 0.09; 'met.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'to)': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; "'%s'": 0.16; '2016': 0.16; 'conditional': 0.16; 'desirable,': 0.16; 'err,': 0.16; 'exception?': 0.16; 'false:': 0.16; 'pdb': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:usage': 0.16; 'subprocess': 0.16; 'trap': 0.16; 'trap.': 0.16; 'basically': 0.18; 'try:': 0.18; 'url:home': 0.18; 'exceptions': 0.22; 'seems': 0.23; 'feb': 0.23; 'import': 0.24; 'mon,': 0.24; 'script': 0.25; "doesn't": 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'linux': 0.26; 'checking': 0.27; 'logging': 0.27; 'least': 0.27; '2.6': 0.27; 'regular': 0.29; '"no': 0.29; 'ret': 0.29; 'raise': 0.29; 'code': 0.30; 'skip:[ 10': 0.31; 'entry': 0.31; "can't": 0.32; 'especially': 0.32; 'run': 0.33; 'null': 0.33; 'except': 0.34; 'info': 0.34; 'false': 0.35; 'skip:> 10': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'suggestion': 0.37; 'charset:us-ascii': 0.37; 'creation': 0.38; 'version': 0.38; 'files': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'show': 0.62; 'confirm': 0.62; 'more': 0.63; 'fall': 0.66; 'validate': 0.76; '>def': 0.84; 'message")': 0.84; 'subject:try': 0.84; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-68-178-69.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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:103732 On Mon, 29 Feb 2016 15:33:18 +0530, Ganesh Pal declaimed the following: >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) If the assert triggers, it will generate an exception But this doesn't look like a place where an assert is really desirable, especially when you are inside an exception trap. At least in my viewpoint -- asserts are 1) not something for regular logic (if you run the script with some optimization, the assert statements are "removed"; 2) they should more be used to confirm that the entry criteria were met. 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. 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. def rcav(cmd, to=1000): assert cmd is not Null, "No command provided in rcav" try: out, err, ret = run(cmd, to) except Exception as e: logging.exception("function run() failed:") #I need to test to see how much of the exception info is #automatic if not ret: logging.error("cmd '%s' failed", cmd) return ret #0 is false, non-0 is true Note that I do not trap the assert exception -- this is the type of event that the caller needs to see. > 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... > # > cmd = "run_scan" > out, err, ret = run(cmd) > > for cmd in ["create_data.py -nfs ", > "validate.py -30 "]: Spawning processes to run Python files seems perverse. You should instead write create_data.py and validate.py so that you can do import create_data import validate create.data.main("-nfs") validate.main(-30) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/