Breakdown it into modular components will help in the future to comprehend a similar code:
def flatFolders(rootPath):
'''
Given a root path (folder) containing deeply nested folders,
returns a dictionary {folder1:[files of folder1],
folder2:[files of folder2], ...}
'''
foldersToFiles = {}
...:
path = ...
files = ...
folders[path] = files
return foldersToFiles
def cartesianSelfProduct(lst):
'''
cartesianSelfProduct([1,2,3]) ->
[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
'''
return [(x,y) for x in lst for y in lst]
def flatFolderPairs(rootPath):
foldersToFiles = flatFolders(rootPath)
return {folder:cartesianSelfProduct(files) for folder,files in foldersToFiles}
source
share