Skip to content

Commit

Permalink
Implement cache with fuzzy matching nicks
Browse files Browse the repository at this point in the history
  • Loading branch information
Moetto committed May 14, 2017
1 parent 084c5fb commit 17cbcbf
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
python
__pycache__
.idea
39 changes: 39 additions & 0 deletions cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import datetime

import logging
from fuzzywuzzy import fuzz


class Cache:
def __init__(self, key, data, case_insensitive=True):
self.data_store = {}
self.case_insensitive = case_insensitive
for entity in data:
key_val = entity[key]
if not case_insensitive:
key_val = key_val.lower()
self.data_store[key_val] = entity
self.timestamp = datetime.datetime.now()

def get(self, key):
if self.case_insensitive:
return self.data_store[key.lower()]
return self.data_store[key]

def fuzzy_get(self, key):
try:
return self.get(key)
except KeyError:
pass
if self.case_insensitive:
key = key.lower()
fuzzes = [(data_key, fuzz.partial_ratio(data_key, key)) for data_key in self.data_store.keys()]
fuzzes = sorted(fuzzes, key=lambda key_ratio_pair: key_ratio_pair[1])
return self.data_store[fuzzes[-1][0]]

def is_valid(self):
if (datetime.datetime.now() - self.timestamp).total_seconds() > 30:
logging.debug("Cache is too old")
return False
logging.debug("Cache ok")
return True
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def game(bot, update):


def rank(bot, update):
logging.info("Rank query")
logging.debug(update.message.text)
try:
nick = update.message.text.split("/rank ", 1)[1]
Expand Down
2 changes: 2 additions & 0 deletions pip_requirements
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
python-telegram-bot
requests
fuzzywuzzu
python-Levenshtein
15 changes: 9 additions & 6 deletions player.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging

from api import API
from cache import Cache


class Player:
NICK = "name"
RANK = "rank"
SCORE = "score"
_cache = None

def __init__(self, nick, rank, score):
self.nick = nick
Expand All @@ -13,12 +17,11 @@ def __init__(self, nick, rank, score):

@classmethod
def by_nick(cls, nick):
json_data = API.get_players()
player_data = None
for p in json_data:
if p[Player.NICK] == nick:
player_data = p
break
if Player._cache is None or not Player._cache.is_valid():
logging.info("Getting newer data from server")
json_data = API.get_players()
Player._cache = Cache(Player.NICK, json_data)
player_data = Player._cache.fuzzy_get(nick)
return cls(player_data[Player.NICK], player_data[Player.RANK], player_data[Player.SCORE])

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from player import Player


print(Player.by_nick("T3mu"))
print(Player.by_nick("T4,mu"))

0 comments on commit 17cbcbf

Please sign in to comment.