1 of 25

Section 9

Classes & csv.DictReader

2 of 25

Logistics

Section - today, classes and csv.DictReader: next week, finals review

DEADLINES

Homework 5 Part 2 due Friday March 3rd

Homework 6 due Friday March 10th

No opportunity to resubmit Homework 6 after the deadline

Start early and go to office hours!

FINAL EXAM two weeks from now Thursday March 16th

3 of 25

Classes

4 of 25

What is a class?

A class is some data equipped with operations - data that does something!

Classes encapsulate and abstract data and logic that you can use in your programs.

We sometimes call this data and logic state and behavior respectively.

Example

We want to store a phone number so we can keep track of them in our program.

5 of 25

A class contains...

Variables to hold the necessary data (fields)

Data (State)

A phone number stores

  • Area code
  • Exchange
  • Number

Other possibilities?

Maybe store a country code

6 of 25

Aside: Creating an ‘instance’ of a class

When we want to start using a class, we typically want to set some of its variables to their correct values

This is done through a special function (a method) defined inside the class called __init__

When we create a phone number, we specify its area code, exchange, and number.

7 of 25

8 of 25

Conceptual Connection - State

A class is state and behavior. State is kind of like a set of variables with values - key-value pairs!

We could use a dictionary to represent state:

num1_state = {“area_code”: 916, “exchange”: 272, “number”: 8010}

9 of 25

A class contains...

Methods to create, query, and modify an instance of a class

Logic (Behavior)

A phone number can be:

  • Created
  • Called
  • Printed

10 of 25

Aside: The ‘self’ parameter

Every method has the form

def method_name(self, arg1, …, argn):

# do something

No magic here - self is just the instance of the class being modified

11 of 25

12 of 25

Conceptual Connection - Behavior

A class is state and behavior. Behavior is just some functions that do something based on the current state.

If we used a dictionary to represent state, then we can think about the self parameter as just being such a dictionary

num1_state = {“area_code”: 916, “exchange”: 272, “number”: 8010}

def print_number(num_state):

print(“(“ + str(num_state[“area_code”]) + “) “ + str(num_state[“exchange”]) \

+ “-” + str(num_state[“number”])

13 of 25

CSV Dictreader

14 of 25

CSV Dictreader

Say you have the following csv called people.csv with the following contents:

id,name,age,height,weight

1,Alice,20,62,120.6

2,Freddie,21,74,190.6

3,Bob,17,68,120.0

15 of 25

CSV Dictreader

Open the file by calling open and then csv.DictReader.

id,name,age,height,weight

1,Alice,20,62,120.6

2,Freddie,21,74,190.6

3,Bob,17,68,120.0

people.csv

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

16 of 25

CSV Dictreader

When you iterate over input_file, you will see each row as a dictionary:

id,name,age,height,weight

1,Alice,20,62,120.6

2,Freddie,21,74,190.6

3,Bob,17,68,120.0

people.csv

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

for row in input_file:

print(row)

f.close()

17 of 25

CSV Dictreader

When you iterate over input_file, you will see each row as a dictionary:

id,name,age,height,weight

1,Alice,20,62,120.6

2,Freddie,21,74,190.6

3,Bob,17,68,120.0

people.csv

Output:

{'age': '20', 'height': '62', 'id': '1', 'weight': '120.6', 'name': 'Alice'}

{'age': '21', 'height': '74', 'id': '2', 'weight': '190.6', 'name': 'Freddie'}

{'age': '17', 'height': '68', 'id': '3', 'weight': '120.0', 'name': 'Bob'}

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

for row in input_file:

print(row)

f.close()

18 of 25

CSV Dictreader

When you iterate over input_file, you will see each row as a dictionary:

id,name,age,height,weight

1,Alice,20,62,120.6

2,Freddie,21,74,190.6

3,Bob,17,68,120.0

people.csv

Output:

{'age': '20', 'height': '62', 'id': '1', 'weight': '120.6', 'name': 'Alice'}

{'age': '21', 'height': '74', 'id': '2', 'weight': '190.6', 'name': 'Freddie'}

{'age': '17', 'height': '68', 'id': '3', 'weight': '120.0', 'name': 'Bob'}

Notice the values in these dictionaries are always strings!

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

for row in input_file:

print(row)

f.close()

19 of 25

Section Problems

20 of 25

Problem 1a

What would the following client code print?

flower_shop = Shop(“Swansons”)

flower_shop.add_item(“Daisy”)

flower_shop.print_inventory()

See it on PythonTutor

21 of 25

Problem 1b

What code would you write to make a new shop called Arcane Comics that has 5 comics in it, and then print out its inventory?

22 of 25

Problem 1c

Adding one item at a time is extremely tedious. Write a method of the class Shop (just a function that takes a Shop as its first parameter) called add_items that given an item and an amount, adds that amount of the given item to the store's inventory.

23 of 25

Problem 2

24 of 25

Problem 2a

Create a function called extract_column that will take in a filename and a column name, and return a list with all the data from that column as floats. Hint: Use CSV DictReader

25 of 25

Problem 2b

For each repetition of the experiment (GFP Rep 1, GFP Rep 2, and GFP Rep 3) plot it on a graph. The x axis should be the hours, the y axis the concentration of GFP.�

Each repetition should be a different line on the graph. Don’t forget to give each line a label. You should also give a title to the plot. You should save the plot as well.