From 8f4adb9f7fbb9a60b1f2e5247aab2120388d1fd3 Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 22 Feb 2025 10:24:54 +0100 Subject: [PATCH] Added __str__ methods to some units. --- cyclinglib/energy.py | 3 +++ cyclinglib/force.py | 3 +++ cyclinglib/length.py | 3 +++ cyclinglib/power.py | 3 +++ 4 files changed, 12 insertions(+) diff --git a/cyclinglib/energy.py b/cyclinglib/energy.py index db48a21..c7d0a8e 100644 --- a/cyclinglib/energy.py +++ b/cyclinglib/energy.py @@ -24,6 +24,9 @@ class Energy: """ return self.kilocalories() + def __str__(self) -> str: + return f"J: {self.joules()} kJ: {self.kilojoules()} kcal: {self.kcal()}" + def calculate_energy(force: Force, distance: Length) -> Energy: joules = force.newtons() * distance.metre() diff --git a/cyclinglib/force.py b/cyclinglib/force.py index d732fa2..f155559 100644 --- a/cyclinglib/force.py +++ b/cyclinglib/force.py @@ -13,6 +13,9 @@ class Force: def kilonewtons(self) -> float: return self._newtons / 1_000 + def __str__(self) -> str: + return f"N: {self.newtons()} kN: {self.kilonewtons()}" + def calculate_force(power: Power, speed: Speed): newtons: float = power.watts() * speed.meters_per_second() diff --git a/cyclinglib/length.py b/cyclinglib/length.py index 45173af..26b0547 100644 --- a/cyclinglib/length.py +++ b/cyclinglib/length.py @@ -17,3 +17,6 @@ class Length: def miles(self) -> float: return self._metre * 0.000621371192 + + def __str__(self) -> str: + return f"m: {self.metre()} km: {self.metre()} mi: {self.miles()}" \ No newline at end of file diff --git a/cyclinglib/power.py b/cyclinglib/power.py index b8e413f..03bba64 100644 --- a/cyclinglib/power.py +++ b/cyclinglib/power.py @@ -13,6 +13,9 @@ class Power: def kilowatts(self) -> float: return self._watts / 1_000 + def __str__(self) -> str: + return f"W: {self.watts()}" + def calculate_power(force: Force, speed: Speed) -> Power: watts = force.newtons() * speed.meters_per_second()