Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgryan committed Nov 11, 2024
1 parent b8335ba commit 42c9423
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 85 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ distribution = true

[tool.pdm.scripts]
test = "pytest"
fmt = "ruff format"

[tool.pdm.dev-dependencies]
lint = [
Expand Down
15 changes: 7 additions & 8 deletions src/detaf/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/detaf/wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
157 changes: 88 additions & 69 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand All @@ -80,32 +72,30 @@ 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(
period=((9, 1), (9, 3)),
change=detaf.Change.BECMG,
phenomena=[
detaf.Wind(150, 5),
]
],
),
# BECMG 0907/0909 13010KT
detaf.WeatherCondition(
period=((9, 7), (9, 9)),
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(
Expand All @@ -114,81 +104,110 @@ 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


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
)
18 changes: 11 additions & 7 deletions tests/test_invariants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -72,20 +76,22 @@ 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"
wind = detaf.decode(report).weather_conditions[0].phenomena[0]
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"
Expand All @@ -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),
Expand All @@ -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

0 comments on commit 42c9423

Please sign in to comment.