1 of 30

CSE 160 Section 7

CSV & Objects!

CSE 160: CSV & Objects

2 of 30

Logistics

  • Quest 3 in lecture on Friday, May 15th @ 1:30 PM
  • Clustering HW due Friday, May 15th @ 11:59 PM
    • Submitting on Gradescope
    • Wait for autograder!
  • Section Assignment 7 due Friday, May 15th @ 11:59 PM
  • Programming Practice 7 due Tuesday, May 19th @ 11:59 PM
  • Resubmission Cycle 5/11 – 5/15
    • Don’t forget to fill out the resubmission form!

CSE 160: CSV & Objects

3 of 30

Lecture Review: CSV Dictreader

CSE 160: CSV & Objects

4 of 30

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

CSE 160: CSV & Objects

5 of 30

CSV Dictreader

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

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

f.close()

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

CSE 160: CSV & Objects

6 of 30

CSV Dictreader

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

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

for row in input_file:

print(row)

f.close()

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

CSE 160: CSV & Objects

7 of 30

CSV Dictreader

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

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

for row in input_file:

print(row)

f.close()

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:

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

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

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

CSE 160: CSV & Objects

8 of 30

CSV Dictreader

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

import csv

f = open("people.csv")

input_file = csv.DictReader(f)

for row in input_file:

print(row)

f.close()

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:

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

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

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

Note how the values in these dictionaries are always strings!

CSE 160: CSV & Objects

9 of 30

Classes

CSE 160: CSV & Objects

10 of 30

What is a Class?

A class encapsulates and abstracts data and logic that you can use in your programs

We want to make a program that can keep track of phone numbers.

CSE 160: CSV & Objects

11 of 30

Classes vs. Objects

A class is a blueprint- it defines what data and behavior an object will have

We can create an object instance from a class

Class

Multiple object instances

CSE 160: CSV & Objects

12 of 30

A Class Contains…

Variables / fields to hold the necessary data

A phone number should probably store:

  • Area code
  • Exchange
  • Number

CSE 160: CSV & Objects

13 of 30

A Class Also Contains…

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

(Methods are basically functions inside classes)

A phone number should probably be able to:

  • Be created
  • Be called
  • Print its number

CSE 160: CSV & Objects

14 of 30

Creating an Instance of A Class

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

This is done through a special method called __init__

When we create a phone number, we want to specify it’s area code, exchange, and number.

CSE 160: CSV & Objects

15 of 30

CSE 160: CSV & Objects

16 of 30

CSE 160: CSV & Objects

17 of 30

Section Handout Problems

CSE 160: CSV & Objects

18 of 30

Problem 1

CSE 160: CSV & Objects

19 of 30

Problem 1a

What would the following client code print?

flower_shop = Shop(“Swansons”)

flower_shop.add_item(“Daisy”)

flower_shop.print_inventory()

CSE 160: CSV & Objects

20 of 30

Problem 1a

What would the following client code print?

flower_shop = Shop(“Swansons”)

flower_shop.add_item(“Daisy”)

flower_shop.print_inventory()

Output:

Daisy 1

CSE 160: CSV & Objects

21 of 30

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 it's inventory?

CSE 160: CSV & Objects

22 of 30

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 it's inventory?

comic_shop = Shop("Arcane Comics")

for i in range(5):

comic_shop.add_item("Comic")

comic_shop.print_inventory()

CSE 160: CSV & Objects

23 of 30

Problem 1c

Adding one item at a time is extremely tedious. Write a function for the class Shop called add_items that given an item and an amount, adds that amount to the store's inventory.

CSE 160: CSV & Objects

24 of 30

Problem 1c

Adding one item at a time is extremely tedious. Write a function for the class Shop called add_items that given an item and an amount, adds that amount to the store's inventory.

def add_items(self, item, amount):

if item not in self.inventory:

self.inventory[item] = 0

self.inventory[item] += amount

CSE 160: CSV & Objects

25 of 30

Problem 2

Given the doc-strings and example output, fill in the code for each method in the StreamingService class.

CSE 160: CSV & Objects

26 of 30

Problem 2

Given the doc-strings and example output, fill in the code for each method in the StreamingService class.

Python Tutor

CSE 160: CSV & Objects

27 of 30

Problem 3

Say we have the csv file “crispra_data.csv” that came from a lab that does research on CRISPR, a gene editing/regulation tool. In this data, an experiment was performed where scientists were testing some conditions and measuring the concentration of green fluorescent protein (GFP) at various time steps.

Write a function extract_column that takes two parameters, filename and column_name. The function should return the data in the specified column from the file specified by filename. Returns the data in the column as a list.

def extract_column(filename, column_name):

"""

Returns the data in the specified column_name from the file specified by filename. Returns the data in the column as a list.

"""

# write your code here

CSE 160: CSV & Objects

28 of 30

Problem 3

def extract_column(filename, column_name):

"""

Returns the data in the specified column_name from the file specified by

filename. Returns the data in the column as a list.

"""

f = open(filename)

input_file = csv.DictReader(f)

result = []

for row in input_file:

result.append(float(row[column_name]))

f.close()

return result

CSE 160: CSV & Objects

29 of 30

Section Code: xx

CSE 160: CSV & Objects

30 of 30

Looking Ahead: �Data Processing

Be sure to remember the section code!

CSE 160: CSV & Objects