diff --git a/tivars/types/list.py b/tivars/types/list.py index a39675c..3e6c936 100644 --- a/tivars/types/list.py +++ b/tivars/types/list.py @@ -57,8 +57,12 @@ def __init__(self, init=None, *, def __format__(self, format_spec: str) -> str: match format_spec: - case "t": return f"{{{','.join(format(entry, 't') for entry in self.list())}}}" - case _: return f"[{', '.join(format(entry, format_spec) for entry in self.list())}]" + case "": + return "[" + ", ".join(format(entry, format_spec) for entry in self.list()) + "]" + case "t": + return "{" + ",".join(format(entry, 't') for entry in self.list()) + "}" + case _: + return super().__format__(format_spec) def __iter__(self) -> Iterator[_E]: return iter(self.list()) diff --git a/tivars/types/matrix.py b/tivars/types/matrix.py index 3911a1c..cd23040 100644 --- a/tivars/types/matrix.py +++ b/tivars/types/matrix.py @@ -38,14 +38,15 @@ def __init__(self, init=None, *, def __format__(self, format_spec: str) -> str: match format_spec: + case "": + inner_sep, outer_sep = ", ", ", " case "t": - return "[" + \ - ''.join(f"[{','.join(format(entry, 't') for entry in row)}]" for row in self.matrix()) \ - + "]" + inner_sep, outer_sep = ",", "" case _: - return "[" + \ - ', '.join(f"[{', '.join(format(entry, format_spec) for entry in row)}]" for row in self.matrix()) \ - + "]" + return super().__format__(format_spec) + + return "[" + outer_sep.join(f"[{inner_sep.join(format(entry, format_spec)for entry in row)}]" + for row in self.matrix()) + "]" def __iter__(self) -> Iterator[TIReal]: for row in self.matrix(): diff --git a/tivars/types/numeric.py b/tivars/types/numeric.py index 27bf806..8e1f002 100644 --- a/tivars/types/numeric.py +++ b/tivars/types/numeric.py @@ -118,9 +118,16 @@ def __float__(self) -> float: def __format__(self, format_spec: str) -> str: match format_spec: - case "": return self.string() - case "t": return self.string().replace("-", "~") - case _: return format(self.decimal(), format_spec) + case "": + return self.string() + case "t": + return self.string().replace("-", "~") + case _: + try: + return format(self.decimal(), format_spec) + + except (TypeError, ValueError): + return super().__format__(format_spec) def __int__(self) -> int: return self.int() @@ -287,9 +294,16 @@ def __complex__(self): def __format__(self, format_spec: str) -> str: match format_spec: - case "": return self.string() - case "t": return squash(replacer(self.string(), {"i": "[i]", "-": "~", "~ ": "- "})) - case _: return format(self.complex(), format_spec) + case "": + return self.string() + case "t": + return squash(replacer(self.string(), {"i": "[i]", "-": "~", "~ ": "- "})) + case _: + try: + return format(self.complex(), format_spec) + + except (TypeError, ValueError): + return super().__format__(format_spec) @Section(min_data_length) def data(self) -> bytearray: diff --git a/tivars/var.py b/tivars/var.py index f595f80..b33f1e9 100644 --- a/tivars/var.py +++ b/tivars/var.py @@ -226,6 +226,9 @@ def __eq__(self, other: 'TIEntry') -> bool: except AttributeError: return False + def __format__(self, format_spec: str) -> str: + raise TypeError(f"unsupported format string passed to {type(self)}.__format__") + def __iter__(self) -> Iterator: raise NotImplementedError