From caf4a464a9e3ef4f9569dc1ac51adcdd419d1d38 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Wed, 12 Jun 2013 18:31:12 -0700 Subject: [PATCH] metar: handle edge case for pressure parsing --- metar.py | 28 ++++++++++++++++------------ test/metar/KBCB_2.txt | 2 ++ 2 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 test/metar/KBCB_2.txt diff --git a/metar.py b/metar.py index de63c9e..d8617da 100644 --- a/metar.py +++ b/metar.py @@ -124,20 +124,24 @@ class Weather(object): return '?' def __repr__(self): + chunks = [self.cover] + chunks.append('{0}°C'.format(self.temperature)) + + if self.pressure: + chunks.append('{0} hPa'.format(self.pressure)) + if self.conditions: - ret = "{cover}, {temperature}°C, {pressure} hPa, {conditions}, "\ - "{windnote} {wind} m/s ({windsock}) - {station} {time}" - else: - ret = "{cover}, {temperature}°C, {pressure} hPa, "\ - "{windnote} {wind} m/s ({windsock}) - {station} {time}" + chunks.append(self.conditions) wind = self.wind_speed if self.wind_speed is not None else '?' + chunks.append('{note} {speed} m/s ({windsock})'.format( + note=self.describe_wind(), + speed=wind, + windsock=self.windsock())) - return ret.format(cover=self.cover, temperature=self.temperature, - pressure=self.pressure, conditions=self.conditions, - wind=wind, windnote=self.describe_wind(), - windsock=self.windsock(), station=self.station, - time=self.time.strftime("%H:%MZ")) + ret = ', '.join(chunks) + ' - {station} {time}' + return ret.format(station=self.station, + time=self.time.strftime("%H:%MZ")) def build_regex(key, classifier): @@ -264,10 +268,10 @@ def parse(data): # pressure pressure_re = re.compile(r"([QA])(\d+)") m = pressure_re.search(w.metar) - if m.group(1) == 'A': + if m and m.group(1) == 'A': # convert inHg to hPa w.pressure = round(float(m.group(2)) * 0.3386389) - else: + elif m: w.pressure = int(m.group(2)) return w diff --git a/test/metar/KBCB_2.txt b/test/metar/KBCB_2.txt new file mode 100644 index 0000000..a90d9ab --- /dev/null +++ b/test/metar/KBCB_2.txt @@ -0,0 +1,2 @@ +2013/06/13 01:15 +KBCB 130115Z AUTO 00000KT 10SM CLR 23/22 RMK AO2