Skip to content

Accessing key name of parent #937

Answered by Jasha10
jzazo asked this question in Q&A
May 18, 2022 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Hi @jzazo!
I hope you don't mind using undocumented/internal OmegaConf methods, because I don't think this can be achieved with OmegaConf's public API.

Custom resolvers can take a special _parent_ argument (this part is documented, just before the "Built in Resolvers" section). This, in combination with the undocumented _key() method, will be enough to do the job:

from omegaconf import OmegaConf

def parent_name_impl(_parent_) -> str:
    return _parent_._key()

OmegaConf.register_new_resolver("parent_name", parent_name_impl)

cfg = OmegaConf.create({"john": {"height": 180, "weight": 75, "player": "${parent_name:}"}})
assert cfg.john.player == "john"

The standard caveats for undocumented/…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@jzazo
Comment options

@jzazo
Comment options

@Jasha10
Comment options

@jzazo
Comment options

Answer selected by jzazo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants