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?
- Dog-fact server:
- Facts endpoint:
- Customer wants 1 fact per call
- The fact should be displayed on the terminal
- The script should be named
- The app should follow the POSIX convention for options and arguments
Positional arguments are simply listed after the command.
my_script.py is a positional argument to
conda install jupyter
install is a subcommand to
jupyter is a positional arguments to the
conda install command.
Named arguments are key-value pairs. They key has a
-- in front of it.
conda create --name cs110 pandas jupyter
--name cs110 is a named argument. The name is
name and the value is
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
conda command supports
-n as shorthand for
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
-y is a flag that indicates that pip should perform the installation
without prompting the user to confirm.
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
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
- 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
- The app will accept a
--how-manyflag 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
"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
-as the default bullet
- Add a command-line option named
--bulletthat overrides the bullet used
Feature Request: Dog mode
- When enabled, dog-mode replaces all mentions of “dog” with a dog emoji
- Replace all occurrences of
- Replace all occurrences of
- Iteration between design and implementation
argparsefor handling commandline arguments
During class we built this code step by step. Here is the code we ended up with: dog_facts_solution.py