Beginning Programming with Python For Dummies (2014)

Part III

Performing Common Tasks

See an example of how you can named arguments in format strings at

www.dummies.com/extras/beginningprogrammingwithpython.

In this part . . .

✓ Gain access to Python modules.

✓ Slice and dice strings to meet your output needs.

✓ Create lists of objects you want to manage.

✓ Use collections to organize data efficiently.

✓ Develop classes to make code reusable.

Chapter 10

Interacting with Modules

In This Chapter

▶ Organizing your code

▶ Adding code from outside sources to your application

▶ Locating code libraries on disk

▶ Looking at the library code

▶ Obtaining and reading the Python library documentation

he examples in this book are small, but the functionality of the resulting applications is extremely limited as well. 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 calledmodules.

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 chapter 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.

184 Part III: Performing Common Tasks

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 chapter 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

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 chapter 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

Chapter 10: Interacting with Modules 185

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 chapter 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. Type the code found

in Listing 10-1 and save it to disk. (This module also appears with the downloadable source code as 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 chapter.

Importing Modules

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.

186 Part III: Performing Common Tasks

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.

✓ 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.

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 modules using the two techniques available in Python.

Changing the current Python directory

The directory that Python is using to access This action imports the Python os library.

code affects which modules you can load. The

You need to import this library to change

Python library files are always included in the

the directory (the location Python sees on

list of locations that Python can access, but

disk) to the working directory for this book.

Python knows nothing of the directory you use

to hold your source code unless you tell it to 3 . Type os.chdir(“C:\BP4D\Chapter10”) and look there. The easiest method for accomplish

press Enter .

ing this task is to change the current Python You need to use the directory that contains directory to point to your code folder using

the downloadable source or your own projthese steps:

ect files on your local hard drive. The book

1 . Open the Python Shell .

uses the default book directory described in

Chapter 4. Python can now use the down

You see the Python Shell window appear.

loadable source code directory to access

2 . Type import os and press Enter .

modules that you create for this chapter.

Chapter 10: Interacting with Modules 187

Using the import statement

The import statement is the most common method for importing a module

into Python. This approach is fast and ensures that the entire module is ready for use. The following steps get you started using the import statement.

1. Open the Python Shell.

You see the Python Shell window appear.

2. Change directories to the downloadable source code directory.

See the instructions found in the “Changing the current Python directory”

sidebar.

3. Type import MyLibrary and press Enter.

Python imports the contents of the MyLibrary.py file that you created

in the “Creating Code Groupings” section of the chapter. The entire library is now ready for use.

It’s important to know that Python also creates a cache of the module

in the __pycache__ subdirectory. If you look into your source code

directory after you import MyLibrary for the first time, you see the new

__pycache__ directory. If you want to make changes to your module,

you must delete this directory. Otherwise, Python will continue to use

the unchanged cache file instead of your updated source code file.

4. Type dir(MyLibrary) and press Enter.

You see a listing of the module contents, which includes the SayHello()

and SayGoodbye() functions, as shown in Figure 10-1. (A discussion of

the other entries appears in the “Viewing the Module Content” section of

the chapter.)

Figure 10-1:

A directory listing

shows that

Python

imports both

functions

from the

module.

188 Part III: Performing Common Tasks

5. Type MyLibrary.SayHello(“Josh”) and press Enter.

The SayHello() function outputs the expected text, as shown in

Figure 10-2.

Figure 10-2:

The Say

Hello()

function

outputs the

expected

greeting.

Notice that you must precede the attribute name, which is the Say

Hello() function in this case, with the module name, which is

MyLibrary. The two elements are separated by a period. Every

call to a module that you import follows the same pattern.

6. Type MyLibrary.SayGoodbye(“Sally”) and press Enter.

The SayGoodbye( ) function outputs the expected text.

7. Close the Python Shell.

The Python Shell window closes.

Using the from...import statement

The from...import statement has the advantage of importing only the

attributes you need from a module. This difference means that the module

uses less memory and other system resources than using the import state-

ment does. In addition, the from...import statement makes the module

a little easier to use because some commands, such as dir(), show less

information, or only the information that you actually need. The point is that you get only what you want and not anything else. The following steps demonstrate using the from...import statement.

Chapter 10: Interacting with Modules 189

1. Open the Python Shell.

You see the Python Shell window appear.

2. Change directories to the downloadable source code directory.

See the instructions found in the “Changing the current Python direc-

tory” sidebar.

3. Type from MyLibrary import SayHello and press Enter.

Python imports the SayHello() function that you create in the “Creating

Code Groupings” section, earlier in the chapter. Only this specific function is now ready for use.

You can still import the entire module, should you want to do so. The

two techniques for accomplishing the task are to create a list of mod-

ules to import (the names can be separated by commas, such as from

MyLibrary import SayHello, SayGoodbye) or to use the asterisk

(*) in place of a specific attribute name. The asterisk acts as a wildcard character that imports everything.

4. Type dir(MyLibrary) and press Enter.

Python displays an error message, as shown in Figure 10-3. Python

imports only the attributes that you specifically request. This means

that the MyLibrary module isn’t in memory — only the attributes that

you requested are in memory.

Figure 10-3:

The

from...

import

statement

imports only

the items

that you

specifically

request.

5. Type dir(SayHello) and press Enter.

You see a listing of attributes that are associated with the SayHello()

function, as shown in Figure 10-4. It isn’t important to know how these

attributes work just now, but you’ll use some of them later in the book.

190 Part III: Performing Common Tasks

Figure 10-4:

Use the

dir()

function

to obtain

information

about the

specific

attributes

you import.

6. Type SayHello(“Angie”) and press Enter.

The SayHello() function outputs the expected text, as shown in

Figure 10-5.

Figure 10-5:

The Say

Hello()

function

no longer

requires

the module

name.

Chapter 10: Interacting with Modules 191

When you import attributes using the from...import statement, you

don’t need to precede the attribute name with a module name. This fea-

ture makes the attribute easier to access.

Using the from...import statement can also cause problems. If two

attributes have the same name, you can import only one of them. The

import statement prevents name collisions, which is important when

you have a large number of attributes to import. In sum, you must exer-

cise care when using the from...import statement.

7. Type SayGoodbye(“Harold”) and press Enter.

You imported only the SayHello() function, so Python knows noth-

ing about SayGoodbye() and displays an error message. The selective

nature of the from...import statement can cause problems when you

assume that an attribute is present when it really isn’t.

8. Close the Python Shell.

The Python Shell window closes.

Finding Modules on Disk

In order to use the code in a module, Python must be able to locate the

module and load it into memory. The location information is stored as paths within Python. Whenever you request that Python import a module, Python

looks at all the files in its list of paths to find it. The path information comes from three sources:

✓ Environment variables: Chapter 3 tells you about Python environment variables, such as PYTHONPATH, that tell Python where to find modules

on disk.

✓ Current directory: Earlier in this chapter, you discover that you can change the current Python directory so that it can locate any modules

used by your application.

✓ Default directories: Even when you don’t define any environment

variables and the current directory doesn’t yield any usable modules,

Python can still find its own libraries in the set of default directories that are included as part of its own path information.

It’s helpful to know the current path information because the lack of a path can cause your application to fail. The following steps demonstrate how you can obtain path information:

1. Open the Python Shell.

You see the Python Shell window appear.

192 Part III: Performing Common Tasks

2. Type import sys and press Enter.

3. Type for p in sys.path: and press Enter.

Python automatically indents the next line for you. The sys.path

attribute always contains a listing of default paths.

4. Type print(p) and press Enter twice.

You see a listing of the path information, as shown in Figure 10-6. Your

listing may be different from the one shown in Figure 10-6, depending on

your platform, the version of Python you have installed, and the Python

features you have installed.

Figure 10-6:

The sys.

path

attribute

contains a

listing of the

individual

paths for

your system.

The sys.path attribute is reliable but may not always contain every path

that Python can see. If you don’t see a needed path, you can always check in another place that Python looks for information. The following steps show how to perform this task:

1. Type import os and press Enter.

2. Type os.environ[‘PYTHONPATH’].split(os.pathsep) and press Enter.

When you have a PYTHONPATH environment variable defined, you see

a list of paths, as shown in Figure 10-7. However, if you don’t have the

environment variable defined, you see an error message instead.

Notice that both the sys.path and the os.environ['PYTHONPATH']

attributes contain the C:\BP4D\Chapter10 entry in this case.

The sys.path attribute doesn’t include the split() function,

Chapter 10: Interacting with Modules 193

which is why the example uses a for loop with it. However, the os.

environ['PYTHONPATH'] attribute does include the split() func-

tion, so you can use it to create a list of individual paths.

You must provide split() with a value to look for in splitting a list of

items. The os.pathsep constant (a variable that has one, unchangeable, defined value) defines the path separator for the current platform so

that you can use the same code on any platform that supports Python.

3. Close the Python Shell.

The Python Shell window closes.

Figure 10-7:

You must

request

information about

environment

variables

separately.

You can also add and remove items from sys.path. For example, if you want to add Chapter 9 to the list of modules, you type sys.path.append("C:\\

BP4D\\Chapter09") and press Enter in the Python Shell window. When you list the sys.path contents again, you see that the new entry is added.

Likewise, when you want to remove an entry, such as Chapter 9, you type

sys.path.remove("C:\\BP4D\\Chapter09") and press Enter.

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.

194 Part III: Performing Common Tasks

Look at Figure 10-1, earlier in the chapter. 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 attributes. Type dir(MyLibrary.SayHello) and press Enter. You see the entries shown in Figure 10-8.

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 additional information is to type help(“__sizeof__”) and press Enter. You see some scanty (but useful) help information, as shown in Figure 10-9.

Chapter 10: Interacting with Modules 195

Figure 10-8:

Drill down

as far as

needed to

understand

the modules

that you use

in Python.

Figure 10-9:

Try getting

some help

information

about the

attribute

you want to

know about.

196 Part III: Performing Common Tasks

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, as shown in Figure 10-10.

Figure 10-10:

Using the

attributes

will help you

get a better

feel for how

they work.

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 in this chapter, and you see the content shown in Figure 10-11.

Chapter 10: Interacting with Modules 197

Figure 10-11:

Directly

viewing

module

code can

help in

understanding it.

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 interesting 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.

198 Part III: Performing Common Tasks

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 like the one shown in Figure 10-12.

Accessing pydoc on Windows

The Windows installation of Python has a prob

2 . Type C:\Python33\python.exe C:\Python33\

lem. When you click Module Docs, nothing

Lib\pydoc.py b and click Next .

happens. Of course, this is a bit disconcerting

because users are apt to feel that something This command line starts a copy of the is wrong with their systems or with Python

pydoc server so that you can access

itself. It turns out that the shortcut is faulty. To

module information.

overcome this problem, you must create a new 3 . Type pydoc and click Finish .

shortcut using the following steps:

Windows creates a new shortcut for you.

1 . Right-click the Desktop and choose New

This shortcut allows you to access the

Shortcut from the context menu .

module help information that currently

You see the Create Shortcut wizard.

doesn’t work with Python 3.3.4 on Windows.

Chapter 10: Interacting with Modules 199

Figure 10-12:

Starting

pydoc

means

opening a

command

(terminal)

window to

start the

server.

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, as shown in Figure 10-13. 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.

200 Part III: Performing Common Tasks

Figure 10-13:

Your

browser

displays a

number of

links that

appear as

part of the

Index page.

Using the quick-access links

Refer back to Figure 10-13. 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.

The Topics link takes you to the page shown in Figure 10-14. This page contains 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.

The Keywords link takes you to the page shown in Figure 10-15. 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.

Chapter 10: Interacting with Modules 201

Figure 10-14:

The Topics

page tells

you about

essential

Python

topics, such

as how

Boolean

values work.

Figure 10-15:

The

Keywords

page

contains

a listing of

keywords

that Python

supports.

202 Part III: Performing Common Tasks

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. Figure 10-16 shows what you see when you type print and click Get.

Figure 10-16:

Using Get

obtains

specific

information

about a

search term.

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. Figure 10-17 shows typical results when you type print and click Search. In this case, you click a link, such as calendar, to see additional information.

Chapter 10: Interacting with Modules 203

Figure 10-17:

Using

Search

obtains a

list of topics

about a

search term.

Viewing the results

The results you get when you view a page depends on the topic. Some topics are brief, such as the one shown in Figure 10-16 for print. However, other topics are extensive. For example, if you were to click the calendar link in Figure 10-17, you would see a significant amount of information, as shown in Figure 10-18.

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 developer 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.

204 Part III: Performing Common Tasks

Figure 10-18:

Some pages

contain

extensive

information.

Chapter 11

Working with Strings

In This Chapter

▶ Considering the string difference

▶ Using special characters in strings

▶ Working with single characters

▶ Performing string-specific tasks

▶ Finding what you need in a string

▶ Modifying the appearance of string output

our computer doesn’t understand strings. It’s a basic fact. Computers understand numbers, not letters. When you see a string on the computer screen, the computer actually sees a series of numbers. However,

humans understand strings quite well, so applications need to be able to

work with them. Fortunately, Python makes working with strings relatively easy. It translates the string you understand into the numbers the computer understands, and vice versa.

In order to make strings useful, you need to be able to manipulate them. Of course, that means taking strings apart and using just the pieces you need or searching the string for specific information. This chapter describes how you can build strings using Python, dissect them as needed, and use just

the parts you want after you find what’s required. String manipulation is an important part of applications because humans depend on computers performing that sort of work for them (even though the computer has no idea of what a string is).

After you have the string you want, you need to present it to the user in an eye-pleasing manner. The computer doesn’t really care how it presents the string, so often you get the information, but it lacks pizzazz. In fact, it may be downright difficult to read. Knowing how to format strings so that they look nice onscreen is important because users need to see information in a form they understand.

By the time you complete this chapter, you know how to create, manipulate, and format strings so that the user sees precisely the right information.

206 Part III: Performing Common Tasks

Understanding That Strings

Are Different

Most aspiring developers (and even a few who have written code for a

long time) really have a hard time understanding that computers truly do

only understand 0s and 1s. Even larger numbers are made up of 0s and 1s.

Comparisons take place with 0s and 1s. Data is moved using 0s and 1s. In

short, strings don’t exist for the computer (and numbers just barely exist).

Although grouping 0s and 1s to make numbers is relatively easy, strings are a lot harder because now you’re talking about information that the computer must manipulate as numbers but present as characters.

There are no strings in computer science. Strings are made up of characters, and individual characters are actually numeric values. When you work with strings in Python, what you’re really doing is creating an assembly of characters that the computer sees as numeric values. That’s why the following sections are so important. They help you understand why strings are so special.

Understanding this material will save you a lot of headaches later.

Defining a character using numbers

To create a character, you must first define a relationship between that

character and a number. More important, everyone must agree that when

a certain number appears in an application and is viewed as a character by that application, the number is translated into a specific character. One of the most common ways to perform this task is to use the American Standard Code for Information Interchange (ASCII). Python uses ASCII to translate the number 65 to the letter A. The chart at http://www.asciitable.com/

shows the various numeric values and their character equivalents.

Every character you use must have a different numeric value assigned to

it. The letter A uses a value of 65. To create a lowercase a, you must assign a different number, which is 97. The computer views A and a as completely different characters, even though people view them as uppercase and lowercase versions of the same character.

The numeric values used in this chapter are in decimal. However, the com-

puter still views them as 0s and 1s. For example, the letter A is really the value 01000001 and the letter a is really the value 01100001. When you see an A onscreen, the computer sees a binary value instead.