Is there a recommended way of saving MPDataDoc objects returned by MPRester offline?
I tried pickling it but that returns an error:
PicklingError: Can't pickle <class 'pydantic.main.MPDataDoc'>: attribute lookup MPDataDoc on pydantic.main failed
pydantic documentation suggests using the model.json()
method but that returns:
TypeError: Object of type 'Structure' is not JSON serializable
Is there a convenience function to store a query locally as is.
Hi @R_Walser, sorry for the late reply – hope you were able to figure this out. In case this helps anyone else in the future, I prefer to use the saving functionality from the monty
package, which we use in pymatgen to serialize/deserialize objects and dump objects to JSON files. For example:
from mp_api.client import MPRester
from monty.serialization import dumpfn, loadfn
with MPRester() as mpr:
example = mpr.summary.search(material_ids=["mp-49"], fields=["structure", "material_id"])[0]
dumpfn(example, "example_mp_doc.json.gz")
You can then reload this object later by using the corresponding loading function:
example = loadfn("example_mp_doc.json.gz")
2 Likes