More work done on unit classes.
This commit is contained in:
parent
a5cb130c89
commit
4c5da3712e
35
cyclinglib/energy.py
Normal file
35
cyclinglib/energy.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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 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)
|
||||
19
cyclinglib/force.py
Normal file
19
cyclinglib/force.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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 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
|
||||
|
||||
|
|
|
|||
23
cyclinglib/power.py
Normal file
23
cyclinglib/power.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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 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