Skip to content

Commit 49083cc

Browse files
authored
xyz.__iter__() -> iter(xyz) (#3228)
1 parent 4b70580 commit 49083cc

File tree

19 files changed

+84
-119
lines changed

19 files changed

+84
-119
lines changed

pymatgen/analysis/chemenv/connectivity/environment_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def isite(self):
4242

4343
def __hash__(self) -> int:
4444
"""Simple hash function based on the hash function of the central site."""
45-
return self.central_site.__hash__()
45+
return hash(self.central_site)
4646

4747
def __eq__(self, other: object) -> bool:
4848
if not isinstance(other, AbstractEnvironmentNode):

pymatgen/analysis/graphs.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,8 @@ def __mul__(self, scaling_matrix):
11511151
# this could probably be a lot smaller
11521152
tol = 0.05
11531153

1154-
for u, v, k, d in new_g.edges(keys=True, data=True):
1155-
to_jimage = d["to_jimage"] # for node v
1154+
for u, v, k, dct in new_g.edges(keys=True, data=True):
1155+
to_jimage = dct["to_jimage"] # for node v
11561156

11571157
# reduce unnecessary checking
11581158
if to_jimage != (0, 0, 0):
@@ -1191,7 +1191,7 @@ def __mul__(self, scaling_matrix):
11911191
if v_present is not None:
11921192
new_u = u
11931193
new_v = v_present
1194-
new_d = d.copy()
1194+
new_d = dct.copy()
11951195

11961196
# node now inside supercell
11971197
new_d["to_jimage"] = (0, 0, 0)
@@ -1229,13 +1229,13 @@ def __mul__(self, scaling_matrix):
12291229
if v_present is not None:
12301230
new_u = u
12311231
new_v = v_present
1232-
new_d = d.copy()
1232+
new_d = dct.copy()
12331233
new_to_jimage = tuple(map(int, v_expec_image))
12341234

12351235
# normalize direction
12361236
if new_v < new_u:
12371237
new_u, new_v = new_v, new_u
1238-
new_to_jimage = tuple(np.multiply(-1, d["to_jimage"]).astype(int))
1238+
new_to_jimage = tuple(np.multiply(-1, dct["to_jimage"]).astype(int))
12391239

12401240
new_d["to_jimage"] = new_to_jimage
12411241

@@ -1250,18 +1250,18 @@ def __mul__(self, scaling_matrix):
12501250
# add/delete marked edges
12511251
for edge in edges_to_remove:
12521252
new_g.remove_edge(*edge)
1253-
for u, v, d in edges_to_add:
1254-
new_g.add_edge(u, v, **d)
1253+
for u, v, dct in edges_to_add:
1254+
new_g.add_edge(u, v, **dct)
12551255

12561256
# return new instance of StructureGraph with supercell
1257-
d = {
1257+
dct = {
12581258
"@module": type(self).__module__,
12591259
"@class": type(self).__name__,
12601260
"structure": new_structure.as_dict(),
12611261
"graphs": json_graph.adjacency_data(new_g),
12621262
}
12631263

1264-
return StructureGraph.from_dict(d)
1264+
return StructureGraph.from_dict(dct)
12651265

12661266
def __rmul__(self, other):
12671267
return self.__mul__(other)
@@ -1307,7 +1307,7 @@ def __str__(self):
13071307

13081308
def __repr__(self):
13091309
s = "Structure Graph"
1310-
s += f"\nStructure: \n{self.structure.__repr__()}"
1310+
s += f"\nStructure: \n{self.structure!r}"
13111311
s += f"\nGraph: {self.name}\n"
13121312
s += self._edges_to_string(self.graph)
13131313
return s
@@ -1363,8 +1363,7 @@ def __eq__(self, other: object) -> bool:
13631363
if not isinstance(other, StructureGraph):
13641364
return NotImplemented
13651365
# sort for consistent node indices
1366-
# PeriodicSite should have a proper __hash__() value,
1367-
# using its frac_coords as a convenient key
1366+
# PeriodicSite should have a proper __hash__() value, using its frac_coords as a convenient key
13681367
mapping = {tuple(site.frac_coords): self.structure.index(site) for site in other.structure}
13691368
other_sorted = other.__copy__()
13701369
other_sorted.sort(key=lambda site: mapping[tuple(site.frac_coords)])
@@ -1406,8 +1405,7 @@ def diff(self, other, strict=True):
14061405

14071406
if strict:
14081407
# sort for consistent node indices
1409-
# PeriodicSite should have a proper __hash__() value,
1410-
# using its frac_coords as a convenient key
1408+
# PeriodicSite should have a proper __hash__() value, using its frac_coords as a convenient key
14111409
mapping = {tuple(site.frac_coords): self.structure.index(site) for site in other.structure}
14121410
other_sorted = copy.copy(other)
14131411
other_sorted.sort(key=lambda site: mapping[tuple(site.frac_coords)])
@@ -2668,7 +2666,7 @@ def __str__(self) -> str:
26682666

26692667
def __repr__(self) -> str:
26702668
out = "Molecule Graph"
2671-
out += f"\nMolecule: \n{self.molecule.__repr__()}"
2669+
out += f"\nMolecule: \n{self.molecule!r}"
26722670
out += f"\nGraph: {self.name}\n"
26732671
out += self._edges_to_string(self.graph)
26742672
return out
@@ -2725,8 +2723,7 @@ def __eq__(self, other: object) -> bool:
27252723
return NotImplemented
27262724

27272725
# sort for consistent node indices
2728-
# PeriodicSite should have a proper __hash__() value,
2729-
# using its frac_coords as a convenient key
2726+
# PeriodicSite should have a proper __hash__() value, using its frac_coords as a convenient key
27302727
try:
27312728
mapping = {tuple(site.coords): self.molecule.index(site) for site in other.molecule}
27322729
except ValueError:
@@ -2789,8 +2786,7 @@ def diff(self, other, strict=True):
27892786

27902787
if strict:
27912788
# sort for consistent node indices
2792-
# PeriodicSite should have a proper __hash__() value,
2793-
# using its frac_coords as a convenient key
2789+
# PeriodicSite should have a proper __hash__() value, using its frac_coords as a convenient key
27942790
mapping = {tuple(site.frac_coords): self.molecule.index(site) for site in other.molecule}
27952791
other_sorted = copy.copy(other)
27962792
other_sorted.sort(key=lambda site: mapping[tuple(site.frac_coords)])

pymatgen/apps/battery/battery_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def __contains__(self, obj):
142142
return obj in self.voltage_pairs
143143

144144
def __iter__(self):
145-
return self.voltage_pairs.__iter__()
145+
return iter(self.voltage_pairs)
146146

147147
def __len__(self):
148148
return len(self.voltage_pairs)

pymatgen/core/composition.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def __len__(self) -> int:
146146
return len(self._data)
147147

148148
def __iter__(self) -> Iterator[Species | Element | DummySpecies]:
149-
return self._data.__iter__()
149+
return iter(self._data)
150150

151151
def __contains__(self, key) -> bool:
152152
try:
@@ -437,8 +437,9 @@ def __str__(self):
437437
return " ".join(f"{k}{formula_double_format(v, ignore_ones=False)}" for k, v in self.as_dict().items())
438438

439439
def to_pretty_string(self) -> str:
440-
"""Returns:
441-
str: Same as output __str__() but without spaces.
440+
"""
441+
Returns:
442+
str: Same output as __str__() but without spaces.
442443
"""
443444
return re.sub(r"\s+", "", str(self))
444445

@@ -1250,7 +1251,7 @@ def get_energy(self, composition: Composition, strict: bool = True) -> float:
12501251
return sum(self.get(k, 0) * v for k, v in composition.items())
12511252

12521253
def __repr__(self):
1253-
return "ChemPots: " + super().__repr__()
1254+
return f"ChemPots: {super()!r}"
12541255

12551256

12561257
class CompositionError(Exception):

pymatgen/core/structure.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def __contains__(self, site: object) -> bool:
327327
return site in self.sites
328328

329329
def __iter__(self) -> Iterator[Site]:
330-
return self.sites.__iter__()
330+
return iter(self.sites)
331331

332332
# TODO return type needs fixing (can be list[Site] but raises lots of mypy errors)
333333
def __getitem__(self, ind: int | slice) -> Site:
@@ -1322,25 +1322,25 @@ def __mul__(self, scaling_matrix: int | Sequence[int] | Sequence[Sequence[int]])
13221322

13231323
new_sites = []
13241324
for site in self:
1325-
for v in c_lat:
1326-
s = PeriodicSite(
1325+
for vec in c_lat:
1326+
periodic_site = PeriodicSite(
13271327
site.species,
1328-
site.coords + v,
1328+
site.coords + vec,
13291329
new_lattice,
13301330
properties=site.properties,
13311331
coords_are_cartesian=True,
13321332
to_unit_cell=False,
13331333
skip_checks=True,
13341334
label=site.label,
13351335
)
1336-
new_sites.append(s)
1336+
new_sites.append(periodic_site)
13371337

13381338
new_charge = self._charge * np.linalg.det(scale_matrix) if self._charge else None
13391339
return Structure.from_sites(new_sites, charge=new_charge, to_unit_cell=True)
13401340

13411341
def __rmul__(self, scaling_matrix):
13421342
"""Similar to __mul__ to preserve commutativeness."""
1343-
return self.__mul__(scaling_matrix)
1343+
return self * scaling_matrix
13441344

13451345
@property
13461346
def frac_coords(self):

pymatgen/core/tensors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def __getitem__(self, ind):
682682
return self.tensors[ind]
683683

684684
def __iter__(self):
685-
return self.tensors.__iter__()
685+
return iter(self.tensors)
686686

687687
def zeroed(self, tol: float = 1e-3):
688688
""":param tol: Tolerance

pymatgen/core/units.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
__status__ = "Production"
2424
__date__ = "Aug 30, 2013"
2525

26-
"""
27-
Some conversion factors
28-
"""
26+
"""Some conversion factors"""
2927
Ha_to_eV = 1 / const.physical_constants["electron volt-hartree relationship"][0]
3028
eV_to_Ha = 1 / Ha_to_eV
3129
Ry_to_eV = Ha_to_eV / 2
@@ -134,11 +132,7 @@
134132
SUPPORTED_UNIT_NAMES = tuple(i for d in ALL_UNITS.values() for i in d)
135133

136134
# Mapping unit name --> unit type (unit names must be unique).
137-
_UNAME2UTYPE = {} # type: ignore
138-
for utype, d in ALL_UNITS.items():
139-
assert not set(d).intersection(_UNAME2UTYPE) # type: ignore
140-
_UNAME2UTYPE.update({uname: utype for uname in d}) # type: ignore
141-
del utype, d
135+
_UNAME2UTYPE = {uname: utype for utype, dct in ALL_UNITS.items() for uname in dct}
142136

143137

144138
def _get_si_unit(unit):
@@ -199,9 +193,6 @@ def __mul__(self, other):
199193
new_units[k] += v
200194
return Unit(new_units)
201195

202-
def __rmul__(self, other):
203-
return self.__mul__(other)
204-
205196
def __div__(self, other):
206197
new_units = collections.defaultdict(int)
207198
for k, v in self.items():
@@ -217,7 +208,7 @@ def __pow__(self, i):
217208
return Unit({k: v * i for k, v in self.items()})
218209

219210
def __iter__(self):
220-
return self._unit.__iter__()
211+
return iter(self._unit)
221212

222213
def __getitem__(self, i):
223214
return self._unit[i]
@@ -526,7 +517,7 @@ def __setstate__(self, state):
526517
self._unit = state["_unit"]
527518

528519
def __repr__(self):
529-
return f"{np.array(self).__repr__()} {self.unit}"
520+
return f"{np.array(self)!r} {self.unit}"
530521

531522
def __str__(self):
532523
return f"{np.array(self)} {self.unit}"
@@ -564,7 +555,7 @@ def __mul__(self, other):
564555
# Same protocol for __div__
565556
if not hasattr(other, "unit_type"):
566557
return self.__class__(
567-
np.array(self).__mul__(np.array(other)),
558+
np.array(self) * np.array(other),
568559
unit_type=self._unit_type,
569560
unit=self._unit,
570561
)
@@ -576,34 +567,30 @@ def __rmul__(self, other):
576567
# pylint: disable=E1101
577568
if not hasattr(other, "unit_type"):
578569
return self.__class__(
579-
np.array(self).__rmul__(np.array(other)),
570+
np.array(self) * np.array(other),
580571
unit_type=self._unit_type,
581572
unit=self._unit,
582573
)
583-
return self.__class__(np.array(self).__rmul__(np.array(other)), unit=self.unit * other.unit)
574+
return self.__class__(np.array(self) * np.array(other), unit=self.unit * other.unit)
584575

585576
def __div__(self, other):
586577
# pylint: disable=E1101
587578
if not hasattr(other, "unit_type"):
588579
return self.__class__(
589-
np.array(self).__div__(np.array(other)),
580+
np.array(self) / np.array(other),
590581
unit_type=self._unit_type,
591582
unit=self._unit,
592583
)
593-
return self.__class__(np.array(self).__div__(np.array(other)), unit=self.unit / other.unit)
584+
return self.__class__(np.array(self) / np.array(other), unit=self.unit / other.unit)
594585

595586
def __truediv__(self, other):
596587
# pylint: disable=E1101
597588
if not hasattr(other, "unit_type"):
598-
return self.__class__(
599-
np.array(self).__truediv__(np.array(other)),
600-
unit_type=self._unit_type,
601-
unit=self._unit,
602-
)
603-
return self.__class__(np.array(self).__truediv__(np.array(other)), unit=self.unit / other.unit)
589+
return self.__class__(np.array(self) / np.array(other), unit_type=self._unit_type, unit=self._unit)
590+
return self.__class__(np.array(self) / np.array(other), unit=self.unit / other.unit)
604591

605592
def __neg__(self):
606-
return self.__class__(np.array(self).__neg__(), unit_type=self.unit_type, unit=self.unit)
593+
return self.__class__(-np.array(self), unit_type=self.unit_type, unit=self.unit)
607594

608595
def to(self, new_unit):
609596
"""Conversion to a new_unit.

pymatgen/electronic_structure/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def __neg__(self):
432432
return Magmom(-self.moment, saxis=self.saxis)
433433

434434
def __hash__(self) -> int:
435-
return (tuple(self.moment) + tuple(self.saxis)).__hash__()
435+
return hash(tuple(self.moment) + tuple(self.saxis))
436436

437437
def __float__(self):
438438
"""Returns magnitude of magnetic moment with a sign with respect to

pymatgen/entries/entry_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def __contains__(self, item):
205205
return item in self.entries
206206

207207
def __iter__(self):
208-
return self.entries.__iter__()
208+
return iter(self.entries)
209209

210210
def __len__(self):
211211
return len(self.entries)

pymatgen/io/abinit/abitimer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __init__(self):
9494
self._timers = {}
9595

9696
def __iter__(self):
97-
return self._timers.__iter__()
97+
return iter(self._timers)
9898

9999
def __len__(self):
100100
return len(self._timers)

0 commit comments

Comments
 (0)