|
| 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 | +# TODO3: instead of reading |
| 17 | +# the file in everytime |
| 18 | +# is there a way to read it in once |
| 19 | +# and if it was already read, return |
| 20 | +# the existing file data? |
| 21 | +try: |
| 22 | +f=open(self.file_path) |
| 23 | +my_file=csv.reader(f) |
| 24 | +returnmy_file |
| 25 | +exceptIOError: |
| 26 | +print("File not found\n") |
| 27 | +returnNone |
| 28 | + |
| 29 | + |
| 30 | +classIrisStats: |
| 31 | + |
| 32 | +def__init__(self,file_path): |
| 33 | +self.sepal_length_column=0 |
| 34 | +self.petal_length_column=2 |
| 35 | +self.iris_data=IrisReader(file_path).open() |
| 36 | + |
| 37 | +defget_avg_petal_length(self): |
| 38 | +# TODO1: write this function |
| 39 | +pass |
| 40 | + |
| 41 | +defget_avg_sepal_length_per_class(self): |
| 42 | +# TODO2: write this function |
| 43 | +pass |
| 44 | + |
| 45 | +importsys |
| 46 | +iris_stats=IrisStats(sys.argv[1]) |
| 47 | +print(iris_stats.get_avg_petal_length()) |