espnet2.main_funcs.pack_funcs.find_path_and_change_it_recursive
Less than 1 minute
espnet2.main_funcs.pack_funcs.find_path_and_change_it_recursive
espnet2.main_funcs.pack_funcs.find_path_and_change_it_recursive(value, src: str, tgt: str)
Recursively find and replace a specified source path with a target path in a
given data structure.
This function can handle nested dictionaries and lists, changing all instances of the source path to the target path if they are found.
- Parameters:
- value (Union *[*dict , list , tuple , str ]) – The data structure to search through, which can be a dictionary, list, tuple, or string.
- src (str) – The source path to search for.
- tgt (str) – The target path to replace the source path with.
- Returns: The modified data structure with all : occurrences of the source path replaced by the target path.
- Return type: Union[dict, list, tuple, str]
Examples
>>> find_path_and_change_it_recursive({'path': '/old/path'}, '/old/path',
... '/new/path')
{'path': '/new/path'}
>>> find_path_and_change_it_recursive(['/old/path', '/another/path'],
... '/old/path', '/new/path')
['/new/path', '/another/path']
>>> find_path_and_change_it_recursive('This is a string with /old/path',
... '/old/path', '/new/path')
'This is a string with /new/path'