Spaces:
Running
on
L40S
Running
on
L40S
File size: 1,286 Bytes
bfed184 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# -*- coding: utf-8 -*-
import json
import logging
import argparse
def log_info(s, use_log=True):
if use_log:
logging.info(s)
else:
print(s)
def write_str_to_txt(file_path, str, mode='a'):
with open(file_path, mode) as f:
f.write(str)
def write_namespace_to_txt(file_path, json_str, indent=4):
with open(file_path, 'a') as f:
f.write(json.dumps(vars(json_str), indent=indent))
f.write('\n')
def read_txt_to_str(file_path):
with open(file_path, 'r') as f:
info_list = f.read().splitlines()
return info_list
def read_txt_to_namespace(file_path):
with open(file_path, 'r') as f:
json_str = json.load(f)
args = argparse.Namespace(**json_str)
if type(args.loss_choice) is str:
# 向旧版本cfg兼容
args.loss_choice=[args.loss_choice]
args.loss_lambda=[1]
args.loss_return_dict=False
return args
def replace_txt_str(txt_path, old_str, new_str):
file_data = ''
with open(txt_path, 'r') as f:
for idx, line in enumerate(f):
if old_str in line:
line = line.replace(old_str, new_str)
file_data += line
with open(txt_path, 'w') as f:
f.write(file_data)
|