Skip to content

Commit 54b7785

Browse files
committed
feature: Add the heat index display
1 parent 50033b0 commit 54b7785

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

chapter02_observer/weather.py

+27
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,39 @@ def display(self):
112112
print("Watch out for cooler, rainy weather")
113113

114114

115+
class HeatIndexDisplay(DisplayElement, Observer):
116+
def __init__(self, weather_data):
117+
self.heat_index = 0.
118+
self._weather_data = weather_data
119+
weather_data.register_observer(self)
120+
121+
def update(self, temp, humidity, pressure):
122+
self.heat_index = self._compute_heat_index(temp, humidity)
123+
self.display()
124+
125+
def display(self):
126+
print(f"Heat index is {self.heat_index:.5f}")
127+
128+
@classmethod
129+
def _compute_heat_index(cls, t, rh):
130+
index = ((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh)
131+
+ (0.00941695 * (t * t)) + (0.00728898 * (rh * rh))
132+
+ (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
133+
(0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) +
134+
(0.0000291583 * (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
135+
(0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
136+
0.000000000843296 * (t * t * rh * rh * rh)) -
137+
(0.0000000000481975 * (t * t * t * rh * rh * rh)))
138+
return index
139+
140+
115141
def weather_station():
116142
weather_data = WeatherData()
117143

118144
current_display = CurrentConditionsDisplay(weather_data)
119145
statistics_display = StatisticsDisplay(weather_data)
120146
forecast_display = ForecastDisplay(weather_data)
147+
heat_index_display = HeatIndexDisplay(weather_data)
121148

122149
weather_data.set_measurements(80, 65, 30.4)
123150
weather_data.set_measurements(82, 70, 29.2)

0 commit comments

Comments
 (0)