Skip to content

Commit

Permalink
new final() method, mirror of next()
Browse files Browse the repository at this point in the history
  • Loading branch information
xoolive committed Jul 14, 2021
1 parent deaed25 commit d96fd9c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "traffic"
homepage = "https://github.com/xoolive/traffic/"
documentation = "https://traffic-viz.github.io/"
version = "2.6.3"
version = "2.6.4"
description = "A toolbox for manipulating and analysing air traffic data"
authors = ["Xavier Olive <git@xoolive.org>"]
license = "MIT"
Expand Down
4 changes: 4 additions & 0 deletions tests/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ def test_landing_ils() -> None:
assert aligned is not None
assert aligned.max("ILS") == "06"

aligned = belevingsvlucht.final("aligned_on_EHLE")
assert aligned is not None
assert aligned.ILS_max == "23"

aligned = airbus_tree.aligned_on_ils("EDHI").next() # noqa: B305
assert aligned is not None
assert aligned.max("ILS") == "23"
Expand Down
21 changes: 21 additions & 0 deletions traffic/core/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,27 @@ def next(
)
return next(fun(self), None)

def final(
self,
method: Union[str, Callable[["Flight"], Iterator["Flight"]]],
) -> Optional["Flight"]:
"""
Returns the first segment of trajectory yielded by flight.method()
>>> flight.final("go_around")
>>> flight.final("runway_change")
>>> flight.final(lambda f: f.aligned_on_ils("LFBO"))
"""
fun = (
getattr(self.__class__, method)
if isinstance(method, str)
else method
)
segment = None
for segment in fun(self):
continue
return segment

# --- Iterators ---

@property
Expand Down
16 changes: 16 additions & 0 deletions traffic/core/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ def next(self) -> Optional["Flight"]:
"""
return next((segment for segment in self), None)

def final(self) -> Optional["Flight"]:
"""Returns the final (last) element in the FlightIterator.
Example usage:
>>> first_attempt = flight.runway_change().final()
This is equivalent to:
>>> flight.final("runway_change")
"""
segment = None
for segment in self:
continue
return segment

def sum(self) -> int:
"""Returns the size of the FlightIterator.
Expand Down

0 comments on commit d96fd9c

Please sign in to comment.