Skip to content

Commit 7658dd6

Browse files
calvingileslarsmans
authored andcommitted
Changed f=open() to with open() as f to eliminate ResourceWarnings. Fixes scikit-learn#3410.
1 parent 46a1250 commit 7658dd6

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
DISTNAME = 'scikit-learn'
2525
DESCRIPTION = 'A set of python modules for machine learning and data mining'
26-
LONG_DESCRIPTION = open('README.rst').read()
26+
with open('README.rst') as f:
27+
LONG_DESCRIPTION = f.read()
2728
MAINTAINER = 'Andreas Mueller'
2829
MAINTAINER_EMAIL = '[email protected]'
2930
URL = 'http://scikit-learn.org'

sklearn/datasets/base.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ def load_files(container_path, description=None, categories=None,
199199
target = target[indices]
200200

201201
if load_content:
202-
data = [open(filename, 'rb').read() for filename in filenames]
202+
data = []
203+
for filename in filenames:
204+
with open(filename, 'rb') as f:
205+
data.append(f.read())
203206
if encoding is not None:
204207
data = [d.decode(encoding, decode_error) for d in data]
205208
return Bunch(data=data,
@@ -317,7 +320,8 @@ def load_digits(n_class=10):
317320
module_path = dirname(__file__)
318321
data = np.loadtxt(join(module_path, 'data', 'digits.csv.gz'),
319322
delimiter=',')
320-
descr = open(join(module_path, 'descr', 'digits.rst')).read()
323+
with open(join(module_path, 'descr', 'digits.rst')) as f:
324+
descr = f.read()
321325
target = data[:, -1]
322326
flat_data = data[:, :-1]
323327
images = flat_data.view()
@@ -418,26 +422,31 @@ def load_boston():
418422
(506, 13)
419423
"""
420424
module_path = dirname(__file__)
421-
data_file = csv.reader(open(join(module_path, 'data',
422-
'boston_house_prices.csv')))
423-
fdescr = open(join(module_path, 'descr', 'boston_house_prices.rst'))
424-
temp = next(data_file)
425-
n_samples = int(temp[0])
426-
n_features = int(temp[1])
427-
data = np.empty((n_samples, n_features))
428-
target = np.empty((n_samples,))
429-
temp = next(data_file) # names of features
430-
feature_names = np.array(temp)
431-
432-
for i, d in enumerate(data_file):
433-
data[i] = np.asarray(d[:-1], dtype=np.float)
434-
target[i] = np.asarray(d[-1], dtype=np.float)
425+
426+
fdescr_name = join(module_path, 'descr', 'boston_house_prices.rst')
427+
with open(fdescr_name) as f:
428+
descr_text = f.read()
429+
430+
data_file_name = join(module_path, 'data', 'boston_house_prices.csv')
431+
with open(data_file_name) as f:
432+
data_file = csv.reader(f)
433+
temp = next(data_file)
434+
n_samples = int(temp[0])
435+
n_features = int(temp[1])
436+
data = np.empty((n_samples, n_features))
437+
target = np.empty((n_samples,))
438+
temp = next(data_file) # names of features
439+
feature_names = np.array(temp)
440+
441+
for i, d in enumerate(data_file):
442+
data[i] = np.asarray(d[:-1], dtype=np.float)
443+
target[i] = np.asarray(d[-1], dtype=np.float)
435444

436445
return Bunch(data=data,
437446
target=target,
438447
# last column is target value
439448
feature_names=feature_names[:-1],
440-
DESCR=fdescr.read())
449+
DESCR=descr_text)
441450

442451

443452
def load_sample_images():

sklearn/datasets/tests/test_svmlight_format.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,19 @@ def test_load_compressed():
113113

114114
with NamedTemporaryFile(prefix="sklearn-test", suffix=".gz") as tmp:
115115
tmp.close() # necessary under windows
116-
shutil.copyfileobj(open(datafile, "rb"), gzip.open(tmp.name, "wb"))
116+
with open(datafile, "rb") as f:
117+
shutil.copyfileobj(f, gzip.open(tmp.name, "wb"))
117118
Xgz, ygz = load_svmlight_file(tmp.name)
118-
assert_array_equal(X.toarray(), Xgz.toarray())
119-
assert_array_equal(y, ygz)
119+
assert_array_equal(X.toarray(), Xgz.toarray())
120+
assert_array_equal(y, ygz)
120121

121122
with NamedTemporaryFile(prefix="sklearn-test", suffix=".bz2") as tmp:
122123
tmp.close() # necessary under windows
123-
shutil.copyfileobj(open(datafile, "rb"), BZ2File(tmp.name, "wb"))
124+
with open(datafile, "rb") as f:
125+
shutil.copyfileobj(f, BZ2File(tmp.name, "wb"))
124126
Xbz, ybz = load_svmlight_file(tmp.name)
125-
assert_array_equal(X.toarray(), Xbz.toarray())
126-
assert_array_equal(y, ybz)
127+
assert_array_equal(X.toarray(), Xbz.toarray())
128+
assert_array_equal(y, ybz)
127129

128130

129131
@raises(ValueError)

sklearn/datasets/twenty_newsgroups.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ def fetch_20newsgroups(data_home=None, subset='train', categories=None,
201201
cache = None
202202
if os.path.exists(cache_path):
203203
try:
204-
compressed_content = open(cache_path, 'rb').read()
204+
with open(cache_path, 'rb') as f:
205+
compressed_content = f.read()
205206
uncompressed_content = codecs.decode(
206207
compressed_content, 'zlib_codec')
207208
cache = pickle.loads(uncompressed_content)

sklearn/tests/test_common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ def test_configure():
952952
# Blas/Atlas development headers
953953
warnings.simplefilter('ignore', UserWarning)
954954
if PY3:
955-
exec(open('setup.py').read(), dict(__name__='__main__'))
955+
with open('setup.py') as f:
956+
exec(f.read(), dict(__name__='__main__'))
956957
else:
957958
execfile('setup.py', dict(__name__='__main__'))
958959
finally:

0 commit comments

Comments
 (0)