Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Recursive function missing results

Coming from Python recursively appending list function Trying to recursively get a list of permissions associated with a file structure.

I have this function:

def get_child_perms(self, folder, request, perm_list):
        # Folder contains other folders
        if folder.get_children():
            # For every sub-folder
            return [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
        return folder.has_read_permission(request)

That returns all the results except the folders that contain other folders.

folder <- Missing (allowed)
    subfolder <- Missing (restricted)
        subsubfolder <- Get this (restricted)
            files

Output from function would be [True, False, False]

another case would be, where A = allowed, R = restricted

folder  A
    subfolder   A
        subsubfolder    R
            files
        files
    subfolder   R
        files
    subfolder   A
        subsubfolder    A
            files
        files
    subfolder   A
        files
    files

Output would be [True,True,False,False,True,True,True]

like image 767
Philippe Fisher Avatar asked Feb 12 '26 10:02

Philippe Fisher


1 Answers

The basic issue occurs you are only returning the folder permission , when folder does not have any children , when it has children, you are not including the folder.has_read_permission(request) in your return result , which is most probably causing you issue. You need to do -

def get_child_perms(self, folder, request, perm_list):
        # Folder contains other folders
        if folder.get_children():
            # For every sub-folder
            return [folder.has_read_permission(request)] + [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
        return [folder.has_read_permission(request)]

This should result in (not tested) -

[folderperm [subfolderperm [subsubfolderperm]]

like image 135
Anand S Kumar Avatar answered Feb 15 '26 17:02

Anand S Kumar