Adding all of the software engineering example files. test-davis-2012-10-ucb
authorMatt Davis <jiffyclub.programatic@gmail.com>
Sun, 21 Oct 2012 22:31:49 +0000 (15:31 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 9 Nov 2013 17:15:46 +0000 (09:15 -0800)
4-SoftwareEngineering/animals.py
4-SoftwareEngineering/argv [new file with mode: 0755]
4-SoftwareEngineering/meananimals [new file with mode: 0755]
4-SoftwareEngineering/test_animals.py

index edebb44e5ed35b5665f569d8d8940c9df4eec192..f967ce2c568648883bf69e974cec49c8b17afae0 100644 (file)
@@ -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 (executable)
index 0000000..e590b4b
--- /dev/null
@@ -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 (executable)
index 0000000..ca7b013
--- /dev/null
@@ -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
index 0f4a53984645bbdf513694750b5d9898632af8a5..cd59fbe16f90033aa3885141536942255545bca0 100644 (file)
@@ -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')