Skip to content

Commit

Permalink
use with statements for open()
Browse files Browse the repository at this point in the history
  • Loading branch information
whoburg committed Jan 5, 2024
1 parent a564516 commit 56b3f52
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions gpkit/solution_array.py
Expand Up @@ -492,7 +492,8 @@ def diff(self, other, showvars=None, *,
if other[-4:] == ".pgz":
other = SolutionArray.decompress_file(other)
else:
other = pickle.load(open(other, "rb"))
with open(other, "rb") as f:
other = pickle.load(f)
svars, ovars = self["variables"], other["variables"]
lines = ["Solution Diff",
"=============",
Expand Down Expand Up @@ -567,8 +568,9 @@ def save(self, filename="solution.pkl",
>>> import pickle
>>> pickle.load(open("solution.pkl"))
"""
with SolSavingEnvironment(self, saveconstraints):
pickle.dump(self, open(filename, "wb"), **pickleargs)
with open(filename, "wb") as f:
with SolSavingEnvironment(self, saveconstraints):
pickle.dump(self, f, **pickleargs)

def save_compressed(self, filename="solution.pgz",
*, saveconstraints=True, **cpickleargs):
Expand Down

0 comments on commit 56b3f52

Please sign in to comment.