Skip to content

Commit 021a280

Browse files
authored
Pass file IO modes as kwarg (#3560)
* pass file IO mode as kwarg * remove pointless Lattice() calls * rename fh/fd -> file
1 parent f463ac1 commit 021a280

File tree

97 files changed

+544
-623
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+544
-623
lines changed

dev_scripts/chemenv/explicit_permutations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class Algo:
3434

3535
if __name__ == "__main__":
3636
# Choose the geometry
37-
allcg = AllCoordinationGeometries()
37+
all_cg = AllCoordinationGeometries()
3838
while True:
3939
cg_symbol = input("Enter symbol of the geometry for which you want to get the explicit permutations : ")
4040
try:
41-
cg = allcg[cg_symbol]
41+
cg = all_cg[cg_symbol]
4242
break
4343
except LookupError:
4444
print("Wrong geometry, try again ...")
@@ -93,5 +93,5 @@ class Algo:
9393
cg._algorithms = [ExplicitPermutationsAlgorithm(permutations=explicit_permutations)]
9494
new_geom_dir = "new_geometry_files"
9595
os.makedirs(new_geom_dir, exist_ok=True)
96-
with open(f"{new_geom_dir}/{cg_symbol}.json", "w") as f:
97-
json.dump(cg.as_dict(), f)
96+
with open(f"{new_geom_dir}/{cg_symbol}.json", mode="w") as file:
97+
json.dump(cg.as_dict(), file)

dev_scripts/chemenv/explicit_permutations_plane_algorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,5 @@
144144
if test == "y":
145145
cg._algorithms = new_algos
146146
cg_dict = cg.as_dict()
147-
with open(f"../coordination_geometries_files_new/{cg_symbol}.json", "w") as f:
148-
json.dump(cg_dict, f)
147+
with open(f"../coordination_geometries_files_new/{cg_symbol}.json", mode="w") as file:
148+
json.dump(cg_dict, file)

dev_scripts/chemenv/get_plane_permutations_optimized.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,5 @@ def random_permutations_iterator(initial_permutation, n_permutations):
432432
if test == "y":
433433
new_geom_dir = "new_geometry_files"
434434
os.makedirs(new_geom_dir, exist_ok=True)
435-
with open(f"{new_geom_dir}/{cg_symbol}.json", "w") as file:
435+
with open(f"{new_geom_dir}/{cg_symbol}.json", mode="w") as file:
436436
json.dump(cg.as_dict(), file)

dev_scripts/potcar_scrambler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def scramble_single_potcar(self, potcar: PotcarSingle):
125125
return scrambled_potcar_str
126126

127127
def to_file(self, filename: str):
128-
with zopen(filename, "wt") as file:
128+
with zopen(filename, mode="wt") as file:
129129
file.write(self.scrambled_potcars_str)
130130

131131
@classmethod

dev_scripts/regen_libxcfunc.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def parse_section(section):
3030
return int(dct["Number"]), dct
3131

3232
dct = {}
33-
with open(path) as fh:
33+
with open(path) as file:
3434
section = []
35-
for line in fh:
35+
for line in file:
3636
if not line.startswith("-"):
3737
section.append(line)
3838
else:
@@ -63,8 +63,8 @@ def write_libxc_docs_json(xcfuncs, jpath):
6363
if desc is not None:
6464
xcfuncs[num][opt] = desc
6565

66-
with open(jpath, "w") as fh:
67-
json.dump(xcfuncs, fh)
66+
with open(jpath, mode="w") as file:
67+
json.dump(xcfuncs, file)
6868

6969
return xcfuncs
7070

@@ -101,8 +101,8 @@ def main():
101101
# Re-generate enumerations.
102102
# [0] read py module.
103103
xc_funcpy_path = f"{pmg_core}/libxcfunc.py"
104-
with open(xc_funcpy_path) as fh:
105-
lines = fh.readlines()
104+
with open(xc_funcpy_path) as file:
105+
lines = file.readlines()
106106

107107
# [1] insert new enum values in list
108108
start = lines.index("#begin_include_dont_touch\n")
@@ -111,8 +111,8 @@ def main():
111111
del lines[start + 1 : stop]
112112

113113
# [2] write new py module
114-
with open(xc_funcpy_path, "w") as fh:
115-
fh.writelines(lines)
114+
with open(xc_funcpy_path, mode="w") as file:
115+
file.writelines(lines)
116116

117117
print("Files have been regenerated")
118118
print("Remember to update libxc_version in libxcfuncs.py!")

dev_scripts/update_pt_data.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def parse_oxi_state():
5656
data[el]["Common oxidation states"] = common_oxi
5757
else:
5858
print(el)
59-
with open("periodic_table2.yaml", "w") as file:
59+
with open("periodic_table2.yaml", mode="w") as file:
6060
yaml.dump(data, file)
6161

6262

@@ -87,7 +87,7 @@ def parse_ionic_radii():
8787
data[el]["Ionic_radii"] = ionic_radii
8888
else:
8989
print(el)
90-
with open("periodic_table2.yaml", "w") as file:
90+
with open("periodic_table2.yaml", mode="w") as file:
9191
yaml.dump(data, file)
9292

9393

@@ -121,9 +121,9 @@ def parse_radii():
121121
data[el]["Van der waals radius"] = vdw_radii
122122
else:
123123
print(el)
124-
with open("periodic_table2.yaml", "w") as file:
124+
with open("periodic_table2.yaml", mode="w") as file:
125125
yaml.dump(data, file)
126-
with open("../pymatgen/core/periodic_table.json", "w") as file:
126+
with open("../pymatgen/core/periodic_table.json", mode="w") as file:
127127
json.dump(data, file)
128128

129129

@@ -140,9 +140,9 @@ def update_ionic_radii():
140140
if "Ionic_radii_ls" in d:
141141
d["Ionic radii ls"] = {k: v / 100 for k, v in d["Ionic_radii_ls"].items()}
142142
del d["Ionic_radii_ls"]
143-
with open("periodic_table2.yaml", "w") as file:
143+
with open("periodic_table2.yaml", mode="w") as file:
144144
yaml.dump(data, file)
145-
with open("../pymatgen/core/periodic_table.json", "w") as file:
145+
with open("../pymatgen/core/periodic_table.json", mode="w") as file:
146146
json.dump(data, file)
147147

148148

@@ -180,14 +180,14 @@ def parse_shannon_radii():
180180
data[el]["Shannon radii"] = dict(radii[el])
181181

182182
dumpfn(data, ptable_yaml_path)
183-
with open("../pymatgen/core/periodic_table.json", "w") as file:
183+
with open("../pymatgen/core/periodic_table.json", mode="w") as file:
184184
json.dump(data, file)
185185

186186

187187
def gen_periodic_table():
188188
data = loadfn(ptable_yaml_path)
189189

190-
with open("../pymatgen/core/periodic_table.json", "w") as file:
190+
with open("../pymatgen/core/periodic_table.json", mode="w") as file:
191191
json.dump(data, file)
192192

193193

pymatgen/alchemy/materials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def write_vasp_input(
207207
**kwargs: All keyword args supported by the VASP input set.
208208
"""
209209
vasp_input_set(self.final_structure, **kwargs).write_input(output_dir, make_dir_if_not_present=create_directory)
210-
with open(f"{output_dir}/transformations.json", "w") as fp:
210+
with open(f"{output_dir}/transformations.json", mode="w") as fp:
211211
json.dump(self.as_dict(), fp)
212212

213213
def __str__(self) -> str:

pymatgen/alchemy/transmuters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ def from_filenames(poscar_filenames, transformations=None, extend_collection=Fal
308308
"""
309309
trafo_structs = []
310310
for filename in poscar_filenames:
311-
with open(filename) as f:
312-
trafo_structs.append(TransformedStructure.from_poscar_string(f.read(), []))
311+
with open(filename) as file:
312+
trafo_structs.append(TransformedStructure.from_poscar_string(file.read(), []))
313313
return StandardTransmuter(trafo_structs, transformations, extend_collection=extend_collection)
314314

315315

pymatgen/analysis/chemenv/utils/chemenv_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def save(self, root_dir=None):
149149
if test != "Y":
150150
print("Configuration not saved")
151151
return config_file
152-
with open(config_file, "w") as f:
153-
json.dump(config_dict, f)
152+
with open(config_file, mode="w") as file:
153+
json.dump(config_dict, file)
154154
print("Configuration saved")
155155
return config_file
156156

pymatgen/analysis/cost.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def __init__(self, filename):
9191
# read in data from file
9292
self._chemsys_entries = defaultdict(list)
9393
filename = os.path.join(os.path.dirname(__file__), filename)
94-
with open(filename) as f:
95-
reader = csv.reader(f, quotechar="|")
94+
with open(filename) as file:
95+
reader = csv.reader(file, quotechar="|")
9696
for row in reader:
9797
comp = Composition(row[0])
9898
cost_per_mol = float(row[1]) * comp.weight.to("kg") * const.N_A

0 commit comments

Comments
 (0)