07. Comprehensive example

A working example of that touches every topic so far.

You are getting the first edition of all these pages. Please let me know if you find an error!

We have covered quite a bit. Let’s go through an example from problem statement to implementation to test using what we’ve learned so far.

We’ll start with this high-level description of the problem:

You are tasked with writing a program that can read in a text file where each line has the name of a species of bird. Your program needs to count the number of times each species appears. An example of the input is below. Ask the user to type in the name of the file they wish to be processed.

White-eared Hummingbird
Townsend's Solitaire
Townsend's Solitaire
Yellow-fronted Canary
Chestnut-fronted Macaw

Your program must handle any text file in this format.

Crafting a problem statement

An ounce of planning is worth a pound of programming.

Implementation

We’ll start by doing the simplest thing that meets the requirements of the problem description.

Sample input files:

Most programs and their constituent functions can be thought of as having three parts: (1) read the input, (2) compute something, (3) generate output.

Testing

Time to test using pytest using what we learned from the pytest lab and testing for exceptions.

Reworking the code to be testable

We cannot test our code as it. We need to reorganize for testability. User interface code is difficult to unit test.

Writing pytest code

Finally time to test. When you write test cases and assertions, you are checking the actual computed result against the expected result for a given input.

Recap

We went from high level problem description, to problem statement, to an initial implementation, to reorganizing our code to be testable, to finally writing our tests.

You need to become comfortable with all these steps!

Ending files:

Last modified September 16, 2024.