Welcome to Computer Programming For Kids (CP4K)

Welcome to the Computer Programming for Kids blog! We are the co-authors of the book "Hello World! Computer Programming for Kids and Other Beginners", released in March 2009. The book is published by Manning Publications. Manning has a web site for the book, which is where you can download the software and other files related to the book. You can purchase it here, at the Manning web site. If you use this link to purchase the print book or e-book from the Manning web site, you can use the discount code aupromo40 at checkout to get a 40% discount. You can also get it through online retailers like Amazon, as well as in many retail locations such as Chapters in Canada and Barnes & Noble in the U.S. This blog is mostly about the book (for now), but anything related to computers and programming, particularly for kids and beginners, is fair game. We will post articles with extra material that didn't make it into the book, and reader feedback and suggestions are always welcome.

Sunday, April 5, 2009

Python Help and Your Modules

Warren: In the book, we talked a bit about Python's help system. This is something that can give you help on using Python's built-in modules and functions as well as external modules and functions. But we didn't tell you how to add help to your own modules. In this post, we will. I'm going to let Carter take it from here.

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()!

2 comments:

  1. To Warren Sande and Carter Sande,

    The 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."

    ReplyDelete
  2. First, I should ask what operating system you are using.

    Second, 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

    ReplyDelete

Note: Only a member of this blog may post a comment.