Skip to content

Commit

Permalink
Dynamically instantiate MPContribs client (#870)
Browse files Browse the repository at this point in the history
* Dynamically instantiate MPContribs client

* Linting

* Fix MPRester test
  • Loading branch information
munrojm committed Nov 27, 2023
1 parent fec167e commit 2bebfa3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
39 changes: 24 additions & 15 deletions mp_api/client/mprester.py
Expand Up @@ -186,6 +186,7 @@ def __init__(
self.use_document_model = use_document_model
self.monty_decode = monty_decode
self.mute_progress_bars = mute_progress_bars
self._contribs = None

self._deprecated_attributes = [
"eos",
Expand Down Expand Up @@ -221,21 +222,6 @@ def __init__(
# Check if emmet version of server is compatible
emmet_version = MPRester.get_emmet_version(self.endpoint)

try:
from mpcontribs.client import Client

self.contribs = Client(api_key, headers=self.headers, session=self.session)
except ImportError:
self.contribs = None
warnings.warn(
"mpcontribs-client not installed. "
"Install the package to query MPContribs data, or construct pourbaix diagrams: "
"'pip install mpcontribs-client'"
)
except Exception as error:
self.contribs = None
warnings.warn(f"Problem loading MPContribs client: {error}")

if version.parse(emmet_version.base_version) < version.parse(
_MAPI_SETTINGS.MIN_EMMET_VERSION
):
Expand Down Expand Up @@ -363,6 +349,29 @@ def __molecules_getattr__(_self, attr):
rester,
)

@property
def contribs(self):
if self._contribs is None:
try:
from mpcontribs.client import Client

self._contribs = Client(
self.api_key, headers=self.headers, session=self.session
)

except ImportError:
self._contribs = None
warnings.warn(
"mpcontribs-client not installed. "
"Install the package to query MPContribs data, or construct pourbaix diagrams: "
"'pip install mpcontribs-client'"
)
except Exception as error:
self._contribs = None
warnings.warn(f"Problem loading MPContribs client: {error}")

return self._contribs

def __enter__(self):
"""Support for "with" context."""
return self
Expand Down
2 changes: 1 addition & 1 deletion mp_api/client/routes/materials/elasticity.py
Expand Up @@ -2,11 +2,11 @@

import warnings
from collections import defaultdict
from mp_api.client.core.utils import validate_ids

from emmet.core.elasticity import ElasticityDoc

from mp_api.client.core import BaseRester
from mp_api.client.core.utils import validate_ids


class ElasticityRester(BaseRester[ElasticityDoc]):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mprester.py
Expand Up @@ -118,7 +118,7 @@ def test_get_entries(self, mpr):
syms = ["Li", "Fe", "O"]
chemsys = "Li-Fe-O"
entries = mpr.get_entries(chemsys)
sorted_entries = mpr.get_entries(chemsys, sort_by_e_above_hull=True)
sorted_entries = mpr.get_entries(chemsys)

elements = {Element(sym) for sym in syms}
for e in entries:
Expand Down

0 comments on commit 2bebfa3

Please sign in to comment.