Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'jeff': 0.04; 'output': 0.04; 'defaults': 0.05; 'none,': 0.05; 'exit': 0.07; 'tests,': 0.07; 'tool,': 0.07; 'wrapper': 0.07; 'none.': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'index': 0.13; 'properly': 0.15; 'skip:f 30': 0.15; 'assigns': 0.16; 'cc:name:python list': 0.16; 'diagnostic': 0.16; 'element.': 0.16; 'failed.': 0.16; 'simplified': 0.16; 'summarize': 0.16; 'tool:': 0.16; 'wrote:': 0.17; 'element': 0.17; 'implementing': 0.17; 'skip:{ 20': 0.17; 'variables': 0.17; 'tests': 0.18; '(or': 0.18; 'written': 0.20; 'tells': 0.22; 'defined': 0.22; 'runs': 0.22; 'cc:2**0': 0.23; '15,': 0.23; 'this:': 0.23; 'idea': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'wrote': 0.26; 'values': 0.26; '(e.g.': 0.27; 'coding': 0.27; 'errors.': 0.27; 'correct': 0.28; 'run': 0.28; 'necessary,': 0.29; 'priority': 0.29; 'array': 0.29; 'summary': 0.29; "skip:' 10": 0.30; 'error': 0.30; 'code': 0.31; 'point': 0.31; 'running': 0.32; 'problem': 0.33; 'that,': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'list': 0.35; 'direction': 0.35; 'fail': 0.35; 'so,': 0.35; 'doing': 0.35; 'subject:?': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'explain': 0.36; 'tool': 0.36; 'but': 0.36; 'level.': 0.36; 'enough': 0.36; 'ok,': 0.37; 'level': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'end': 0.40; 'high': 0.61; 'first': 0.61; 'back': 0.62; 'provide': 0.62; 'series': 0.63; 'our': 0.65; 'results': 0.65; 'else.': 0.65; 'subject:there': 0.65; 'counts': 0.81; 'low': 0.83; '2013': 0.84; 'aggregated': 0.84; 'subject:handle': 0.84; 'subject:status': 0.84; 'this...': 0.84 Newsgroups: comp.lang.python Date: Tue, 15 Jan 2013 15:52:43 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=74.61.37.162; posting-account=j77mzQ8AAAANT_vs79yTKlJVSM4DQEXi References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 74.61.37.162 MIME-Version: 1.0 Subject: Re: Is there a more elegant way to handle determing fail status? From: donarb To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 201 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358293973 news.xs4all.nl 6960 [2001:888:2000:d::a6]:60513 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36876 On Tuesday, January 15, 2013 3:24:44 PM UTC-8, J wrote: > Ok, so I have a diagnostic tool, written by someone else. That tool >=20 > runs a series of small tests defined by the user and can simplified >=20 > summary output that can be one of the following: >=20 >=20 >=20 > FAILED_CRITICAL >=20 > FAILED_HIGH >=20 > FAILED_MEDIUM >=20 > FAILED_LOW >=20 > PASSED >=20 >=20 >=20 > I also have a wrapper script I wrote to run these tests, summarize the >=20 > results of all tests aggregated and then fail based on a particular >=20 > fail level. >=20 >=20 >=20 > The idea is that if I run 3 tests with the diagnostic tool and it >=20 > tells me the following: >=20 >=20 >=20 > testA: PASSED >=20 > testB: FAILED_MEDIUM >=20 > testC: PASSED >=20 >=20 >=20 > AND I told the wrapper to only fail on HIGH or above, the wrapper will >=20 > tell me that I had a medium failure, but the wrapper will still exit >=20 > with a 0 (success) >=20 >=20 >=20 > if I get the same results as above, but tell the wrapper to fail on >=20 > LOW, then it will tell me I had that medium failure, but the wrapper >=20 > will exit with a 1 (failure). >=20 >=20 >=20 > The problem is that my exit determination looks like this: >=20 >=20 >=20 > if fail_priority =3D=3D fail_levels['FAILED_CRITICAL']: >=20 > if critical_fails: >=20 > return 1 >=20 > if fail_priority =3D=3D fail_levels['FAILED_HIGH']: >=20 > if critical_fails or high_fails: >=20 > return 1 >=20 > if fail_priority =3D=3D fail_levels['FAILED_MEDIUM']: >=20 > if critical_fails or high_fails or medium_fails: >=20 > return 1 >=20 > if fail_priority =3D=3D fail_levels['FAILED_LOW']: >=20 > if critical_fails or high_fails or medium_fails or low_fails: >=20 > return 1 >=20 >=20 >=20 > return 0 >=20 >=20 >=20 > So, to explain the above... the fail level can be set by the user when >=20 > running the wrapper using -f (or it defaults to 'high') >=20 > the wrapper assigns a number to each level using this: >=20 >=20 >=20 > # Set correct fail level >=20 > args.fail_level =3D 'FAILED_%s' % args.fail_level.upper() >=20 >=20 >=20 > # Get our failure priority and create the priority values >=20 > fail_levels =3D {'FAILED_CRITICAL':4, >=20 > 'FAILED_HIGH':3, >=20 > 'FAILED_MEDIUM':2, >=20 > 'FAILED_LOW':1} >=20 > fail_priority =3D fail_levels[args.fail_level] >=20 >=20 >=20 > the variables critical_fails, high_fails, medium_fails, low_fails are >=20 > all counters that are etiher None, or the number of tests that were >=20 > failed. >=20 >=20 >=20 > So using this output from the diagnostic tool: >=20 >=20 >=20 > testA: PASSED >=20 > testB: FAILED_HIGH >=20 > testC: PASSED >=20 > testD: FAILED_MEDIUM >=20 > testE: PASSED >=20 >=20 >=20 > critical_fails would be None >=20 > high_fails would be 1 >=20 > medium_fails would be 1 >=20 > low_fails would be None. >=20 >=20 >=20 > The exit code determination above works, but it just feels inelegant. >=20 > It feels like there's a better way of implementing that, but I can't >=20 > come up with one that still honors the fail level properly (e.g. other >=20 > solutions will fail on medium, but won't fail properly on medium OR >=20 > higher). >=20 >=20 >=20 > I can provide the full script if necessary, if the above isn't enough >=20 > to point me in a direction that has a better way of doing this... >=20 >=20 >=20 > Thanks for looking, >=20 >=20 >=20 > Jeff My back of the envelope coding would do it this way. Use an array of fail_c= ounters, with PASSED as the first element all the way up to FAILED_CRITICAL= as the last element. Then use a simple loop starting from index fail_prior= ity to the end of the list looking for errors. Like this: # Array of fail counters fail_counters =3D [ 0, # PASSED 0, # LOW 0, # MEDIUM 0, # HIGH 0 # CRITICAL ] ... run tests, accumulate error counts in fail_counters for i in range(fail_priority, len(fail_counters)): if fail_counters[i]: return 1 return 0