From ebf053ee4c4f0abac9b7e9d941070e9e6c7b3daa Mon Sep 17 00:00:00 2001 From: Sean Kavanagh Date: Thu, 10 Aug 2023 09:00:14 +0100 Subject: [PATCH] Remove `with` from `_read` method --- easyunfold/procar.py | 59 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/easyunfold/procar.py b/easyunfold/procar.py index 6126bc9..192302b 100644 --- a/easyunfold/procar.py +++ b/easyunfold/procar.py @@ -60,37 +60,36 @@ def _read(self, fobj, parsed_kpoints=None): tot_count = 0 # count the instances of lines starting with "tot" -> (4 + 1) * nbands * nkpts for SOC calcs fobj.seek(0) - with fobj: - line = fobj.readline() - while line: - if line.startswith(' k-point'): - line = re.sub(r'(\d)-', r'\1 -', line) - tokens = line.strip().split() - kvec = tuple(round(float(val), 5) for val in # tuple to make it hashable - tokens[-6:-3]) # round to 5 decimal places to ensure proper kpoint matching - if kvec not in parsed_kpoints: - parsed_kpoints.add(kvec) - kvecs.append(list(kvec)) - kweights.append(float(tokens[-1])) - else: - # skip ahead to the next instance of two blank lines in a row - while line.strip() or fobj.readline().strip(): - line = fobj.readline() - continue - - elif not re.search(r'[a-zA-Z]', line) and line.strip() and len(line.strip().split()) - 2 == len(self.proj_names): - # only parse data if line is expected length, in case of LORBIT >= 12 - proj_data.append([float(token) for token in line.strip().split()[1:-1]]) + line = fobj.readline() + while line: + if line.startswith(' k-point'): + line = re.sub(r'(\d)-', r'\1 -', line) + tokens = line.strip().split() + kvec = tuple(round(float(val), 5) for val in # tuple to make it hashable + tokens[-6:-3]) # round to 5 decimal places to ensure proper kpoint matching + if kvec not in parsed_kpoints: + parsed_kpoints.add(kvec) + kvecs.append(list(kvec)) + kweights.append(float(tokens[-1])) + else: + # skip ahead to the next instance of two blank lines in a row + while line.strip() or fobj.readline().strip(): + line = fobj.readline() + continue + + elif not re.search(r'[a-zA-Z]', line) and line.strip() and len(line.strip().split()) - 2 == len(self.proj_names): + # only parse data if line is expected length, in case of LORBIT >= 12 + proj_data.append([float(token) for token in line.strip().split()[1:-1]]) + + elif line.startswith('band'): + tokens = line.strip().split() + energies.append(float(tokens[4])) + occs.append(float(tokens[-1])) + + elif line.startswith('tot'): + tot_count += 1 - elif line.startswith('band'): - tokens = line.strip().split() - energies.append(float(tokens[4])) - occs.append(float(tokens[-1])) - - elif line.startswith('tot'): - tot_count += 1 - - line = fobj.readline() + line = fobj.readline() # dynamically determine whether PROCARs are SOC or not if tot_count == 4 * len(occs):