Calc/dec_type.py

56 lines
1.5 KiB
Python

"""
Clase híbrida para números decimales
"""
from sympy_Base import SympyClassBase
import re
class Class_Dec(SympyClassBase):
"""Clase híbrida para números decimales"""
def __new__(cls, value_input):
"""Crear objeto SymPy válido"""
obj = SympyClassBase.__new__(cls)
return obj
def __init__(self, value_input):
"""Inicialización de Dec"""
# Convertir input a entero
if isinstance(value_input, str):
value = int(value_input)
else:
value = int(value_input)
# Llamar al constructor base
super().__init__(value, str(value))
def __str__(self):
"""Representación string para display"""
return str(self._value)
def _sympystr(self, printer):
"""Representación SymPy string"""
return str(self)
@staticmethod
def Helper(input_str):
"""Ayuda contextual para Dec"""
if re.match(r"^\s*Dec\b", input_str, re.IGNORECASE):
return 'Ej: Dec[42], Dec[100]\nFunciones: toHex(), toBin()'
return None
@staticmethod
def PopupFunctionList():
"""Lista de métodos sugeridos para autocompletado de Dec"""
return [
("toHex", "Convierte a hexadecimal"),
("toBin", "Convierte a binario"),
]
def toHex(self):
"""Convierte a hexadecimal"""
return f"0x{self._value:02x}"
def toBin(self):
"""Convierte a binario"""
return f"0b{self._value:08b}"