Skip to content

Commit

Permalink
Close files after opening them
Browse files Browse the repository at this point in the history
Using open() as context manager is the preferred way to close files to
guarantee exception safety. This commit solves an occasional
ResourceWarning, when CylP is used in unit tests run by pytest. There
are more direct calls of open() without context manager in setup.py.
  • Loading branch information
lumbric authored and tkralphs committed Feb 11, 2021
1 parent 9e776ed commit b92ba0a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cylp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from os.path import realpath, join
currentDir = os.path.dirname(realpath(__file__))
__version__ = open(join(currentDir, 'VERSION')).read().strip()
with open(join(currentDir, 'VERSION')) as f:
__version__ = f.read().strip()
7 changes: 3 additions & 4 deletions cylp/py/QP/QP.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,15 +1259,14 @@ def Wolfe(self, method='w'):
qobj = 0.5 * x.T * G * x + np.dot(c, x) - self.objectiveOffset

print(s.iteration)
f = open('qpout', 'a')
st = '%s %s %s %s %s %s %s\n' % (self.filename.ljust(30), method.ljust(2),
with open('qpout', 'a') as f:
st = '%s %s %s %s %s %s %s\n' % (self.filename.ljust(30), method.ljust(2),
str(round(s.objectiveValue, 5)).ljust(8),
str(round(qobj, 5)).ljust(8),
str(timeToMake),
str(timeToSolve),
str(timeToMake + timeToSolve))
f.write(st)
f.close()
f.write(st)

# print(checkComp(s.primalVariableSolution['k1'],
# s.primalVariableSolution['zk1']))
Expand Down

0 comments on commit b92ba0a

Please sign in to comment.