Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Modular Programming with Python

You're reading from   Modular Programming with Python Introducing modular techniques for building sophisticated programs using Python

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785884481
Length 246 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Erik Westra Erik Westra
Author Profile Icon Erik Westra
Erik Westra
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Introducing Modular Programming 2. Writing Your First Modular Program FREE CHAPTER 3. Using Modules and Packages 4. Using Modules for Real-World Programming 5. Working with Module Patterns 6. Creating Reusable Modules 7. Advanced Module Techniques 8. Testing and Deploying Modules 9. Modular Programming as a Foundation for Good Programming Technique Index

Using modules and packages to organize a program

Modules and packages aren't just there to spread your Python code across multiple source files and directories—they allow you to organize your code to reflect the logical structure of what your program is trying to do. For example, imagine that you have been asked to create a web application to store and report on university examination results. Thinking about the business requirements that you have been given, you come up with the following overall structure for your application:

Using modules and packages to organize a program

The program is broken into two main parts: a web interface, which interacts with the user (and with other computer programs via an API), and a backend, which handles the internal logic of storing information in a database, generating reports, and e-mailing results to students. As you can see, the web interface itself has been broken down into four parts:

  • A user authentication section, which handles user sign-up, sign-in, and sign-out
  • A web interface to view and enter exam results
  • A web interface to generate reports
  • An API, which allows other systems to retrieve exam results on request

As you consider each logical component of your application (that is, each of the boxes in the preceding illustration), you are also starting to think about the functionality that each component will provide. As you do this, you are already thinking in modular terms. Indeed, each of the logical components of your application can be directly implemented as a Python module or package. For example, you might choose to break your program into two main packages named web and backend, where:

  • The web package has modules named authentication, results, reports, and api
  • The backend package has modules named database, reportgenerator, and emailer

As you can see, each shaded box in the preceding illustration becomes a Python module, and each of the groupings of boxes becomes a Python package.

Once you have decided on the collection of packages and modules that you want to define, you can start to implement each component by writing the appropriate set of functions within each module. For example, the backend.database module might have a function named get_students_results(), which returns a single student's exam results for a given subject and year.

Note

In a real web application, your modular structure may actually be somewhat different. This is because you typically create a web application using a web application framework such as Django, which imposes its own structure on your program. However, in this example we are keeping the modular structure as simple as possible to show how business functionality translates directly into packages and modules.

Obviously, this example is fictitious, but it shows how you can think about a complex program in modular terms, breaking it down into individual components and then using Python modules and packages to implement each of these components in turn.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime