Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit472901e

Browse files
committed
2 parentsa10d67a +12a40ff commit472901e

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

‎Module8/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#Module 8
2+
Reminder:
3+
- Class is completely online next week.
4+
- Wednesday office hours are online next week.
5+
- Office hours for Sunday, March 12th moved to Monday, March 13th at 8pm
6+
27
##[Review Homework 7](https://github.com/summerela/intro_programming_python/blob/master/Module8/hw7_answer.py)
38
##[Install Jupyter Notebook](https://github.com/summerela/intro_programming_python/blob/master/Module8/1_install_jupyter.ipynb)
49
##[Working with Jupyter Notebook: Interactive Python Notebooks](https://github.com/summerela/intro_programming_python/blob/master/Module8/2_using_jupyter.ipynb)

‎Module9/Lab1.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Module 9 Lab 1
2+
3+
Classes are just different ways to organize the variables and functions you've already been writing.
4+
Think about them as containers for variables that the internal class functions should only be able to change
5+
6+
##Instructions:
7+
8+
0. Let's walk through`lab1.py` together and play with these two classes. We'll highlight some of things below that Summer was talking about in her lecture:
9+
10+
0. How to instantiate a class
11+
12+
0.`__init__`. When it runs. What goes in it.
13+
14+
0.`self`. What is it. What does it refer to
15+
16+
0. How to call functions on an instance.
17+
18+
0. Special functions in a class that you can "override" or use:`__str__`,`__repr__`,`__dict__`

‎Module9/Lab2.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Module 9 Lab 2
2+
3+
We lover iris flowers in this class. Let's return to the[iris.csv](https://github.com/summerela/intro_programming_python/blob/master/Module6/iris.csv) dataset in this lab and try to write our simple statistics
4+
inside classes.
5+
6+
##Instructions:
7+
8+
0. Copy`lab2.py` to your workspace
9+
10+
0. Look for`TODO1` in the code. Can you finish writing this function without accessing any variables outside of the`IrisStats` class?
11+
12+
0. Look for`TODO2` in the code. Can you finish writing this function without accessing any variables outside of the`IrisStats` class?
13+
14+
0. The final TODO is a little harder to explain. The idea is that we don't want to read the csv for every statistical function we write because it's repetative. There should be multiple ways to refactor the code to read the file once and cache the`self.iris_data` read from the csv. This can be done in the`IrisReader` or`IrisStats` class. So you have options.

‎Module9/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
[Lab1](Lab1.md)
4+
5+
[Lab2](Lab2.md)

‎Module9/lab1.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
3+
classCounter:
4+
5+
def__init__(self,start_value=0):
6+
# counter always has to start at 0
7+
ifstart_value>0:
8+
self._count=start_value
9+
else:
10+
self._count=0
11+
12+
defincrement(self):
13+
self._count+=1
14+
returnself._count
15+
16+
defdecrement(self):
17+
# counter cannot go below 0
18+
ifself._count-1<0:
19+
self._count=0
20+
else:
21+
self._count-=1
22+
returnself._count
23+
24+
25+
classNamer:
26+
27+
def__init__(self,name,age):
28+
self._name=name
29+
self._age=age
30+
31+
def__str__(self):
32+
return"[ {} ]: name={} age={}".format(
33+
self.__class__,
34+
self._name,
35+
self._age,
36+
)
37+
38+
defsay_my_name(self):
39+
# NOTE: this calls the __str__ override method
40+
print(self)
41+
42+
defcountdown(self):
43+
age_counter=Counter(start_value=self._age)
44+
next_age=age_counter._count
45+
whilenext_age>0:
46+
print(next_age)
47+
next_age=age_counter.decrement()
48+
print("[ DONE ]")
49+
50+
defshow_me_my_data_guts(self):
51+
#
52+
# A Python class is just a fancy wrapper
53+
# around a dictionary. It has special ways to access
54+
# the dictionary internally from the class. So we can
55+
# do things such as this to get an internal view of the class
56+
#
57+
return"{}".format(self.__dict__)

‎Module9/lab2.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
importcsv
3+
4+
classIrisReader:
5+
"""
6+
A kinda-sorta rule of thumb is that if you see a Python class
7+
with a single function and an initializer
8+
then it probably shouldn't be a class at all.
9+
Ignore that in this case
10+
"""
11+
12+
def__init__(self,file_path):
13+
self.file_path=file_path
14+
15+
defopen(self):
16+
try:
17+
f=open(self.file_path)
18+
my_file_data=csv.reader(f)
19+
returnmy_file_data
20+
exceptIOError:
21+
print("File not found\n")
22+
returnNone
23+
24+
25+
classIrisStats:
26+
27+
def__init__(self,file_path):
28+
self.sepal_length_column=0
29+
self.petal_length_column=2
30+
self.iris_data=None# this is set in each stats function
31+
32+
defget_avg_petal_length(self):
33+
# TODO1: finish this function
34+
self.iris_data=IrisReader(file_path).open()
35+
ifself.iris_dataisnotNone:
36+
pass
37+
38+
defget_avg_sepal_length_per_class(self):
39+
# TODO2: finish this function
40+
self.iris_data=IrisReader(file_path).open()
41+
ifself.iris_dataisnotNone:
42+
pass
43+
44+
importsys
45+
iris_stats=IrisStats(sys.argv[1])
46+
print(iris_stats.get_avg_petal_length())

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp