'Lazy lookup values.
@param values: list of values to lookup
@param rows: rows referenced by values (if values are foreign keys)
optional'

def _lookup(self, values, rows=None):

    theset = self.theset
    keys = {}
    items = {}
    lookup = {}
    table = self.table
    for _v in values:
        v = _v
        if ((v is not None) and table and isinstance(v, basestring)):
            try:
                v = int(_v)
            except ValueError:
                pass
        keys[v] = _v
        if (v is None):
            items[_v] = self.none
        elif (v in theset):
            items[_v] = theset[v]
        else:
            lookup[v] = True
    if ((table is None) or (not lookup)):
        return items
    if (table and self.hierarchy):
        from s3hierarchy import S3Hierarchy
        h = S3Hierarchy(table._tablename)
        if h.config:
            def lookup_parent(node_id):
                parent = h.parent(node_id)
                if (parent and (parent not in theset) and (parent not in lookup)):
                    lookup[parent] = False
                    lookup_parent(parent)
                return
            for node_id in lookup.keys():
                lookup_parent(node_id)
        else:
            h = None
    else:
        h = None
    pkey = self.key
    ogetattr = object.__getattribute__
    try:
        key = ogetattr(table, pkey)
    except AttributeError:
        return items
    pop = lookup.pop
    represent_row = self.represent_row
    represent_path = self._represent_path
    if (rows and (not self.custom_lookup)):
        rows_ = dict(((row[key], row) for row in rows))
        self.rows.update(rows_)
        for row in rows:
            k = row[key]
            if (k not in theset):
                if h:
                    theset[k] = represent_path(k, row, rows=rows_, hierarchy=h)
                else:
                    theset[k] = represent_row(row)
            if pop(k, None):
                items[keys.get(k, k)] = theset[k]
    if lookup:
        if (not self.custom_lookup):
            try:
                fields = [ogetattr(table, f) for f in self.fields]
            except AttributeError:
                current.log.error(sys.exc_info()[1])
                fields = [ogetattr(table, f) for f in self.fields if hasattr(table, f)]
        else:
            fields = []
        rows = self.lookup_rows(key, lookup.keys(), fields=fields)
        rows = dict(((row[key], row) for row in rows))
        self.rows.update(rows)
        if h:
            for (k, row) in rows.items():
                if lookup.pop(k, None):
                    items[keys.get(k, k)] = represent_path(k, row, rows=rows, hierarchy=h)
        else:
            for (k, row) in rows.items():
                lookup.pop(k, None)
                items[keys.get(k, k)] = theset[k] = represent_row(row)
    if lookup:
        for k in lookup:
            items[keys.get(k, k)] = self.default
    return items
