Skip to content

Commit

Permalink
Improve format logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Jun 5, 2023
1 parent 9e42119 commit d26d3a7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
8 changes: 6 additions & 2 deletions tivars/types/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
13 changes: 7 additions & 6 deletions tivars/types/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
26 changes: 20 additions & 6 deletions tivars/types/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions tivars/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d26d3a7

Please sign in to comment.