Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103692
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ganesh Pal <ganesh1pal@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | usage of try except for review. |
| Date | Mon, 29 Feb 2016 13:43:29 +0530 |
| Lines | 190 |
| Message-ID | <mailman.1.1456733616.20602.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de ZyF+vnOH6+qtPmGHNUvbXgi2TGT0XXjnBOq1he32ibAg== |
| 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; 'modified': 0.05; 'sys': 0.05; '__name__': 0.07; 'except:': 0.07; 'main()': 0.07; 'skip:/ 10': 0.07; '%s\\n"': 0.09; 'cmd': 0.09; 'err:': 0.09; 'exited': 0.09; 'sake': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; "'__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; 'errors': 0.23; 'import': 0.24; 'command': 0.26; 'linux': 0.26; '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; 'ret': 0.29; 'code:': 0.29; 'skip:[ 10': 0.31; 'included': 0.32; 'screen': 0.32; 'run': 0.33; 'displayed': 0.33; 'correctly': 0.34; 'file': 0.34; 'except': 0.34; 'running': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'false': 0.35; 'but': 0.36; 'needed': 0.36; 'received:209.85': 0.36; 'to:addr:python- list': 0.36; 'expect': 0.37; 'suggestion': 0.37; 'creation': 0.38; 'received:209': 0.38; 'log': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'your': 0.60; 'further': 0.62; 'subject:try': 0.84; 'improvement': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to :content-transfer-encoding; bh=v1+zjubcN4C8bXEmKG9GHLlyU3FN/BhdOl1duY62rNA=; b=h4NoR2vApY+NusIHjk//HMOo19W9epJMw+xh9TyhsFzIG9Z66++hzRMR22hCMxvNwo 8IaxhHledURCQkKX2mdYAD7SQGanp2AP4cu658/DponlSYA+J4nxd3NLe37sUN1d1CQQ xS353vuvs66VMucy4IgP3c2uO4rNxc/+yRF1SSdCL5iXF03KfS0DeYyngObA1UVDy2tj BNFUIPHkQ4XT4Vl0gwfOvHebz6ve1W7nvOb2LvHqIlrRIVhN4HMBVRDJAUIFVqsUOYeF 69SytH7PIDQhqYr+g4WS5oRwVcPueCqTi+qXMr9g97nJIynABzW59e9rhOeAZ99TJauJ /ZUQ== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-transfer-encoding; bh=v1+zjubcN4C8bXEmKG9GHLlyU3FN/BhdOl1duY62rNA=; b=BBZ/kUDB8zCVDnBoNZIcyz0d8T0Ws7FSWruUA70Y6j41S8//zLXNoXYbJCeLNA5BxT 1TQ/J1gHvu16CEXSltIA/2+r9dwQNwoTqW1aMuyKgNtBaDSDSHDx2rekuv52kbVE5WZj eKTJnZnrz2MOgN0CwcKt082l3WBinxAskGoMmo39tYrBs6fQtpfeSYyJ0GZZcpvsHNxe SJA6I/w3mvz2VaVrTHD79JFF5dkCvcV/oV6GLganacc3+hqELVFoF35zEMGyBqn6uE2Q iijr8W0LEuVK0Mjtq0Llvmw5zOCmkDbmYBM1A4bHfcxX6Dapy6dIbkOil7XLb1t8ZbYx y5Jw== |
| X-Gm-Message-State | AD7BkJL03NGixPxnvEbvCiRIs7Fx5bfx/S9y1UWyU7Oli9HkcVf+X04JzKb074tgn0ObJPpG0Rwan4q6GYqUxg== |
| X-Received | by 10.112.54.201 with SMTP id l9mr5071872lbp.105.1456733609297; Mon, 29 Feb 2016 00:13:29 -0800 (PST) |
| 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:103692 |
Show key headers only | View raw
Iam on python 2.6 and Linux , need your suggestion on the usage of try
and except in this program and
Modified code:
#!/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("Failure while running command %")
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("")
return False
logging.info("Mount IS START.....Done !!!")
def main():
if not run_test():
sys.exit("Exiting Main")
if __name__ == '__main__':
main()
Question 1:
1. Have I used try and expect block correctly ? , In my case I have
the except block that's is not needed it just gives an message I
have still included for the sake of try block
try:
if not run_cmd_and_verify(cmd, timeout=3600):
return False
except:
logging.error("inside except")
return False
2. 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
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 — Next in thread | Find similar | Unroll thread
usage of try except for review. Ganesh Pal <ganesh1pal@gmail.com> - 2016-02-29 13:43 +0530 Re: usage of try except for review. Steven D'Aprano <steve@pearwood.info> - 2016-03-02 03:58 +1100
csiph-web