questionable questions and a lazy way to add command line support to a Python module

[Thu, 31 Jan 2013 17:30:02 +0000]
Ugh. I just took this Myers-Brigg [] questionnaire and I just "love" (sarcasm) some of these questions: Do you usually get along better with imaginative people, or realistic people? What? Truly imaginative people have to be "realistic" otherwise they're just spewing out pipe-dreams. People who can think of the new and make it a reality have to have a large degree of realism. In reading for pleasure, do you enjoy odd or original ways of saying things, or like writers to say exactly what they mean? First of call, I question why the question is making some kind of statement as to writer, not the book or the "message", as the basis for selection. Secondly, show me a writer who thinks they are saying "exactly" what they mean and I'll show you a "writer" who knows not one thing about interpretation of the self let alone the words of others. Anyway ... Ok, on to the second thing I wanted to say before I jump in the shower past noon (I'm home ... say it with me ... sick). I have a Python module with lots of functions. I want them to be importable in other Python scripts AND callable via the command line. And I want them all callable via the command line without taking the time to write out command line option support. :P For example, in a script with a function "echo" that prints an argument, "add" that adds two integers, and "times" that multiplies two integers, I just want to do this: $ python echo('hello world') hello world $ python echo(add(1,2)) 3 $ python echo((add(100, times(2,10)))) 120 instead of stuff like this: $ python --function=echo arg="hello world" etc. ... Using the built-in "eval" function and "sys.argv" seems to be working: def echo(s): print s def add(x, y): return x + y def times(x, y): return x * y def main(): import sys try: funks = sys.argv[1:] #user must pass strings in single quotes. funks = " ".join(funks) eval(funks) except: pass if __name__ == "__main__": main()