Compare commits
2 commits
a5cb130c89
...
8f4adb9f7f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f4adb9f7f | ||
|
|
4c5da3712e |
38
cyclinglib/energy.py
Normal file
38
cyclinglib/energy.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from cyclinglib.force import Force
|
||||
from cyclinglib.length import Length
|
||||
from cyclinglib.power import Power
|
||||
from cyclinglib.time import Time
|
||||
|
||||
|
||||
class Energy:
|
||||
|
||||
def __init__(self, joules: float):
|
||||
self._joules = joules
|
||||
|
||||
def joules(self) -> float:
|
||||
return self._joules
|
||||
|
||||
def kilojoules(self) -> float:
|
||||
return self._joules / 1_000
|
||||
|
||||
def kilocalories(self) -> float:
|
||||
return self.kilojoules() / 4.184
|
||||
|
||||
def kcal(self) -> float:
|
||||
"""
|
||||
This is alias for kilocalories.
|
||||
"""
|
||||
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()
|
||||
return Energy(joules)
|
||||
|
||||
|
||||
def calculate_energy_cycling(power: Power, duration: Time) -> Energy:
|
||||
joules = power.watts() * duration.seconds()
|
||||
return Energy(joules)
|
||||
22
cyclinglib/force.py
Normal file
22
cyclinglib/force.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
from cyclinglib.power import Power
|
||||
from cyclinglib.speed import Speed
|
||||
|
||||
|
||||
class Force:
|
||||
|
||||
def __init__(self, newtons: float):
|
||||
self._newtons = newtons
|
||||
|
||||
def newtons(self) -> float:
|
||||
return self._newtons
|
||||
|
||||
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()
|
||||
return Force(newtons)
|
||||
|
|
@ -3,6 +3,9 @@ class Length:
|
|||
def __init__(self, metre: float) -> None:
|
||||
self._metre = metre
|
||||
|
||||
def metre(self) -> float:
|
||||
return self._metre
|
||||
|
||||
def kilometre(self) -> float:
|
||||
return self._metre / 1_000
|
||||
|
||||
|
|
@ -14,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()}"
|
||||
26
cyclinglib/power.py
Normal file
26
cyclinglib/power.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from cyclinglib.force import Force
|
||||
from cyclinglib.speed import Speed
|
||||
|
||||
|
||||
class Power:
|
||||
|
||||
def __init__(self, watts: float):
|
||||
self._watts = watts
|
||||
|
||||
def watts(self) -> float:
|
||||
return self._watts
|
||||
|
||||
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()
|
||||
return Power(watts)
|
||||
|
||||
|
||||
def calculate_power_si(newtons: float, meters_per_second: float) -> Power:
|
||||
return calculate_power(Force(newtons), Speed(meters_per_second))
|
||||
19
cyclinglib/time.py
Normal file
19
cyclinglib/time.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class Time:
|
||||
|
||||
def __init__(self, seconds: float):
|
||||
self._seconds = seconds
|
||||
|
||||
def seconds(self) -> float:
|
||||
return self._seconds
|
||||
|
||||
def milliseconds(self) -> float:
|
||||
return self._seconds * 1_000
|
||||
|
||||
def hours(self) -> float:
|
||||
return self._seconds / 3_600
|
||||
|
||||
def minutes(self) -> float:
|
||||
return self._seconds / 60
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"s: {self.seconds()} min: {self.minutes()} h: {self.hours()}"
|
||||
Loading…
Reference in a new issue