From 0e7af57977aa6af552f0bcb7c96461303d49928b Mon Sep 17 00:00:00 2001 From: Shreyas Date: Fri, 17 Nov 2023 14:00:35 -0500 Subject: [PATCH] [ignore] Addition of comments to the listify code in the 'listify_plugin' --- plugins/filter/listify.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/filter/listify.py b/plugins/filter/listify.py index 89daf0154..13d280937 100644 --- a/plugins/filter/listify.py +++ b/plugins/filter/listify.py @@ -227,11 +227,17 @@ """ +# This function takes a dictionary and a series of keys, +# and returns a list of dictionaries using recursive helper function 'listify_worker' def listify(d, *keys): return list(listify_worker(d, keys, 0, {}, "")) +# This function walks through a dictionary 'd', depth-first, +# using the keys provided, and generates a new dictionary for each key:value pair encountered def listify_worker(d, keys, depth, cache, prefix): + # The prefix in the code is used to store the path of keys traversed in the nested dictionary, + # which helps to generate unique keys for each value when flattening the dictionary. prefix += keys[depth] + "_" if keys[depth] in d: @@ -240,10 +246,12 @@ def listify_worker(d, keys, depth, cache, prefix): if isinstance(item, dict): for k, v in item.items(): if isinstance(v, list) and all(isinstance(x, (str, int, float, bool, bytes)) for x in v) or not isinstance(v, (dict, list)): + # The cache in this code is a temporary storage that holds key-value pairs as the function navigates through the nested dictionary. + # It helps to generate the final output by remembering the traversed path in each recursive call. cache_key = prefix + k cache_value = v cache_work[cache_key] = cache_value - + # If we're at the deepest level of keys if len(keys) - 1 == depth: yield cache_work else: