Skip to content

Commit d26d3a7

Browse files
committed
Improve format logic
1 parent 9e42119 commit d26d3a7

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

tivars/types/list.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ def __init__(self, init=None, *,
5757

5858
def __format__(self, format_spec: str) -> str:
5959
match format_spec:
60-
case "t": return f"{{{','.join(format(entry, 't') for entry in self.list())}}}"
61-
case _: return f"[{', '.join(format(entry, format_spec) for entry in self.list())}]"
60+
case "":
61+
return "[" + ", ".join(format(entry, format_spec) for entry in self.list()) + "]"
62+
case "t":
63+
return "{" + ",".join(format(entry, 't') for entry in self.list()) + "}"
64+
case _:
65+
return super().__format__(format_spec)
6266

6367
def __iter__(self) -> Iterator[_E]:
6468
return iter(self.list())

tivars/types/matrix.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ def __init__(self, init=None, *,
3838

3939
def __format__(self, format_spec: str) -> str:
4040
match format_spec:
41+
case "":
42+
inner_sep, outer_sep = ", ", ", "
4143
case "t":
42-
return "[" + \
43-
''.join(f"[{','.join(format(entry, 't') for entry in row)}]" for row in self.matrix()) \
44-
+ "]"
44+
inner_sep, outer_sep = ",", ""
4545
case _:
46-
return "[" + \
47-
', '.join(f"[{', '.join(format(entry, format_spec) for entry in row)}]" for row in self.matrix()) \
48-
+ "]"
46+
return super().__format__(format_spec)
47+
48+
return "[" + outer_sep.join(f"[{inner_sep.join(format(entry, format_spec)for entry in row)}]"
49+
for row in self.matrix()) + "]"
4950

5051
def __iter__(self) -> Iterator[TIReal]:
5152
for row in self.matrix():

tivars/types/numeric.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ def __float__(self) -> float:
118118

119119
def __format__(self, format_spec: str) -> str:
120120
match format_spec:
121-
case "": return self.string()
122-
case "t": return self.string().replace("-", "~")
123-
case _: return format(self.decimal(), format_spec)
121+
case "":
122+
return self.string()
123+
case "t":
124+
return self.string().replace("-", "~")
125+
case _:
126+
try:
127+
return format(self.decimal(), format_spec)
128+
129+
except (TypeError, ValueError):
130+
return super().__format__(format_spec)
124131

125132
def __int__(self) -> int:
126133
return self.int()
@@ -287,9 +294,16 @@ def __complex__(self):
287294

288295
def __format__(self, format_spec: str) -> str:
289296
match format_spec:
290-
case "": return self.string()
291-
case "t": return squash(replacer(self.string(), {"i": "[i]", "-": "~", "~ ": "- "}))
292-
case _: return format(self.complex(), format_spec)
297+
case "":
298+
return self.string()
299+
case "t":
300+
return squash(replacer(self.string(), {"i": "[i]", "-": "~", "~ ": "- "}))
301+
case _:
302+
try:
303+
return format(self.complex(), format_spec)
304+
305+
except (TypeError, ValueError):
306+
return super().__format__(format_spec)
293307

294308
@Section(min_data_length)
295309
def data(self) -> bytearray:

tivars/var.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ def __eq__(self, other: 'TIEntry') -> bool:
226226
except AttributeError:
227227
return False
228228

229+
def __format__(self, format_spec: str) -> str:
230+
raise TypeError(f"unsupported format string passed to {type(self)}.__format__")
231+
229232
def __iter__(self) -> Iterator:
230233
raise NotImplementedError
231234

0 commit comments

Comments
 (0)