FAQ
Hi All,

I'm collating a bunch of my utility scripts into one, creating a
single script to which I will symbolic link multiple times. This way
I only have to write code for error checking, output-formatting etc a
single time.

So, I have

~/bin/foo -> ~/Code/python/mother_of_all_utility_scripts.py
~/bin/bar -> ~/Code/python/mother_of_all_utility_scripts.py
~/bin/baz -> ~/Code/python/mother_of_all_utility_scripts.py

I would like "bar" to run the bar method (and so on).

-------------
class Statistic()
def __init__(self):
pass

def foo(self):
return "foo!"

def bar(self):
return "bar!"

# ... and so on...

def main():
stats_obj = Statistic()
name = re.sub("[^A-Za-z]", "", sys.argv[0])
method = getattr(stats_obj, name, None)
if callable(method):
stats_obj.name() # <------------HERE
else:
print "nope, not sure what you're after...."
-----------

However, as I'm sure you've all noticed already, there is no method
called "name". I would really prefer to get a nudge in the right
direction before I start evaling variables and so on.

Does my approach make sense? If not, please give me a hint...

Thanks

SM

Search Discussions

  • Diez B. Roggisch at Nov 4, 2009 at 2:57 pm

    Simon Mullis wrote:

    Hi All,

    I'm collating a bunch of my utility scripts into one, creating a
    single script to which I will symbolic link multiple times. This way
    I only have to write code for error checking, output-formatting etc a
    single time.

    So, I have

    ~/bin/foo -> ~/Code/python/mother_of_all_utility_scripts.py
    ~/bin/bar -> ~/Code/python/mother_of_all_utility_scripts.py
    ~/bin/baz -> ~/Code/python/mother_of_all_utility_scripts.py

    I would like "bar" to run the bar method (and so on).

    -------------
    class Statistic()
    def __init__(self):
    pass

    def foo(self):
    return "foo!"

    def bar(self):
    return "bar!"

    # ... and so on...

    def main():
    stats_obj = Statistic()
    name = re.sub("[^A-Za-z]", "", sys.argv[0])
    method = getattr(stats_obj, name, None)
    if callable(method):
    stats_obj.name() # <------------HERE
    else:
    print "nope, not sure what you're after...."
    -----------

    However, as I'm sure you've all noticed already, there is no method
    called "name". I would really prefer to get a nudge in the right
    direction before I start evaling variables and so on.

    Does my approach make sense? If not, please give me a hint...
    You are almost there. Why don't you do

    if callable(method):
    method()

    ?

    Diez
  • Carsten Haese at Nov 4, 2009 at 3:04 pm

    Simon Mullis wrote:
    def main():
    stats_obj = Statistic()
    name = re.sub("[^A-Za-z]", "", sys.argv[0])
    method = getattr(stats_obj, name, None)
    if callable(method):
    stats_obj.name() # <------------HERE
    else:
    print "nope, not sure what you're after...."
    -----------

    However, as I'm sure you've all noticed already, there is no method
    called "name". I would really prefer to get a nudge in the right
    direction before I start evaling variables and so on.
    At the point you marked "HERE", you've already found the method, and you
    have determined that it is callable. You just need to call it. Like
    this: method().

    HTH,
  • Simon Mullis at Nov 4, 2009 at 3:20 pm
    May I be the first to say "Doh!"

    Problem solved, many thanks to both Carsten and Diez!

    SM

    2009/11/4 Carsten Haese <carsten.haese at gmail.com>:
    Simon Mullis wrote:
    def main():
    ? ? stats_obj = Statistic()
    ? ? name = re.sub("[^A-Za-z]", "", sys.argv[0])
    ? ? method = getattr(stats_obj, name, None)
    ? ? if callable(method):
    ? ? ? ? stats_obj.name() ? ? ? ? ? ? ?# ?<------------HERE
    ? ? else:
    ? ? ? ? print "nope, not sure what you're after...."
    -----------

    However, as I'm sure you've all noticed already, there is no method
    called "name". I would really prefer to get a nudge in the right
    direction before I start evaling variables and so on.
    At the point you marked "HERE", you've already found the method, and you
    have determined that it is callable. You just need to call it. Like
    this: method().

    HTH,

    --
    Carsten Haese
    http://informixdb.sourceforge.net

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


    --
    Simon Mullis
    _________________
    simon at mullis.co.uk

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 4, '09 at 2:54p
activeNov 4, '09 at 3:20p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase