Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ganesh Pal Newsgroups: comp.lang.python Subject: Re: common mistakes in this simple program Date: Mon, 29 Feb 2016 22:56:43 +0530 Lines: 105 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de b6LnpjhHiByel+lyVsAengS8WYzZNkJcQ8+E+Fm4n8rQ== 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; 'modify': 0.04; '"""': 0.05; 'modified': 0.05; '__name__': 0.07; 'except:': 0.07; 'exception.': 0.07; 'main()': 0.07; 'wrapper': 0.07; 'cc:addr :python-list': 0.09; '%s\\n"': 0.09; 'cmd': 0.09; 'func': 0.09; 'imported': 0.09; 'returncode': 0.09; 'stdout': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; '"mkdir': 0.16; "'__main__':": 0.16; '2016': 0.16; 'buggy': 0.16; 'clustering': 0.16; 'e))': 0.16; 'err,': 0.16; 'main():': 0.16; 'raises.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reraise': 0.16; 'subject:program': 0.16; 'subject:simple': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'runs': 0.18; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '%s"': 0.22; 'exceptions': 0.22; 'pass': 0.22; 'am,': 0.23; 'feb': 0.23; 'import': 0.24; 'header :In-Reply-To:1': 0.24; 'mon,': 0.24; 'module': 0.25; 'command': 0.26; 'logging': 0.27; 'skip:# 10': 0.27; 'message- id:@mail.gmail.com': 0.27; '2.6': 0.27; 'function': 0.28; 'ret': 0.29; 'raise': 0.29; 'code': 0.30; 'generally': 0.32; 'run': 0.33; 'utility': 0.33; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'false': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209': 0.38; 'log': 0.38; 'sure': 0.39; 'where': 0.40; 'further': 0.62; 'above,': 0.63; 'here': 0.66; 'catch?': 0.84; 'subject:common': 0.84; 'subject:this': 0.85 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=AP/VXTO8ENrFiuX6QJ+mMOmkgvDJJbym+U1LrILaiwM=; b=a+KiDXgu6bgU85LohLp2LuHNtrjGEEkZnfWXN5hXy4ZzkCzI4S5PN8/UWOQ+bbGDWj Hsbr5bqE1PbEpWIO+cB+MWz8WyPSYWhzNDM0cBfx+oX5GWHRqvQFlaJNB/iLQ8WoP38T FIeDf5MaKh1jans3O4qHOBlvfT/G4SqxxGUBSt2SpqoruzjRLCQzPIwQPtf/moIy7gW7 r3FDenQFqDG5n8T9g2Qen5DpgeKLEjojbFMb1UDfygXLKdw29mq0K5GRlVE/cEoYlvV0 ijMWqhpidH4Uwcjf7FM0Qq4fe2lV4iwP8qit73g0yKmGQNlFBJg4V3+xrqZUz6FMlkqg i2+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=AP/VXTO8ENrFiuX6QJ+mMOmkgvDJJbym+U1LrILaiwM=; b=Iu1rg4QnPbEK0QiArPulG4/u+vF3lTD5bAg2KZx8m4tnMG1Dff0a0zvkw563ICCNFD 1ywhJJotjfMFFoKQIBhQwjvRzYlVI9kpuI1DsV8dkwxBzo33tUVVWRM3SaDucKRRGlp9 Z99WqAPNyRV39GAzo135qiwuXeMRJaOocr7dkTEksPv9c29odHj8frg67/IEowRQsAix XGUDbbMtdlmN2WvI/FtAjKZCpz6dTNSyMMoc976Ol0gtabtQhGxeaTDyYY8auBVDZEMm Eee3r8WGK0C2bvtLahOjKej8CFJvCK8RuaCWxHhPwpnDWpXIEgV5WpLcQinNDWxD3ZYi BMsA== X-Gm-Message-State: AD7BkJLU+21JqV3BueavFc29HUCeaowqpBH+ZobZmREW7eSxSLyAvQ3YJcYQU8nnxfOxwYXhvICRiG2tUeIVHw== X-Received: by 10.112.14.102 with SMTP id o6mr5910755lbc.87.1456766803916; Mon, 29 Feb 2016 09:26:43 -0800 (PST) In-Reply-To: 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:103739 On Mon, Feb 29, 2016 at 9:59 PM, Ian Kelly wrote: > On Mon, Feb 29, 2016 at 8:18 AM, Ganesh Pal wrote: >> Iam on python 2.6 >> 1. usage of try- expect > > try-except in every single function is a code smell. You should only > be using it where you're actually going to handle the exception. If > you catch an exception just to log it, you generally should also > reraise it so that something further up the call chain has the > opportunity to handle it. How do we reraise the exception in python , I have used raise not sure how to reraise the exception > >> def run_cmd_and_verify(cmd, timeout=1000): >> try: >> pdb.set_trace() >> out, err, ret = run(cmd, timeout=timeout) > > What is "run"? It's imported like a module above, but here you're > using it like a function. Sorry run is a function which was imported from a library , the function had to be # from utility import run , >> assert ret ==0,"ERROR (ret %d): " \ >> " \nout: %s\nerr: %s\n" % (ret, out, err) >> except Exception as e: >> print("Failed to run %s got %s" % (cmd, e)) >> return False >> return True >> >> def prep_host(): >> """ >> Prepare clustering >> """ >> for cmd in ["ls -al", >> "touch /tmp/file1", >> "mkdir /tmp/dir1"]: >> try: >> if not run_cmd_and_verify(cmd, timeout=3600): >> return False >> except: > > What exceptions are you expecting this to catch? run_cmd_and_verify > already catches any expected exceptions that it raises. This is a wrapper for Popen it runs the command and returns stdout ,stderror and returncode .The only exception it can return is a time out exception Here is the modified buggy code , Can I have Try and except with Pass , how do I modify the try and expect in the pre-host ? #!/usr/bin/env python """ bugging code """ import logging from utility 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 prep_host(): """ Prepare clustering """ for cmd in ["ls -al", "touch /tmp/file1", "mkdir /tmp/dir1"]: try: if not run_cmd_and_verify(cmd, timeout=3600): logging.info("Preparing cluster failed ...") return False except: pass logging.info("Preparing Cluster.....Done !!!") return True def main(): functions = [prep_host] for func in functions: try: func() except Exception as e: logging.info(e) return False if __name__ == '__main__': main() Regards, Gpal