Carter: Hi! It's Carter here. This blog post is a guide to adding help to your Python modules. We will be starting with our TempConv module from Chapter 15:
# this is the file "my_module.py"
# we're going to use it in another program
def c_to_f(celsius):
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
First, let's see what help() brings up in Python:
>>> import my_module
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named my_module
Oops! We forgot that Python can't find our module unless it's in a folder where python can find it. Navigate to this folder in Windows Explorer (or your favourite file browser):
C:\Python25\Lib\site-packages\
Among other things, you should see a Pygame folder, a PythonCard folder, and easygui.py (you might have to scroll down). Save my_module.py there, restart IDLE (or SPE, or Python Shell), and then try importing it again to make sure Python can find it.
Now, let's see what help() brings:
>>> import my_module
>>> help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
# this is the file "my_module.py"
# we're going to use it in another program
FUNCTIONS
c_to_f(celsius)
# this is the file "my_module.py"
# we're going to use it in another program
Not very user-friendly. Let's try changing the comments.
# My Module
# Our first module.
def c_to_f(celsius):
#converts celsius to fahrenheit
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
And now restart IDLE again (this is getting annoying...) and call help():
>>>import my_module
>>>help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
#My Module
#Our first module.
FUNCTIONS
c_to_f(celsius)
Huh. Why didn't c_to_f's comment appear? The answer is that help() uses strings, but supports comments at the start of the program. Let's change my_module again:
"""My Module
Our first module."""
def c_to_f(celsius):
"converts celsius to fahrenheit"
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
And now restart IDLE and use help again:
>>> import my_module
>>> help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
My Module
Our first module.
FUNCTIONS
c_to_f(celsius)
converts celsius to fahrenheit
There, now that looks a lot better. Also, Python classes display similar help. Try typing this into my_module:
"""My Module
Our first module."""
def c_to_f(celsius):
"converts celsius to fahrenheit"
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
class SampleObject:
"Sample object"
def __init__(self, number):
"Initializes object"
self.number = number
def addOne(self):
"Adds one to the number"
self.number += 1
And the help() is:
>>> import my_module
>>> help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
My Module
Our first module.
CLASSES
SampleObject
class SampleObject
Sample object
Methods defined here:
__init__(self, number)
Initializes object
addOne(self)
Adds one to the number
FUNCTIONS
c_to_f(celsius)
converts celsius to fahrenheit
One more thing: For help() to work correctly, your string also has to be on the first line of your program (or function, or class, or method), and be indented correctly.
And that's how you can make your modules look great in help()!
To Warren Sande and Carter Sande,
ReplyDeleteThe email address I have for you is not working. I decided to post here in hopes that you would contact me again. Thank you!
Students in my class are having trouble with two things in particular. Can you help?
1] Skier program will not compile and run after copy and pasting from your website, some students get an error message about skiier.pngs missing
2] Python card will not install
Here's what a student reported to me:
"I saved the installer to program files, and then I installed it and it went into python26. then it said ''could not set key value'' and gave me more error messages."
First, I should ask what operating system you are using.
ReplyDeleteSecond, to run Skier, you need not only the code, but also the graphics (all the .png files). You can find them at the download site:
http://www.manning.com/sande/sourcecode/All_Files_By_Chapter/hw_ch10_code/
The version of Python installed by the book's installer is Python 2.5. The quote from the student above mentions Python 2.6 If there is another version of Python already installed, that could cause problems.
By the way, you can also post at the Author Online forum on the books' Manning page:
http://www.manning-sandbox.com/forum.jspa?forumID=410
Regards,
Warren Sande