Last active
February 14, 2018 20:59
-
-
Save voodoonofx/254fde198ce79987c5285f1b2815b5c5 to your computer and use it in GitHub Desktop.
TempTracker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
class TempTracker(object): | |
""" | |
A simple class to track recorded temperatures, with some methods to get useful measurements. | |
""" | |
_records = [] | |
def __init__(self, records=[]): | |
""" | |
A simple constructor. You may initialize the class with a pre-defined list of :obj:`int` objects. | |
Args: | |
records (:obj:`list`, optional): | |
""" | |
self._records = records | |
def insert(self, record): | |
""" | |
Args: | |
record (int_or_float): A numeric value to store for further analysis. | |
""" | |
self._records.append(record) | |
def get_max(self): | |
""" | |
Returns: | |
int_or_float: The maximum recorded temperature value. | |
""" | |
return max(self._records) | |
def get_min(self): | |
""" | |
Returns: | |
int_or_float: The minimum recorded temperature value. | |
""" | |
return min(self._records) | |
def get_mean(self): | |
""" | |
Note: | |
If using python 3.4+, this could have been implemented with the `statistics` module. | |
Returns: | |
The arithmatic mean (sum of data divided by number of data points) in the recorded | |
temperatures. If no values have been recorded, returns 1. | |
""" | |
return (sum(self._records) / len(self._records) if any(self._records) else 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from temptracker import TempTracker | |
class UninitializedTestCase(unittest.TestCase): | |
def setUp(self): | |
""" Testing uninitialized class with 3 default values """ | |
self.tt = TempTracker() # uninitialized test | |
def testInsert(self): | |
""" Test case: 3 integers. Should have 3 items in the internal attribute _records """ | |
self.tt.insert(1) | |
self.tt.insert(2) | |
self.tt.insert(3) | |
self.tt.insert(4) | |
self.tt.insert(5) | |
assert len(self.tt._records) == 5, 'class attribute _records does not contain the expected 5 entries' | |
def testMax(self): | |
""" Test case: max value: Should return the highest numeric value given """ | |
assert self.tt.get_max() == 5, 'max value was {0} instead of expected 5'.format(self.tt.get_max()) | |
def testMin(self): | |
""" Test case: min value. Should return the lowest numeric value """ | |
assert self.tt.get_min() == 1, 'min value was {0} instead of expected 1'.format(self.tt.get_min()) | |
def testMean(self): | |
""" Test case: mean (arithmetic average) value. Should return an int or float """ | |
assert self.tt.get_mean() == 3.0, 'Mean value was {0} instead of expected 3.0'.format(self.tt.get_mean()) | |
class InitializedTestCase(unittest.TestCase): | |
def setUp(self): | |
""" Testing initialized class with 5 values """ | |
self.tt = TempTracker([1, 2, 3, 4, 5]) # initialized test | |
def testInsert(self): | |
""" Test case: 2 integers. Should have 6 items in the internal attribute _records """ | |
self.tt.insert(6) | |
assert len(self.tt._records) == 6, 'class attribute _records does not contain the expected 6 entries' | |
self.tt._records.pop() # Undo this test's changes. | |
def testMax(self): | |
""" Test case: max value: Should return the highest numeric value given """ | |
assert self.tt.get_max() == 5, 'max value was {0} instead of expected 5'.format(self.tt.get_max()) | |
def testMin(self): | |
""" Test case: min value. Should return the lowest numeric value """ | |
assert self.tt.get_min() == 1, 'min value was {0} instead of expected 1'.format(self.tt.get_min()) | |
def testMean(self): | |
""" Test case: mean (arithmetic average) value. Should return an int or float """ | |
assert self.tt.get_mean() == 3.0, 'Mean value was {0} instead of expected 3.0'.format(self.tt.get_mean()) | |
if __name__ == "__main__": | |
unittest.main() # run all tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment