官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 闽清县| 同仁县| 蒙阴县| 天津市| 丹棱县| 大同市| 青岛市| 阿坝县| 独山县| 永年县| 普宁市| 遵义市| 奉贤区| 迁西县| 镇平县| 团风县| 罗平县| 容城县| 扎鲁特旗| 垣曲县| 焉耆| 土默特左旗| 塘沽区| 南投市| 贺兰县| 顺义区| 弥勒县| 宜昌市| 剑河县| 天门市| 定襄县| 彰化市| 桐梓县| 密山市| 玛沁县| 荥阳市| 邢台县| 新干县| 兴义市| 鹤岗市| 西峡县|