Dealing With Modules Part 1
Even tiny real-world
applications contain thousands of lines of code. In fact, applications that
contain millions of lines of code are somewhat common. Imagine trying to work
with a file large enough to contain millions of lines of code — you’d never find
anything. In short, you need some method to organize code into small pieces
that are easier to manage, much like the examples in this book. The Python
solution is to place code in separate code groupings called modules. Commonly
used modules that contain source code for generic needs are called libraries.
Modules are contained in
separate files. In order to use the module, you must tell Python to grab the
file and read it into the current application. The process of obtaining code
found in external files is called importing. You import a module or library to
use the code it contains. A few examples in the book have already shown the
import statement in use, but this post explains the import statement in detail
so that you know how to use it.
As part of the initial
setup, Python created a pointer to the general-purpose libraries that it uses.
That’s why you can simply add an import statement with the name of the library
and Python can find it. However, it pays to know how to locate the files on
disk in case you ever need to update them or you want to add your own modules
and libraries to the list of files that Python can use.
The library code is
self-contained and well documented (at least in most cases it is). Some
developers might feel that they never need to look at the library code, and
they’re right to some degree — you never have to look at the library code in
order to use it. You might want to view the library code, though, to ensure
that you understand how the code works. In addition, the library code can teach
you new programming techniques that you might not otherwise discover. So,
viewing the library code is optional, but it can be helpful.
The one thing you do
need to know how to do is obtain and use the Python library documentation. This
post shows you how to obtain and use the library documentation as part of the
application-creation process.
Creating Code Groupings
It’s important to group like pieces of code together to make the
code easier to use, modify, and understand. As an application grows, managing
the code found in a single file becomes harder and harder. At some point, the
code becomes impossible to manage because the file has become too large for
anyone to work with.
The term code is used
broadly in this particular case. Code groupings can include:
- Classes
- Functions
- Variables
- Runnable code
from...import: You use the from...import statement when you want to selectively import individual module attributes. This method saves resources, but at the cost of complexity. In addition, if you try to use an attribute that you didn’t import, Python registers an error. Yes, the module still contains the attribute, but Python can’t see it because you didn’t import it.
The collection of
classes, functions, variables, and runnable code within a module is known as
attributes. A module has attributes that you access by that attribute’s name.
Later sections in this Post discuss precisely how module access works.
The runnable code can
actually be written in a language other than Python. For example, it’s somewhat
common to find modules that are written in C/C++ instead of Python. The reason
that some developers use runnable code is to make the Python application
faster, less resource intensive, and better able to use a particular platform’s
resources. However, using runnable code comes with the downside of making your
application less portable (able to run on other platforms) unless you have
runnable code modules for each platform that you want to support. In addition,
dual-language applications can be harder to maintain because you must have
developers who can speak each of the computer languages used in the
application.
The most common way to
create a module is to define a separate file containing the code you want to
group separately from the rest of the application. For example, you might want
to create a print routine that an application uses in a number of places. The
print routine isn’t designed to work on its own but is part of the application
as a whole. You want to separate it because the application uses it in numerous
places and you could potentially use the same code in another application. The
ability to reuse code ranks high on the list of reasons to create modules.
To make things easier to
understand, the examples in this Post use a common module. The module doesn’t
do anything too amazing, but it demonstrates the principles of working with
modules. Open a Python File window and create a new file named MyLibrary.py.
Listing 10-1: A
Simple Demonstration Module
def SayHello(Name):
print("Hello ", Name) return
def SayGoodbye(Name):
print("Goodbye ", Name) return
The example code
contains two simple functions named SayHello() and SayGoodbye(). In both cases,
you supply a Name to print and the func-tion prints it onscreen along with a
greeting for you. At that point, the function returns control to the caller.
Obviously, you normally create more complicated functions, but these functions
work well for the purposes of this Post.
In order to use a
module, you must import it. Python places the module code inline with the rest
of your application in memory — as if you had created one huge file. Neither
file is changed on disk — they’re still separate, but the way Python views the
code is different.
You have two ways to
import modules. Each technique is used in specific circumstances:
import: You use the import statement when you
want to import an entire module. This is the most common method that developers
use to import modules because it saves time and requires only one line of code.
However, this approach also uses more memory resources than does the approach
of selectively importing the attributes you need, which the next paragraph
describes.
Now that you have a
better idea of how to import modules, it’s time to look at them in detail. The
following sections help you work through importing mod-ules using the two
techniques available in Python.
Changing the current Python directory
The directory that
Python is using to access code affects which modules you can load. The Python
library files are always included in the list of locations that Python can
access, but Python knows nothing of the directory you use to hold your source
code unless you tell it to look there. The easiest method for accomplish ing
this task is to change the current Python directory to point to your code
folder using these steps:
Open the Python Shell..
You see the Python Shell
window appear.
Type import os and press Enter..
This action imports the
Python os library. You need to import this library to change the directory (the
location Python sees on disk) to the working directory for this book.
Type os.chdir(“C:\BP4D\New folder”) and press
Enter..
You need to use the directory
that contains the downloadable source or your own proj ect files on your local
hard drive. Python can now use the down loadable source code directory to
access modules that you create for this Post
0 comments:
Post a Comment