omegaconf icon indicating copy to clipboard operation
omegaconf copied to clipboard

Add a Get_leaves_str() function for printing all the leave values in an OmegaConf object

Open songyuc opened this issue 3 years ago • 1 comments

Describe the solution you'd like I want to print all the leave values of an OmegaConf object, so that I can check them directly.

Additional context Thanks sincerely for the help from @Jasha10, and here is a simple implementation as:

def get_leaves_str(cfg, parent=None):
    if not OmegaConf.is_config(cfg):
        assert parent, cfg
        return [f"{parent} = {str(cfg)}"]
    if OmegaConf.is_list(cfg):
        keys = range(len(cfg))
    elif OmegaConf.is_config(cfg):
        keys = cfg.keys()
    else:
        raise NotImplementedError
    string = []
    parent = f"{parent}." if parent else ""
    for k in keys:
        child_key = f"{parent}{k}"
        v = cfg[k] if not OmegaConf.is_missing(cfg, k) else "???"
        string += get_leaves_str(v, child_key)
    return string

songyuc avatar Jun 18 '22 06:06 songyuc

Cross-linking to related discussion: https://github.com/omry/omegaconf/discussions/961

Jasha10 avatar Jun 21 '22 03:06 Jasha10

I think this is not useful enough as an API. Given the lack of community attention in the last 6 months, I think it's safe to close.

omry avatar Jan 10 '23 19:01 omry