Primitive way to keep dict in sync with RB.
503138
def _prune(self):
        """"""
        delkeys = [k for k in self.keys() if k not in self.__ringbuffer]
        for k in delkeys:  # necessary because dict is changed during iterations
            super(KRingbuffer,self).__delitem__(k)
268115
def stable_format_dict(d):
    """"""
    inner = ', '.join('{}: {}'.format(repr(k)[1:]
                                      if repr(k).startswith(u"u'") or repr(k).startswith(u'u"')
                                      else repr(k),
                                      v)
                      for k, v in sorted(d.items()))
    return '{%s}' % inner
957763
def combine_dicts(recs):
    """"""
    if not recs:
        return None

    if len(recs) == 1:
        return recs.pop()

    new_rec = {}
    for rec in recs:
        for k, v in rec.iteritems():
            if k in new_rec:
                new_rec[k] = "%s, %s" % (new_rec[k], v)
            else:
                new_rec[k] = v
    return new_rec
204594
def primitive(self):
        """"""
        primitive = {}
        if self.entry_number is not None:
            primitive['entry-number'] = self.entry_number

        if self.item_hash is not None:
            primitive['item-hash'] = self.item_hash

        primitive['timestamp'] = self.timestamp.strftime(fmt)
        return primitive
1041774
def sync(self):
        """"""
        if self.writeback and self.cache:
            super(_TimeoutMixin, self).__delitem__(self._INDEX)
            super(_TimeoutMixin, self).sync()
            self.writeback = False
            super(_TimeoutMixin, self).__setitem__(self._INDEX, self._index)
            self.writeback = True
        if hasattr(self.dict, 'sync'):
            self.dict.sync()
519021
def _nary_dict_update(dicts, **kwargs):
    
    copy = kwargs['copy']
    res = dicts[0].copy() if copy else dicts[0]
    if len(dicts) == 1:
        return res

    # decide what strategy to use when updating a dictionary
    # with the values from another: {(non)recursive} x {(non)overwriting}
    deep = kwargs['deep']
    overwrite = kwargs['overwrite']
    if deep:
        dict_update = curry(_recursive_dict_update, overwrite=overwrite)
    else:
        if overwrite:
            dict_update = res.__class__.update
        else:
            def dict_update(dict_, other):
                for k, v in iteritems(other):
                    dict_.setdefault(k, v)

    for d in dicts[1:]:
        dict_update(res, d)
    return res
1032609
def update_dict(obj, dict, attributes):
    
    for attribute in attributes:
        if hasattr(obj, attribute) and getattr(obj, attribute) is not None:
            dict[attribute] = getattr(obj, attribute)
742648
def unite_dict(a, b):
    
    c = {}
    c.update(a)
    c.update(b)
    return c
365426
def __sync(self):
    """"""
    pad_length = _BLOCK_SIZE - self.__reader.tell() % _BLOCK_SIZE
    if pad_length and pad_length != _BLOCK_SIZE:
      data = self.__reader.read(pad_length)
      if len(data) != pad_length:
        raise EOFError('Read %d bytes instead of %d' %
                       (len(data), pad_length))
815471
def save_dict(self, key: str, my_dict: dict, hierarchical: bool = False):
        """"""
        for _key, _value in my_dict.items():
            if isinstance(_value, dict):
                if not hierarchical:
                    self._db.hmset(key, {_key: json.dumps(_value)})
                else:
                    self.save_dict(key + ':' + _key, _value, hierarchical)
            elif isinstance(_value, list):
                if not hierarchical:
                    self._db.hmset(key, {_key: str(_value)})
                else:
                    print('saving list at ', key + ':' + _key)
                    self._db.lpush(key + ':' + _key, *_value[::-1])
            elif isinstance(_value, bool):
                self._db.hmset(key, {_key: str(_value)})
            else:
                self._db.hmset(key, {_key: _value})
1307879
def _update_dict(self, initial: JSON, other: Mapping) -> JSON:
        
        for key, value in other.items():
            if isinstance(value, collections.Mapping):
                r = self._update_dict(initial.get(key, {}), value)
                initial[key] = r
            else:
                initial[key] = other[key]
        return initial
1329667
def clean_dict(d, test=lambda v: v):
    
    return {k: v for k, v in d.items() if test(v)}
1019084
def _clean_dict(in_dict):
    
    out = {}
    for key, value in iteritems(in_dict):
        if isinstance(value, dict):
            value = _clean_dict(value)
        if value is None:
            continue
        out[key] = value
    return out
971337
def synchronised(func):
        """"""

        # pylint: disable=E0213
        def Wrapper(main_obj, *args, **kwargs):
            main_obj._lock.acquire()  # pylint: disable=W0212
            try:
                return func(main_obj, *args, **kwargs)  # pylint: disable=E1102
            finally:
                main_obj._lock.release()  # pylint: disable=W0212

        return Wrapper
39195
def sorted_dict_repr(d):
    
    keys = list(d.keys())
    keys.sort()
    return "{" + ", ".join(["%r: %r" % (k, d[k]) for k in keys]) + "}"
1106265
def synchronized(sync_lock):
    """"""

    def wrapper(f):
        """The decorator's core function.

        :param f:
        :return:
        """

        @functools.wraps(f)
        def inner_wrapper(*args, **kw):
            """

            :param args:
            :param kw:
            :return:
            """
            with sync_lock:
                return f(*args, **kw)

        return inner_wrapper

    return wrapper
1295860
def sanitize_dict(input_dict):
    
    """
    plain_dict = dict()
    for key in input_dict.keys():
        value = input_dict[key]
        if hasattr(value, 'keys'):
            plain_dict[key] = sanitize_dict(value)
        else:
            plain_dict[key] = value
    return plain_dict