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


Groups > comp.lang.python > #103744

Re: common mistakes in this simple program

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: common mistakes in this simple program
Date Mon, 29 Feb 2016 10:45:41 -0700
Lines 57
Message-ID <mailman.32.1456767983.20602.python-list@python.org> (permalink)
References <CACT3xuWUk02Or78XHTdkFLsy6CMVHEYKOyid3hFJxw8Wh6-g1A@mail.gmail.com> <CALwzid=3sN3ic=wrq8qn+0P+Zs8xGn1TJRLYL35B1Qk-vDeh5g@mail.gmail.com> <CACT3xuXn1zCip-io9aQ_i0Av_tR-7AHbwq3bX2HbvdmyC_i9qA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de qr9fb5tpGZBy5j1XZbX5/ATLCOGvoO5/M5m99zNEbsZQ==
Return-Path <ian.g.kelly@gmail.com>
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; 'received:209.85.223': 0.03; 'modify': 0.04; '"""': 0.05; 'modified': 0.05; 'except:': 0.07; 'exception.': 0.07; 'wrapper': 0.07; '%s\\n"': 0.09; 'cmd': 0.09; 'exception,': 0.09; 'exception:': 0.09; 'handled.': 0.09; 'handler.': 0.09; 'returncode': 0.09; 'stdout': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; '"mkdir': 0.16; '10:26': 0.16; '2016': 0.16; 'buggy': 0.16; 'clustering': 0.16; 'e))': 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; '>>>': 0.20; '%s"': 0.22; 'arguments': 0.22; 'exceptions': 0.22; 'function,': 0.22; 'pass': 0.22; 'am,': 0.23; 'feb': 0.23; 'header:In-Reply- To:1': 0.24; 'all.': 0.24; 'mon,': 0.24; 'command': 0.26; 'message-id:@mail.gmail.com': 0.27; '2.6': 0.27; 'function': 0.28; 'went': 0.28; 'ret': 0.29; 'raise': 0.29; 'code': 0.30; 'generally': 0.32; 'run': 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; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'received:209': 0.38; 'log': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'further': 0.62; 'virtually': 0.66; 'here': 0.66; 'catch?': 0.84; 'subject:common': 0.84; 'to:name:python': 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:from:date:message-id:subject:to; bh=u0vKaVoFndSttGAL2eKuRZxJSJLYTD7VcLmaW4tk/Qs=; b=b9h12PfeUagCCK7na2MUsKJVMxDqd1WKwyQqaPZ/x3TCiGvC/+hUoNWTUe+wzCXloW 2V8yEfMMp2i6KabGgOpaRMmIhrdAJs4czaPMHI0p7qtq7LmgGa/a87XG+YqvpaV7Lskj 4RI4O8LKeDEHdsm07IKa2hr5hE8mNP2fSdIx20NutfIUuFFcHU1HD04o0QuFwPetwp9b aLtac9l9ALkn/MJkaW1J/X8LggTxnWbeKJrCjQ8V96i1XAhYFgdGV9bHORwUx466VcLm FT0kY+opa0eRNhHTSNB/cmLPkl3X0RAfC5+3rw1suPWQwMmhSaaWJah9jdFtmYnko+KW fi1g==
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:to; bh=u0vKaVoFndSttGAL2eKuRZxJSJLYTD7VcLmaW4tk/Qs=; b=kvjx86STZrazRQgQc/xAp0uVJRong6zm13Cw7Ylnya/ViC4OlqcDo1/TURIRJuUt4g AbBLC42q+WtTyoearGvsULByzJROuP5AyTYFlPQuXavIvZn227NdSTTq9cUi5TbWOh5i RFjBAgKFaKxG+mF2EtBbSSW+e6pDZROdiBn/pPG8Ocloe2VgrsUpJaREPtjJIRVMdszR GEVqBforDQbGxG1U5WemdOUcTgDExR5h1AD9DHumJvdcxKi9S+o4Lacta0OQmgUnJmas Sb391SRN2BFlSWqD3+LK3XDjeAhswnHS3ZrpKY94qqBGKxZcj+52jHGWoReEmdnJAMPs /FpA==
X-Gm-Message-State AG10YOTpjPPW1yyTCQT9wNNROJcoQkpRRfPWtDKz721E90kLB7VZl979qBj8NWX/c1SqEwRZ7pp2NIl3ULGrDQ==
X-Received by 10.107.185.214 with SMTP id j205mr21101532iof.111.1456767980896; Mon, 29 Feb 2016 09:46:20 -0800 (PST)
In-Reply-To <CACT3xuXn1zCip-io9aQ_i0Av_tR-7AHbwq3bX2HbvdmyC_i9qA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103744

Show key headers only | View raw


On Mon, Feb 29, 2016 at 10:26 AM, Ganesh Pal <ganesh1pal@gmail.com> wrote:
> On Mon, Feb 29, 2016 at 9:59 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> On Mon, Feb 29, 2016 at 8:18 AM, Ganesh Pal <ganesh1pal@gmail.com> 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

raise with no arguments will reraise the exception currently being handled.

except Exception:
    logging.error("something went wrong")
    raise

>>>         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

But that exception is already caught by the run_cmd_and_verify
function, so what exception are you expecting to be caught *here*?

> 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 ?

You should virtually never just pass in an exception handler. Either
handle the exception, or log it and reraise it. If you're going to do
neither of those things, then don't use a try-except at all.

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


Thread

Re: common mistakes in this simple program Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-29 10:45 -0700

csiph-web