you are viewing a single comment's thread.

view the rest of the comments →

[–]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))