minmin
03/18/2025, 11:04 AMbreakfast:
meal_name: ${parent_name:}
lunch:
meal_name: ${parent_name:}
then breakfast.meal_name would return 'breakfast'
my motivation is simple: when namespacing pipelines the parents are part of the namespace, and sometimes I want access to those names in a node - usually just to have a column label. e.g:
node(
func = lambda df, x: df.assign('meal' == x),
inputs = ["<dataframe output from node above>", "params:meal_name"],
...
),
I found this for omegaconf, but couldn't make it work for Kedro's omegaconfigloader.
https://github.com/omry/omegaconf/discussions/937Hall
03/18/2025, 11:04 AMNok Lam Chan
03/18/2025, 11:29 AMminmin
03/18/2025, 1:35 PMCONFIG_LOADER_CLASS = OmegaConfigLoader
def parent_name_impl(_parent_) -> str:
return _parent_._key()
def grandparent_name_impl(_parent_) -> str:
return _parent_._get_parent()._key()
CONFIG_LOADER_CLASS._register_new_resolvers({"parent_name": parent_name_impl,
"grandparent_name": grandparent_name_impl})
then in the parameters:
park:
playground:
swing: ${parent_name:} # returns "playground"
slide: ${grandparent_name:} # returns "park"
Nok Lam Chan
03/18/2025, 1:36 PMNok Lam Chan
03/18/2025, 1:37 PMNok Lam Chan
03/18/2025, 1:43 PMdef date_today():
return date.today()
CONFIG_LOADER_ARGS = {
"custom_resolvers": {
"add": lambda *my_list: sum(my_list),
"polars": lambda x: getattr(pl, x),
"today": lambda: date_today(),
}
}
The lambda is redundant, what you really need is a dictionary of callable. So it should be:
def date_today():
return date.today()
CONFIG_LOADER_ARGS = {
"custom_resolvers": {
"add": lambda *my_list: sum(my_list),
"polars": lambda x: getattr(pl, x),
"today": date_today,
}
}
minmin
03/18/2025, 1:54 PM"parent_name": lambda: parent_name_impl
kedro would complain that that I was missing a parameter:
TypeError: parent_name_impl() missing 1 required positional argument: '_parent_'
but if I stick in an input:
"parent_name": lambda x: parent_name_impl(x)
that obviously doesn't make any sense because it then expects me to give it an input in the parameters.yml, which then overrides parent completely
.......but.......
I took your advice and tried it without the lambda and it worked!
CONFIG_LOADER_ARGS = {
"custom_resolvers": {
"grandparent_name": grandparent_name_impl,
"parent_name": parent_name_impl,
},
I always thought you needed the lambda (like you say, it's not super clear from the documentation that that isn't the case) but I am glad this works. Thanks so much for your help with this.Nok Lam Chan
03/19/2025, 2:17 PM