Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Martin A. Brown" Newsgroups: comp.lang.python Subject: Re: how to optimize the below code with a helper function Date: Mon, 4 Apr 2016 07:45:19 -0700 Lines: 88 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Trace: news.uni-berlin.de lfv7iRuVK+dRcmUOdFo7jQ0GlyKnu6mHDCe62bLIJqJw== 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; 'sys': 0.05; '__name__': 0.07; 'skip:/ 10': 0.07; 'subject:code': 0.07; 'cc:addr:python- list': 0.09; '__future__': 0.09; 'answering': 0.09; 'collections': 0.09; 'logger': 0.09; 'def': 0.13; "'__main__':": 0.16; 'division,': 0.16; 'enjoy,': 0.16; 'from:addr:martin': 0.16; 'function?': 0.16; 'i.e': 0.16; 'received:hsd1.or.comcast.net': 0.16; 'received:io': 0.16; 'received:or.comcast.net': 0.16; 'received:psf.io': 0.16; 'reproduce': 0.16; 'config': 0.18; 'helper': 0.18; 'skip:l 30': 0.18; 'tests.': 0.18; 'variable': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'martin': 0.22; 'object.': 0.22; 'parser': 0.22; 'file.': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'figure': 0.27; 'logging': 0.27; 'parameters': 0.27; 'question': 0.27; 'received:24': 0.28; 'function': 0.28; "skip:' 10": 0.28; 'creating': 0.30; 'call.': 0.30; "i'd": 0.31; 'another': 0.32; 'problem': 0.33; 'optimize': 0.33; 'received:comcast.net': 0.33; 'case,': 0.34; 'file': 0.34; 'handle': 0.34; 'could': 0.35; 'should': 0.36; 'cases': 0.36; 'subject:: ': 0.37; 'charset:us- ascii': 0.37; 'skip:p 20': 0.38; 'end': 0.39; 'test': 0.39; 'data': 0.39; 'subject:the': 0.39; 'subject:with': 0.40; 'called': 0.40; 'your': 0.60; 'received:network': 0.61; 'capture': 0.66; '100': 0.79; 'configparser': 0.84; 'subject:below': 0.84; '"how': 0.91; 'luck': 0.95 X-X-Sender: mabrown@macron.wonderfrog.net In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:106442 Hello again, >(1) Any tips how I can optimize this i.e test case, should have a >helper function that all test cases call. > >(2) Also note that failure.run_tool function can have variable >number of argments how to handle this in the helper function? Here's a little example of how you could coerce your problem into a ConfigParser-style configuration file. With this example, I'd think you could also see how to create a config section called [lin_02] that contains the parameters you want for creating that object. Then, it's a new problem to figure out how to refer to that object for one of your tests. Anyway, this is just another way of answering the question of "how do I simplify this repetitive code". Good luck and enjoy, -Martin #! /usr/bin/python from __future__ import absolute_import, division, print_function import os import sys import collections from ConfigParser import SafeConfigParser as ConfigParser import logging logging.basicConfig(stream=sys.stderr, level=logging.INFO) logger = logging.getLogger(__name__) LOG_DIR = '/var/log/frobnitz' def readCfgTestCases(cfgfile): data = collections.defaultdict(dict) parser = ConfigParser() parser.read(cfgfile) for section in parser.sections(): for name, value in parser.items(section): data[section][name] = value return data def main(cfgfile): testdata = readCfgTestCases(cfgfile) for k, v in testdata.items(): print(k, v) if __name__ == '__main__': main(sys.argv[1]) # -- end of file # -- config file [test01] offset = 18 size = 4 object = inode optype = set [test02] # -- no way to capture lin=lin_02; must reproduce contents of lin_02 object = lin offset = 100 size = 5 optype = set [test100] # -- no way to capture baddr=lin_02; must reproduce contents of lin_02 object = baddr offset = 100 size = 5 optype = set -- Martin A. Brown http://linux-ip.net/