Skip to content

Commit

Permalink
Fix summary_search.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Oct 4, 2023
1 parent b12c2c7 commit a1992c3
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pymatgen/ext/matproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def __getattr__(self, item):
f"{item} is not an attribute of this implementation of MPRester, which only supports functionality"
"used by 80% of users. If you are looking for the full functionality MPRester, pls install the mp-api ."
)
raise AttributeError

def __enter__(self):
"""Support for "with" context."""
Expand Down Expand Up @@ -140,10 +139,15 @@ def summary_search(self, **kwargs) -> list[dict]:
MPRester().summary.search(material_ids="mp-19770,mp-19017", _fields="formula_pretty,energy_above_hull")
"""
criteria = {k: v for k, v in kwargs.items() if not k.startswith("_")}
params = [f"{k}={v}" for k, v in kwargs.items() if k.startswith("_")]
if "_fields" not in params:
params = [f"{k}={v}" for k, v in kwargs.items() if k.startswith("_") and k != "_fields"]
if "_fields" not in kwargs:
params.append("_all_fields=True")
else:
fields = ",".join(kwargs["_fields"]) if isinstance(kwargs["_fields"], list) else kwargs["_fields"]
params.append(f"_fields={fields}")
params.append("_all_fields=False")
get = "&".join(params)
logger.info(f"query={get}")
return self.request(f"materials/summary?{get}", payload=criteria)

def get_summary(self, criteria: dict, fields: list | None = None) -> list[dict]:
Expand Down Expand Up @@ -387,14 +391,14 @@ def __new__(cls, *args, **kwargs):
if len(api_key) != 32:
from pymatgen.ext.matproj_legacy import _MPResterLegacy

return _MPResterLegacy.__new__(cls)
return _MPResterLegacy(*args, **kwargs)

try:
from mp_api.client import MPRester as _MPResterNew

return _MPResterNew.__new__(cls)
return _MPResterNew(*args, **kwargs)
except Exception:
return _MPResterBasic.__new__(cls)
return _MPResterBasic(*args, **kwargs)


class MPRestError(Exception):
Expand Down

0 comments on commit a1992c3

Please sign in to comment.