import ast


def filter_token_by_type(token_type, tokens):
    """
    Filter tokens by types (function, variable, etc)
    """
    if token_type == 'function':
        return lambda token: isinstance(token, ast.FunctionDef) and not \
            (token.name.startswith('__') and
             token.name.endswith('__'))
    elif token_type == 'variable':
        return lambda token: isinstance(token, ast.Name)
    else: return lambda token: False