Categories
python

Modular Programming with Python – review of first 3 chapters

Review Post – Modular Python by Erik Westra, Chapters 1-3

  • Functions are one of the most powerful tools in programming. As with any powerful tool, they also raise many questions and issues. Python has a specific terminology around functions, along with how to organize them into files that define Python modules. This book teaches firstly, Python modules as files that have Python functions, and secondly, modular programming in general. Modular programming is organizing large programs into pieces that you can reuse as requirements change. In this post I review how the book explains Python modules from the perspective of files of Python functions for those of us who want to learn how python modules work.
  • Chapter 1: Introducing Modular Programming
    • The introduction has some good basic goals. However the very first modular program uses very slightly advanced points that are irrelevant for the purpose of explaining modules, for example module-level global variables and exceptions. That said, for programmers who are not beginners, this is fine.
    • The next example, though somewhat simpler, similarly glosses over concepts which Westra hasn’t really introduced yet. The example packages a group of animal modules (toy Python functions in toy Python files) into an animal package, along with its initialization file – although we are still trying to learn what a module is . Next is an example of a larger program that might tell us why and how modularization improves our codebase and workflow.
    • Westra next shows us how to implement a module involving a cache. He gives us the definition of the module-level global variable we first saw, along with some public and private module functions. Overall this first chapter doesn’t teach us anything specific, and I recommend skimming it just to get an idea of his approach.
  • Chapter 2: Writing your first Modular Program
    • This chapter involves an inventory control system with modules for a report generator, data storage, and a user interface. The code covers a number of details that help us understand the size and scope of a program that begins to benefit from a modular approach.
    • Though seeing a big program is useful, the program again goes into many technical details that aren’t relevant to the modular programming concept – Westra acknowledges this and clearly says “don’t worry too much about the details of these functions”
    • In the implementation of the main program we see how to import the modules we created – this is the important point. This too is a chapter we can skim.
  • Chapter 3: Using Modules and Packages
    • Here we begin to actually learn how to create and use a directory (called a package) of Python module files.
    • From the outset Westra discusses packages within packages – this is a potentially confusing concept that he illustrates well, early, and consistently so that we can understand the principles clearly.
    • He diagrams directory trees and how these relate to packages which makes these clear. He next discusses initialization of modules – giving specific code examples, presented well and clearly.
    • After that, Westra goes into what the import statement does – likewise, specific, succinct and thorough.
    • Relative imports are likewise complex and so a major source of confusion. But again Westra diagrams the situation helpfully and clearly. He gives straightforward diagrams, and then writes the exact code that executes the depicted relative import. This method lets us quickly understand how the relative import system works.
Categories
python

While loops: 5 things to know

Number 1: what is a while loop:

Loops cause computers to repeat certain calculations in certain ways. While loops are one of the basic building blocks of programs. They can be a simple way to cause the program to repeat a particular set of instructions while a specific condition holds.

For example, we may want to repeatedly add numbers, sales or units, to find a total. While loops tell a computer to repeatedly perform such an instruction while a certain condition holds. Continuing the example, we may have a sales log file. Whenever a sale occurs, a program may add row a with the date, order number, quantity, price, and part number.

We may want to write a new program that instructs the
computer to read rows of the logfile and sum up the quantity*price. The while loop would help us total amount over the whole logfile, reading and summing while there are rows of data.

But we can do more complex tasks, such as finding sales up to Jan 1 of this year, by adding a condition to the while that the date is before jan 1 of this year. Similarly we can sum while the subtotal is below $20,000, which would tell us how long and what transactions let to that amount.

Though powerful, we face a learning curve when starting to write while loops. Here we cover 2 main categories of issues.

While Condition

Number 2: Never stopping

The while loop repeats the instructions while a certain condition holds, and stops repeating when that condition no longer holds. So the condition in the while loop needs to correctly capture all situations when the instructions are supposed to

  • run, and
  • stop

The classic error is the runaway while loop
here the stop is not reached:

i = 0
while i < 10:
    print(i)

Number 3: Never starting

Another classic is one where the loop never runs to begin with

i = 10
while i < 10:
    print(i)

Issues with updating

Number 4: updating issues

The other key issue is that the while loop typically needs to update some aspect of the data, such as a variable or a file
Often some arithmetic or complex logic is involved. These lead to many issues and need to be thoroughly understood while example with

i = 0
while i <= 10:
    print(i)
    i = i * 1.1

will never end because multiplication by 0 always results in 0.
Similarly

i = 0.1
while i <= sqrt(i):
    i = sqrt(i)
    print(i)

will never terminate because although i gets larger each time through the loop, it will never reach 1 since sqrt(x) < 1 for all x

Number 5: The off-by-1 error

The last is the off-by-1. Often loops have a small issue with the
update or with the while condition that causes errors that are
either 1 more or 1 less than the desired outcome in some way.
For example we may want to print 10 numbers but get 11:

i = 0
while i <= 10:
    print(i)
    i += 1

or we may get 9 numbers:

i = 1
while i < 10:
    print(i)
    i += 1

There are likewise 2 ways to get 10 numbers, only one of which might
be desired outcome

i = 1
while i <= 10:
    print(i)
    i += 1

or

i = 0
while i < 10:
    print(i)
    i += 1