Skip to content

Commit

Permalink
pep8 cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Aug 21, 2019
1 parent 318e6fb commit 31c2b58
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 235 deletions.
5 changes: 4 additions & 1 deletion pymatgen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

import sys
import os
Expand Down Expand Up @@ -101,4 +104,4 @@ def loadfn(fname):
return Vasprun(fname)
elif fnmatch(fname, "*.json*"):
from monty.serialization import loadfn
return loadfn(fname)
return loadfn(fname)
1 change: 0 additions & 1 deletion pymatgen/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@

__author__ = "Shyue Ping Ong"
__date__ = "Dec 15, 2010 7:21:29 PM"

72 changes: 35 additions & 37 deletions pymatgen/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from monty.serialization import loadfn


from functools import total_ordering

from monty.fractions import gcd, gcd_float
Expand All @@ -24,7 +23,6 @@
from monty.json import MSONable
from pymatgen.core.units import unitized


"""
This module implements a Composition class to represent compositions,
and a ChemicalPotential class to represent potentials.
Expand Down Expand Up @@ -92,7 +90,7 @@ class Composition(collections.abc.Hashable, collections.abc.Mapping, MSONable):
"""
special_formulas = {"LiO": "Li2O2", "NaO": "Na2O2", "KO": "K2O2",
"HO": "H2O2", "CsO": "Cs2O2", "RbO": "Rb2O2",
"O": "O2", "N": "N2", "F": "F2", "Cl": "Cl2",
"O": "O2", "N": "N2", "F": "F2", "Cl": "Cl2",
"H": "H2"}

oxi_prob = None # prior probability of oxidation used by oxi_state_guesses
Expand Down Expand Up @@ -257,7 +255,7 @@ def __hash__(self):
@property
def average_electroneg(self):
return sum((el.X * abs(amt) for el, amt in self.items())) / \
self.num_atoms
self.num_atoms

@property
def total_electrons(self):
Expand Down Expand Up @@ -616,7 +614,6 @@ def valid(self):
"""
return not any([isinstance(el, DummySpecie) for el in self.elements])


def __repr__(self):
return "Comp: " + self.formula

Expand Down Expand Up @@ -899,7 +896,7 @@ def _get_oxid_state_guesses(self, all_oxi_states, max_sites,
all_scores.append(score)

# collect the combination of oxidation states for each site
all_oxid_combo.append(dict((e,el_best_oxid_combo[idx][v]) for idx, (e,v) in enumerate(zip(els,x))))
all_oxid_combo.append(dict((e, el_best_oxid_combo[idx][v]) for idx, (e, v) in enumerate(zip(els, x))))

# sort the solutions by highest to lowest score
if len(all_scores) > 0:
Expand Down Expand Up @@ -928,21 +925,21 @@ def ranked_compositions_from_indeterminate_formula(fuzzy_formula,
A ranked list of potential Composition matches
"""

#if we have an exact match and the user specifies lock_if_strict, just
#return the exact match!
# if we have an exact match and the user specifies lock_if_strict, just
# return the exact match!
if lock_if_strict:
#the strict composition parsing might throw an error, we can ignore
#it and just get on with fuzzy matching
# the strict composition parsing might throw an error, we can ignore
# it and just get on with fuzzy matching
try:
comp = Composition(fuzzy_formula)
return [comp]
except (CompositionError, ValueError):
pass

all_matches = Composition._comps_from_fuzzy_formula(fuzzy_formula)
#remove duplicates
# remove duplicates
all_matches = list(set(all_matches))
#sort matches by rank descending
# sort matches by rank descending
all_matches = sorted(all_matches,
key=lambda match: match[1], reverse=True)
all_matches = [m[0] for m in all_matches]
Expand Down Expand Up @@ -1003,14 +1000,14 @@ def _parse_chomp_and_rank(m, f, m_dict, m_points):
# specified as lowercase
points_second_lowercase = 100

