how to optimize the below code with a helper function
Martin A. Brown
martin at linux-ip.net
Mon Apr 4 10:45:19 EDT 2016
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/
More information about the Python-list
mailing list