Skip to content

Commit

Permalink
Allow decimal format params on t
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Nov 17, 2024
1 parent 53d2500 commit 8e331bb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
7 changes: 4 additions & 3 deletions tivars/types/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ def make_imag(entry: 'RealEntry') -> str:
case _:
return replacer(f"{self.real:{format_spec}} + " + make_imag(self.imag), {"+ -": "- ", " 1i": " i"})

case "t":
return squash(replacer(self.string(), {"i": "[i]", "-": "~"}))

case _:
if format_spec.endswith("t"):
spec = "" if format_spec == "t" else format_spec[:-1] + "f"
return squash(replacer(format(self, spec), {"i": "[i]", "-": "~"}))

try:
return format(self.complex(), format_spec)

Expand Down
10 changes: 5 additions & 5 deletions tivars/types/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ def __init__(self, init=None, *,
super().__init__(init, for_flash=for_flash, name=name, version=version, archived=archived, data=data)

def __format__(self, format_spec: str) -> str:
match format_spec:
case "t":
return "{" + ",".join(format(entry, "t") for entry in self.list()) + "}"
case _:
return "[" + ", ".join(format(entry, format_spec) for entry in self.list()) + "]"
if format_spec.endswith("t"):
return "{" + ",".join(format(entry, format_spec) for entry in self.list()) + "}"

else:
return "[" + ", ".join(format(entry, format_spec) for entry in self.list()) + "]"

def __iter__(self) -> Iterator[_E]:
return iter(self.list())
Expand Down
10 changes: 5 additions & 5 deletions tivars/types/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def __init__(self, init=None, *,
super().__init__(init, for_flash=for_flash, name=name, version=version, archived=archived, data=data)

def __format__(self, format_spec: str) -> str:
match format_spec:
case "t":
inner_sep, outer_sep = ",", ""
case _:
inner_sep, outer_sep = ", ", ", "
if format_spec.endswith("t"):
inner_sep, outer_sep = ",", ""

else:
inner_sep, outer_sep = ", ", ", "

return "[" + outer_sep.join(f"[{inner_sep.join(format(entry, format_spec) for entry in row)}]"
for row in self.matrix()) + "]"
Expand Down
7 changes: 5 additions & 2 deletions tivars/types/real.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ def __format__(self, format_spec: str) -> str:

case "j":
return str(self.json_number())
case "t":
return squash(re.sub(r"(√\d*)", r"sqrt(\1)", replacer(self.string(), {"-": "~", "/": "n/d"})))

case _:
if format_spec.endswith("t"):
spec = "" if format_spec == "t" else format_spec[:-1] + "f"
return squash(re.sub(r"(√\d*)", r"sqrt(\1)", replacer(format(self, spec), {"-": "~", "/": "n/d"})))

try:
return format(self.decimal(), format_spec)

Expand Down

0 comments on commit 8e331bb

Please sign in to comment.