Like _index_list_of_values, but uses get to access and
    returns an empty list if the key is absent.
    Returns d[k] or [d[k]] if the value is not a list
486272
def _get_index_list_of_values(d, k, def_value=None):
    """"""
    v = d.get(k, def_value)
    if v is None:
        return []
    if isinstance(v, list):
        return v
    return [v]
1213985
def _key_to_index(keys, key, single_only=False):
    ''
    if isinstance(key, int): # validate the int index
        try:
            keys[key]
        except IndexError:
            raise KeyError('Index out of range of keys: %s' % (key,))
        if key < 0:
            key += len(keys) # always positive index
        return key
#    keys = d.keys()
    if not single_only:
        if isinstance(key, (tuple, list)):
            return [_key_to_index_single(keys, k) for k in key]

        if isinstance(key, slice):
            start, stop, step = key.start, key.stop, key.step
            try:
                MI_check_index_name(start)
                start = keys.index(start)
            except TypeError:
                pass
            try:
                MI_check_index_name(stop)
                stop = keys.index(stop)
            except TypeError:
                pass
#            return slice(start, stop, step)
            args = slice(start, stop, step).indices(len(keys))
            return force_list(range(*args)) # list of indices
    try:
        return keys.index(key)
    except ValueError: # not IndexError
        raise KeyError('Key not found: %s' % (key,))
696178
def _index_list(key_or_list, direction=None):
    
    if direction is not None:
        return [(key_or_list, direction)]
    else:
        if isinstance(key_or_list, string_type):
            return [(key_or_list, ASCENDING)]
        elif not isinstance(key_or_list, (list, tuple)):
            raise TypeError("if no direction is specified, "
                            "key_or_list must be an instance of list")
        return key_or_list
851324
def indexBy(self, val=None):
        
        if val is None:
            val = lambda *args: args[0]

        def by(result, key, value):
            result[key] = value

        res = self._group(self.obj, val, by)

        return self._wrap(res)
397313
def hmget(self, name, keys, *args):
        
        member_encode = self.memberparse.encode
        keys = [k for k in self._parse_values(keys, args)]
        with self.pipe as pipe:
            f = Future()
            res = pipe.hmget(self.redis_key(name),
                             [member_encode(k) for k in keys])

            def cb():
                f.set([self._value_decode(keys[i], v)
                       for i, v in enumerate(res.result)])

            pipe.on_execute(cb)
            return f
950089
function atKey(list, index) {
        if (!isArray(list.value))
            return;

        // see docs/impl-notes.md#at-1
        list = (!isArray(list.value[0].value) &&
            (list.type === "Expression"))
                ? [list] : list.value;

        var pair, type, key;
        for (var i = 0; i < list.length; i++) {
            pair = list[i];
            // return empty thing if value is missing but key matches:
            if (!isArray(pair) &&
                (Node.compare(index, pair) === 0))
                    return new Node();

            type = pair.type;
            pair = pair.value;
            key  = pair[0];
            if (Node.compare(index, key) === 0)
                return (pair.length < 2) ? pair[1]
                    : newLessList(pair.slice(1), type);
        }
    }
610023
def _get_key(args, kwargs, remove_callback):
    """"""
    # Use tuples, because lists are not hashable.
    weak_args = tuple(_try_weakref(arg, remove_callback) for arg in args)
    # Use a tuple of (key, values) pairs, because dict is not hashable.
    # Sort it, so that we don't depend on the order of keys.
    weak_kwargs = tuple(sorted(
        (key, _try_weakref(value, remove_callback))
        for (key, value) in kwargs.items()))
    return weak_args, weak_kwargs
474521
def lists(self, column, key=None):
        
        columns = self._get_list_select(column, key)

        if key is not None:
            results = {}
            for result in self.get(columns):
                results[result[key]] = result[column]
        else:
            results = Collection(list(map(lambda x: x[column], self.get(columns))))

        return results
133540
def keys(self, index=None):
        
        if index is not None and index not in self._indexes:
            raise ValueError('Index {} does not exist'.format(index))

        db = self._indexes[index][0] if index else self._main_db
        with self._lmdb.begin(db=db) as txn:
            return [
                key.decode()
                for key in txn.cursor().iternext(keys=True, values=False)
            ]
192269
def get_by(cls, field, value):
        '''  '''
        redis = cls.get_redis()
        key = cls.cls_key()+':index_'+field

        id = redis.hget(key, value)

        if id:
            return cls.get(debyte_string(id))

        return None
1207384
def get(self, key, index=None):
        
        records = self.get_multi([key], index=index)

        try:
            return records[0][1]  # return the value from the key/value tuple
        except IndexError:
            return None
246385
def _index_list_of_values(d, k):
    """"""
    v = d[k]
    if isinstance(v, list):
        return v
    return [v]
450683
def get(self, key, default=None, cast=True):
        
        tablename, _, key = key.rpartition(':')
        if tablename and tablename not in self.fields.name.split('+'):
            raise ItsdbError('column requested from wrong table: {}'
                             .format(tablename))
        try:
            index = self.fields.index(key)
            value = list.__getitem__(self, index)
        except (KeyError, IndexError):
            value = default
        else:
            if cast:
                field = self.fields[index]
                value = _cast_to_datatype(value, field)
        return value
1164555
def []=(key, value)
    debug { '[]= ' << key.to_s }

    entry = get_locked_entry(key)
    begin
      return entry.value = value
    ensure
      entry.sync.unlock
    end
  end
39884
def _get_list(self, key, operation, create=False):
        
        return self._get_by_type(key, operation, create, b'list', [])
497156
def safe_get(d, key, def_val=None):
    
    if d.has_key(key):
        return d[key]
    else:
        return def_val
530169
def index(self, value):
        
        # pylint: disable=arguments-differ
        for idx, val in enumerate(self):
            if value == val:
                return idx
        raise ValueError('{0!r} is not in dict'.format(value))