From: Matt Davis Date: Sun, 21 Oct 2012 22:31:49 +0000 (-0700) Subject: Adding all of the software engineering example files. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=refs%2Fheads%2Ftest-davis-2012-10-ucb;p=swc-testing-nose.git Adding all of the software engineering example files. --- diff --git a/4-SoftwareEngineering/animals.py b/4-SoftwareEngineering/animals.py index edebb44..f967ce2 100644 --- a/4-SoftwareEngineering/animals.py +++ b/4-SoftwareEngineering/animals.py @@ -34,3 +34,40 @@ def mean(l): return None return float(sum(l)) / len(l) + + +def filter_animals(date, time, animals, count, species): + """ + Return only the data that apply to species. + + """ + filtered_date = [] + filtered_time = [] + filtered_animals = [] + filtered_count = [] + + for i, a in enumerate(animals): + if a == species: + filtered_date.append(date[i]) + filtered_time.append(time[i]) + filtered_animals.append(animals[i]) + filtered_count.append(count[i]) + + return filtered_date, filtered_time, filtered_animals, filtered_count + + +def mean_animals_sighted(filename, species): + """ + Return the mean number of animals seen per sighting + in filename. + + """ + date, time, animals, count = read_animals(filename) + date, time, animals, count = \ + filter_animals(date, time, animals, count, species) + + if len(count) == 0: + raise ValueError('That animal is not in the file!') + + mean_sighted = mean(count) + return mean_sighted diff --git a/4-SoftwareEngineering/argv b/4-SoftwareEngineering/argv new file mode 100755 index 0000000..e590b4b --- /dev/null +++ b/4-SoftwareEngineering/argv @@ -0,0 +1,4 @@ +#!/usr/bin/env python +import sys + +print sys.argv diff --git a/4-SoftwareEngineering/meananimals b/4-SoftwareEngineering/meananimals new file mode 100755 index 0000000..ca7b013 --- /dev/null +++ b/4-SoftwareEngineering/meananimals @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import sys + +import animals + +if len(sys.argv) != 3: + print 'Usage: meananimals filename species' + sys.exit() + +filename = sys.argv[1] +species = sys.argv[2] + +sighted = animals.mean_animals_sighted(filename, species) +print sighted diff --git a/4-SoftwareEngineering/test_animals.py b/4-SoftwareEngineering/test_animals.py index 0f4a539..cd59fbe 100644 --- a/4-SoftwareEngineering/test_animals.py +++ b/4-SoftwareEngineering/test_animals.py @@ -36,3 +36,34 @@ def test_mean3(): def test_mean4(): l = [] assert animals.mean(l) == None + + +def test_filter_animals(): + date, time, animal, count = \ + animals.read_animals('animals.txt') + + species = 'Elk' + date, time, animal, count = \ + animals.filter_animals(date, time, animal, count, species) + + assert count == [25, 26] + assert animal == ['Elk', 'Elk'] + + +def test_mean_animals_sighted1(): + mean_sighted = animals.mean_animals_sighted('animals.txt', + 'Wolverine') + assert mean_sighted == 31 + + +def test_mean_animals_sighted2(): + mean_sighted = animals.mean_animals_sighted('animals.txt', + 'Elk') + assert mean_sighted == 25.5 + + +import nose +@nose.tools.raises(ValueError) +def test_mean_animals_sighted3(): + mean_sighted = animals.mean_animals_sighted('animals.txt', + 'Pangolin')