Home > Dive Into Python > An Object-Oriented Framework > A brief review | << >> |
diveintopython.org | |
Python for experienced programmers | |
Looking over this program, several things should look vaguely familiar.
First of all, each function has a doc string which explains the function's purpose. In addition, here's something you haven't seen before: the module itself has a doc string which explains what the module does and how to use it. This is standard practice in Python programming, and no serious module should be without one. Besides being the first thing that an adventurous programmer sees when she opens up your source code, don't forget that it is available at run-time, and from the Python IDE.
Example 3.3. Accessing a module's doc string
>>> import fileinfo >>> print fileinfo.__doc__ Framework for getting file information, including filetype-specific metadata. Instantiate appropriate class with filename to get metadata. Returned object acts just like a dictionary, with key-value pairs for each piece of metadata. import fileinfo info = fileinfo.MP3FileInfo("/music/ap/mahadeva.mp3") print "\\n".join(["%s=%s" % (k, info[k]) for k in info.keys()]) Or use listDirectory function to get info on all files in a directory. infoList = fileinfo.listDirectory("/music/ap/", [".mp3"]) for info in infoList: ... Framework can be extended by adding classes for particular file types, e.g. HTMLFileInfo, MPGFileInfo, DOCFileInfo. Each class is completely responsible for parsing its files appropriately; see MP3FileInfo for example.
![]() | |
Always include a doc string for each function, each class, each method, and the module itself. |
Something else that should look familiar: importing other modules, using both the import module syntax and the from module import syntax. The modules themselves (os and UserDict) are ones we haven't covered yet; don't worry, we'll explain them later in this chapter.
And of course, don't forget the if __name__ technique.
« An Object-Oriented Framework | Private functions » |