Pattern catalog

Patterns are common ways of doing things, like recipes. Part of learning a skill is learning the patterns that help you be more productive.

Here are patterns on this website. When you start a new project, you can use the patterns to remind yourself of useful chunks of code.

Name Tags Summary Where referenced
Accumulate output in a variable Output

Rather than have a bunch of print()s, assemble what you want to output into a variable, and print() that.

Service level
Break down complex conditions Program design

Businesses often have complex rules. That can lead to code that is complicated, and hard to read. Use boolean variables for individual conditions. Your code will be easier to think about.

Count-controlled while loop Loops

A while loop that runs a certain number of times.

Counted whiles
Data machine: Cleaner Data cleaning, Data sets, Data machines, Validation

Write a function that takes a data set as a param. Some of the records in the data set might have errors. The function returns a data set with no errors.

Data machines, Even cleaner
Data machine: Computation Data sets, Data machines, Stats

Use existing computation functions where you can, like statistics.mean. If you can't, like when you identify records with lowest/highest values in a data set, write your own loopy function.

Data machines, Stats
Data machine: Computed field Computed fields, Data machines, Data sets

Write a function that adds a new field to records in a data set (a list of dictionaries), based on existing fields.

Data machines
Data machine: Field extractor Data sets, Data machines

Write a function that extracts a list with the values of one field from a data set.

Data machines, Stats
Data machine: Reader Data sets, Data machines, CSV

A function to read a comma-separated values (CSV) file into a data set.

Data machines, Reading data sets
Data machine: Records subset Data sets, Data machines

Write a function taking a data set as a param, and returning another data set with a subset of the original records, based on criteria you choose.

Data machines, Record subsets
Flag Conditions

Use a variable as a flag. Set the flag if any of a number of things happens. After, check the flag to see if any of those things happened.

Keep asking: numbers