Skip to content

Commit

Permalink
Expand string loading to fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Jan 4, 2025
1 parent ab006cf commit c4489c0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
19 changes: 16 additions & 3 deletions tests/tivars.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,18 @@ def test_real_list(self):
self.assertEqual(test_real_list.length, 3)
self.assertEqual(test_real_list.list(), test_list)
self.assertEqual(list(iter(test_real_list)), test_list)
self.assertEqual(str(test_real_list), "[-1, 2, 999]")
self.assertEqual(str(test_real_list), string := "[-1, 2, 999]")
self.assertEqual(f"{test_real_list:t}", "{~1,2,999}")

test_real_list.clear()
test_real_list.load_list(test_list)
self.assertEqual(list(test_real_list), test_list)

test_real_list.clear()
test_real_list.load_string(string)
self.assertEqual(list(test_real_list), test_list)


def test_complex_list(self):
test_comp_list = TIComplexList.open("tests/data/var/ComplexList.8xl")
self.assertEqual(test_comp_list, TIList.open("tests/data/var/ComplexList.8xl"))
Expand All @@ -379,13 +384,17 @@ def test_complex_list(self):
self.assertEqual(test_comp_list.length, 3)
self.assertEqual(test_comp_list.list(), test_list)
self.assertEqual(list(iter(test_comp_list)), test_list)
self.assertEqual(str(test_comp_list), "[1 + i, -3 + 2i, 4]")
self.assertEqual(str(test_comp_list), string := "[1 + i, -3 + 2i, 4]")
self.assertEqual(f"{test_comp_list:t}", "{1+[i],~3+2[i],4}")

test_comp_list.clear()
test_comp_list.load_list(test_list)
self.assertEqual(list(test_comp_list), test_list)

test_comp_list.clear()
test_comp_list.load_string(string)
self.assertEqual(list(test_comp_list), test_list)

def test_matrix(self):
test_matrix = TIMatrix.open("tests/data/var/Matrix_3x3_standard.8xm")

Expand All @@ -399,7 +408,7 @@ def test_matrix(self):
self.assertEqual(test_matrix.matrix(), test_array)
self.assertEqual(list(iter(test_matrix)), [entry for row in test_array for entry in row])

self.assertEqual(str(test_matrix), "[[0.5, -1, 2.6457513110646],"
self.assertEqual(str(test_matrix), string := "[[0.5, -1, 2.6457513110646],"
" [2.7386127875258, 0.5, 3.1415926535898],"
" [1, 99999999, 0]]")
self.assertEqual(f"{test_matrix:t}", "[[0.5,~1,2.6457513110646]"
Expand All @@ -410,6 +419,10 @@ def test_matrix(self):
test_matrix.load_matrix(test_array)
self.assertEqual(test_matrix.matrix(), test_array)

test_matrix.clear()
test_matrix.load_string(string)
self.assertEqual(test_matrix.matrix(), test_array)

def test_exact_matrix(self):
test_matrix = TIMatrix.open("tests/data/var/Matrix_2x2_exact.8xm")

Expand Down
4 changes: 2 additions & 2 deletions tivars/types/gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def equation(self) -> TIEquation:
return TIEquation(self.bytes()[:-self.calc_data_length] + self.bytes()[-self.calc_data_length + 1:])

@Loader[str]
def load_string(self, string: str, *, model: TIModel = None):
def load_string(self, string: str, *, model: TIModel = None, lang: str = None, mode: str = None):
equation = TIEquation()
equation.load_string(string, model=model)
equation.load_string(string, model=model, lang=lang, mode=mode)
self.load_equation(equation)

def coerce(self):
Expand Down
10 changes: 2 additions & 8 deletions tivars/types/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,10 @@ def list(self) -> list[_E]:

@Loader[str]
def load_string(self, string: str):
lst = []

for string in ''.join(string.strip("[]{}").split()).split(","):
entry = self._E()
entry.load_string(string)
lst.append(entry)

self.load_list(lst)
self.load_list([self._E(element) for element in "".join(string.strip("[]{}")).split(",")])

def coerce(self):
# This is a bit lengthy
match self.data[0] & 31:
case TIReal.type_id | TIUndefinedReal.type_id | TIRealFraction.type_id \
| TIRealRadical.type_id | TIRealPi.type_id | TIRealPiFraction.type_id:
Expand Down
14 changes: 2 additions & 12 deletions tivars/types/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,8 @@ def matrix(self) -> list[list[RealEntry]]:

@Loader[str]
def load_string(self, string: str):
matrix = []

for string in ''.join(string.split())[1:-1].replace("],[", "][").split("]["):
row = []
for item in string.replace("[", "").replace("]", "").split(","):
entry = RealEntry()
entry.load_string(item)
row.append(entry)

matrix.append(row.copy())

self.load_matrix(matrix)
self.load_matrix([[RealEntry(item) for item in row.replace("[", "").replace("]", "").split(",")]
for row in "".join(string.split())[1:-1].replace("],[", "][").split("][")])


__all__ = ["TIMatrix"]
19 changes: 17 additions & 2 deletions tivars/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from io import BytesIO
from sys import version_info
from typing import BinaryIO
from warnings import warn
from warnings import catch_warnings, simplefilter, warn

from .data import *
from .models import *
Expand Down Expand Up @@ -862,10 +862,25 @@ def load_string(self, string: str):
"""
Loads this entry from a string representation
If there is no dedicated handler for an entry type, all subclasses of the type will be considered.
:param string: The string to load
"""

raise NotImplementedError
with catch_warnings():
simplefilter("ignore")

for entry_type in self._type_ids.values():
if issubclass(entry_type, self.__class__):
try:
# Try out each possible string format
self.load_bytes(entry_type(string).bytes())
return

except Exception:
continue

raise ValueError(f"could not parse '{string}' as entry type")

def string(self) -> str:
"""
Expand Down

0 comments on commit c4489c0

Please sign in to comment.