BYU logo Computer Science

How to write a program 💻

You have a job. You write terminal applications.

Your manager comes in one day and says that a new order has come in.

You need to write a program that retrieves dog facts from a remote server and displays them in the terminal.

Dog-lovers around the world will go wild for this app, the company stock will soar, and you’ll have a warm-fuzzy feeling inside for making a difference in the world.

And you say, “I’ve got this!”

😎

Step 0: Review the requirements

  • Do you know what your program is supposed to do?
  • What information will you need to get it done?
  • Are there decisions you are supposed to make?
  • Are there decisions the customer needs to make?

Requirements

  • Dog-fact server: http://dog-api.kinduff.com
  • Facts endpoint: /api/facts
  • Customer wants 1 fact per call
  • The fact should be displayed on the terminal
  • The script should be named dog_facts.py
  • The app should follow the POSIX convention for options and arguments

POSIX

Positional Arguments

Positional arguments are simply listed after the command.

python my_script.py

my_script.py is a positional argument to python.

Sub-commands

conda install jupyter

install is a subcommand to conda.

jupyter is a positional arguments to the conda install command.

Named Arguments

Named arguments are key-value pairs. They key has a -- in front of it.

conda create --name cs110 pandas jupyter

The --name cs110 is a named argument. The name is name and the value is cs110.

pandas jupyter are positional arguments.

Short Named Arguments

Short named arguments are indicated with a single - followed by a letter.

conda create -n cs110 pandas

The conda command supports -n as shorthand for --name.

Flags

A “flag” is a named argument (could be short) that does not have a value.

Flags are also called options.

Typically, these arguments turn a feature on or off.

pip install -y byubit

The -y is a flag that indicates that pip should perform the installation without prompting the user to confirm.

The argparse library allows us to easily implement command-line arguments and options that adhere to the POSIX conventions.

argparse is a built-in package. You don’t need to pip install it.

You can find a thorough tutorial for argparse here: https://docs.python.org/3/howto/argparse.html

Step 1: Set up the skeleton

  • Start a project in PyCharm
    • Decide what environment you will use for this project
    • Will you need specific libraries?
  • Create a python file
    • What is it called?
    • Setup the basic structure of the file
      • main() method and __main__ clause
  • Add some comments to flesh out the basic strategy
    • Take a first-pass at decomposing the problem

Step 2: Implement

  • Write functions that decompose the problem
  • Test your functions as you go
  • Identify any areas where the requirements are incomplete

We write the program and deliver it to the sales department.

It’s a hit.

Feature requests start coming in.

Feature Request: More than 1 fact

  • Customers want to multiple facts per invocation

Requirements

  • The app will accept a -n/--how-many flag that indicates how many facts to retrieve
  • The default will be 1

Feature Request: Display facts in a bulleted list

  • Instead of just printing the facts line after line, make it pretty

Requirements

  • Print "Did you know:" on the first line
  • Print each fact after that with an indentation of 2 spaces followed by a "- ", then the fact.

Feature Request: Let the user chose the bullet

  • Provide a command-line option that allows the user to specify what they want to use as the bullet in the fact list

Requirements

  • Use - as the default bullet
  • Add a command-line option named -b/--bullet that overrides the bullet used

Feature Request: Dog mode

  • When enabled, dog-mode replaces all mentions of “dog” with a dog emoji

Requirements

  • Replace all occurrences of dog and Dog with 🐶
  • Replace all occurrences of dogs and Dogs with 🐶🐶

Key Ideas

  • Decomposition!
  • Iteration between design and implementation
  • argparse for handling commandline arguments

Final code

During class we built this code step by step. Here is the code we ended up with: dog_facts_solution.py