提交 87cb0880 编写于 作者: Y yejianwu

use 'with open(...) as f' in python

上级 92686bae
...@@ -74,9 +74,8 @@ def tuning_param_codegen(binary_dirs, ...@@ -74,9 +74,8 @@ def tuning_param_codegen(binary_dirs,
binary_dirs, binary_file_name, variable_name) binary_dirs, binary_file_name, variable_name)
if os.path.isfile(output_path): if os.path.isfile(output_path):
os.remove(output_path) os.remove(output_path)
w_file = open(output_path, "w") with open(output_path, "w") as w_file:
w_file.write(cpp_binary_source) w_file.write(cpp_binary_source)
w_file.close()
def parse_args(): def parse_args():
......
...@@ -44,22 +44,22 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path): ...@@ -44,22 +44,22 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path):
for file_name in os.listdir(cl_kernel_dir): for file_name in os.listdir(cl_kernel_dir):
file_path = os.path.join(cl_kernel_dir, file_name) file_path = os.path.join(cl_kernel_dir, file_name)
if file_path[-2:] == ".h": if file_path[-2:] == ".h":
f = open(file_path, "r") with open(file_path, "r") as f:
header_code += f.read() header_code += f.read()
encrypted_code_maps = {} encrypted_code_maps = {}
for file_name in os.listdir(cl_kernel_dir): for file_name in os.listdir(cl_kernel_dir):
file_path = os.path.join(cl_kernel_dir, file_name) file_path = os.path.join(cl_kernel_dir, file_name)
if file_path[-3:] == ".cl": if file_path[-3:] == ".cl":
f = open(file_path, "r") with open(file_path, "r") as f:
code_str = "" code_str = ""
for line in f.readlines(): for line in f.readlines():
if "#include <common.h>" in line: if "#include <common.h>" in line:
code_str += header_code code_str += header_code
else: else:
code_str += line code_str += line
encrypted_code_arr = encrypt_code(code_str) encrypted_code_arr = encrypt_code(code_str)
encrypted_code_maps[file_name[:-3]] = encrypted_code_arr encrypted_code_maps[file_name[:-3]] = encrypted_code_arr
env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0])) env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0]))
cpp_cl_encrypted_kernel = env.get_template( cpp_cl_encrypted_kernel = env.get_template(
...@@ -70,9 +70,8 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path): ...@@ -70,9 +70,8 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path):
if os.path.isfile(output_path): if os.path.isfile(output_path):
os.remove(output_path) os.remove(output_path)
w_file = open(output_path, "w") with open(output_path, "w") as w_file:
w_file.write(cpp_cl_encrypted_kernel) w_file.write(cpp_cl_encrypted_kernel)
w_file.close()
print("Generate encrypted opencl source done!") print("Generate encrypted opencl source done!")
......
...@@ -86,9 +86,8 @@ def opencl_codegen(output_path, ...@@ -86,9 +86,8 @@ def opencl_codegen(output_path,
platform_info_file_name) platform_info_file_name)
if os.path.isfile(output_path): if os.path.isfile(output_path):
os.remove(output_path) os.remove(output_path)
w_file = open(output_path, "w") with open(output_path, "w") as w_file:
w_file.write(cpp_cl_binary_source) w_file.write(cpp_cl_binary_source)
w_file.close()
def parse_args(): def parse_args():
......
...@@ -173,9 +173,8 @@ def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate, ...@@ -173,9 +173,8 @@ def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate,
with open(output_dir + 'tensor_data' + '.cc', "wb") as f: with open(output_dir + 'tensor_data' + '.cc', "wb") as f:
f.write(source) f.write(source)
if not embed_model_data: if not embed_model_data:
f = open(output_dir + model_tag + '.data', "wb") with open(output_dir + model_tag + '.data', "wb") as f:
f.write(bytearray(model_data)) f.write(bytearray(model_data))
f.close()
# generate op source files # generate op source files
template_name = 'operator.jinja2' template_name = 'operator.jinja2'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册