48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
{
|
|
"files": {
|
|
"ai-core/utils/conversion.py": """import sys
|
|
|
|
def convert_temperature(value, units):
|
|
"""
|
|
Convert temperature between Celsius, Fahrenheit, and Kelvin.
|
|
|
|
Args:
|
|
value (float): The temperature value to be converted.
|
|
units (str): The units of the input value ('Celsius', 'Fahrenheit', 'Kelvin').
|
|
|
|
Returns:
|
|
float: The converted temperature value.
|
|
|
|
Raises:
|
|
ValueError: If an invalid unit is provided or if the input value cannot be parsed as a number.
|
|
"""
|
|
|
|
try:
|
|
value = float(value)
|
|
except ValueError:
|
|
raise ValueError("Input value must be a numeric type.")
|
|
|
|
if units == 'Celsius':
|
|
# Convert to Fahrenheit
|
|
return (value * 9/5) + 32
|
|
|
|
elif units == 'Fahrenheit':
|
|
# Convert to Celsius
|
|
return (value - 32) * 5/9
|
|
|
|
elif units == 'Kelvin':
|
|
# Convert to Celsius
|
|
return value - 273.15
|
|
|
|
else:
|
|
raise ValueError("Invalid unit provided. Choose from 'Celsius', 'Fahrenheit', or 'Kelvin'.")
|
|
|
|
# Example usage and testing
|
|
if __name__ == "__main__":
|
|
print(convert_temperature(0, "Celsius")) # Output: 32.0 (Freezing point of water in Fahrenheit)
|
|
print(convert_temperature(32, "Fahrenheit")) # Output: 0.0 (Freezing point of water in Celsius)
|
|
print(convert_temperature(273.15, "Kelvin")) # Output: 0.0 (Freezing point of water in Celsius)
|
|
"""
|
|
},
|
|
"commit_message": "feat: add temperature conversion utility function"
|
|
} |