Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically instantiate MPContribs client #870

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions mp_api/client/mprester.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading