feat: initial starter state for unit-converter
This commit is contained in:
parent
a6c481cf14
commit
e4c715b61f
32
Jenkinsfile
vendored
Normal file
32
Jenkinsfile
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
pipeline {
|
||||||
|
agent any
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Checkout') {
|
||||||
|
steps {
|
||||||
|
checkout scm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Test') {
|
||||||
|
steps {
|
||||||
|
sh '''
|
||||||
|
if python3 -m pytest --version >/dev/null 2>&1; then
|
||||||
|
python3 -m pytest -q
|
||||||
|
else
|
||||||
|
python3 -m venv .venv
|
||||||
|
. .venv/bin/activate
|
||||||
|
pip install --quiet --no-cache-dir -r requirements.txt
|
||||||
|
pytest -q
|
||||||
|
fi
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
always {
|
||||||
|
deleteDir()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
cli.py
Normal file
35
cli.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
"""Command-line interface for unit-converter."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from converter import convert
|
||||||
|
|
||||||
|
|
||||||
|
def build_parser() -> argparse.ArgumentParser:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog="unit-converter",
|
||||||
|
description="Convert temperature, distance, and mass measurements.",
|
||||||
|
)
|
||||||
|
parser.add_argument("value", type=float, help="Numeric measurement to convert")
|
||||||
|
parser.add_argument("from_unit", help="Source unit (c, f, k, km, mi, kg, lb)")
|
||||||
|
parser.add_argument("to_unit", help="Target unit (c, f, k, km, mi, kg, lb)")
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str] | None = None) -> int:
|
||||||
|
parser = build_parser()
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
try:
|
||||||
|
result = convert(args.value, args.from_unit, args.to_unit)
|
||||||
|
except ValueError as exc:
|
||||||
|
print(f"Error: {exc}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
print(f"{args.value:g} {args.from_unit} = {result:g} {args.to_unit}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
127
converter.py
Normal file
127
converter.py
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
"""Clean-room unit conversion utilities for CND-400 Module 1, Module 2, and Module 5."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
ABSOLUTE_ZERO_C = -273.15
|
||||||
|
KM_PER_MILE = 1.609344
|
||||||
|
LBS_PER_KG = 2.20462262185
|
||||||
|
|
||||||
|
_UNIT_ALIASES: dict[str, str] = {
|
||||||
|
"c": "c",
|
||||||
|
"celsius": "c",
|
||||||
|
"f": "f",
|
||||||
|
"fahrenheit": "f",
|
||||||
|
"k": "k",
|
||||||
|
"kelvin": "k",
|
||||||
|
"km": "km",
|
||||||
|
"kilometer": "km",
|
||||||
|
"kilometers": "km",
|
||||||
|
"mi": "mi",
|
||||||
|
"mile": "mi",
|
||||||
|
"miles": "mi",
|
||||||
|
"kg": "kg",
|
||||||
|
"kilogram": "kg",
|
||||||
|
"kilograms": "kg",
|
||||||
|
"lb": "lb",
|
||||||
|
"lbs": "lb",
|
||||||
|
"pound": "lb",
|
||||||
|
"pounds": "lb",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def c_to_f(celsius: float) -> float:
|
||||||
|
"""Convert degrees Celsius to Fahrenheit."""
|
||||||
|
if celsius < ABSOLUTE_ZERO_C:
|
||||||
|
raise ValueError(f"Temperature below absolute zero: {celsius} C")
|
||||||
|
return round((celsius * 9.0 / 5.0) + 32.0, 4)
|
||||||
|
|
||||||
|
|
||||||
|
def f_to_c(fahrenheit: float) -> float:
|
||||||
|
"""Convert degrees Fahrenheit to Celsius."""
|
||||||
|
celsius = (fahrenheit - 32.0) * 5.0 / 9.0
|
||||||
|
if celsius < ABSOLUTE_ZERO_C:
|
||||||
|
raise ValueError(f"Temperature below absolute zero: {fahrenheit} F")
|
||||||
|
return round(celsius, 4)
|
||||||
|
|
||||||
|
|
||||||
|
def c_to_k(celsius: float) -> float:
|
||||||
|
"""Convert degrees Celsius to Kelvin."""
|
||||||
|
if celsius < ABSOLUTE_ZERO_C:
|
||||||
|
raise ValueError(f"Temperature below absolute zero: {celsius} C")
|
||||||
|
return round(celsius - ABSOLUTE_ZERO_C, 4)
|
||||||
|
|
||||||
|
|
||||||
|
def k_to_c(kelvin: float) -> float:
|
||||||
|
"""Convert Kelvin to degrees Celsius."""
|
||||||
|
if kelvin < 0:
|
||||||
|
raise ValueError(f"Kelvin cannot be negative: {kelvin} K")
|
||||||
|
return round(kelvin + ABSOLUTE_ZERO_C, 4)
|
||||||
|
|
||||||
|
|
||||||
|
def km_to_miles(km: float) -> float:
|
||||||
|
"""Convert kilometers to statute miles."""
|
||||||
|
if km < 0:
|
||||||
|
raise ValueError(f"Distance cannot be negative: {km} km")
|
||||||
|
return round(km / KM_PER_MILE, 6)
|
||||||
|
|
||||||
|
|
||||||
|
def miles_to_km(miles: float) -> float:
|
||||||
|
"""Convert statute miles to kilometers."""
|
||||||
|
if miles < 0:
|
||||||
|
raise ValueError(f"Distance cannot be negative: {miles} mi")
|
||||||
|
return round(miles * KM_PER_MILE, 6)
|
||||||
|
|
||||||
|
|
||||||
|
def kg_to_lbs(kg: float) -> float:
|
||||||
|
"""Convert kilograms to avoirdupois pounds."""
|
||||||
|
if kg < 0:
|
||||||
|
raise ValueError(f"Mass cannot be negative: {kg} kg")
|
||||||
|
return round(kg * LBS_PER_KG, 6)
|
||||||
|
|
||||||
|
|
||||||
|
def lbs_to_kg(lbs: float) -> float:
|
||||||
|
"""Convert avoirdupois pounds to kilograms."""
|
||||||
|
if lbs < 0:
|
||||||
|
raise ValueError(f"Mass cannot be negative: {lbs} lb")
|
||||||
|
return round(lbs / LBS_PER_KG, 6)
|
||||||
|
|
||||||
|
|
||||||
|
_CONVERTERS = {
|
||||||
|
("c", "f"): c_to_f,
|
||||||
|
("f", "c"): f_to_c,
|
||||||
|
("c", "k"): c_to_k,
|
||||||
|
("k", "c"): k_to_c,
|
||||||
|
("f", "k"): lambda v: c_to_k(f_to_c(v)),
|
||||||
|
("k", "f"): lambda v: c_to_f(k_to_c(v)),
|
||||||
|
("km", "mi"): km_to_miles,
|
||||||
|
("mi", "km"): miles_to_km,
|
||||||
|
("kg", "lb"): kg_to_lbs,
|
||||||
|
("lb", "kg"): lbs_to_kg,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_unit(unit: str) -> str:
|
||||||
|
"""Normalize a unit name or abbreviation."""
|
||||||
|
key = unit.strip().lower()
|
||||||
|
if key not in _UNIT_ALIASES:
|
||||||
|
raise ValueError(f"Unsupported unit: {unit!r}")
|
||||||
|
return _UNIT_ALIASES[key]
|
||||||
|
|
||||||
|
|
||||||
|
def convert(value: float, from_unit: str, to_unit: str) -> float:
|
||||||
|
"""Convert a numeric value between compatible units."""
|
||||||
|
src = normalize_unit(from_unit)
|
||||||
|
dst = normalize_unit(to_unit)
|
||||||
|
if src == dst:
|
||||||
|
if src in ("km", "mi", "kg", "lb", "k") and value < 0:
|
||||||
|
raise ValueError(f"Value cannot be negative for unit {src}: {value}")
|
||||||
|
if src == "c" and value < ABSOLUTE_ZERO_C:
|
||||||
|
raise ValueError(f"Temperature below absolute zero: {value} C")
|
||||||
|
if src == "f" and ((value - 32.0) * 5.0 / 9.0) < ABSOLUTE_ZERO_C:
|
||||||
|
raise ValueError(f"Temperature below absolute zero: {value} F")
|
||||||
|
return round(float(value), 6)
|
||||||
|
|
||||||
|
fn = _CONVERTERS.get((src, dst))
|
||||||
|
if fn is None:
|
||||||
|
raise ValueError(f"Incompatible conversion: {from_unit!r} -> {to_unit!r}")
|
||||||
|
return fn(float(value))
|
||||||
3
pyproject.toml
Normal file
3
pyproject.toml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[tool.pytest.ini_options]
|
||||||
|
pythonpath = ["."]
|
||||||
|
testpaths = ["tests"]
|
||||||
54
tests/test_converter.py
Normal file
54
tests/test_converter.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
"""Unit tests for unit-converter."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from converter import (
|
||||||
|
c_to_f,
|
||||||
|
convert,
|
||||||
|
f_to_c,
|
||||||
|
kg_to_lbs,
|
||||||
|
km_to_miles,
|
||||||
|
lbs_to_kg,
|
||||||
|
miles_to_km,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_celsius_to_fahrenheit() -> None:
|
||||||
|
assert c_to_f(0.0) == 32.0
|
||||||
|
assert c_to_f(100.0) == 212.0
|
||||||
|
assert c_to_f(-40.0) == -40.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_fahrenheit_to_celsius() -> None:
|
||||||
|
assert f_to_c(32.0) == 0.0
|
||||||
|
assert f_to_c(212.0) == 100.0
|
||||||
|
assert f_to_c(-40.0) == -40.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_distance_round_trip() -> None:
|
||||||
|
miles = km_to_miles(10.0)
|
||||||
|
assert miles == pytest.approx(6.213712, rel=1e-5)
|
||||||
|
assert miles_to_km(miles) == pytest.approx(10.0, rel=1e-5)
|
||||||
|
|
||||||
|
|
||||||
|
def test_weight_round_trip() -> None:
|
||||||
|
lbs = kg_to_lbs(5.0)
|
||||||
|
assert lbs == pytest.approx(11.023113, rel=1e-5)
|
||||||
|
assert lbs_to_kg(lbs) == pytest.approx(5.0, rel=1e-5)
|
||||||
|
|
||||||
|
|
||||||
|
def test_generic_convert_aliases() -> None:
|
||||||
|
assert convert(25, "Celsius", "Fahrenheit") == 77.0
|
||||||
|
assert convert(0, "c", "k") == 273.15
|
||||||
|
assert convert(42, "km", "km") == 42.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_inputs_raise_value_error() -> None:
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
c_to_f(-300.0)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
km_to_miles(-1.0)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
convert(10, "km", "kg")
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
convert(10, "parsecs", "km")
|
||||||
Loading…
Reference in New Issue
Block a user