diff --git a/mace/python/tools/binary_codegen.py b/mace/python/tools/binary_codegen.py index 3c78e889b1c5f270fdf8f9853b2512b2e1562e5a..d04dee4b4b6512a7cb1f08b0c849341daac1de56 100644 --- a/mace/python/tools/binary_codegen.py +++ b/mace/python/tools/binary_codegen.py @@ -74,9 +74,8 @@ def tuning_param_codegen(binary_dirs, binary_dirs, binary_file_name, variable_name) if os.path.isfile(output_path): os.remove(output_path) - w_file = open(output_path, "w") - w_file.write(cpp_binary_source) - w_file.close() + with open(output_path, "w") as w_file: + w_file.write(cpp_binary_source) def parse_args(): diff --git a/mace/python/tools/encrypt_opencl_codegen.py b/mace/python/tools/encrypt_opencl_codegen.py index 6292b8349e60154824807d070fe0f4802092db07..1c227ab98fe6aed5ed021dc342db589e65e0c6b6 100644 --- a/mace/python/tools/encrypt_opencl_codegen.py +++ b/mace/python/tools/encrypt_opencl_codegen.py @@ -44,22 +44,22 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path): for file_name in os.listdir(cl_kernel_dir): file_path = os.path.join(cl_kernel_dir, file_name) if file_path[-2:] == ".h": - f = open(file_path, "r") - header_code += f.read() + with open(file_path, "r") as f: + header_code += f.read() encrypted_code_maps = {} for file_name in os.listdir(cl_kernel_dir): file_path = os.path.join(cl_kernel_dir, file_name) if file_path[-3:] == ".cl": - f = open(file_path, "r") - code_str = "" - for line in f.readlines(): - if "#include " in line: - code_str += header_code - else: - code_str += line - encrypted_code_arr = encrypt_code(code_str) - encrypted_code_maps[file_name[:-3]] = encrypted_code_arr + with open(file_path, "r") as f: + code_str = "" + for line in f.readlines(): + if "#include " in line: + code_str += header_code + else: + code_str += line + encrypted_code_arr = encrypt_code(code_str) + encrypted_code_maps[file_name[:-3]] = encrypted_code_arr env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0])) cpp_cl_encrypted_kernel = env.get_template( @@ -70,9 +70,8 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path): if os.path.isfile(output_path): os.remove(output_path) - w_file = open(output_path, "w") - w_file.write(cpp_cl_encrypted_kernel) - w_file.close() + with open(output_path, "w") as w_file: + w_file.write(cpp_cl_encrypted_kernel) print("Generate encrypted opencl source done!") diff --git a/mace/python/tools/opencl_codegen.py b/mace/python/tools/opencl_codegen.py index cfb12f744b0b25d84099b8b82a4eeab75f9ef2f0..c21e5b974df0ff767607398336090c385b3a7608 100644 --- a/mace/python/tools/opencl_codegen.py +++ b/mace/python/tools/opencl_codegen.py @@ -86,9 +86,8 @@ def opencl_codegen(output_path, platform_info_file_name) if os.path.isfile(output_path): os.remove(output_path) - w_file = open(output_path, "w") - w_file.write(cpp_cl_binary_source) - w_file.close() + with open(output_path, "w") as w_file: + w_file.write(cpp_cl_binary_source) def parse_args(): diff --git a/mace/python/tools/source_converter_lib.py b/mace/python/tools/source_converter_lib.py index 404e56b994460700d7c8b2a08782c11f859e236e..9d6bc8970d84b4c77303cce66798a4d3bb5bedb2 100644 --- a/mace/python/tools/source_converter_lib.py +++ b/mace/python/tools/source_converter_lib.py @@ -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: f.write(source) if not embed_model_data: - f = open(output_dir + model_tag + '.data', "wb") - f.write(bytearray(model_data)) - f.close() + with open(output_dir + model_tag + '.data', "wb") as f: + f.write(bytearray(model_data)) # generate op source files template_name = 'operator.jinja2'