Python Stuff

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:


at the end of your program.

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.

To catch signals:

import signal
def signal_handler(sig, frame):
  print "Terminating ", __file__

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.txt
  • Last modified: 2017/07/16 21:34
  • by amora