Skip to content

Commit

Permalink
batch replace 'except:' with 'except Exception:' (#703)
Browse files Browse the repository at this point in the history
The latter will not catch `KeyboardInterrupt` and `SystemExit`. See https://stackoverflow.com/a/18982726/9567349.
  • Loading branch information
njzjz authored Apr 19, 2022
1 parent ddbcaa9 commit 80ad9c1
Show file tree
Hide file tree
Showing 25 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion dpgen/auto_test/Lammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def compute(self,
if len(line) and str(ii) == line[0]:
try:
[float(kk) for kk in line]
except:
except Exception:
continue
stress.append([])
virial.append([])
Expand Down
6 changes: 3 additions & 3 deletions dpgen/data/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def make_scale(jdata):
try:
pos_src = os.path.join(os.path.join(init_path, ii), 'CONTCAR')
assert(os.path.isfile(pos_src))
except:
except Exception:
raise RuntimeError("not file %s, vasp relaxation should be run before scale poscar")
scale_path = os.path.join(work_path, ii)
scale_path = os.path.join(scale_path, "scale-%.3f" % jj)
Expand Down Expand Up @@ -604,7 +604,7 @@ def make_scale_ABACUS(jdata):
try:
pos_src = os.path.join(os.path.join(init_path, ii), 'OUT.ABACUS/STRU_ION_D')
assert(os.path.isfile(pos_src))
except:
except Exception:
raise RuntimeError("not file %s, vasp relaxation should be run before scale poscar")
scale_path = os.path.join(work_path, ii)
scale_path = os.path.join(scale_path, "scale-%.3f" % jj)
Expand Down Expand Up @@ -1238,7 +1238,7 @@ def gen_init_bulk(args) :
jdata=loadfn(args.PARAM)
if args.MACHINE is not None:
mdata=loadfn(args.MACHINE)
except:
except Exception:
with open (args.PARAM, 'r') as fp :
jdata = json.load (fp)
if args.MACHINE is not None:
Expand Down
2 changes: 1 addition & 1 deletion dpgen/data/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def gen_init_reaction(args):
jdata = loadfn(args.PARAM)
if args.MACHINE is not None:
mdata = loadfn(args.MACHINE)
except:
except Exception:
with open(args.PARAM, 'r') as fp:
jdata = json.load(fp)
if args.MACHINE is not None:
Expand Down
4 changes: 2 additions & 2 deletions dpgen/data/surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def make_scale(jdata):
try:
pos_src = os.path.join(os.path.join(init_path, ii), 'CONTCAR')
assert(os.path.isfile(pos_src))
except:
except Exception:
raise RuntimeError("not file %s, vasp relaxation should be run before scale poscar")
scale_path = os.path.join(work_path, ii)
scale_path = os.path.join(scale_path, "scale-%.3f" % jj)
Expand Down Expand Up @@ -583,7 +583,7 @@ def gen_init_surf(args):
jdata=loadfn(args.PARAM)
if args.MACHINE is not None:
mdata=loadfn(args.MACHINE)
except:
except Exception:
with open (args.PARAM, 'r') as fp :
jdata = json.load (fp)
if args.MACHINE is not None:
Expand Down
2 changes: 1 addition & 1 deletion dpgen/data/tools/cessp2force_lin.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def Parser():
try:
name = str(item.split('=')[0])
number = int(item.split('=')[1])
except:
except Exception:
sys.stderr.write("\nERROR: Could not read the -c string\n")
sys.exit()
if number >= max_types:
Expand Down
2 changes: 1 addition & 1 deletion dpgen/database/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _parsing_vasp(paths,config_info_dict, id_prefix,iters=True):
iter_record.sort()
dlog.info("iter_record")
dlog.info(iter_record)
except:
except Exception:
pass
for path in paths:
try:
Expand Down
6 changes: 3 additions & 3 deletions dpgen/database/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self,symbols=None,functional="PBE",pp_file=None,pp_lists=None):
else:
try:
self.potcars=Potcar(symbols=symbols, functional=functional)
except:
except Exception:
warnings.warn ("""Inproperly configure of POTCAR !""")
self.potcars=None

Expand Down Expand Up @@ -80,7 +80,7 @@ def from_file(cls,filename):
try:
potcars=Potcar.from_file(filename)
return cls(pp_lists=potcars)
except:
except Exception:
with open(filename,'r') as f:
content=f.readlines()
functional=content[0].strip().split(':')[-1].strip()
Expand Down Expand Up @@ -179,7 +179,7 @@ def from_directory(input_dir, optional_files=None):
("POSCAR", Poscar), ("POTCAR", DPPotcar)]:
fullzpath = zpath(os.path.join(input_dir, fname))
sub_d[fname.lower()] = ftype.from_file(fullzpath)
except:
except Exception:
for fname, ftype in [("INCAR", Incar),
("POSCAR", Poscar), ("POTCAR", DPPotcar)]:
fullzpath = zpath(os.path.join(input_dir, fname))
Expand Down
8 changes: 4 additions & 4 deletions dpgen/dispatcher/ALI.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def delete_template(self):
response = self.client.do_action_with_exception(request)
flag = 1
break
except:
except Exception:
count += 1
# count = 10 and still failed, continue

