Improve library
This commit is contained in:
@@ -10,14 +10,14 @@ class Type(Enum):
|
||||
LIST = (list, lambda x: f"({', '.join([e.stringify() for e in x])})")
|
||||
PERCENT = (float, lambda x: f"{int(x * 100)}%")
|
||||
NOTE = (Note, lambda x: x.note.name)
|
||||
VOID = (type(None), lambda x: _failStringify(x))
|
||||
VOID = (type(None), lambda x: _failStringify(Type.VOID))
|
||||
|
||||
def stringify(self, element):
|
||||
return self.value[1](element)
|
||||
|
||||
|
||||
def _failStringify(obj):
|
||||
raise RuntimeException(None, f"Not able to interpret '{obj.type.name()}'")
|
||||
def _failStringify(t):
|
||||
raise RuntimeException(None, f"Not able to interpret {t.name}'")
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,17 +1,33 @@
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class Value:
|
||||
def __init__(self, objectType, value):
|
||||
self.value = value
|
||||
|
||||
if type(value) == objectType.value[0]:
|
||||
self.type = objectType
|
||||
|
||||
elif type(value) == Value:
|
||||
raise RuntimeError("Trying to pass object of 'Value' type as value of it")
|
||||
|
||||
else:
|
||||
raise RuntimeError(f"Invalid type '{objectType.name}' for value '{value}'")
|
||||
|
||||
def stringify(self):
|
||||
return self.type.stringify(self.value)
|
||||
|
||||
def decompose(self):
|
||||
if self.type != Type.LIST:
|
||||
raise RuntimeError(f"Method 'decompose' can be applied only for lists")
|
||||
|
||||
if len(self.value) == 1:
|
||||
return Value(self.value[0].type, self.value[0].value)
|
||||
|
||||
return self
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.type.name}({self.stringify()})"
|
||||
return f"{self.type.name}({self.value})"
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
Reference in New Issue
Block a user