Skip to content

Commit

Permalink
move get_deep to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rgao committed Feb 6, 2025
1 parent b422a61 commit b44977d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/fairchem/core/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from bisect import bisect
from contextlib import contextmanager
from dataclasses import dataclass
from functools import wraps
from functools import reduce, wraps
from itertools import product
from pathlib import Path
from typing import TYPE_CHECKING, Any
Expand Down Expand Up @@ -1488,3 +1488,14 @@ def get_checkpoint_format(config: dict) -> str:
"dcp",
), f"checkpoint format can only be pt or dcp, found {format}"
return format


def get_deep(dictionary: dict, keys: str, default: str | None = None):
# given a nested dictionary and a dot separated query, retrieve the item
# example:
# get_deep(dictionary{"oc20":{"energy",1}}, keys="oc20.energy") -> 1
return reduce(
lambda d, key: d.get(key, default) if isinstance(d, dict) else default,
keys.split("."),
dictionary,
)

0 comments on commit b44977d

Please sign in to comment.