-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implemented search.py and wrote basic tests #86
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ | |
import asyncio | ||
import itertools | ||
import os | ||
|
||
import requests | ||
from unidecode import unidecode | ||
import json | ||
from urllib.request import urlopen | ||
|
||
|
@@ -38,7 +41,8 @@ | |
from .models.team import Team | ||
from .models.user import User | ||
from .utils import (average, fetch, get_current_user, logged_in, | ||
position_converter, scale, team_converter) | ||
position_converter, scale, team_converter, | ||
levenshtein_distance) | ||
|
||
|
||
class FPL: | ||
|
@@ -291,6 +295,61 @@ async def get_players(self, player_ids=None, include_summary=False, | |
|
||
return players | ||
|
||
async def search_players(self, player_name, num_players=1, | ||
include_summary=False, return_json=False): | ||
"""Returns the player(s) given input search term by using Levenshtein distance, | ||
or the mininum number of edits to turn one string into the other. Specifically, | ||
the distance is defined as the minimum of Levenshtein distances from search string | ||
to player's full name (`first_name` and `last_name`) and player's `web_name` | ||
|
||
:param str plauer_name: The player name to search on | ||
:param int num_players: (optional) The number of players to return in the search | ||
:param boolean include_summary: (optional) Includes a player's summary | ||
if ``True``. | ||
:param return_json: (optional) Boolean. If ``True`` returns a list of | ||
``dict``s, if ``False`` returns a list of :class:`Player` | ||
objects. Defaults to ``False``. | ||
""" | ||
|
||
players = getattr(self, "elements") | ||
N = len(players) | ||
search_term = player_name.lower() | ||
min_id = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't used for anything. |
||
min_ed = float('inf') | ||
eds = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
player_ids = [0]*N | ||
for i, player in enumerate(players.values()): | ||
player_ids[i] = player['id'] | ||
full_name = unidecode(player['first_name'] + ' ' + player['second_name']).lower() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be something like first_name = unidecode(player["first_name"])
second_name = unidecode(player["second_name"])
full_name = f"{first_name} {second_name}".lower() |
||
web_name = unidecode(player['web_name']).lower() | ||
ed = min(levenshtein_distance(full_name, player_name), | ||
levenshtein_distance(web_name, player_name)) | ||
Comment on lines
+325
to
+326
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess |
||
|
||
if ed < min_ed: | ||
min_ed = ed | ||
min_id = player['id'] | ||
|
||
eds[player['id']] = ed | ||
|
||
player_ids.sort(key=lambda player_id: eds[player_id]) | ||
|
||
players_list = [] | ||
for i in range(num_players): | ||
player = players[player_ids[i]] | ||
|
||
if include_summary: | ||
player_summary = await self.get_player_summary( | ||
player["id"], return_json=True) | ||
player.update(player_summary) | ||
|
||
players_list.append(player) | ||
|
||
if return_json: | ||
return players_list | ||
else: | ||
return [Player(p, self.session) for p in players_list] | ||
|
||
|
||
async def get_fixture(self, fixture_id, return_json=False): | ||
"""Returns the fixture with the given ``fixture_id``. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,6 +186,7 @@ async def test_init(self, loop): | |
|
||
async def test_get_gameweek_history_unknown_gameweek_cached( | ||
self, loop, user): | ||
print(user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be removed. |
||
history = await user.get_gameweek_history() | ||
assert history is user._history["current"] | ||
assert isinstance(history, list) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't used for anything.