pymatgen.io.abinit.db module¶
Objects and helper function used to store the results in a MongoDb database
-
class
DBConnector(**kwargs)[source]¶ Bases:
object
-
mongo_getattr(rec, key)[source]¶ Get value from dict using MongoDB dot-separated path semantics. For example:
>>> assert mongo_getattr({'a': {'b': 1}, 'x': 2}, 'a.b') == 1 >>> assert mongo_getattr({'a': {'b': 1}, 'x': 2}, 'x') == 2 >>> assert mongo_getattr({'a': {'b': 1}, 'x': 2}, 'a.b.c') is None
Parameters: - rec – mongodb document
- key – path to mongo value
- default – default to return if not found
Returns: value, potentially nested, or default if not found
Raise: AttributeError, if record is not a dict or key is not found.
-
scan_nestdict(d, key)[source]¶ Scan a nested dict d, and return the first value associated to the given key. Returns None if key is not found.
>>> d = {0: 1, 1: {"hello": {"world": {None: [1,2,3]}}}, "foo": [{"bar": 1}, {"color": "red"}]} >>> assert scan_nestdict(d, 1) == {"hello": {"world": {None: [1,2,3]}}} >>> assert scan_nestdict(d, "hello") == {"world": {None: [1,2,3]}} >>> assert scan_nestdict(d, "world") == {None: [1,2,3]} >>> assert scan_nestdict(d, None) == [1,2,3] >>> assert scan_nestdict(d, "color") == "red"