pypi-metadata / store /

from utils.char import utf8getter

def enumerate(sequence):
    return [(i, sequence[i]) for i in range(len(sequence))]

class ResultRow:
    '''Turn a tuple of row values into something that may be looked up by
    both column index and name.

    Also, convert any unicode values coming out of the database into UTF-8
    encoded 8-bit strings.
    def __init__(self, cols, info=None):
        self.cols = cols
        self.cols_d = {}
        for i, col in enumerate(cols):
            self.cols_d[col] = i = info
    def __getitem__(self, item):
        if isinstance(item, int):
            value =[item]
            n = self.cols_d[item]
            value =[n]
        return self.decode(value)
    def __nonzero__(self):
        return bool(
    def items(self):
        return [(col, self.decode([i]))
                for i, col in enumerate(self.cols)]
    def as_dict(self):
        d = {}
        for i, col in enumerate(self.cols):
            d[col] = self.decode([i])
        return d
    def keys(self):
        return self.cols
    def values(self):
        return map(self.decode,

    def decode(self, value):
        if value is None:
            return value
        if isinstance(value, str):
            # decode strings stored as utf-8 into unicode
            return value.decode('utf-8')
        return value

def itemgetter(n):
    return lambda fields:fields[n]

def FastResultRow(cols):
    """Create a ResultRow-like class that has all fields already preparsed.
    Non-UTF-8-String columns must be suffixed with !."""
    getters = {}
    _keys = []
    for i, col in enumerate(cols.split()):
        if col[-1] == '!':
            col = col[:-1]
            getter = itemgetter(i)
            getter = utf8getter(i)
        getters[i] = getters[col] = getter
    class _FastResultRow:
        _getters = getters
        cols = _keys

        def __init__(self, cols, info):
   = info

        def __getitem__(self, index):
                if is None:
                    raise KeyError
                return self._getters[index](
            except KeyError:
                if isinstance(index, int):
                    raise IndexError, 'row index out of range'

        def __len__(self):
            return len(

        def __nonzero__(self):
            return bool(

        def as_dict(self):
            res = {}
            for key in self.cols:
                res[key] = self[key]
            return res

        def keys(self):
            return self.cols

        def values(self):
            res = [None] * len(
            for i in xrange(len(
                res[i] = self[i]
            return res

        def items(self):
            res = [None] * len(
            for i, col in enumerate(self.cols):
                res[i] = (col, self[col])
            return res

    return _FastResultRow

def Result(cols, sequence, type=ResultRow):
    return [type(cols, item) for item in iter(sequence)]