import colors class WeatherDisplay: def __init__(self, characterDisplay, topRow): self.characterDisplay = characterDisplay self.topRow = topRow self.twoDigitStartColumn = 5 self.oneDigitStartColumn = 7 self.digitWidth = 4 self.currentTemperature = '' self.color = colors.ORANGE self.weatherFile = 'weather.txt' def showWeather(self): temperature = self.getTemperature() if temperature != self.currentTemperature: self.currentTemperature = temperature self.characterDisplay.clearRow(self.topRow) if self.isTemperatureValid(temperature): self.showTemperature(list(temperature)) def getTemperature(self): try: with open(self.weatherFile) as file: return file.readline().strip() except Exception as e: print(e, flush=True) return 'error' def isTemperatureValid(self, temperature): return not (temperature == 'error' or temperature == 'init' or temperature == 'halted') def showTemperature(self, temperature): start = self.getStartColumn(temperature) for c in temperature: if c == '-': self.characterDisplay.displayNegative( x=start-3, y=self.topRow, color=self.color ) else: self.characterDisplay.displayDigit( x=start, y=self.topRow, digit=c, color=self.color ) start = start + self.digitWidth self.characterDisplay.displayDegree( x=start, y=self.topRow, color=self.color ) def getStartColumn(self, temperature): return self.oneDigitStartColumn if (self.isOneDigit(temperature)) else self.twoDigitStartColumn def isOneDigit(self, temperature): return (len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1