How to handle calling functions from cli

2012-02-24 Thread Rodrick Brown
I have a bunch of sub routines that run independently to perform various system 
checks on my servers. I wanted to get an opinion on the following code I have 
about 25 independent checks and I'm adding the ability to disable certain 
checks that don't apply to certain hosts.


m = { 'a': 'checkDisks()',
  'b': 'checkMemSize()',
  'c': 'checkBondInterfaces()'
}
 
parser = argparse.ArgumentParser(description='Parse command line args.')
parser.add_argument('-x', action=store, dest=d)
r = parser.parse_args(sys.argv[1:])
 
runlist = [ c for c in m.keys() if c not in r.d ]
for runable in runlist:
eval(m[runable])

I'm using temp variable names for now until I find an approach I like.

Is this a good approach ? It doesn't look too pretty and to be honest feels 
awkward? 

Sent from my iPhone
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to handle calling functions from cli

2012-02-24 Thread Chris Rebert
On Fri, Feb 24, 2012 at 2:16 PM, Rodrick Brown rodrick.br...@gmail.com wrote:
 I have a bunch of sub routines that run independently to perform various 
 system checks on my servers. I wanted to get an opinion on the following code 
 I have about 25 independent checks and I'm adding the ability to disable 
 certain checks that don't apply to certain hosts.


 m = { 'a': 'checkDisks()',
          'b': 'checkMemSize()',
          'c': 'checkBondInterfaces()'
    }

    parser = argparse.ArgumentParser(description='Parse command line args.')
    parser.add_argument('-x', action=store, dest=d)
    r = parser.parse_args(sys.argv[1:])

    runlist = [ c for c in m.keys() if c not in r.d ]
    for runable in runlist:
        eval(m[runable])

 I'm using temp variable names for now until I find an approach I like.

 Is this a good approach ? It doesn't look too pretty and to be honest feels 
 awkward?

You should make use of the fact that functions are first-class objects
in Python:

m = { 'a': checkDisks,
'b': checkMemSize,
'c': checkBondInterfaces }
# …
for runable in runlist:
m[runable]()


Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to handle calling functions from cli

2012-02-24 Thread Chris Angelico
On Sat, Feb 25, 2012 at 9:16 AM, Rodrick Brown rodrick.br...@gmail.com wrote:
 m = { 'a': 'checkDisks()',
          'b': 'checkMemSize()',
          'c': 'checkBondInterfaces()'
    }

    runlist = [ c for c in m.keys() if c not in r.d ]
    for runable in runlist:
        eval(m[runable])

It's a reasonable technique. Does have the downside that your
functions will be called in an unpredictable order, though. If that's
a problem, replace the dictionary with a tuple of tuples (and then
just take off the .items() in the list comp).

I would be inclined to avoid eval, especially if none of your
functions need parameters. Just hold references to the functions
themselves:

checks = {
 'a': checkDisks,
 'b': checkMemSize,
 'c': checkBondInterfaces, # note that this comma is perfectly
legal - all these lines can be structured identically
}

[func[option]() for option,func in checks.items() if option not in r.d]

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list