Skip to content
Snippets Groups Projects
Commit e50344a0 authored by Franck Thollard's avatar Franck Thollard
Browse files

weather station: bug fixes plus API changes

parent bc4731a3
Branches
No related tags found
1 merge request!5Merging Testing
import unittest
#!/usr/bin/env python3
class WeatherStation(object):
""" A weather station that holds wind and temperature
......@@ -7,62 +7,38 @@ class WeatherStation(object):
wind and temperature must have the same length.
"""
def __init__(self, wind, temperature):
self.wind = list(wind)
def __init__(self, wind: list[int], temperature: list[float]):
self.wind = [abs(x) for x in wind]
self.temp = list(temperature)
if len(self.wind) != len(self.temp):
raise ValueError(
"wind and temperature should have the same size"
)
def max_temp(self):
def max_temp(self, percieved=True) -> float:
""" returns the maximum temperature recorded in the station"""
if percieved:
return max(self.all_percieved_temp())
else:
return max(self.temp)
def arg_max_temp(self):
def arg_max_temp(self, percieved=True) -> int:
""" returns the index of (one of the) maximum temperature recorded in the station"""
return self.temp.index(self.max_temp())
paris = WeatherStation([10, 0, 20, 30, 20, 0], [1, 5, 1, -1, -1, 3])
# OR paris = WeatherStation(wind=[10, 0, 20, 30, 20, 0], temperature=[1, 5, 1, -1, -1, 3])
idx_max_temp = paris.arg_max_temp()
print(f"max temp is {paris.max_temp()}°C at index {paris.arg_max_temp()}")
print(f"wind speed at max temp = {paris.wind[idx_max_temp]} km/h")
# testing
class TestWeatherStation(unittest.TestCase):
"""Test the weather station """
def setUp(self):
"""Generates a well structured station (paris)"""
self.paris = WeatherStation(
[10, 0, 20, 30, 20, 0],
[1, 5, 1, -1, -1, 3],
)
def test_building_with_good_input_arrays(self):
""" test that things goes smoothly if the input are correct"""
self.assertEqual(0, self.paris.wind[1])
self.assertEqual(5, self.paris.temp[1])
def test_building_with_input_iterables(self):
""" test that things goes smoothly if the input are correct"""
r_station = WeatherStation(range(10), range(10))
self.assertEqual(4, r_station.wind[4])
self.assertEqual(5, r_station.temp[5])
def test_building_with_bad_arrays(self):
""" test that an exception is raised with incorrect inputs"""
with self.assertRaises(ValueError):
bad_station = WeatherStation([10, 0, 20, 30, 20, 0], [1, 5, 1])
def test_max_temp(self):
""" test test_max_temp function"""
self.assertEqual(5, self.paris.max_temp())
def test_arg_max_temp(self):
""" test arg_max_temp function"""
self.assertEqual(1, self.paris.arg_max_temp())
if percieved:
temp = self.all_percieved_temp()
return temp.index(self.max_temp(temp))
else:
return self.temp.index(self.max_temp(percieved=False))
def percieve_temp(self, temp: float, wind: int) -> float:
"""returns the percieved temp given a temp and a wind speed """
if wind == 0:
return temp
else:
return temp - wind/10
def percieved_temp_at(self, index) -> float:
return self.percieve_temp(self.temp[index], self.wind[index])
def all_percieved_temp(self) -> list[float]:
return [self.percieved_temp_at(index) for index in range(len(self.wind))]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment