提交 87f5f609 authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #3478 from piotrfrankowski/with_statement

Issue #3429 - refactor some code to python 'with' statement
......@@ -1214,22 +1214,17 @@ class FunctionMaker(object):
print('graph_db already exists')
else:
# create graph_db
f = open(graph_db_file, 'wb')
with open(graph_db_file, 'wb') as f:
print('create new graph_db in %s' % graph_db_file)
# file needs to be open and closed for every pickle
f.close()
# load the graph_db dictionary
try:
f = open(graph_db_file, 'rb')
with open(graph_db_file, 'rb') as f:
# Temporary hack to allow
# theano.scan_module.tests.test_scan.T_Scan to
# finish. Should be changed in definitive version.
tmp = theano.config.unpickle_function
theano.config.unpickle_function = False
graph_db = pickle.load(f)
# hack end
f.close()
print('graph_db loaded and it is not empty')
except EOFError as e:
# the file has nothing in it
......@@ -1358,9 +1353,8 @@ class FunctionMaker(object):
before_opt = self.fgraph.clone(check_integrity=False)
optimizer_profile = optimizer(self.fgraph)
graph_db.update({before_opt: self.fgraph})
f = open(graph_db_file, 'wb')
with open(graph_db_file, 'wb') as f:
pickle.dump(graph_db, f, -1)
f.close()
print('new graph saved into graph_db')
release_lock()
return optimizer_profile
......
......@@ -18,9 +18,8 @@ def test_function_dump():
tmpdir = tempfile.mkdtemp()
fname = os.path.join(tmpdir, 'test_function_dump.pkl')
theano.function_dump(fname, [v], v + 1)
f = open(fname, 'rb')
with open(fname, 'rb') as f:
l = pickle.load(f)
f.close()
finally:
if tmpdir is not None:
shutil.rmtree(tmpdir)
......
......@@ -86,9 +86,8 @@ def d3viz(fct, outfile, copy_deps=True, *args, **kwargs):
# Read template HTML file
template_file = os.path.join(__path__, 'html', 'template.html')
f = open(template_file)
with open(template_file) as f:
template = f.read()
f.close()
# Copy dependencies to output directory
src_deps = __path__
......
......@@ -10,18 +10,16 @@ class CallCache(object):
try:
if filename is None:
raise IOError('bad filename') # just goes to except
f = open(filename, 'r')
with open(filename, 'r') as f:
self.cache = pickle.load(f)
f.close()
except IOError:
self.cache = {}
def persist(self, filename=None):
if filename is None:
filename = self.filename
f = open(filename, 'w')
with open(filename, 'w') as f:
pickle.dump(self.cache, f)
f.close()
def call(self, fn, args=(), key=None):
if key is None:
......
......@@ -2143,7 +2143,7 @@ class GCC_compiler(Compiler):
lib_dirs = std_lib_dirs() + lib_dirs
cppfilename = os.path.join(location, 'mod.cpp')
cppfile = open(cppfilename, 'w')
with open(cppfilename, 'w') as cppfile:
_logger.debug('Writing module C++ code to %s', cppfilename)
......@@ -2151,7 +2151,6 @@ class GCC_compiler(Compiler):
# Avoid gcc warning "no newline at end of file".
if not src_code.endswith('\n'):
cppfile.write('\n')
cppfile.close()
lib_filename = os.path.join(
location,
......
......@@ -361,11 +361,9 @@ def print_compiledir_content():
total_key_sizes = 0
nb_keys = {}
for dir in os.listdir(compiledir):
file = None
try:
try:
filename = os.path.join(compiledir, dir, "key.pkl")
file = open(filename, 'rb')
with open(filename, 'rb') as file:
try:
keydata = pickle.load(file)
ops = list(set([x for x in flatten(keydata.keys)
if isinstance(x, theano.gof.Op)]))
......@@ -387,9 +385,6 @@ def print_compiledir_content():
nb_keys[len(keydata.keys)] += 1
except IOError:
pass
finally:
if file is not None:
file.close()
print("List of %d compiled individual ops in this theano cache %s:" % (
len(table), compiledir))
......
......@@ -339,9 +339,8 @@ def refresh_lock(lock_file):
''.join([str(random.randint(0, 9)) for i in range(10)]),
hostname)
try:
lock_write = open(lock_file, 'w')
with open(lock_file, 'w') as lock_write:
lock_write.write(unique_id + '\n')
lock_write.close()
except Exception:
# In some strange case, this happen. To prevent all tests
# from failing, we release the lock, but as there is a
......
......@@ -24,6 +24,7 @@ if __name__ == "__main__":
import pdb
pdb.set_trace()
if len(sys.argv) > 1:
print(filter_output(open(sys.argv[1])))
with open(sys.argv[1]) as f:
print(filter_output(f))
else:
print(filter_output(sys.stdin))
......@@ -23,9 +23,8 @@ for dir in dirs:
key = None
try:
f = open(os.path.join(dir, "key.pkl"))
with open(os.path.join(dir, "key.pkl")) as f:
key = f.read()
f.close()
keys.setdefault(key, 0)
keys[key] += 1
del f
......@@ -36,9 +35,8 @@ for dir in dirs:
path = os.path.join(dir, "mod.cpp")
if not os.path.exists(path):
path = os.path.join(dir, "mod.cu")
f = open(path)
with open(path) as f:
mod = f.read()
f.close()
mods.setdefault(mod, ())
mods[mod] += (key,)
del mod
......
......@@ -145,31 +145,27 @@ def get_file_contents(filename, revision="tip"):
def save_commit_message(filename):
commit_message = run_mercurial_command("tip --template '{desc}'")
save_file = open(filename, "w")
with open(filename, "w") as save_file:
save_file.write(commit_message)
save_file.close()
def save_diffs(diffs, filename):
diff = "\n\n".join(diffs)
diff_file = open(filename, "w")
with open(filename, "w") as diff_file:
diff_file.write(diff)
diff_file.close()
def should_skip_commit():
if not os.path.exists(SKIP_WHITESPACE_CHECK_FILENAME):
return False
whitespace_check_file = open(SKIP_WHITESPACE_CHECK_FILENAME, "r")
with open(SKIP_WHITESPACE_CHECK_FILENAME, "r") as whitespace_check_file:
whitespace_check_changeset = whitespace_check_file.read()
whitespace_check_file.close()
return whitespace_check_changeset == parent_commit()
def save_skip_next_commit():
whitespace_check_file = open(SKIP_WHITESPACE_CHECK_FILENAME, "w")
with open(SKIP_WHITESPACE_CHECK_FILENAME, "w") as whitespace_check_file:
whitespace_check_file.write(parent_commit())
whitespace_check_file.close()
def main(argv=None):
......
......@@ -132,9 +132,8 @@ def check(file):
shutil.copyfile(file, bak)
if verbose:
print("backed up", file, "to", bak)
f = open(file, "w")
with open(file, "w") as f:
r.write(f)
f.close()
if verbose:
print("wrote new", file)
return True
......
......@@ -233,12 +233,11 @@ class NVCC_compiler(Compiler):
lib_dirs.append(python_lib)
cppfilename = os.path.join(location, 'mod.cu')
cppfile = open(cppfilename, 'w')
with open(cppfilename, 'w') as cppfile:
_logger.debug('Writing module C++ code to %s', cppfilename)
cppfile.write(src_code)
cppfile.close()
lib_filename = os.path.join(location, '%s.%s' %
(module_name, get_lib_extension()))
......
......@@ -75,7 +75,8 @@ except ImportError:
)
raise ImportError("The file lazylinker_c.c is not available.")
code = open(cfile).read()
with open(cfile) as f:
code = f.read()
loc = os.path.join(config.compiledir, dirname)
if not os.path.exists(loc):
try:
......@@ -113,7 +114,8 @@ except ImportError:
hide_symbols=False)
# Save version into the __init__.py file.
init_py = os.path.join(loc, '__init__.py')
open(init_py, 'w').write('_version = %s\n' % version)
with open(init_py, 'w') as f:
f.write('_version = %s\n' % version)
# If we just compiled the module for the first time, then it was
# imported at the same time: we need to make sure we do not
# reload the now outdated __init__.pyc below.
......
......@@ -250,16 +250,10 @@ class T_Scan(unittest.TestCase):
tmpdir = mkdtemp()
os.chdir(tmpdir)
f_out = open('tmp_scan_test_pickle.pkl', 'wb')
try:
with open('tmp_scan_test_pickle.pkl', 'wb') as f_out:
pickle.dump(_my_f, f_out, protocol=-1)
finally:
f_out.close()
f_in = open('tmp_scan_test_pickle.pkl', 'rb')
try:
with open('tmp_scan_test_pickle.pkl', 'rb') as f_in:
my_f = pickle.load(f_in)
finally:
f_in.close()
finally:
# Get back to the original dir, and delete the temporary one.
os.chdir(origdir)
......
......@@ -3852,16 +3852,13 @@ class test_shapeoptimizer(unittest.TestCase):
# Due to incompatibilities between python 2 and 3 in the format
# of pickled numpy ndarray, we have to force an encoding
from theano.misc.pkl_utils import CompatUnpickler
pkl_file = open(pkl_filename, "rb")
try:
with open(pkl_filename, "rb") as pkl_file:
if PY3:
u = CompatUnpickler(pkl_file, encoding="latin1")
else:
u = CompatUnpickler(pkl_file)
fn_args = u.load()
theano.function(**fn_args)
finally:
pkl_file.close()
class test_assert(utt.InferShapeTester):
......
......@@ -265,7 +265,9 @@ def run(stdout, stderr, argv, theano_nose, batch_size, time_profile,
fields = ('Fields: computation time; nosetests sequential id;'
' test name; parent class (if any); outcome\n\n')
path_nosort = os.path.join(sav_dir, 'timeprof_nosort')
f_nosort = open(path_nosort, 'w')
# probably this part can be extracted for function with many args
with open(path_nosort, 'w') as f_nosort:
# begin of saving nosort
f_nosort.write('TIME-PROFILING OF THEANO\'S NOSETESTS'
' (by sequential id)\n\n' + stamp + fields)
f_nosort.flush()
......@@ -347,7 +349,7 @@ def run(stdout, stderr, argv, theano_nose, batch_size, time_profile,
# saving results to readable files
path_sort = os.path.join(sav_dir, 'timeprof_sort')
f_sort = open(path_sort, 'w')
with open(path_sort, 'w') as f_sort:
f_sort.write('TIME-PROFILING OF THEANO\'S NOSETESTS'
' (sorted by computation time)\n\n' + stamp + fields)
for i in xrange(len(prof_master_nosort)):
......@@ -356,8 +358,8 @@ def run(stdout, stderr, argv, theano_nose, batch_size, time_profile,
prof_master_sort[i][2] + prof_master_sort[i][3] +
"\n")
f_sort.write(s_sort)
f_nosort.close()
f_sort.close()
# end of saving nosort
if __name__ == '__main__':
sys.exit(main())
......@@ -249,7 +249,7 @@ def check_all_files(dir_path=theano.__path__[0], pattern='*.py'):
the "whitelist_flake8" in this file.
"""
f_txt = open('theano_filelist.txt', 'a')
with open('theano_filelist.txt', 'a') as f_txt:
for (dir, _, files) in os.walk(dir_path):
for f in files:
if fnmatch(f, pattern):
......@@ -259,7 +259,6 @@ def check_all_files(dir_path=theano.__path__[0], pattern='*.py'):
path = os.path.relpath(os.path.join(dir, f),
theano.__path__[0])
f_txt.write('"' + path + '",\n')
f_txt.close()
if __name__ == "__main__":
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论