+ cls = indexed_tables[record['table']]
+ obj = session.query(cls).get(record['row_id'])
+
+ results.append(LookupResult(object=obj,
+ name=record['display_name'],
+ language=record['language'],
+ iso3166=record['iso3166'],
+ exact=exact))
+
+ return results
+
+
+def lookup(input, valid_types=[], session=None, indices=None, exact_only=False):
+ """Attempts to find some sort of object, given a database session and name.
+
+ Returns a list of named (object, name, language, iso3166, exact) tuples.
+ `object` is a database object, `name` is the name under which the object
+ was found, `language` and `iso3166` are the name and country code of the
+ language in which the name was found, and `exact` is True iff this was an
+ exact match.
+
+ This function currently ONLY does fuzzy matching if there are no exact
+ matches.
+
+ Formes are not returned unless requested; "Shaymin" will return only grass
+ Shaymin.
+
+ Extraneous whitespace is removed with extreme prejudice.
+
+ Recognizes:
+ - Names: "Eevee", "Surf", "Run Away", "Payapa Berry", etc.
+ - Foreign names: "Iibui", "Eivui"
+ - Fuzzy names in whatever language: "Evee", "Ibui"
+ - IDs: "133", "192", "250"
+ Also:
+ - Type restrictions. "type:psychic" will only return the type. This is
+ how to make ID lookup useful. Multiple type specs can be entered with
+ commas, as "move,item:1". If `valid_types` are provided, any type prefix
+ will be ignored.
+ - Alternate formes can be specified merely like "wash rotom".
+
+ `input`
+ Name of the thing to look for.
+
+ `valid_types`
+ A list of table objects or names, e.g., `['pokemon', 'moves']`. If
+ this is provided, only results in one of the given tables will be
+ returned.
+
+ `session`
+ A database session to use for retrieving objects. As with get_index,
+ if this is not provided, a connection to the default database will be
+ attempted.
+
+ `indices`
+ Tuple of index, speller as returned from `open_index()`. Defaults to
+ a call to `open_index()`.
+
+ `exact_only`
+ If True, only exact matches are returned. If set to False (the
+ default), and the provided `name` doesn't match anything exactly,
+ spelling correction will be attempted.
+ """
+
+ if not session:
+ session = connect()