#get element and amount from regex match
# get element and amount from regex match
el = m.group(1)
if len(el) > 2 or len(el) < 1:
raise CompositionError("Invalid element symbol entered!")
amt = float(m.group(2)) if m.group(2).strip() != "" else 1

#convert the element string to proper [uppercase,lowercase] format
#and award points if it is already in that format
# convert the element string to proper [uppercase,lowercase] format
# and award points if it is already in that format
char1 = el[0]
char2 = el[1] if len(el) > 1 else ""

Expand All @@ -1021,15 +1018,15 @@ def _parse_chomp_and_rank(m, f, m_dict, m_points):

el = char1.upper() + char2.lower()

#if it's a valid element, chomp and add to the points
# if it's a valid element, chomp and add to the points
if Element.is_valid_symbol(el):
if el in m_dict:
m_dict[el] += amt * factor
else:
m_dict[el] = amt * factor
return f.replace(m.group(), "", 1), m_dict, m_points + points

#else return None
# else return None
return None, None, None

fuzzy_formula = fuzzy_formula.strip()
Expand All @@ -1050,19 +1047,19 @@ def _parse_chomp_and_rank(m, f, m_dict, m_points):
# Match the stuff inside the parenthesis with the appropriate
# factor
for match in \
Composition._comps_from_fuzzy_formula(mp.group(1),
mp_dict,
mp_points,
factor=mp_factor):
Composition._comps_from_fuzzy_formula(mp.group(1),
mp_dict,
mp_points,
factor=mp_factor):
only_me = True
# Match the stuff outside the parentheses and return the
# sum.

for match2 in \
Composition._comps_from_fuzzy_formula(mp_form,
mp_dict,
mp_points,
factor=1):
Composition._comps_from_fuzzy_formula(mp_form,
mp_dict,
mp_points,
factor=1):
only_me = False
yield (match[0] + match2[0], match[1] + match2[1])
# if the stuff inside the parenthesis is nothing, then just
Expand All @@ -1080,15 +1077,15 @@ def _parse_chomp_and_rank(m, f, m_dict, m_points):
(m_form1, m_dict1, m_points1) = \
_parse_chomp_and_rank(m1, m_form1, m_dict1, m_points1)
if m_dict1:
#there was a real match
# there was a real match
for match in \
Composition._comps_from_fuzzy_formula(m_form1,
m_dict1,
m_points1,
factor):
Composition._comps_from_fuzzy_formula(m_form1,
m_dict1,
m_points1,
factor):
yield match

#try to match two-letter elements
# try to match two-letter elements
m2 = re.match(r"([A-z]{2})([\.\d]*)", fuzzy_formula)
if m2:
m_points2 = m_points
Expand All @@ -1097,11 +1094,11 @@ def _parse_chomp_and_rank(m, f, m_dict, m_points):
(m_form2, m_dict2, m_points2) = \
_parse_chomp_and_rank(m2, m_form2, m_dict2, m_points2)
if m_dict2:
#there was a real match
# there was a real match
for match in \
Composition._comps_from_fuzzy_formula(m_form2, m_dict2,
m_points2,
factor):
Composition._comps_from_fuzzy_formula(m_form2, m_dict2,
m_points2,
factor):
yield match


Expand Down Expand Up @@ -1180,7 +1177,7 @@ def __init__(self, *args, **kwargs):
"""
d = dict(*args, **kwargs)
super().__init__((get_el_sp(k), v)
for k, v in d.items())
for k, v in d.items())
if len(d) != len(self):
raise ValueError("Duplicate potential specified")

Expand Down Expand Up @@ -1219,7 +1216,7 @@ def __add__(self, other):
def get_energy(self, composition, strict=True):
"""
Calculates the energy of a composition.
Args:
composition (Composition): input composition
strict (bool): Whether all potentials must be specified
Expand All @@ -1235,4 +1232,5 @@ def __repr__(self):

if __name__ == "__main__":
import doctest

doctest.testmod()
Loading

0 comments on commit 31c2b58

Please sign in to comment.