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