Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to handle calling functions from cli
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rodrick Brown  
View profile  
 More options Feb 24, 5:16 pm
Newsgroups: comp.lang.python
From: Rodrick Brown <rodrick.br...@gmail.com>
Date: Fri, 24 Feb 2012 17:16:09 -0500
Local: Fri, Feb 24 2012 5:16 pm
Subject: How to handle calling functions from cli
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Feb 24, 5:41 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Fri, 24 Feb 2012 14:41:28 -0800
Local: Fri, Feb 24 2012 5:41 pm
Subject: Re: How to handle calling functions from cli

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Feb 24, 5:43 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sat, 25 Feb 2012 09:43:20 +1100
Local: Fri, Feb 24 2012 5:43 pm
Subject: Re: How to handle calling functions from cli

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »