Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fixit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
CONVERSIONS = {
"c2f": convert.celsius_to_fahrenheit,
"f2c": convert.fahrenheit_to_celsius,
"kg2lb": convert.kg_to_lbs,
"km2mi": convert.km_to_miles,
"lb2kg": convert.lbs_to_kg,
"mi2km": convert.miles_to_km,
}

Expand Down
20 changes: 20 additions & 0 deletions fixit/convert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit conversions."""

KM_PER_MILE = 1.609344
LBS_PER_KG = 2.20462


def celsius_to_fahrenheit(c: float) -> float:
Expand Down Expand Up @@ -37,3 +38,22 @@ def miles_to_km(miles: float) -> float:
miles_to_km(1) returns approximately 1.6093.
"""
return miles * KM_PER_MILE


def kg_to_lbs(kg: float) -> float:
"""Convert kilograms to pounds.

Example:
kg_to_lbs(1) returns approximately 2.20462.
"""
return kg * LBS_PER_KG


def lbs_to_kg(lbs: float) -> float:
"""Convert pounds to kilograms.

Example:
lbs_to_kg(2.20462) returns approximately 1.0.
"""
return lbs / LBS_PER_KG

13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from fixit.cli import main


Expand Down Expand Up @@ -43,3 +45,14 @@ def test_top_words_command_defaults_to_three(capsys):

assert result == 0
assert capsys.readouterr().out == "one: 1\ntwo: 1\nthree: 1\n"


def test_convert_command_weight(capsys):
result = main(["convert", "1", "kg2lb"])
assert result == 0
assert float(capsys.readouterr().out.strip()) == pytest.approx(2.20462)

result = main(["convert", "2.20462", "lb2kg"])
assert result == 0
assert float(capsys.readouterr().out.strip()) == pytest.approx(1.0)

13 changes: 13 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from fixit.convert import (
celsius_to_fahrenheit,
fahrenheit_to_celsius,
kg_to_lbs,
km_to_miles,
lbs_to_kg,
miles_to_km,
)

Expand Down Expand Up @@ -38,3 +40,14 @@ def test_absolute_zero():

def test_float_km_to_miles():
assert km_to_miles(1.5) == pytest.approx(0.932056788)


def test_kg_to_lbs():
assert kg_to_lbs(1) == pytest.approx(2.20462)
assert kg_to_lbs(10) == pytest.approx(22.0462)


def test_lbs_to_kg():
assert lbs_to_kg(2.20462) == pytest.approx(1.0)
assert lbs_to_kg(10) == pytest.approx(4.53592, rel=1e-3)

Loading