Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103699
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ganesh Pal <ganesh1pal@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: usage of try except for review. |
| Date | Mon, 29 Feb 2016 15:33:18 +0530 |
| Lines | 117 |
| Message-ID | <mailman.5.1456740206.20602.python-list@python.org> (permalink) |
| References | <CACT3xuWm56OE7LpR8kBLfWfkDoU18sPcN5DRiXjr=68+Kywo8Q@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de cInJVdfA9VLoJsOqlhB/lA8Ym/WNXwf6Z5C+XKZCoJtQ== |
| 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; 'sys': 0.05; '(b)': 0.07; '__name__': 0.07; 'except:': 0.07; 'formatting': 0.07; 'main()': 0.07; 'skip:/ 10': 0.07; '%s\\n"': 0.09; 'cmd': 0.09; 'err:': 0.09; 'exited': 0.09; 'exits': 0.09; 'meaningful': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'resend': 0.15; "'__main__':": 0.16; '1):': 0.16; 'e))': 0.16; 'err,': 0.16; 'main():': 0.16; 'pdb': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:usage': 0.16; 'subprocess': 0.16; 'try:': 0.18; 'to:name:python-list@python.org': 0.20; '%s"': 0.22; '(a)': 0.22; 'pass': 0.22; 'errors': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'linux': 0.26; 'earlier': 0.27; 'error': 0.27; 'logging': 0.27; 'skip:# 10': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; '2.6': 0.27; 'function': 0.28; 'fine': 0.28; 'looks': 0.29; 'ret': 0.29; 'becomes': 0.30; 'skip:[ 10': 0.31; 'post': 0.31; 'posting': 0.32; 'screen': 0.32; 'returned': 0.32; 'run': 0.33; 'displayed': 0.33; 'correctly': 0.34; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'displays': 0.35; 'false': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'suggestion': 0.37; 'creation': 0.38; 'received:209': 0.38; 'log': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; 'further': 0.62; 'future.': 0.67; 'message")': 0.84; 'subject:try': 0.84; 'improvement': 0.93; 'mistakes': 0.95 |
| 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 :content-transfer-encoding; bh=ALOYc2q+5OX8UbjsOaglAKa9bwFmvJVZJmugMXVT9aU=; b=IekVn2cAy5SBL4x/UdYjEDfp5pmc2Hf89U+bLujdei4uDK15+zGTRLEtDBbHKzP8GI 25ulunTyomBUrjxKrP14iQ5azF6gzfwaLCPAhEVTm+x9f3Iskj73DjcWnVnd1fBSJ5hh PbyWZUAORIaQCJl9RAs+YBxIgVuJtcK1hWAY2J6tVNbK6IwUapt6vkr+HJF/hG5Mvctc 8OPie0xvwDwhlEuG6aWDGwv4zMy7IYjfN9aQDcArgCuNX1Ol7QM/xs1+dL7xxOMrVUpb Z1tTNOKNEE2I7yGhPifsEir9hC1W3wm/UVzD3ga6N7kJ0sLWVDwDPU5L7xATvbsMnt8H qMXA== |
| 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:content-transfer-encoding; bh=ALOYc2q+5OX8UbjsOaglAKa9bwFmvJVZJmugMXVT9aU=; b=dxSQN465iAyO1iGQ7fvMOUr4QSFJc5FzQnrEVD1oKkWVUxOJ3lPwUb7hazWEAzViKt mGEFFaw8a9dM6xQdhqIpsOS4GpS8hiVBKUf0T5s0PDFkJcRuBTmKlhcJ5roubORyG/0n Sh9HafIKwId1TAG/HnhV45KPtyVGW3fLE6ChnedTPZ5HQSeQu9WQ3xQQpI3wjRP6OnxB O3/BEbyVDZmE6pYLTPI8GUgRSliMACBb7kO76dnszDGhluGg3eg77j9saRTa2QXfQ7FT q+wrKood34a8gIGruXsJCUbHyOhxdU30+fRAuELrM5arlpjWPu0gec3qXGSVMTsK6N32 GGTA== |
| X-Gm-Message-State | AD7BkJIWXG1i8mw/aW58OVHXo1KWZc+j7YqyEQ6US0ygzOyv4Qq8chJo3Z6cOjnr+KIyrQSzZaW5tIKPchjwtw== |
| X-Received | by 10.112.14.102 with SMTP id o6mr5072120lbc.87.1456740198409; Mon, 29 Feb 2016 02:03:18 -0800 (PST) |
| In-Reply-To | <CACT3xuWm56OE7LpR8kBLfWfkDoU18sPcN5DRiXjr=68+Kywo8Q@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:103699 |
Show key headers only | View raw
Iam really sorry , I will have to resend my question again , because
the earlier post had mistakes and formatting was bad , so apologies
for top posting will try to avoid such mistakes in future.
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)
except Exception as e:
logging.error("Failed to run %s got %s" % (cmd, e))
return False
return True
def run_test():
"""
Mount
"""
pdb.set_trace()
for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]:
try:
if not run_cmd_and_verify(cmd, timeout=3600):
return False
except:
logging.error("Some meaningful message")
logging.info("Setup and Creation ....Done !!!")
#
cmd = "run_scan"
out, err, ret = run(cmd)
for cmd in ["create_data.py -nfs ",
"validate.py -30 "]:
try:
if not run_cmd_and_verify(cmd, timeout=3600):
return False
except:
logging.error("some meaningful message")
return False
logging.info("Mount IS START.....Done !!!")
def main():
if not run_test():
sys.exit("Exiting Main")
if __name__ == '__main__':
main()
Question 1:
(a) Have I used try and expect block correctly ? in run_test() I
have expect block which displays some error message instead of pass
, is it fine ?
(b) Have I returned True and False correctly , looks fine for me
try:
if not run_cmd_and_verify(cmd, timeout=3600):
return False
except:
logging.error("inside except")
return False
Question 2.
(a) If a failure’s are encountered the error by assert condition the
errors are now displayed on the screen , how do I redirect it to log
file using logging error
because the moment assert ret==0 becomes true the program exits
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
#script_10.py
Failed to run mount /nfs got ERROR (ret 1):
out:
host-44-3 exited with status 1
err:
host-44-3: mount_efs: on /nfs: efs is already mounted
3. my function def has 1000 but Iam using 3600 in the calling fnx etc
, Time out value are overwritten ?
4. Any further improvement particularly on try -except ?
Regards,
Ganesh
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: usage of try except for review. Ganesh Pal <ganesh1pal@gmail.com> - 2016-02-29 15:33 +0530
csiph-web