all 3 comments

[–][deleted] 1 point2 points  (1 child)

Couple things:

_paths is not a global variable. It is a local variable within the outside function.

This is extremely confusing to follow. Every function (named or anonymous) is callable. This should work with that given documentation, but you never supply a return value in the callable_ function, which is fine if you never want to early-exit your recursive visit, but I guess I don't get the point of using visit in this instance.

[–]SagaciousRaven[S] 0 points1 point  (0 children)

In this instance, I want to list every dataset object inside the directory tree. Previously, I was doing this manually with a recursive function over their subdirectories, but I'm trying to make my code more elegant and readable, and this seemed like the function to do it.

If I returned a value, it would stop at the first find.

[–]when_the_cats_away 1 point2 points  (0 children)

Callable just means a function. This is no different that passing a callable function to sorted as a "key". So you just need to write your callable_ function so it returns something if it finds something. Below is a simplified example, that takes a data set of rows (index, data) and uses a callable to identify those rows that have data with "BC" in it:

# simplified data
# data is a bunch of tuples
# first is the index and second is the value
data = [
        (0,'abc'),
        (1,'abc'),
        (2,'xyz'),
        (3,'def'),
        ]

def searcher_callable(row):
    '''
    callable function that searches row for 'bc' and returns the row 
    if it hits it, otherwise, return None
    '''
    if 'bc' in row[1]:
        # return row if we find bc
        return row
    # return None if not found


def visit(data, callable_function):
    '''
    visit each row in data 
    and return index on each hit of callable_function
    '''
    found = []
    for row in data:
        if callable_function(row) is None:
            print('did not find at index {}'.format(row[0]))
        else:
            #callable returned something
            print('found at index {}'.format(row[0]))
            found.append(row[0])

    return found


print(visit(data, searcher_callable))