Python Documentation Fran Fitzpatrick Overview Comments Documentation Strings

  • Slides: 11
Download presentation
Python Documentation Fran Fitzpatrick

Python Documentation Fran Fitzpatrick

Overview § Comments § Documentation Strings § Pydoc

Overview § Comments § Documentation Strings § Pydoc

Comments § Python Comments Symbol: # § Block Comments § Inline Comments

Comments § Python Comments Symbol: # § Block Comments § Inline Comments

Comments import string, sys # If no arguments were given, print a helpful message

Comments import string, sys # If no arguments were given, print a helpful message if len(sys. argv)==1: � print 'Usage: celsius temp 1 temp 2. . . ' 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 '%i260 F = %i260 C' % (int(fahrenheit), int(celsius+. 5)) �

Documentation Strings § Shortened to “Docstrings” § Symbol: “ ” ” (three quotes) §

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

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 get. Obstacle("right"): backward(1, . 1) turn. Left(. 7, . 1) elif get. Obstacle("left"): backward(1, . 1) turn. Right(. 7, . 1) else: forward(1) wait(. 1)

Pydoc § Automatic Doc Generation� § Command line options: § pydoc <module> -- man-like

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 a few programs that would work well # for our robot.

Pydoc # Here 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 get. Obstacle("right"): backward(1, . 1) turn. Left(. 7, . 1) elif get. Obstacle("left"): backward(1, . 1) turn. Right(. 7, . 1) else: forward(1) wait(. 1)

Pydoc >>> import robot >>> help(robot) Help on module robot: NAME robot FILE /Volumes/THAWSPACE/Fran/robot.

Pydoc >>> import robot >>> help(robot) Help on module robot: NAME robot FILE /Volumes/THAWSPACE/Fran/robot. py DESCRIPTION # Here 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

Summary § Comments § Documentation Strings § Pydoc