Writing /var/lib/dokuwiki/data/meta/tutorials/python_stuff.meta failed
tutorials:python_stuff
This is an old revision of the document!
Table of Contents
Python Stuff
Main
Don't program your code on your global area, make a main function and to stuff there. To execute this main function when you run your python program use:
__name__="__main__": main()
at the end of your program.
Command line options
To parse command line options use:
import sys, os import optparse #parsing args parser=optparse.OptionParser("usage: %prog [options]") parser.add_option("-r", "--robot", dest="robot", default="lwr", type="string", help="robot name") (options, args)=parser.parse_args(sys.argv[1:])
options.<variable> will contain the data you need from the command line.
Signals
To catch signals:
import signal def signal_handler(sig, frame): print "Terminating ", __file__ stop=True stop=False signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler)
and later you can use variable “stop” to close the program correctly. You could also try to do this at the signal_handler directly if you think is more appropriate.
tutorials/python_stuff.1500258845.txt.gz · Last modified: 2022/09/20 00:08 (external edit)