From 42c94232ba254559862c2dde4d21fa3e2ce9304e Mon Sep 17 00:00:00 2001 From: andrewgryan Date: Mon, 11 Nov 2024 21:49:06 +0000 Subject: [PATCH] format code --- pyproject.toml | 1 + src/detaf/lib.py | 15 ++-- src/detaf/wx.py | 10 ++- tests/test_interface.py | 157 ++++++++++++++++++++++----------------- tests/test_invariants.py | 18 +++-- 5 files changed, 116 insertions(+), 85 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c43b3af..edc1480 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ distribution = true [tool.pdm.scripts] test = "pytest" +fmt = "ruff format" [tool.pdm.dev-dependencies] lint = [ diff --git a/src/detaf/lib.py b/src/detaf/lib.py index e0767f4..d028ac5 100644 --- a/src/detaf/lib.py +++ b/src/detaf/lib.py @@ -72,9 +72,7 @@ class WeatherCondition: phenomena: list[Phenomenon] = field(default_factory=list) def taf_encode(self): - parts = [ - encode_period(self.period) - ] + parts = [encode_period(self.period)] if self.probability: parts.insert(0, f"PROB{self.probability:02}") if self.change: @@ -123,7 +121,7 @@ def parse(bulletin: str) -> TAF: else: print(f"unrecognised token: {words[cursor]}") cursor += 1 # Skip: bad token - + return TAF(version, icao_identifier, issue_time, conditions) @@ -133,7 +131,7 @@ def parse_format(tokens, cursor=0): return TAF, cursor + 1 else: raise UnknownFormat(token) - + def parse_version(tokens, cursor=0) -> (Version, int): token = peek(tokens, cursor) @@ -165,7 +163,9 @@ def parse_condition(tokens, cursor=0): phenomena.append(phenomenon) else: break - return WeatherCondition(period, probability, change, phenomena=phenomena), cursor + return WeatherCondition( + period, probability, change, phenomena=phenomena + ), cursor else: return None, cursor @@ -182,7 +182,6 @@ def parse_issue_time(tokens, cursor=0): return issue(day, hour, minute), cursor + 1 - def parse_period(tokens, cursor=0): token = peek(tokens, cursor) if not token: @@ -225,7 +224,7 @@ def parse_phenomenon(tokens, cursor=0): return phenomenon, cursor return None, cursor - + def parse_visibility(tokens, cursor=0): pattern = re.compile(r"[0-9]{4}") token = peek(tokens, cursor) diff --git a/src/detaf/wx.py b/src/detaf/wx.py index 76c0fd3..b7d196c 100644 --- a/src/detaf/wx.py +++ b/src/detaf/wx.py @@ -131,8 +131,16 @@ def parse(token: str) -> Wx | None: break if (precipitation is not None) or (obscuration is not None) or (other is not None): - return Wx(proximity=proximity, intensity=intensity, descriptor=descriptor, precipitation=precipitation, obscuration=obscuration, other=other) + return Wx( + proximity=proximity, + intensity=intensity, + descriptor=descriptor, + precipitation=precipitation, + obscuration=obscuration, + other=other, + ) else: return None + decode = parse diff --git a/tests/test_interface.py b/tests/test_interface.py index 1f11946..0db3c02 100644 --- a/tests/test_interface.py +++ b/tests/test_interface.py @@ -20,26 +20,21 @@ def test_integration(): phenomena=[ detaf.Wind(140, 10), detaf.Visibility(9999), - detaf.Cloud("BKN", 1500) - ] + detaf.Cloud("BKN", 1500), + ], ), detaf.WeatherCondition( period=((8, 12), (9, 6)), change=detaf.Change.TEMPO, - phenomena=[ - detaf.Visibility(6000), - detaf.Cloud("BKN", 800) - ] + phenomena=[detaf.Visibility(6000), detaf.Cloud("BKN", 800)], ), detaf.WeatherCondition( period=((9, 6), (9, 12)), probability=30, change=detaf.Change.TEMPO, - phenomena=[ - detaf.Cloud("BKN", 800) - ] - ) - ] + phenomena=[detaf.Cloud("BKN", 800)], + ), + ], ) assert actual == expected @@ -65,12 +60,9 @@ def test_integration_eigw(): phenomena=[ detaf.Wind(140, 10), detaf.Visibility(4000), - wx.Wx( - intensity="-", - precipitation="DZ" - ), - detaf.Cloud("BKN", 700) - ] + wx.Wx(intensity="-", precipitation="DZ"), + detaf.Cloud("BKN", 700), + ], ), # BECMG 0818/0820 9999 NSW SCT010 BKN015 detaf.WeatherCondition( @@ -80,8 +72,8 @@ def test_integration_eigw(): detaf.Visibility(9999), detaf.Wx.NO_SIGNIFICANT_WEATHER, detaf.Cloud("SCT", 1000), - detaf.Cloud("BKN", 1500) - ] + detaf.Cloud("BKN", 1500), + ], ), # BECMG 0901/0903 15005KT detaf.WeatherCondition( @@ -89,7 +81,7 @@ def test_integration_eigw(): change=detaf.Change.BECMG, phenomena=[ detaf.Wind(150, 5), - ] + ], ), # BECMG 0907/0909 13010KT detaf.WeatherCondition( @@ -97,15 +89,13 @@ def test_integration_eigw(): change=detaf.Change.BECMG, phenomena=[ detaf.Wind(130, 10), - ] + ], ), # TEMPO 0907/0918 BKN012 detaf.WeatherCondition( period=((9, 7), (9, 18)), change=detaf.Change.TEMPO, - phenomena=[ - detaf.Cloud("BKN", 1200) - ] + phenomena=[detaf.Cloud("BKN", 1200)], ), # PROB30 TEMPO 0907/0912 4000 -DZ BKN008 detaf.WeatherCondition( @@ -114,74 +104,104 @@ def test_integration_eigw(): probability=30, phenomena=[ detaf.Visibility(4000), - wx.Wx( - intensity="-", - precipitation="DZ" - ), - detaf.Cloud("BKN", 800) - ] + wx.Wx(intensity="-", precipitation="DZ"), + detaf.Cloud("BKN", 800), + ], ), - ] + ], ) assert actual == expected -@pytest.mark.parametrize("bulletin,expected", [ - ("TAF", detaf.Version.ORIGINAL), - ("TAF AMD", detaf.Version.AMMENDED), - ("TAF COR", detaf.Version.CORRECTED) -]) +@pytest.mark.parametrize( + "bulletin,expected", + [ + ("TAF", detaf.Version.ORIGINAL), + ("TAF AMD", detaf.Version.AMMENDED), + ("TAF COR", detaf.Version.CORRECTED), + ], +) def test_parse_version(bulletin, expected): taf = detaf.parse(bulletin) assert isinstance(taf, detaf.TAF) assert taf.version == expected -@pytest.mark.parametrize("bulletin,expected", [ - ("TAF", None), - ("TAF EIDW", "EIDW"), - ("TAF AMD LFPG", "LFPG"), -]) +@pytest.mark.parametrize( + "bulletin,expected", + [ + ("TAF", None), + ("TAF EIDW", "EIDW"), + ("TAF AMD LFPG", "LFPG"), + ], +) def test_parse_icao_code(bulletin, expected): taf = detaf.parse(bulletin) assert taf.icao_identifier == expected -@pytest.mark.parametrize("bulletin,expected", [ - ("TAF LFPG 080500Z", (8, 5, 0)), -]) +@pytest.mark.parametrize( + "bulletin,expected", + [ + ("TAF LFPG 080500Z", (8, 5, 0)), + ], +) def test_parse_issue_time(bulletin, expected): taf = detaf.parse(bulletin) assert taf.issue_time == expected -@pytest.mark.parametrize("bulletin,expected", [ - ("TAF LFPG 080500Z", []), - ("TAF LFPG 080500Z 0805/0905", [detaf.WeatherCondition(((8, 5), (9, 5)))]), - ("TAF EIDW 080500Z 0805/0905 0807/0809", [ - detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), - detaf.WeatherCondition(detaf.period((8, 7), (8, 9))), - ]), - ("TAF EIDW 080500Z 0805/0905 PROB30 TEMPO 0807/0809", [ - detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), - detaf.WeatherCondition(detaf.period((8, 7), (8, 9)), probability=30, change=detaf.Change.TEMPO), - ]), - ("TAF EIDW 080500Z 0805/0905 TEMPO 0807/0809", [ - detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), - detaf.WeatherCondition(detaf.period((8, 7), (8, 9)), change=detaf.Change.TEMPO), - ]), - ("TAF EIDW 080500Z 0805/0905 BECMG 0807/0809", [ - detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), - detaf.WeatherCondition(detaf.period((8, 7), (8, 9)), change=detaf.Change.BECMG), - ]) -]) +@pytest.mark.parametrize( + "bulletin,expected", + [ + ("TAF LFPG 080500Z", []), + ("TAF LFPG 080500Z 0805/0905", [detaf.WeatherCondition(((8, 5), (9, 5)))]), + ( + "TAF EIDW 080500Z 0805/0905 0807/0809", + [ + detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), + detaf.WeatherCondition(detaf.period((8, 7), (8, 9))), + ], + ), + ( + "TAF EIDW 080500Z 0805/0905 PROB30 TEMPO 0807/0809", + [ + detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), + detaf.WeatherCondition( + detaf.period((8, 7), (8, 9)), + probability=30, + change=detaf.Change.TEMPO, + ), + ], + ), + ( + "TAF EIDW 080500Z 0805/0905 TEMPO 0807/0809", + [ + detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), + detaf.WeatherCondition( + detaf.period((8, 7), (8, 9)), change=detaf.Change.TEMPO + ), + ], + ), + ( + "TAF EIDW 080500Z 0805/0905 BECMG 0807/0809", + [ + detaf.WeatherCondition(detaf.period((8, 5), (9, 5))), + detaf.WeatherCondition( + detaf.period((8, 7), (8, 9)), change=detaf.Change.BECMG + ), + ], + ), + ], +) def test_parse_weather_conditions(bulletin, expected): taf = detaf.parse(bulletin) assert taf.weather_conditions == expected -@pytest.mark.parametrize("bulletin,expected", [ - ("TAF EIDW 081647Z 0816/0916 9999", [detaf.Visibility(9999)]) -]) + +@pytest.mark.parametrize( + "bulletin,expected", [("TAF EIDW 081647Z 0816/0916 9999", [detaf.Visibility(9999)])] +) def test_parse_visibility(bulletin, expected): taf = detaf.parse(bulletin) assert taf.weather_conditions[0].phenomena == expected @@ -189,6 +209,5 @@ def test_parse_visibility(bulletin, expected): def test_parse_wx(): assert wx.parse("-DZ") == wx.Wx( - intensity=wx.Intensity.LIGHT, - precipitation=wx.Precipitation.DRIZZLE + intensity=wx.Intensity.LIGHT, precipitation=wx.Precipitation.DRIZZLE ) diff --git a/tests/test_invariants.py b/tests/test_invariants.py index 66ddd04..209d472 100644 --- a/tests/test_invariants.py +++ b/tests/test_invariants.py @@ -6,12 +6,15 @@ # WEATHER + @given( intensity=sampled_from(detaf.weather.Intensity), descriptor=sampled_from(detaf.weather.Descriptor), precipitation=sampled_from(detaf.weather.Precipitation), ) -def test_decode_weather_given_intensity_descriptor_precipitation(intensity, descriptor, precipitation): +def test_decode_weather_given_intensity_descriptor_precipitation( + intensity, descriptor, precipitation +): report = intensity + descriptor + precipitation assert detaf.weather.decode(report).intensity == intensity assert detaf.weather.decode(report).descriptor == descriptor @@ -64,6 +67,7 @@ def test_decode_weather_given_other(other): # VISIBILITY + @given(distance=integers(min_value=0, max_value=9999)) def test_visibility(distance): report = f"TAF AAAA 000000Z 0000/0000 {distance:04}" @@ -72,9 +76,10 @@ def test_visibility(distance): # WIND + @given( direction=integers(min_value=0, max_value=360), - speed=integers(min_value=0, max_value=99) + speed=integers(min_value=0, max_value=99), ) def test_wind_direction_and_speed(direction, speed): report = f"TAF AAAA 000000Z 0000/0000 {direction:03}{speed:02}KT" @@ -82,10 +87,11 @@ def test_wind_direction_and_speed(direction, speed): assert wind.speed == speed assert wind.direction == direction + @given( direction=integers(min_value=0, max_value=360), speed=integers(min_value=0, max_value=99), - gust=integers(min_value=0, max_value=99) + gust=integers(min_value=0, max_value=99), ) def test_wind_direction_speed_gust(direction, speed, gust): report = f"TAF AAAA 000000Z 0000/0000 {direction:03}{speed:02}G{gust:02}KT" @@ -97,6 +103,7 @@ def test_wind_direction_speed_gust(direction, speed, gust): # Encode + @given( version=sampled_from(detaf.Version), icao_identifier=text("ABCDEFGHIJKLMNOPQRSTUVWXYZ", min_size=4, max_size=4), @@ -111,9 +118,6 @@ def test_encode_decoded_report(version, icao_identifier): # CLOUD -@pytest.mark.parametrize("report", [ - "BKN008", - "CAVOK" -]) +@pytest.mark.parametrize("report", ["BKN008", "CAVOK"]) def test_encode_decode_cloud(report): assert detaf.encode(detaf.cloud.decode(report)) == report