E
Eli Stevens \(WG.c\)
I have a question about proper Python style when it comes to having a main
function in a module. I'm fairly new to Python - a few months of
very-part-time tinkering (lots'o'Java at work, shrug); my apologies if this
has been hashed out before. Random Googling didn't enlighten me, so instead
I'll ask here.
Take the following strmod.py (it's silly, I know):
----
import sys
import getopt
def main(sys_argv):
try:
opts, args = getopt.getopt(sys_argv, "clstu")
for str_toPrint in args:
for opt, arg in opts:
if opt == '-c':
str_toPrint = str_toPrint.capitalize()
elif opt == '-l':
str_toPrint = str_toPrint.lower()
elif opt == '-s':
str_toPrint = str_toPrint.swapcase()
elif opt == '-t':
str_toPrint = str_toPrint.title()
elif opt == '-u':
str_toPrint = str_toPrint.upper()
print str_toPrint
except getopt.GetoptError:
pass
if (__name__ == '__main__'):
main(sys.argv[1:])
----
Now, from what I have seen in terms of examples etc. would do something like
(note the lack of sys_argv, and how sys.argv[1:] is imbedded in the
getop.getopt call):
----
import sys
import getopt
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "clstu")
# ...
print str_toPrint
except getopt.GetoptError:
pass
if (__name__ == '__main__'):
main()
----
This essentially makes strmod.main() uncallable by anything else that needs
command line args, right? The first pattern allows calls like
strmod.main("-t -s pRINT mE tHE sAME uNTESTED".split()) from elsewhere. It
seems to me that this would be very helpful when writing low-level utilities
that could be driven by other higher-level utilities without needing to fall
back to OS calls, etc.
So... Is this a good idea? Bad idea? Is there a better way? I'm just
trying to not fall into any newbie pit traps ("Hey, what's at the bottom of
this nifty hole?" .
TIA,
Eli
--
Give a man some mud, and he plays for a day.
Teach a man to mud, and he plays for a lifetime.
WickedGrey.com uses SpamBayes on incoming email:
http://spambayes.sourceforge.net/
--
function in a module. I'm fairly new to Python - a few months of
very-part-time tinkering (lots'o'Java at work, shrug); my apologies if this
has been hashed out before. Random Googling didn't enlighten me, so instead
I'll ask here.
Take the following strmod.py (it's silly, I know):
----
import sys
import getopt
def main(sys_argv):
try:
opts, args = getopt.getopt(sys_argv, "clstu")
for str_toPrint in args:
for opt, arg in opts:
if opt == '-c':
str_toPrint = str_toPrint.capitalize()
elif opt == '-l':
str_toPrint = str_toPrint.lower()
elif opt == '-s':
str_toPrint = str_toPrint.swapcase()
elif opt == '-t':
str_toPrint = str_toPrint.title()
elif opt == '-u':
str_toPrint = str_toPrint.upper()
print str_toPrint
except getopt.GetoptError:
pass
if (__name__ == '__main__'):
main(sys.argv[1:])
----
Now, from what I have seen in terms of examples etc. would do something like
(note the lack of sys_argv, and how sys.argv[1:] is imbedded in the
getop.getopt call):
----
import sys
import getopt
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "clstu")
# ...
print str_toPrint
except getopt.GetoptError:
pass
if (__name__ == '__main__'):
main()
----
This essentially makes strmod.main() uncallable by anything else that needs
command line args, right? The first pattern allows calls like
strmod.main("-t -s pRINT mE tHE sAME uNTESTED".split()) from elsewhere. It
seems to me that this would be very helpful when writing low-level utilities
that could be driven by other higher-level utilities without needing to fall
back to OS calls, etc.
So... Is this a good idea? Bad idea? Is there a better way? I'm just
trying to not fall into any newbie pit traps ("Hey, what's at the bottom of
this nifty hole?" .
TIA,
Eli
--
Give a man some mud, and he plays for a day.
Teach a man to mud, and he plays for a lifetime.
WickedGrey.com uses SpamBayes on incoming email:
http://spambayes.sourceforge.net/
--