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


Groups > comp.lang.python > #103749

Re: common mistakes in this simple program

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Subject Re: common mistakes in this simple program
Date Mon, 29 Feb 2016 23:51:37 +0530
Lines 77
Message-ID <mailman.35.1456770105.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> <CALwzidn8-HDrGSqZT2ODxhdNYL7yFt5hdmC8RnB4WC=gkD4Wtg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de vTKWX94C+iF7VjegxQvXpA7k7tZmyKOmWx6KUxaOUedQ==
Return-Path <ganesh1pal@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; '"""': 0.05; 'error:': 0.05; 'except:': 0.07; 'cc:addr:python-list': 0.09; '"running': 0.09; '%s\\n"': 0.09; 'cmd': 0.09; 'exception,': 0.09; 'exception:': 0.09; 'handled.': 0.09; 'handler.': 0.09; 'python': 0.10; 'exception': 0.13; 'output': 0.13; 'def': 0.13; '"mkdir': 0.16; 'behaviour.': 0.16; 'cc:name:python': 0.16; 'clustering': 0.16; 'e))': 0.16; 'i.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; 'try:': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '%s"': 0.22; 'arguments': 0.22; 'exceptions': 0.22; 'function,': 0.22; 'pass': 0.22; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'example': 0.26; 'command': 0.26; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'went': 0.28; 'fine': 0.28; 'ret': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'run': 0.33; 'raising': 0.33; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'false': 0.35; 'expected': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'keyword': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'say': 0.37; 'thanks': 0.37; 'thought': 0.37; 'wanted': 0.37; 'received:209': 0.38; 'log': 0.38; 'sure': 0.39; 'virtually': 0.66; 'case?': 0.84; '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=KzOwVKuCTIW+rypmGoeZaC/5wo+wfXqmr+gxnaCX/AE=; b=EIMITwm6rwgJNOuW1ljhIf6bUF/rtSEqvprPWL3ixMwtFzSO+3U8RveKeZ+t8eA9GU Zm/y+eSEqRj0ZRpqdZgEPSw4jiVnjlZFUXuIvOadnUcxQ2lHyB4wDJsG5bK65LBpVwYr WaRAQqVxKHFAKAWs5j+kNAQCaFoFMHyDjvmDTTR+bMthkhAWCzpOty8vc8g3C3RnbFvA Dfn6wT8NNb10ZFN4z6RtnZvuIOB2SJGsmV2FbWJaI3NgnaSS40SWQnzjSAv5qjmqnvOS yDUu3RHSMTKfqu4/RFwttgKLNI/0I7nwvqUJbuWonzdi6ThkqhNu5iQOxjg9mncLmscS LZHA==
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=KzOwVKuCTIW+rypmGoeZaC/5wo+wfXqmr+gxnaCX/AE=; b=cNmB1/vXDgoQccXUTWWCQ1NGBC4tkVLFUUXiIFGdb8OjW8wD7UY/PXeks2hevB0dX+ J03i92vg1JaKbKYkEqyJP5k8tsCt+ql2zNvbjkeMb7o4H8PrJBiM661AdsFcL1RTU4WT osr3xxGUmKVXYxdu2R+OE5oJak2obYkj1HyktExIWrStk0m4qVQKcPeMcw11DCAFw7er 0I8u1FoK+KHj36GPC56BsRAz2sO1GfVjR4PlFP8JVxjtQie8uvHXNeSQ2lz20T478KzB r19kfmemPmui6og4z/pvfKlNmkHDLTKPDmsn09jlWAehicS/TUQZnMg0By9zjtSQRNw8 GZVg==
X-Gm-Message-State AD7BkJKHeYyGe6bTAbYVmhNvnXRcxG5mwDeuRaQJ18Zurct9q8rjfNPttr2foa5dm9R+ITcmZtXLe5k6J7GRDA==
X-Received by 10.25.162.144 with SMTP id l138mr6558365lfe.32.1456770097186; Mon, 29 Feb 2016 10:21:37 -0800 (PST)
In-Reply-To <CALwzidn8-HDrGSqZT2ODxhdNYL7yFt5hdmC8RnB4WC=gkD4Wtg@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:103749

Show key headers only | View raw


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

Thanks Ian for taking time and looking into the code ,  o k raise
keyword for raising exception is fine .

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

In my case the exception is nothing but the error  example if  we plan
to run the command  say  #ifconfig -a and the command fails because of
a type ( say u ran #igconfig -a).

we will the output as

# Failed to run igconfig -a got Error (ret=127)
out :
error: command not found: igconfig

So the execption is the error i.e Error (ret=127) out : error: command
not found: igconfig,  Iam fine with this behaviour.


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

I wanted to run the command in a loop  and have a fxn for the pattern
that repeats in this case the function is run_cmd_and_verify  , the
only known way to me was using try with expect

I thought I will use try and have pass in except which  you don't recommend

   for cmd in ["ls -al",
                 "touch /tmp/file1",
                "mkdir /tmp/dir1"]:
          try:
                   if not run_cmd_and_verify(cmd, timeout=3600):
                        print "running command failed "
                   return False
        except:
                pass

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

What alternative do I have other than try-expect ? can try - else be
used  for my case?

Regards,
GPal

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


Thread

Re: common mistakes in this simple program Ganesh Pal <ganesh1pal@gmail.com> - 2016-02-29 23:51 +0530
  Re: common mistakes in this simple program sohcahtoa82@gmail.com - 2016-02-29 11:23 -0800
    Re: common mistakes in this simple program Marko Rauhamaa <marko@pacujo.net> - 2016-02-29 22:29 +0200

csiph-web