Using truth tables for program logic

We have primarily used truth tables in the context of our boolean operators (e.g., and), but they are also a useful tool when figuring out our program logic.

Consider the “aggregator” part of the lab. How do we know when we want to write to the file? We can use a truth table to help us figure that out, and then implement the necessary logic. There are two “inputs”:

  1. Does the file exist?
  2. Is there an existing entry in the file for the current date and time?

Based on the lab specification, we can define the following truth table for this functionality:

File exists Existing entry Write new entry
False N/A True
True True False
True False True

That truth table leads to the following pseudo code for this operation:

not file_exists(filename) or not entry_in_file(filename, date, hour)

As noted in the assignment guide, the exists function within the os.path module will return True if the file specified by its string argument exists (i.e., the “file_exists” operation above).