Spaces:
Running
Running
File size: 3,208 Bytes
9a6a4dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
"""
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 |