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


Groups > comp.lang.python > #106442 > unrolled thread

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

Started by"Martin A. Brown" <martin@linux-ip.net>
First post2016-04-04 07:45 -0700
Last post2016-04-04 07:45 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

From"Martin A. Brown" <martin@linux-ip.net>
Date2016-04-04 07:45 -0700
SubjectRe: how to optimize the below code with a helper function
Message-ID<mailman.20.1459781126.32530.python-list@python.org>
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/

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web