Chapter 5. Working with Module Patterns

In the previous chapters, we have looked in detail at how Python modules and packages work, and learned how you can use them in your programs. When using modular programming techniques, you will find that the ways in which modules and packages are used tend to follow standard patterns. In this chapter, we will examine a number of these common patterns for using modules and packages to deal with a range of programming challenges. In particular, we will:

  • Learn how the divide and conquer technique helps you to solve programming problems
  • See how the principle of abstraction helps you to separate what you want to do from how you do it
  • Discover how encapsulation allows you to hide the details of how information is represented from the rest of your system
  • See that wrappers are modules that call other modules to simplify or alter the way a module is used
  • Learn how to create extensible modules

Let's start by looking at the principle of divide and conquer.

Divide and conquer

Divide and conquer is the process of breaking a problem down into smaller parts. You might not know how to solve a particular problem, but by breaking it down into smaller parts, you can then solve each part in turn, which then solves the original problem.

This is a very general technique, of course, and doesn't just apply to the use of modules and packages. However, modular programming helps you work through the divide and conquer process: as you break your problem down, you discover that you'll need a part of your program which performs a given task or range of tasks, and Python modules (and packages) are the perfect way of organizing those tasks.

We have done this several times already in this book. For example, when faced with the challenge of creating a chart-generation library, we used the divide and conquer technique to come up with the notion of a renderer that could draw a single chart element. We then realized that we would need several different renderers, which translated perfectly to the renderers package containing a separate module for each renderer.

The divide and conquer approach doesn't just suggest a possible modular structure for your code, it works the other way around too. As you think about the design for your program, you may come up with the notion of a module or package that does something related to the problem you're trying to solve. You might even map out the individual functions that each module and package provides. Even though you don't yet know how to solve the whole problem, this modular design helps you to clarify your thinking about the problem, which in turn makes it easier to use the divide-and-conquer approach to solve the remainder of the problem. In other words, modules and packages help you to clarify your thinking as you work through the divide and conquer process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.194