parent
dce8fde29a
commit
e1688629a3
2 changed files with 29 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import datetime
|
||||
import typing
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from pycountry.db import Country
|
||||
|
|
@ -141,6 +142,25 @@ class Trip:
|
|||
else None
|
||||
)
|
||||
|
||||
def distances_by_transport_type(self) -> list[tuple[str, float]]:
|
||||
"""Calculate the total distance travelled for each type of transport.
|
||||
|
||||
Any travel item with a missing or None 'distance' field is ignored.
|
||||
"""
|
||||
transport_distances: Counter[float] = Counter()
|
||||
|
||||
for item in self.travel:
|
||||
distance = item.get("distance")
|
||||
if distance:
|
||||
transport_type = item.get("type", "unknown")
|
||||
transport_distances[transport_type] += distance
|
||||
|
||||
return list(transport_distances.items())
|
||||
|
||||
|
||||
# Example usage:
|
||||
# You would call the function with your travel list here to get the results.
|
||||
|
||||
|
||||
@dataclass
|
||||
class Holiday:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue