aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--geldschieberbot.py2
-rw-r--r--tanken.py48
2 files changed, 31 insertions, 19 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py
index ff285db..f21b7d6 100644
--- a/geldschieberbot.py
+++ b/geldschieberbot.py
@@ -620,6 +620,8 @@ def _tanken(sender, args, msg) -> dict[str, str]:
if err:
return {'err': err}
+ assert parts
+
output = ""
change = [args]
for pname, values in parts.items():
diff --git a/tanken.py b/tanken.py
index b19fb53..2c65c73 100644
--- a/tanken.py
+++ b/tanken.py
@@ -1,47 +1,57 @@
-#!/bin/env python3
+"""Utilities to support splitting fuel and car maintainance costs"""
+
+import typing as T
+
+PassengerResult = dict[str, int]
+Results = dict[str, PassengerResult]
+TankenFailure = T.Tuple[None, str]
+TankenSuccess = T.Tuple[Results, None]
+TankenResult = T.Union[TankenSuccess, TankenFailure]
# cost should be given in cents
-def tanken(drives, cost, service_charge=0):
+def tanken(_drives: list[str], cost: int, service_charge=0) -> TankenResult:
+ """calculate costs per passange for one tank load"""
passengers = {}
distance = 0.
- drives = [d.split(' ') for d in drives]
+ drives = [d.split(' ') for d in _drives]
- for d in drives:
+ for drive in drives:
try:
- d[0] = int(d[0])
+ drive_distance = int(drive[0])
except (IndexError, ValueError):
return None, "Lines have to start with the driven distance!"
# calculate overall distance
- distance += d[0]
+ distance += drive_distance
# collect distances per passenger
- for p in d[1:]:
- if p not in passengers:
- passengers[p] = {
- "distance": d[0],
+ for passenger in drive[1:]:
+ if passenger not in passengers:
+ passengers[passenger] = {
+ "distance": drive_distance,
"cost": 0,
"service_charge": 0
}
else:
- passengers[p]["distance"] += d[0]
+ passengers[passenger]["distance"] += drive_distance
# calculate cost per kilometer
if distance <= 0:
return None, "Driven distance must be greater than 0!"
- c = cost / distance
+ c_km = cost / distance
- for d in drives:
+ for drive in drives:
+ drive_distance = int(drive[0])
# calculate cost per drive split among passengers
- c_d = int(c * d[0] / (len(d) - 1))
- for p in d[1:]:
- passengers[p]["cost"] += c_d
+ c_d = int(c_km * drive_distance / (len(drive) - 1))
+ for passenger in drive[1:]:
+ passengers[passenger]["cost"] += c_d
# calculate service charge per drive split among passengers
- sc_d = int(service_charge * d[0] / (len(d) - 1))
- for p in d[1:]:
- passengers[p]["service_charge"] += sc_d
+ sc_d = int(service_charge * drive_distance / (len(drive) - 1))
+ for passenger in drive[1:]:
+ passengers[passenger]["service_charge"] += sc_d
return passengers, None