""" Clase híbrida para caracteres """ from hybrid_base import HybridCalcType import re class HybridChr(HybridCalcType): """Clase híbrida para caracteres""" def __new__(cls, str_input): """Crear objeto SymPy válido""" obj = HybridCalcType.__new__(cls) return obj def __init__(self, str_input: str): """Inicialización de Chr""" # Convertir input a entero (código ASCII) if isinstance(str_input, str): if len(str_input) == 1: value = ord(str_input) else: raise ValueError("Input must be a single character") else: value = int(str_input) if not 0 <= value <= 255: raise ValueError("Value must be between 0 and 255") # Llamar al constructor base super().__init__(value, chr(value)) def __str__(self): """Representación string para display""" return f"'{self._original_str}' ({self._value})" def _sympystr(self, printer): """Representación SymPy string""" return str(self) @staticmethod def Helper(input_str): """Ayuda contextual para Chr""" if re.match(r"^\s*Chr\b", input_str, re.IGNORECASE): return "Ej: Chr[A], Chr[65]\nFunciones: toDecimal(), toHex(), toBin()" return None @staticmethod def PopupFunctionList(): """Lista de métodos sugeridos para autocompletado de Chr""" return [ ("toDecimal", "Obtiene código ASCII"), ("toHex", "Convierte a hexadecimal"), ("toBin", "Convierte a binario"), ] def toDecimal(self): """Obtiene código ASCII""" return self._value def toHex(self): """Convierte a hexadecimal""" return f"0x{self._value:02x}" def toBin(self): """Convierte a binario""" return f"0b{self._value:08b}"