Dealing With Modules Part 3
Viewing the Module Content
Python gives you several
different ways to view module content. The method that most developers use is
to work with the dir() function, which tells you about the attributes that the
module provides.
In addition to the
SayGoodbye() and SayHello() function entries discussed previously, the list has
other entries. These attributes are automatically generated by Python for you.
These attributes perform the following tasks or contain the following information:
__builtins__: Contains a listing of all the
built-in attributes that are accessible from the module. Python adds these
attributes automatically for you.
__cached__: Tells you the name and location of
the cached file that is associated with the module. The location information
(path) is relative to the current Python directory.
__doc__: Outputs help information for the
module, assuming that you’ve actually filled it in. For example, if you type
os.__doc__ and press Enter, Python will output the help information associated
with the os library.
__file__: Tells you the name and location of
the module. The location information (path) is relative to the current Python
directory.
__initializing__: Determines whether the
module is in the process of initializing itself. Normally this attribute
returns a value of False. This attribute is useful when you need to wait until
one module is done loading before you import another module that depends on it.
__loader__: Outputs the loader information for
this module. The loader is a piece of software that gets the module and puts it
into memory so that Python can use it. This is one attribute you rarely (if
ever) use.
__name__: Tells you just the name of the
module.
__package__: This attribute is used internally
by the import system to make it easier to load and manage modules. You don’t
need to worry about this particular attribute.
It may surprise you to
find that you can drill down even further into the attri-butes. Type
dir(MyLibrary.SayHello) and press Enter. You see the entries.
Some of these entries,
such as __name__, also appeared in the module listing. However, you might be
curious about some of the other entries. For example, you might want to know
what __sizeof__ is all about. One way to get addi-tional information is to type
help(“__sizeof__”) and press Enter. You see some scanty (but useful) help
information.
Drill down as far as
needed to understand the modules that you use in Python.
Python isn’t going to
blow up if you try the attribute. Even if the shell does experience problems,
you can always start a new one. So, another way to check out a module is to
simply try the attributes. For example, if you type
MyLibrary.SayHello.__sizeof__( ) and press Enter, you see the size of the
SayHello() function in bytes,
Unlike many other
programming languages, Python also makes the source code for its native
language libraries available. For example, when you look into the \Python33\Lib
directory, you see a listing of .py files that you can open in IDLE with no
problem at all. Try opening the os.py library that you use for various tasks.
Viewing the content
directly can help you discover new programming tech-niques and better
understand how the library works. The more time you spend working with Python,
the better you’ll become at using it to build inter-esting applications.
Make sure that you just
look at the library code and don’t accidentally change it. If you accidentally
change the code, your applications can stop working. Worse yet, you can
introduce subtle bugs into your application that will appear only on your system
and nowhere else. Always exercise care when working with library code
Using the Python Module Documentation
You can use the doc()
function whenever needed to get quick help. However, you have a better way to
study the modules and libraries located in the Python path — the Python Module
Documentation. This feature often appears as Module Docs in the Python folder
on your system. It’s also referred to as pydoc. Whatever you call it, the
Python Module Documentation makes life a lot easier for developers. The
following sections describe how to work with this feature.
Opening the pydoc application
Pydoc is just another
Python application. It actually appears in the \Python33\Lib directory of your
system as pydoc.py. As with any other .py file, you can open this one with IDLE
and study how it works. You can start it using the Module Docs shortcut that
appears in the Python folder on your system or by using a command at the
command prompt.
The application creates
a localized server that works with your browser to display information about
the Python modules and libraries. So, when you start this application, you see
a command (terminal) window open
Accessing pydoc on Windows
The Windows installation
of Python has a problem. When you click Module Docs, nothing happens. Of
course, this is a bit disconcerting because users are apt to feel that something
is wrong with their systems or with Python itself. It turns out that the
shortcut is faulty. To overcome this problem, you must create a new shortcut
using the following steps:
Right-click the Desktop and choose New➪
Shortcut from the
context menu..
You see the Create
Shortcut wizard.
Type C:\Python33\python.exe C:\Python33\
Lib\pydoc.py -b and click Next..
This command line starts
a copy of the pydoc server so that you can access module information.
Type pydoc and click Finish..
Windows creates a new
shortcut for you. This shortcut allows you to access the module help
information that currently doesn’t work with Python 3.3.4 on Windows.
As with any server, your
system may prompt you for permissions. For exam-ple, you may see a warning from
your firewall telling you that pydoc is attempt-ing to access the local system.
You need to give pydoc permission to work with the system so that you can see
the information it provides. Any virus detection that you have installed may
need permission to let pydoc continue as well. Some platforms, such as Windows,
may require an elevation in privileges to run pydoc.
Normally, the server
automatically opens a new browser window for you. This window contains links to
the various modules that are contained on your system, including any custom
modules you create and include in the Python path. To see information about any
module, you can simply click its link.
The command prompt
provides you with two commands to control the server. You simply type the
letter associated with the command and press Enter to activate it. Here are the
two commands:
b: Starts a new copy of the default browser
with the index page loaded.
q: Stops the server.
When you’re done
browsing the help information, make sure that you stop the server by typing q
and pressing Enter at the command prompt. Stopping the server frees any
resources it uses and closes any holes you made in your firewall to accommodate
pydoc.
Using the quick-access links
Near the top of
the page, you see three links. These links provide quick access to the site
features. The browser always begins at the Module Index. If you need to return
to this page, simply click the Module Index link.
This page
con-tains links for essential Python topics. For example, if you want to know
more about Boolean values, click the BOOLEAN link. The page you see next
describes how Boolean values work in Python. At the bottom of the page are
related links that lead to pages that contain additional helpful information.
What you see is a
list of the keywords that Python supports. For example, if you want to know
more about creating for loops, you click the for link.
Typing a search term
The pages also include
two text boxes near the top. The first has a Get button next to it and the
second has a Search button next to it. When you type a search term in the first
text box and click Get, you see the documentation for that particular module or
attribute.
When you type a search
term in the second text box and click Search, you see all the topics that could
relate to that search term.
Viewing the results
In this particular case,
you see related module information, error information, functions, data, and all
sorts of additional information about the calendar printing functions. The
amount of information you see depends partly on the complexity of the topic and
partly on the amount of information the devel-oper provided with the module.
For example, if you were to select MyLibrary from the Module Index page, you
would see only a list of functions and no documentation at all.
0 comments:
Post a Comment