From: Eevee Date: Tue, 21 Jul 2009 07:12:25 +0000 (-0700) Subject: Stubbing out a lookup function. #15 X-Git-Tag: veekun-promotions/2010050901~247 X-Git-Url: http://git.veekun.com/zzz-pokedex.git/commitdiff_plain/a846073cb5288638fa103eade44098fa86d679d8?ds=sidebyside;hp=ef580770c5935192a261e4d0e5a27f2d95def396 Stubbing out a lookup function. #15 --- diff --git a/pokedex/lookup.py b/pokedex/lookup.py new file mode 100644 index 0000000..90fb3d3 --- /dev/null +++ b/pokedex/lookup.py @@ -0,0 +1,31 @@ +# encoding: utf8 +from sqlalchemy.sql import func + +import pokedex.db.tables as tables + +def lookup(session, name): + """Attempts to find some sort of object, given a database session and name. + + Returns a list of (object, matchiness) tuples. Matchiness is 1 for exact + matches. It is possible to get multiple exact matches; for example, + 'Metronome' will match both the move and the item. In these cases, the + results are returned in rough order of "importance", e.g., Pokémon come + before moves come before types. + + This function does fuzzy matching iff there are no exact matches. + + Formes are not returned; "Shaymin" will return only grass Shaymin. + + Currently recognizes: + - Pokémon names: "Eevee" + """ + + q = session.query(tables.Pokemon) \ + .filter(func.lower(tables.Pokemon.name) == name.lower()) \ + .filter_by(forme_base_pokemon_id=None) + + try: + result = q.one() + return [ (result, 1) ] + except: + return []