From 30c78107afeb17d20ac510463f8634fae7265d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eli=C3=A1n=20Hanisch?= Date: Wed, 4 Jul 2012 19:36:19 -0300 Subject: [PATCH] added FuzzyDict class, gives a more flexible matching of the time units. --- Bantracker/plugin.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Bantracker/plugin.py b/Bantracker/plugin.py index 1c09bab..f82f2cb 100644 --- a/Bantracker/plugin.py +++ b/Bantracker/plugin.py @@ -79,14 +79,27 @@ def fromTime(x): return cPickle.dumps(datetime.datetime(*time.gmtime(x)[:6], **{'tzinfo': pytz.timezone("UTC")})) -timeUnits = { - 'm': 60, 'minutes': 60, 'minute': 60, 'min': 60, - 'h': 3600, 'hours': 3600, 'hour': 3600, - 'd': 86400, 'days': 86400, 'day': 86400, - 'w': 604800, 'weeks': 604800, 'week': 604800, - 'M': 2592000, 'months': 2592000, 'month': 2592000, - 'y': 31536000, 'years': 31536000, 'year': 31536000, - } +class FuzzyDict(dict): + def __getitem__(self, k): + try: + return dict.__getitem__(self, k) + except KeyError: + # ok, lets find the closest match + n = len(k) + keys = [ s for s in self if s[:n] == k ] + if len(keys) != 1: + # ambiguous + raise + return dict.__getitem__(self, keys[0]) + +timeUnits = FuzzyDict({ + 'minutes': 60, 'm': 60, + 'hours' : 3600, 'M': 2592000, + 'days' : 86400, + 'weeks' : 604800, + 'months' : 2592000, + 'years' : 31536000, + }) def readTimeDelta(s): """convert a string like "2 days" or "1h2d3w" into seconds"""