Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: common mistakes in this simple program Date: Mon, 29 Feb 2016 09:29:21 -0700 Lines: 92 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Emq6sP1Y8MCly8S5GLX7EgdWDEt+6NjcCOzTLde3PS+A== 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; '"""': 0.05; 'sys': 0.05; '__name__': 0.07; 'caller': 0.07; 'except:': 0.07; 'exception.': 0.07; 'main()': 0.07; 'versions.': 0.07; 'cc:addr:python-list': 0.09; '%s\\n"': 0.09; 'cmd': 0.09; 'func': 0.09; 'here?': 0.09; 'imported': 0.09; 'python': 0.10; '"this': 0.13; 'exception': 0.13; 'def': 0.13; 'instead.': 0.15; '"mkdir': 0.16; "'__main__':": 0.16; '2016': 0.16; 'clustering': 0.16; 'docstring': 0.16; 'e))': 0.16; 'err,': 0.16; 'inspect,': 0.16; 'main():': 0.16; 'pdb': 0.16; 'preparing': 0.16; 'pythonic': 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; 'subprocess': 0.16; 'systemexit': 0.16; 'unsupported': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'version.': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '%s"': 0.22; 'exceptions': 0.22; 'am,': 0.23; 'feb': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'module': 0.25; 'logging': 0.27; 'message- id:@mail.gmail.com': 0.27; '2.6': 0.27; 'function': 0.28; 'this.': 0.28; 'ret': 0.29; 'code': 0.30; 'generally': 0.32; 'run': 0.33; 'raised': 0.33; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'false': 0.35; 'returning': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'received:209': 0.38; 'log': 0.38; 'mean': 0.38; 'means': 0.39; 'where': 0.40; 'your': 0.60; 'further': 0.62; 'above,': 0.63; 'more': 0.63; 'wanting': 0.66; 'here': 0.66; 'completed': 0.69; 'catch?': 0.84; 'subject:common': 0.84; 'using.': 0.84; 'subject:this': 0.85; 'to:none': 0.91; '2013.': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=XNNaK8IzqIbBYy7e1S4jyXiUWQKPKxpcxlhZL2w4+ds=; b=qniBAtnrLx4MdmkMmfXRg+dvNkE6+bEdK9RYLwaqbW0AVPrN5EMY7o5uZTZ1/GxEtx nBw7NXiqWndIlC78wdhI/AX4u84MBsrThNFRNpyBFno47MNVZKrVkkQkb4E7MxpMEpCY dogh5Cx/EyfMsaw8sAyHQjMbZSGFHZ14Q0+dhUZhia5ffo2AevUzCYRVFQCmyeDA2S+/ eDleyFZ5H0tsINrWsq0s5y5+nij5JSAM2JVlYcZx9kjLNmSMGVB5HcwFHuU+OUFyfBXV 15CjrGOWwF8ht9WG5q750xzCg92NmvsiF+yTHLBv0zmPY9syYDiNzU5/TwVe7J3qq5+u P/2g== 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:from:date :message-id:subject:cc; bh=XNNaK8IzqIbBYy7e1S4jyXiUWQKPKxpcxlhZL2w4+ds=; b=A1felO6tSu7CE1bD+5cFDRHQm3NJPRv2wMTQaPnBd7Do6Il6W07uUhWoSa3zAvoFlb ULY0BRbvoyf+hZNAsT5xQEJlnZ8SQjwBEfA2UI4YyUi/T+c5L+jP+Q/OfUp4k9QpRPjF GKz5ZRzUTlequH+KgwFk2dnrcbcZkhemlsGRaq72elfpf7F2YlT+dbFh/03fqgadNp8f 7QwmAAnA6aB08AcYJBPkkTWV6kuQ2nHbkEa9ix7tPK69qeeMHJg5tyIKh18LecuEy6BL Phv3h04vf04gCFbCKlvgNp803YvrtjX8MOifYtqA+b4YqpwK43XnCdf2mtYfkzWR3SNP PiwQ== X-Gm-Message-State: AD7BkJKEQpDp4UTaLA1DmmEKivul+dzRVPKc4DxYro4L5XzoNu/mDDvrLrZSKvENzkJzehFnIbcVQl/OyKNz+A== X-Received: by 10.50.131.201 with SMTP id oo9mr10256040igb.68.1456763400532; Mon, 29 Feb 2016 08:30:00 -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:103729 On Mon, Feb 29, 2016 at 8:18 AM, Ganesh Pal wrote: > Iam on python 2.6 Python 2.6 has been unsupported since October 2013. Among other things, that means it is no longer receiving security updates like more recent versions. Unless you have an extremely strong reason for wanting to stay to Python 2.6, you should update your Python version. > 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. > 2. Return of True/ False If you're returning True to mean "This completed without exception" and False to mean "This raised an exception", then the more Pythonic thing to do would just be to let the exception propagate, giving the caller the opportunity to catch, inspect, and handle the exception. > """ > """ An empty docstring is pointless. > import os > import shlex > import subprocess > import sys > import time > import logging > import run > import pdb Most of these are unused. Only import modules that your code is actually using. > 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. > 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. Also, you should almost never use a bare except like this. Use "except Exception" instead. A bare except will catch things like KeyboardInterrupt and SystemExit that should not be caught. > print("Error: While preparing cluster !!!") > return False > print("Preparing Cluster.....Done !!!") > return True > > > def main(): > functions = [prep_host] > for func in functions: > try: > func() > except Exception as e: As above, what exceptions are you expecting here? > print(e) > return False > if __name__ == '__main__': > main()