Expand Down Expand Up @@ -382,7 +382,7 @@ def get_image_id(self, img_name):
return img["ImageId"]
flag = 1
break
except:
except Exception:
count += 1
time.sleep(10)
if not flag:
Expand Down Expand Up @@ -441,7 +441,7 @@ def change_apg_capasity(self, capasity):
response = self.client.do_action_with_exception(request)
flag = 1
break
except:
except Exception:
count += 1
time.sleep(10)
if not flag:
Expand Down Expand Up @@ -510,5 +510,5 @@ def get_ip(self, instance_list):
else:
ip_list.append(response["Instances"]["Instance"][0]["VpcAttributes"]["PrivateIpAddress"]['IpAddress'][0])
return ip_list
except: return []
except Exception: return []

2 changes: 1 addition & 1 deletion dpgen/dispatcher/Dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def make_dispatcher(mdata, mdata_resource=None, work_path=None, run_tasks=None,
context_type = 'local'
try:
batch_type = mdata['batch']
except:
except Exception:
dlog.info('cannot find key "batch" in machine file, try to use deprecated key "machine_type"')
batch_type = mdata['machine_type']
lazy_local = (mdata.get('lazy-local', False)) or (mdata.get('lazy_local', False))
Expand Down
4 changes: 2 additions & 2 deletions dpgen/dispatcher/DispatcherList.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def make_dispatcher(self, ii):
self.dispatcher_list[ii]["dispatcher_status"] = "unsubmitted"
flag = 1
break
except:
except Exception:
count += 1
time.sleep(60)
if not flag:
Expand All @@ -201,7 +201,7 @@ def check_dispatcher_status(self, ii, allow_failue=False):
finished = self.dispatcher_list[ii]["dispatcher"].all_finished(self.dispatcher_list[ii]["entity"].job_handler, allow_failue, clean)
if finished:
self.dispatcher_list[ii]["dispatcher_status"] = "finished"
except:
except Exception:
pass
elif status == 1:
# self.dispatcher_list[ii]["dispatcher_status"] = "terminated"
Expand Down
2 changes: 1 addition & 1 deletion dpgen/dispatcher/LSF.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LSF(Batch) :
def check_status(self):
try:
job_id = self._get_job_id()
except:
except Exception:
return JobStatus.terminated
if job_id == "" :
raise RuntimeError("job %s has not been submitted" % self.context.remote_root)
Expand Down
2 changes: 1 addition & 1 deletion dpgen/dispatcher/SSHContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__ (self,
self.ssh_session.ensure_alive()
try:
self.sftp.mkdir(self.remote_root)
except:
except Exception:
pass

@property
Expand Down
4 changes: 2 additions & 2 deletions dpgen/dispatcher/Shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def sub_script_cmd(self,
fp_max_errors = 3
try:
fp_max_errors = res['fp_max_errors']
except:
except Exception:
pass
except:
except Exception:
cvasp=False

_cmd = cmd.split('1>')[0].strip()
Expand Down
4 changes: 2 additions & 2 deletions dpgen/dispatcher/Slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def sub_script_cmd(self,
fp_max_errors = 3
try:
fp_max_errors = res['fp_max_errors']
except:
except Exception:
pass
except:
except Exception:
cvasp=False

_cmd = cmd.split('1>')[0].strip()
Expand Down
8 changes: 4 additions & 4 deletions dpgen/generator/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def run_train (iter_index,

try:
train_group_size = mdata['train_group_size']
except:
except Exception:
train_group_size = 1

api_version = mdata.get('api_version', '0.9')
Expand Down Expand Up @@ -2530,11 +2530,11 @@ def post_fp_vasp (iter_index,
for oo in sys_outcars :
try:
_sys = dpdata.LabeledSystem(oo, type_map = jdata['type_map'])
except:
except Exception:
dlog.info('Try to parse from vasprun.xml')
try:
_sys = dpdata.LabeledSystem(oo.replace('OUTCAR','vasprun.xml'), type_map = jdata['type_map'])
except:
except Exception:
_sys = dpdata.LabeledSystem()
dlog.info('Failed fp path: %s'%oo.replace('OUTCAR',''))
if len(_sys) == 1:
Expand Down Expand Up @@ -2914,7 +2914,7 @@ def run_iter (param_file, machine_file) :
warnings.simplefilter('ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning)
jdata=loadfn(param_file)
mdata=loadfn(machine_file)
except:
except Exception:
with open (param_file, 'r') as fp :
jdata = json.load (fp)
with open (machine_file, 'r') as fp:
Expand Down
14 changes: 7 additions & 7 deletions dpgen/remote/RemoteJob.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def download(self,
elif os.path.isdir(os.path.join(self.remote_root,job_dir,ii)):
try:
os.rmdir(os.path.join(self.local_root,job_dir,ii))
except:
except Exception:
print('dir is not empty '+str(os.path.join(self.local_root,job_dir,ii)))
else:
shutil.copytree(os.path.join(self.remote_root,job_dir,ii),os.path.join(self.local_root,job_dir,ii))
Expand Down Expand Up @@ -176,7 +176,7 @@ def __init__ (self,
sftp = self.ssh.open_sftp()
sftp.mkdir(self.remote_root)
sftp.close()
except:
except Exception:
pass
# open('job_uuid', 'w').write(self.job_uuid)

Expand Down Expand Up @@ -410,7 +410,7 @@ def _submit():
else:
dlog.debug('task is finished')

except:
except Exception:
dlog.debug('no job_id file')
dlog.debug('task restart point !!!')
_submit()
Expand Down Expand Up @@ -529,9 +529,9 @@ def _make_script(self,
cvasp=res['cvasp']
try:
fp_max_errors = res['fp_max_errors']
except:
except Exception:
fp_max_errors = 3
except:
except Exception:
cvasp=False

for ii,jj in zip(job_dirs, args) :
Expand Down Expand Up @@ -765,7 +765,7 @@ def submit(self,
dlog.debug('task is running')
else:
dlog.debug('task is finished')
#except:
#except Exception:
#dlog.debug('no job_id file')
#dlog.debug('task restart point !!!')
#self._submit(job_dirs, cmd, args, resources)
Expand Down Expand Up @@ -805,7 +805,7 @@ def check_limit(self, task_max):
def check_status(self) :
try:
job_id = self._get_job_id()
except:
except Exception:
return JobStatus.terminated
if job_id == "" :
raise RuntimeError("job %s is has not been submitted" % self.remote_root)
Expand Down
6 changes: 3 additions & 3 deletions dpgen/remote/decide_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def convert_mdata(mdata, task_types=["train", "model_devi", "fp"]):
# if 'command' in profile:
# mdata['train_command'] = profile["command"]
# continue_flag = True
# except:
# except Exception:
# pass
# if ("hostname" not in mdata["train"][0]["machine"]) or (len(mdata["train"]) == 1):
# mdata["train_machine"] = mdata["train"][0]["machine"]
Expand Down Expand Up @@ -180,7 +180,7 @@ def convert_mdata(mdata, task_types=["train", "model_devi", "fp"]):
# mdata['model_devi_command'] = profile['command']
# mdata['model_devi_group_size'] = profile['group_size']
# continue_flag = True
# except:
# except Exception:
# pass
# if ("hostname" not in mdata["model_devi"][0]["machine"]) or (len(mdata["model_devi"]) == 1):
# mdata["model_devi_machine"] = mdata["model_devi"][0]["machine"]
Expand Down Expand Up @@ -263,7 +263,7 @@ def convert_mdata(mdata, task_types=["train", "model_devi", "fp"]):
# mdata['fp_group_size'] = profile['group_size']
#
# continue_flag = True
# except:
# except Exception:
# pass
# if ("hostname" not in mdata["fp"][0]["machine"]) or (len(mdata["fp"]) == 1):
# mdata["fp_machine"] = mdata["fp"][0]["machine"]
Expand Down
4 changes: 2 additions & 2 deletions dpgen/remote/group_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def group_slurm_jobs(ssh_sess,
if not job_fin[idx] :
try:
status = rjob.check_status()
except:
except Exception:
ssh_sess = SSHSession(ssh_sess.remote_profile)
for _idx,_rjob in enumerate(job_list):
job_list[_idx] = SlurmJob(ssh_sess, work_path, _rjob.job_uuid)
Expand Down Expand Up @@ -426,5 +426,5 @@ def delete(self):
f_path_map=self.f_path_map
try:
os.remove(f_path_map)
except:
except Exception:
pass
4 changes: 2 additions & 2 deletions dpgen/simplify/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def run_model_devi(iter_index, jdata, mdata):
# submit
try:
model_devi_group_size = mdata['model_devi_group_size']
except:
except Exception:
model_devi_group_size = 1

forward_files = [rest_data_name]
Expand Down Expand Up @@ -599,7 +599,7 @@ def run_iter(param_file, machine_file):
'ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning)
jdata = loadfn(param_file)
mdata = loadfn(machine_file)
except:
except Exception:
with open(param_file, 'r') as fp:
jdata = json.load(fp)
with open(machine_file, 'r') as fp:
Expand Down
4 changes: 2 additions & 2 deletions dpgen/tools/relabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def create_init_tasks(target_folder, param_file, output, fp_json, verbose = True
try:
fp_params = fp_jdata['user_fp_params']
user_input = True
except:
except Exception:
fp_params = fp_jdata['fp_params']
user_input = False
make_pwscf('.', fp_params, mass_map, fp_pp_files, fp_pp_files, user_input)
Expand Down Expand Up @@ -240,7 +240,7 @@ def create_tasks(target_folder, param_file, output, fp_json, verbose = True, num
try:
fp_params = fp_jdata['user_fp_params']
user_input = True
except:
except Exception:
fp_params = fp_jdata['fp_params']
user_input = False
make_pwscf('.', fp_params, mass_map, fp_pp_files, fp_pp_files, user_input)
Expand Down
4 changes: 2 additions & 2 deletions dpgen/tools/stat_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def stat_iter(target_folder,
pk_id, task_dirname, OUTCAR_filename=path_doc.rsplit('/', 2) # pylint: disable=unused-variable
try:
_sys = dpdata.LabeledSystem(path_doc, type_map = jdata['type_map'] )
except:
except Exception:
try:
_sys = dpdata.LabeledSystem(path_doc.replace('OUTCAR','vasprun.xml'), type_map = jdata['type_map'])
except:
except Exception:
_sys = dpdata.LabeledSystem()
if len(_sys) == 1:
pass
Expand Down
Loading

0 comments on commit 80ad9c1

Please sign in to comment.