For a config validation schema I need to have a nested dictionary which repeats itself. For example assume we have the following dictionary:
{
"conf1": "x",
"conf2": "x",
}
Now I want to have a key conf3
which contains the existing dictionary recursively for a maximum amount of iterations (i.e. 3 times)
So the final result needs to look like this:
{
"conf1": "x",
"conf2": "x",
"conf3": {
"conf1": "x",
"conf2": "x",
"conf3": {
"conf1": "x",
"conf2": "x",
}
}
}
I tried to write a recursive function, but with this solution conf3
is repeated indefinitely
def build_nested_configuration_schema(schema: dict, iteration: int = 0) -> dict:
iteration += 1
if iteration == 3:
return schema
schema.update({"conf3": build_nested_configuration_schema(schema, iteration)})
return schema
new_config = build_nested_configuration_schema({"conf1": "x", "conf2": "x"})