I am solving a system of linear equations with simpy, this takes some time and I need to use the result several times for several different applications. For this reason I would like to store the results of the computation and load them when needed.
I developed this method:
@property
def solutions(self) -> list[Expr]:
directory = SYMPY_DATA_PATH
filename = os.path.join(directory, f'{self.__class__.__name__}.pkl')
if not os.path.exists(directory):
os.makedirs(directory)
if os.path.exists(filename):
with open(filename, 'rb') as f:
print(f"Loading {filename}")
solution_set = pickle.load(f)
else:
solution_set = together(linsolve(self.equations, self.pins))
print(f"Saving {filename}")
with open(filename, 'wb') as f:
pickle.dump(list(solution_set)[0], f)
return list(solution_set)[0]
def solution(self, pin) -> Expr:
return self.solutions[pin]
but I get the error:
TypeError: 'One' object is not subscriptable
Previously what I did was:
@property
def solutions(self) -> list[Expr]:
solution_set = together(linsolve(self.equations, self.pins))
return list(solution_set)[0]
and the code worked as I was expecting.