recursion - Python recursively appending list function -
having issues recursive function should relatively simple do, can`t seem right.
i have folder structure folders can contain other folders, images or files. associated each folder there permissions. want have function recursively build list of permissions associated each folder.
i have function has_read_permission(request)
returns true
if folder has permissions , false
if not
i have built function so:
def get_child_perms(self, folder, request, perm_list): # folder contains other folders if folder.get_children(): # every sub-folder subfolder in folder.get_children(): return perm_list.append(self.get_child_perms(subfolder, request, perm_list)) else: # if folder doesn't have sub-folders containing folders return [folder.has_read_permission(request)]
i keep getting none
given folders so:
folder (allowed) - wont check 1 |_first folder (allowed) | |_first sub folder (restricted) | | |_file | | |_file | | |_image | |__second sub folder (allowed) |_second folder (allowed)
then running get_child_perms()
return [true,false,true,true] or [true, [false, true], true]
edit
disregard edit -> asked other question python recursive function missing results
changed abit,
def get_child_perms(self, folder, request, perm_list): if folder.get_children(): subfolder in folder.get_children(): perm_list.append(self.get_child_perms(subfolder, request, perm_list)) return perm_list else: return [folder.has_read_permission(request)]
getting:
[[true], [...], [true], [...], [...], [true], [true], [true], [true], [true], [true], [true], [true], [...], [true], [...]] admin -folder 1 - files -folder 2 - files -folder 3 - files -folder 4 - subfolder 1 -subsubfolder 1 - files - files - subfolder 2 - subsubfolder 2 - files - files -folder 5 - subfolder 3 - files - subfolder 4 - files - subfolder 5 -files - files -folder 6 - files -folder 7 - subfoler 6 - files - files -folder 8 - files
as pointed out append inplace operation returns none, can avoid using append
@ returning list comp:
def get_child_perms(self, folder, request, perm_list): # folder contains other folders children = folder.get_children() if children: # every sub-folder return [self.get_child_perms(subfolder, request, perm_list) subfolder in children] return [folder.has_read_permission(request)]
you don't need else either can return once function.
Comments
Post a Comment