+++ /dev/null
-#!/usr/bin/env python
-
-# The line above tells the shell to run this script with Python.
-import sys
-
-from meananimals import mean_animals
-
-# sys.argv contains the arguments the user entered at the command line when
-# calling this script. See more at http://docs.python.org/2/library/sys.html.
-# Another great way of putting together command line interfaces for scripts
-# is the argparse module: http://docs.python.org/2/library/argparse.html
-
-# Try running this script by typing "./mean_sighted.py big_animals.txt Elk"
-# at the command line.
-
-if len(sys.argv) != 3:
- print 'Usage: mean_sighted.py <filename> <species>'
-else:
- filename = sys.argv[1]
- species = sys.argv[2]
- print mean_animals(filename, species)
+++ /dev/null
-def read_file(ifile):
- open_file = open(ifile, 'r')
-
- time = []
- date = []
- animal = []
- count = []
-
- for iline in open_file:
- s = iline.split()
- date.append(s[0])
- time.append(s[1])
- animal.append(s[2])
- count.append(int(s[3]))
-
- open_file.close()
-
- return date, time, animal, count
-
-def calc_mean(ilist):
- '''
- returns the mean of input list
- '''
- if len(ilist) == 0:
- raise ValueError('Input is empty.')
-
- total = 0.0
- for num in ilist:
- total = total + num
- return total/float(len(ilist))
-
-def filter_animals(species, date, time, animal, count):
- """
- Given a particular species, filter out the data for just that species.
-
- Returns four lists: date, time, animal, count.
- """
- fdate = []
- ftime = []
- fanimal = []
- fcount = []
-
- for d, t, a, c in zip(date, time, animal, count):
- if a == species:
- fdate.append(d)
- ftime.append(t)
- fanimal.append(a)
- fcount.append(c)
-
- return fdate, ftime, fanimal, fcount
-
-def mean_animals(filename, species):
- d, t, a, c = read_file(filename)
- d, t, a, c = filter_animals(species, d, t, a, c)
- return calc_mean(c)
\ No newline at end of file
+++ /dev/null
-import nose.tools as nt
-from meananimals import read_file, calc_mean, filter_animals, mean_animals
-
-def test_read_animals():
- date, time, animal, count = read_file('animals.txt')
- ref_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']
- ref_time = ['21:06', '14:12', '10:24', '20:08', '18:46']
- ref_animal = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox']
- ref_count = [36, 25, 26, 31, 20]
-
- assert date == ref_date, 'Dates do not match!'
- assert time == ref_time, 'Times do not match!'
- assert animal == ref_animal, 'Animals do not match!'
- assert count == ref_count, 'Counts do not match!'
-
-def test_mean1():
- m = calc_mean([1, 2, 3])
- assert m == 2
-
-def test_mean2():
- m = calc_mean([1])
- assert m == 1
-
-def test_mean3():
- m = calc_mean([3.4, 3.5, 3.6])
- assert m == 3.5
-
-@nt.raises(ValueError)
-def test_mean4():
- m = calc_mean([])
-
-def test_filter_animals1():
- date, time, animal, count = read_file('animals.txt')
- fdate, ftime, fanimal, fcount = filter_animals('Elk', date, time, animal, count)
-
- assert fdate == ['2011-04-23', '2011-04-23']
- assert ftime == ['14:12', '10:24']
- assert fanimal == ['Elk', 'Elk']
- assert fcount == [25, 26]
-
-def test_mean_animals1():
- m = mean_animals('animals.txt', 'Elk')
- assert m == 25.5
-
-def test_mean_animals2():
- m = mean_animals('animals.txt', 'Grizzly')
- assert m == 36
\ No newline at end of file