Skip to content

Instantly share code, notes, and snippets.

@Jawabiscuit
Created March 19, 2021 16:53
Show Gist options
  • Save Jawabiscuit/3a58a748e7474453036d6ee44957d547 to your computer and use it in GitHub Desktop.
Save Jawabiscuit/3a58a748e7474453036d6ee44957d547 to your computer and use it in GitHub Desktop.
Code question answers
"""
This is my really awesome module!
Examples
--------
>>> flatten_list([[1, 2, [3, 4], 5]])
... [1, 2, 3, 4, 5]
>>> tt = TempTracker()
>>> tt.insert(1)
>>> tt.insert(2)
>>> tt.insert(3)
>>> tt.get_mean()
... 2
"""
import unittest
def flatten_list(iterable):
"""
Completely flatten a list of nested lists of any depth
:param list iterable: Preferably a list which could contain additional lists
:returns: Lazy iterator that loads successive values of iterable
"""
for x in iterable:
try:
iter(x)
except TypeError:
yield x
else:
yield from flatten_list(x)
class TempTracker(object):
"""A container for storing and analyzing temperature data"""
def __init__(self):
self._record_data = []
def insert(self, value):
"""
Record a temp
.. note:: Float values will be converted to integer before recording
:param int value: Temp value to record
"""
if isinstance(value, (float, int)):
self._record_data.append(int(value))
def get_max(self):
"""
The highest temp recorded
:rtype: int
"""
return max(self._record_data)
def get_min(self):
"""
The lowest temp recorded
:rtype: int
"""
return min(self._record_data)
def get_mean(self):
"""
The average temp recorded
:rtype: int
"""
return sum(self._record_data)/len(self._record_data)
"""
Tests
-----
.. note:: Tests are usually in a separate file, but including them here.
"""
class Test_FlattenList(unittest.TestCase):
def test_flatten_list(self):
nested_list = [[1, 2, [3, 4], 5]]
flattened_list = list(flatten_list(nested_list))
self.assertTrue(
flattened_list == [1, 2, 3, 4, 5],
"{!r} does not equal [1, 2, 3, 4, 5]".format(flattened_list))
class Test_TempTracker(unittest.TestCase):
def setUp(self):
self.tt = TempTracker()
def tearDown(self):
del(self.tt)
def test_insert(self):
self.tt.insert(1)
self.assertTrue(
self.tt._record_data == [1],
"The content of {!r} is not [1]".format(self.tt._record_data))
def test_get_max(self):
self.tt.insert(1)
self.tt.insert(2)
self.tt.insert(3)
self.assertTrue(
self.tt.get_max() == 3,
"The max of {!r} is not 3".format(self.tt._record_data))
self.tt.insert(6)
self.tt.insert(5)
self.tt.insert(4)
self.assertTrue(
self.tt.get_max() == 6,
"The max of {!r} is not 6".format(self.tt._record_data))
def test_get_min(self):
self.tt.insert(6)
self.tt.insert(5)
self.tt.insert(4)
self.assertTrue(
self.tt.get_min() == 4,
"The min of {!r} is not 4".format(self.tt._record_data))
self.tt.insert(1)
self.tt.insert(2)
self.tt.insert(3)
self.assertTrue(
self.tt.get_min() == 1,
"The min of {!r} is not 1".format(self.tt._record_data))
def test_get_mean(self):
self.tt.insert(1)
self.tt.insert(2)
self.tt.insert(3)
self.assertTrue(
self.tt.get_mean() == 2,
"Value: {!r} is not 2".format(self.tt._record_data))
def test_get_mean_complex(self):
self.tt.insert(21.3)
self.tt.insert(38.4)
self.tt.insert(12.7)
self.tt.insert(41.6)
self.assertTrue(
self.tt.get_mean() == 28,
"Value: {!r} is not 28".format(self.tt._record_data))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment