Spaces:
Running
Running
""" | |
Conversion factors and constants for unit conversions. | |
This module contains all the numerical constants used for converting between | |
different units of measurement in the BasicAgent calculation tools. | |
Extracted from BasicAgent._init_calculation_tools() for better modularity | |
and maintainability. | |
""" | |
# Length conversion factors | |
LENGTH_CONVERSIONS = { | |
"meters_to_feet": 3.28084, | |
"feet_to_meters": 0.3048, | |
"inches_to_cm": 2.54, | |
"cm_to_inches": 0.393701, | |
"miles_to_km": 1.60934, | |
"km_to_miles": 0.621371, | |
} | |
# Weight conversion factors | |
WEIGHT_CONVERSIONS = { | |
"kg_to_pounds": 2.20462, | |
"pounds_to_kg": 0.453592, | |
} | |
# Area conversion factors | |
AREA_CONVERSIONS = { | |
"sqft_to_sqm": 0.092903, | |
"sqm_to_sqft": 10.7639, | |
} | |
# Temperature conversion formulas (as constants for reference) | |
# Note: Temperature conversions are handled by formulas, not simple factors | |
TEMPERATURE_CONVERSION_INFO = { | |
"celsius_to_fahrenheit": "F = (C * 9/5) + 32", | |
"fahrenheit_to_celsius": "C = (F - 32) * 5/9", | |
} | |
# Combined conversion factors dictionary | |
# This maintains compatibility with the original implementation | |
CONVERSION_FACTORS = { | |
**LENGTH_CONVERSIONS, | |
**WEIGHT_CONVERSIONS, | |
**AREA_CONVERSIONS, | |
} | |
# Additional conversion factors that might be useful for future expansion | |
EXTENDED_CONVERSIONS = { | |
# Volume conversions | |
"liters_to_gallons": 0.264172, | |
"gallons_to_liters": 3.78541, | |
"ml_to_fl_oz": 0.033814, | |
"fl_oz_to_ml": 29.5735, | |
# Time conversions | |
"minutes_to_seconds": 60, | |
"hours_to_minutes": 60, | |
"days_to_hours": 24, | |
"weeks_to_days": 7, | |
# Speed conversions | |
"mph_to_kph": 1.60934, | |
"kph_to_mph": 0.621371, | |
"mps_to_mph": 2.23694, | |
"mph_to_mps": 0.44704, | |
# Energy conversions | |
"joules_to_calories": 0.239006, | |
"calories_to_joules": 4.184, | |
"kWh_to_joules": 3600000, | |
"joules_to_kWh": 2.77778e-7, | |
} | |
# Utility functions for conversion operations | |
def get_conversion_factor(from_unit: str, to_unit: str) -> float: | |
""" | |
Get conversion factor for converting from one unit to another. | |
Args: | |
from_unit (str): Source unit | |
to_unit (str): Target unit | |
Returns: | |
float: Conversion factor, or None if not found | |
Example: | |
>>> get_conversion_factor("meters", "feet") | |
3.28084 | |
""" | |
key = f"{from_unit}_to_{to_unit}" | |
return CONVERSION_FACTORS.get(key) | |
def get_all_conversions(): | |
""" | |
Get all available conversion factors. | |
Returns: | |
dict: All conversion factors including extended ones | |
""" | |
return {**CONVERSION_FACTORS, **EXTENDED_CONVERSIONS} | |
def get_conversion_categories(): | |
""" | |
Get conversion factors organized by category. | |
Returns: | |
dict: Conversion factors grouped by type | |
""" | |
return { | |
"length": LENGTH_CONVERSIONS, | |
"weight": WEIGHT_CONVERSIONS, | |
"area": AREA_CONVERSIONS, | |
"extended": EXTENDED_CONVERSIONS, | |
} | |
# Constants for precision and formatting | |
CONVERSION_PRECISION = 2 # Default decimal places for conversion results | |
MAX_DECIMAL_PLACES = 6 # Maximum decimal places to avoid floating point errors |