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:
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 namedauthentication
,results
,reports
, andapi
- The
backend
package has modules nameddatabase
,reportgenerator
, andemailer
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.