Optimize this code further

Ganesh Pal ganesh1pal at gmail.com
Tue Apr 4 12:56:03 EDT 2017


Dear friends.



I am trying  optimize the below  sample code any suggestion on this please
let me know



yy-1# cat file1.py

#! /usr/bin/python

from __future__ import absolute_import, division, print_function

import os

import sys

import logging



logging.basicConfig(stream=sys.stderr, level=logging.INFO)

logger = logging.getLogger(__name__)

LOG_DIR = '/var/log/frobnitz'



def createTestCases(LOG_DIR):

    '''create a test case data dictionary with parameters'''

    d = dict()

    d['test01'] = dict(object="inode", offset="18", size="4", optype="set")

    lin_02 = "something"

    d['test02'] = dict(object="lin", lin=lin_02, offset="18", size="5",

                       optype="set")

    return addLogFilename(d, LOG_DIR)



def run_tool(logfile, **kw):

    logger.info('%s would execute with %r', logfile, kw)



def addLogFilename(d, logdir):

    '''put the logfile name into the test case data dictionary'''

    for casename, args in d.items():

        args['logfile'] = os.path.join(logdir, casename + '.log')

    return d



def main():

    testcases = createTestCases(LOG_DIR)

    get_baddr = dict()

    for casename, kw in testcases.items():

        # -- yank the logfile name out of the dictionary, before calling
func

        logfile = kw.pop('logfile')

        get_baddr[casename] = run_tool(logfile, **kw)



if __name__ == '__main__':

    main()



# -- end of file

yy-1# python file1.py

INFO:__main__:/var/log/frobnitz/test02.log would execute with {'size': '5',
'lin': 'something', 'optype': 'set', 'object': 'lin', 'offset': '18'}

INFO:__main__:/var/log/frobnitz/test01.log would execute with {'object':
'inode', 'optype': 'set', 'offset': '18', 'size': '4'}



1.   I now need to have sub tests for every test i.e  , so in the above
program I will now need to store logs  in the below format  say for
/var/log/frobnitz/Test_inode_01/ test01.log  and
/var/log/frobnitz/Test_inode_01/ test02.log .. etc

2.   Everything else remains same  and  I am supposed to retain the same
directory structure and logging



Here is what I plan to do :

1.   Use dictionary and Change testcases  variable as dictionary and let
its key store  the subtest cases , so we have a nested dictionary

testcase = {}

testcase[‘Test_inode_1’] = createTestCases(LOG_DIR)

2.    Since I need to retain the same logging  directory structure , maybe
I should try to get the key first and then append it to the log file


Question :

1.   Any other simple tips and tricks to solve the problem  ,  I am a Linux
user using python 2.7



Regards,

Ganesh



More information about the Python-list mailing list