140 likes | 457 Views
Python Documentation. Fran Fitzpatrick. Overview. Comments Documentation Strings Pydoc. Comments. Python Comments Symbol: # Block Comments Inline Comments. Comments. import string, sys # If no arguments were given, print a helpful message if len(sys.argv)==1:
E N D
Python Documentation Fran Fitzpatrick
Overview • Comments • Documentation Strings • Pydoc
Comments • Python Comments Symbol: # • Block Comments • Inline Comments
Comments import string, sys # If no arguments were given, print a helpful message if len(sys.argv)==1: print 'Usage: celsius temp1 temp2 ...' sys.exit(0) # Loop over the arguments for i in sys.argv[1:]: try: fahrenheit=float(string.atoi(i)) except string.atoi_error: #ascii to integer error print repr(i), "not a numeric value" else: celsius=(fahrenheit-32)*5.0/9.0 print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5))
Documentation Strings • Shortened to “Docstrings” • Symbol: “ ” ” (three quotes) • Use for all public: • Functions • Methods • Modules • Classes • Why?
Documentation Strings from myro import * init("/dev/tty.scribbler") def avoid(): """ This is a simple function that will simple make the robot wander around avoiding all obstacles. If it encounters an obstacle, it will back up and turn the other way. """ while True: if getObstacle("right"): backward(1, .1) turnLeft(.7,.1) elif getObstacle("left"): backward(1,.1) turnRight(.7,.1) else: forward(1) wait(.1)
Pydoc • Automatic Doc Generation • Command line options: • pydoc <module> -- man-like command • pydoc -w <module> -- write HTML file to current directory • pydoc -k <arg> -- will search synopsis of all available modules for the search string ‘arg’ • pydoc -p <port> -- will start a webserver on specified port
Pydoc # Here are a few programs that would work well # for our robot. def avoid(): """ This is a simple function that will simple make the robot wander around avoiding all obstacles. If it encounters an obstacle, it will back up and turn the other way. """ while True: if getObstacle("right"): backward(1, .1) turnLeft(.7,.1) elif getObstacle("left"): backward(1,.1) turnRight(.7,.1) else: forward(1) wait(.1)
Pydoc >>> import robot >>> help(robot) Help on module robot: NAME robot FILE /Volumes/THAWSPACE/Fran/robot.py DESCRIPTION # Here are a few programs that would work well # for our robot. FUNCTIONS avoid() This is a simple function that will simple make the robot wander around avoiding all obstacles. If it encounters an obstacle, it will back up and turn the other way.
Summary • Comments • Documentation Strings • Pydoc