55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""
|
|
Clase híbrida para números hexadecimales
|
|
"""
|
|
from sympy_Base import SympyClassBase
|
|
import re
|
|
|
|
|
|
class Class_Hex(SympyClassBase):
|
|
"""Clase híbrida para números hexadecimales"""
|
|
|
|
def __new__(cls, value_input):
|
|
"""Crear objeto SymPy válido"""
|
|
obj = SympyClassBase.__new__(cls)
|
|
return obj
|
|
|
|
def __init__(self, value_input):
|
|
"""Inicialización de Hex"""
|
|
# Convertir input a entero
|
|
if isinstance(value_input, str):
|
|
# Remover prefijo 0x si existe
|
|
if value_input.startswith('0x'):
|
|
value_input = value_input[2:]
|
|
# Convertir a entero
|
|
value = int(value_input, 16)
|
|
else:
|
|
value = int(value_input)
|
|
|
|
# Llamar al constructor base
|
|
super().__init__(value, f"0x{value:02x}")
|
|
|
|
def __str__(self):
|
|
"""Representación string para display"""
|
|
return f"0x{self._value:02x}"
|
|
|
|
def _sympystr(self, printer):
|
|
"""Representación SymPy string"""
|
|
return str(self)
|
|
|
|
@staticmethod
|
|
def Helper(input_str):
|
|
"""Ayuda contextual para Hex"""
|
|
if re.match(r"^\s*Hex\b", input_str, re.IGNORECASE):
|
|
return 'Ej: Hex[FF], Hex[255]\nFunciones: toDecimal()'
|
|
return None
|
|
|
|
@staticmethod
|
|
def PopupFunctionList():
|
|
"""Lista de métodos sugeridos para autocompletado de Hex"""
|
|
return [
|
|
("toDecimal", "Convierte a decimal"),
|
|
]
|
|
|
|
def toDecimal(self):
|
|
"""Convierte a decimal"""
|
|
return self._value |