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


Groups > comp.lang.python > #106442

Re: how to optimize the below code with a helper function

From "Martin A. Brown" <martin@linux-ip.net>
Newsgroups comp.lang.python
Subject Re: how to optimize the below code with a helper function
Date 2016-04-04 07:45 -0700
Message-ID <mailman.20.1459781126.32530.python-list@python.org> (permalink)
References <CACT3xuUQc9wFPqG1DHM0ecwKE2J8KiFKFDui=FvWBqccAGLEoA@mail.gmail.com> <alpine.LSU.2.11.1604040739440.18407@znpeba.jbaqresebt.arg>

Show all headers | View raw


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/

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


Thread

Re: how to optimize the below code with a helper function "Martin A. Brown" <martin@linux-ip.net> - 2016-04-04 07:45 -0700

csiph-web