2020-07-28 22:11:16 +00:00
|
|
|
"""
|
|
|
|
Convert International System of Units (SI) and Binary prefixes
|
|
|
|
"""
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-07-28 22:11:16 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
class SIUnit(Enum):
|
2020-07-28 22:11:16 +00:00
|
|
|
yotta = 24
|
|
|
|
zetta = 21
|
|
|
|
exa = 18
|
|
|
|
peta = 15
|
|
|
|
tera = 12
|
|
|
|
giga = 9
|
|
|
|
mega = 6
|
|
|
|
kilo = 3
|
|
|
|
hecto = 2
|
|
|
|
deca = 1
|
|
|
|
deci = -1
|
|
|
|
centi = -2
|
|
|
|
milli = -3
|
|
|
|
micro = -6
|
|
|
|
nano = -9
|
|
|
|
pico = -12
|
|
|
|
femto = -15
|
|
|
|
atto = -18
|
|
|
|
zepto = -21
|
|
|
|
yocto = -24
|
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
class BinaryUnit(Enum):
|
2020-07-28 22:11:16 +00:00
|
|
|
yotta = 8
|
|
|
|
zetta = 7
|
|
|
|
exa = 6
|
|
|
|
peta = 5
|
|
|
|
tera = 4
|
|
|
|
giga = 3
|
|
|
|
mega = 2
|
|
|
|
kilo = 1
|
|
|
|
|
|
|
|
|
|
|
|
def convert_si_prefix(
|
|
|
|
known_amount: float,
|
2022-10-12 22:54:20 +00:00
|
|
|
known_prefix: str | SIUnit,
|
|
|
|
unknown_prefix: str | SIUnit,
|
2020-07-28 22:11:16 +00:00
|
|
|
) -> float:
|
|
|
|
"""
|
|
|
|
Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix
|
|
|
|
Wikipedia reference: https://en.wikipedia.org/wiki/International_System_of_Units
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> convert_si_prefix(1, SIUnit.giga, SIUnit.mega)
|
2020-07-28 22:11:16 +00:00
|
|
|
1000
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> convert_si_prefix(1, SIUnit.mega, SIUnit.giga)
|
2020-07-28 22:11:16 +00:00
|
|
|
0.001
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> convert_si_prefix(1, SIUnit.kilo, SIUnit.kilo)
|
2020-07-28 22:11:16 +00:00
|
|
|
1
|
|
|
|
>>> convert_si_prefix(1, 'giga', 'mega')
|
|
|
|
1000
|
|
|
|
>>> convert_si_prefix(1, 'gIGa', 'mEGa')
|
|
|
|
1000
|
|
|
|
"""
|
|
|
|
if isinstance(known_prefix, str):
|
2022-10-12 22:54:20 +00:00
|
|
|
known_prefix = SIUnit[known_prefix.lower()]
|
2020-07-28 22:11:16 +00:00
|
|
|
if isinstance(unknown_prefix, str):
|
2022-10-12 22:54:20 +00:00
|
|
|
unknown_prefix = SIUnit[unknown_prefix.lower()]
|
2021-04-04 13:25:49 +00:00
|
|
|
unknown_amount: float = known_amount * (
|
|
|
|
10 ** (known_prefix.value - unknown_prefix.value)
|
|
|
|
)
|
2020-07-28 22:11:16 +00:00
|
|
|
return unknown_amount
|
|
|
|
|
|
|
|
|
|
|
|
def convert_binary_prefix(
|
|
|
|
known_amount: float,
|
2022-10-12 22:54:20 +00:00
|
|
|
known_prefix: str | BinaryUnit,
|
|
|
|
unknown_prefix: str | BinaryUnit,
|
2020-07-28 22:11:16 +00:00
|
|
|
) -> float:
|
|
|
|
"""
|
|
|
|
Wikipedia reference: https://en.wikipedia.org/wiki/Metric_prefix
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> convert_binary_prefix(1, BinaryUnit.giga, BinaryUnit.mega)
|
2020-07-28 22:11:16 +00:00
|
|
|
1024
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> convert_binary_prefix(1, BinaryUnit.mega, BinaryUnit.giga)
|
2020-07-28 22:11:16 +00:00
|
|
|
0.0009765625
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> convert_binary_prefix(1, BinaryUnit.kilo, BinaryUnit.kilo)
|
2020-07-28 22:11:16 +00:00
|
|
|
1
|
|
|
|
>>> convert_binary_prefix(1, 'giga', 'mega')
|
|
|
|
1024
|
|
|
|
>>> convert_binary_prefix(1, 'gIGa', 'mEGa')
|
|
|
|
1024
|
|
|
|
"""
|
|
|
|
if isinstance(known_prefix, str):
|
2022-10-12 22:54:20 +00:00
|
|
|
known_prefix = BinaryUnit[known_prefix.lower()]
|
2020-07-28 22:11:16 +00:00
|
|
|
if isinstance(unknown_prefix, str):
|
2022-10-12 22:54:20 +00:00
|
|
|
unknown_prefix = BinaryUnit[unknown_prefix.lower()]
|
2021-04-04 13:25:49 +00:00
|
|
|
unknown_amount: float = known_amount * (
|
2020-07-28 22:11:16 +00:00
|
|
|
2 ** ((known_prefix.value - unknown_prefix.value) * 10)
|
|
|
|
)
|
|
|
|
return unknown_amount
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|