Beispiel #1
0
def team_search(name_search, limit=3, **kwargs):
    """Search database for a team by team name.

    Args:
        session (Session): Database session object.
        name_search (str): Team name to search for.
        threshold (int, optional): Levenshtein ratio cutoff.  Defaults to 
            80.

    Returns:
        Team object from database that matches the name.

    Raises:
        SearchError: If no match above 'threshold' is found.
    """

    ## Query database or use provided team list
    session = None
    team_list = None
    if "team_list" in kwargs:
        team_list = kwargs['team_list']
    elif "session" in kwargs:
        session = kwargs['session']
        team_list = Team.from_db(session)
    else:
        raise TypeError('Must provided either session=' +
                        'or team_list= parameters')
    
    ## Look for matches for the team
    choices = [team.name for team in team_list]
    matches = process.extract(name_search, choices, limit=limit)

    ## Get corresponding Team object for the results
    search_results = []
    for match in matches:
        for team in team_list:
            if team.name == match[0]:
                search_results.append((team, match[1]))

    ## Sort results
    search_results.sort(key=lambda x: x[1], reverse=True)
    return search_results