danoan.perchance_tools.core.utils module

danoan.perchance_tools.core.utils.collect_key_path(source: WordDict, target_key: str)[source]

Finds all the sequences of dictionary keys leading to some target key.

>>> d = {
...     'root':
...     {
...         'Category_A':
...         {
...             'Category_B1':
...             {
...                 'words':
...                 [
...                     'word_a',
...                     'word_b',
...                     'word_c'
...                 ]
...             },
...             'Category_B2':
...             {
...                 'words':
...                 [
...                     'word_d',
...                     'word_e'
...                 ]
...             }
...         }
...     }
... }
>>> w = model.WordDict(d)
>>> L = list(collect_key_path(w,'words'))
>>> assert( L[0]['path'] == ['root', 'Category_A', 'Category_B1'] )
>>> assert( L[0]['words'] == ['word_a', 'word_b', 'word_c'] )
Parameters: