Book Image

Modular Programming with Python

By : Erik Westra
Book Image

Modular Programming with Python

By: Erik Westra

Overview of this book

Python has evolved over the years and has become the primary choice of developers in various fields. The purpose of this book is to help readers develop readable, reliable, and maintainable programs in Python. Starting with an introduction to the concept of modules and packages, this book shows how you can use these building blocks to organize a complex program into logical parts and make sure those parts are working correctly together. Using clearly written, real-world examples, this book demonstrates how you can use modular techniques to build better programs. A number of common modular programming patterns are covered, including divide-and-conquer, abstraction, encapsulation, wrappers and extensibility. You will also learn how to test your modules and packages, how to prepare your code for sharing with other people, and how to publish your modules and packages on GitHub and the Python Package Index so that other people can use them. Finally, you will learn how to use modular design techniques to be a more effective programmer.
Table of Contents (16 chapters)
Modular Programming with Python
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

Modular programming is a way of organizing your program's source code. By organizing your code into modules (Python source files) and packages (collections of modules), and then importing those modules and packages into your program, you can keep your programs logically organized and keep potential problems to a minimum.

As your program grows and changes, you will often have to rewrite or expand certain parts of your code. Modular programming techniques help to manage these changes, minimizing side-effects, and keeping your code under control.

As you work with modular programming techniques, you will learn a number of common patterns for using modules and packages, including the divide and conquer approach to programming, the use of abstraction and encapsulation, and the idea of writing extensible modules.

Modular programming techniques are also a great way of sharing your code, either by making it available for other people to use or by reusing your code in another program. Using popular tools such as GitHub and the Python Package Index, you will learn how to publish your code, as well as use code written by other people.

Putting all these techniques together, you will learn how apply "modular thinking" to create better programs. You will see how modules can be used to deal with complexity and change in a large program and how modular programming really is the foundation of good programming technique.

By the end of the book, you will have an excellent understanding of how modules and packages work in Python and how to use them to create high-quality and robust software that can be shared with others.

What this book covers

Chapter 1, Introducing Modular Programming, looks at the ways you can use Python modules and packages to help organize your programs, why it is important to use modular techniques, and how modular programming helps you to deal with the ongoing process of programming.

Chapter 2, Writing Your First Modular Program, introduces the divide and conquer approach to programming and applies this technique to the process of building an inventory control system based on modular programming principles.

Chapter 3, Using Modules and Packages, covers the nuts and bolts of modular programming using Python, including nested packages, package and module initialization techniques, relative imports, choosing what gets imported, and how to deal with circular references.

Chapter 4, Using Modules for Real-World Programming, uses the implementation of a chart-generation library to show how modular techniques help to deal with changing requirements in the best possible way.

Chapter 5, Working with Module Patterns, looks at a number of standard patterns for working with modules and packages, including the divide and conquer technique, abstraction, encapsulation, wrappers, and how to write extensible modules using dynamic imports, plugins, and hooks.

Chapter 6, Creating Reusable Modules, shows how to design and create modules and packages that are intended to be shared with other people.

Chapter 7, Advanced Module Techniques, looks at some of the more distinctive aspects of modular programming in Python, including optional and local imports, tweaking the module search path, "gotchas" to be aware of, how to use modules and packages for rapid application development, working with package globals, package configuration, and package data files.

Chapter 8, Testing and Deploying Modules, examines the concept of unit testing, how to prepare your modules and packages for publication, how to upload and publish your work, and how to make use of modules and packages written by other people.

Chapter 9, Modular Programming as a Foundation for Good Programming Technique, shows how modular techniques help to deal with the ongoing process of programming by dealing with change and managing complexity, and how modular programming techniques help you to be a more effective programmer.

What you need for this book

All you need to follow through the examples in this book is a computer running any recent version of Python. While the examples all use Python 3, they can easily be adapted to work with Python 2 only a few changes.

Who this book is for

This book is aimed at the beginner to intermediate level Python programmer who wishes to use modular programming techniques to create high-quality and well organized programs. While the reader must know the basics of Python, no prior knowledge of modular programming is required.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " This one-line program would be saved in a file on disk, typically named something like hello.py "

A block of code is set as follows:

def init():
    global _stats
    _stats = {}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)

Any command-line input or output is written as follows:

# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample
     /etc/asterisk/cdr_mysql.conf

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Modular-Programming-with-Python. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.