本文整理汇总了Python中pymatgen.core.composition.Composition.__hash__方法的典型用法代码示例。如果您正苦于以下问题:Python Composition.__hash__方法的具体用法?Python Composition.__hash__怎么用?Python Composition.__hash__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymatgen.core.composition.Composition
的用法示例。
在下文中一共展示了Composition.__hash__方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_equals
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import __hash__ [as 别名]
def test_equals(self):
random_z = random.randint(1, 92)
fixed_el = Element.from_Z(random_z)
other_z = random.randint(1, 92)
while other_z == random_z:
other_z = random.randint(1, 92)
comp1 = Composition({fixed_el: 1, Element.from_Z(other_z): 0})
other_z = random.randint(1, 92)
while other_z == random_z:
other_z = random.randint(1, 92)
comp2 = Composition({fixed_el: 1, Element.from_Z(other_z): 0})
self.assertEqual(
comp1,
comp2,
"Composition equality test failed. " + "%s should be equal to %s" % (comp1.formula, comp2.formula),
)
self.assertEqual(comp1.__hash__(), comp2.__hash__(), "Hashcode equality test failed!")
示例2: Ion
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import __hash__ [as 别名]
#.........这里部分代码省略.........
@property
def charge(self):
"""
Charge of the ion
"""
return self._charge
@property
def composition(self):
"""
Return composition object
"""
return self._composition
def as_dict(self):
"""
Returns:
dict with composition, as well as charge
"""
d = self._composition.as_dict()
d['charge'] = self._charge
return d
@classmethod
def from_dict(cls, d):
"""
Generates an ion object from a dict created by as_dict().
Args:
d:
{symbol: amount} dict.
"""
# composition = Composition.from_dict(d['composition'])
charge = d['charge']
composition = Composition({i: d[i] for i in d if i != 'charge'})
return Ion(composition, charge)
@property
def to_reduced_dict(self):
"""
Returns:
dict with element symbol and reduced amount e.g.,
{"Fe": 2.0, "O":3.0}.
"""
reduced_formula = self._composition.reduced_formula
c = Composition(reduced_formula)
d = c.as_dict()
d['charge'] = self._charge
return d
def __eq__(self, other):
if self.composition != other.composition:
return False
if self.charge != other.charge:
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def __add__(self, other):
"""
Addition of two ions.
"""
new_composition = self.composition + other.composition
new_charge = self.charge + other.charge
return Ion(new_composition, new_charge)
def __sub__(self, other):
"""
Subtraction of two ions
"""
new_composition = self.composition - other.composition
new_charge = self.charge - other.charge
return Ion(new_composition, new_charge)
def __mul__(self, other):
"""
Multiplication of an Ion with a factor
"""
new_composition = self.composition * other
new_charge = self.charge * other
return Ion(new_composition, new_charge)
def __hash__(self):
#for now, just use the composition hash code.
return self._composition.__hash__()
def __len__(self):
return len(self._composition)
def __str__(self):
return self.formula
def __repr__(self):
return "Ion: " + self.formula
def __getitem__(self, el):
return self._composition.get(el, 0)