|
17 | 17 | import sys
|
18 | 18 | import warnings
|
19 | 19 | from collections import namedtuple
|
20 |
| -from functools import partial |
| 20 | +from functools import lru_cache, partial |
21 | 21 | from typing import TYPE_CHECKING, NamedTuple
|
22 | 22 |
|
23 | 23 | import requests
|
|
42 | 42 |
|
43 | 43 | class MPRester:
|
44 | 44 | """
|
45 |
| - This is pymatgen's implementation of MPRester. |
| 45 | + Pymatgen's implementation of MPRester. Unlike mp-api, this implementation mirrors the exact MP-API end points |
| 46 | + without modification. You just need to refer to https://api.materialsproject.org/docs and use the field names |
| 47 | + exactly. No need to deal with strange renames of various fields. Featurity parity is close to 100% with mp-api. |
46 | 48 |
|
47 |
| - We have decided to drop support for the mp-api package and directly implement most of the relevant functionality |
48 |
| - within pymatgen. This implementation mirrors the actual MP API REST fields, rather than having strange renamed |
49 |
| - variables. |
| 49 | + Furthermore, we support both the mp-api as well as a simplified syntax. E.g., to query for a summary, you can use |
| 50 | + mpr.summary.search(material_ids="mp-1234") or mpr.materials.summary.search(material_ids="mp-1234"). |
50 | 51 |
|
51 |
| - If you are a power user, feel free to install the mp-api package. All issues regarding that implementation should |
52 |
| - be directed to the maintainers of that repository and not pymatgen. We will support only issues with regards to |
53 |
| - our implementation only. |
| 52 | + If you are a power user that requires some esoteric feature not covered, feel free to install the mp-api package. |
| 53 | + All issues regarding that implementation should be directed to the maintainers of that repository and not |
| 54 | + pymatgen. We will support only issues pertaining to our implementation only. |
54 | 55 | """
|
55 | 56 |
|
56 | 57 | def __init__(self, api_key: str | None = None, include_user_agent: bool = True) -> None:
|
@@ -121,6 +122,7 @@ class Search(NamedTuple):
|
121 | 122 | for doc in docs:
|
122 | 123 | setattr(self, doc, Search(partial(self.search, doc)))
|
123 | 124 |
|
| 125 | + # This hacks make the syntax similar to mp-api's MPRester.materials.search... |
124 | 126 | Materials = namedtuple("Materials", " ".join(docs)) # noqa: PYI024
|
125 | 127 |
|
126 | 128 | self.materials = Materials(*[getattr(self, doc) for doc in docs])
|
@@ -167,6 +169,7 @@ def request(self, sub_url, payload=None, method="GET", mp_decode=True):
|
167 | 169 | raise MPRestError(msg)
|
168 | 170 | return all_data
|
169 | 171 |
|
| 172 | + @lru_cache(maxsize=100) # noqa:B019 |
170 | 173 | def search(self, doc, **kwargs) -> list[dict]:
|
171 | 174 | """
|
172 | 175 | Queries a Materials PI end point doc. A notable difference with the mp-api's implementation is that this uses
|
|
0 commit comments