def _zset_to_keys(self, key, values=None, alpha=False):
        """
        Convert a redis sorted set to a list of keys, to be used by sort.
        Each key is on the following format, for each value in the sorted set:
            ramdom_string:value-in-the-sorted-set => score-of-the-value
        The random string is the same for all keys.
        If values is not None, only these values from the sorted set are saved
        as keys.
        If a value in values is not on the sorted set, it's still saved as a key
        but with a default value ('' is alpha is True, else '-inf')
        """
        conn = self.cls.get_connection()
        default = '' if alpha else '-inf'
        if values is None:
            # no values given, we get scores from the whole sorted set
            result = conn.zrange(key, start=0, end=-1, withscores=True)
            values = list(islice(chain.from_iterable(result), 0, None, 2))
        else:
            # we have values, we'll get only their scores

            if isinstance(self.cls.database, PipelineDatabase):
                # if available, use the pipeline of our database to get all
                # scores in one redis call
                with self.cls.database.pipeline(transaction=False) as pipe:
                    for value in values:
                        pipe.zscore(key, value)
                    scores = pipe.execute()
            else:
                # no pipeline, we have to do a call for each value
                scores = []
                for value in values:
                    scores.append(conn.zscore(key, value))

            # combine values and scores in one list
            result = []
            for index, value in enumerate(values):
                score = scores[index]
                if score is None:
                    score = default
                result.append((value, score))

        # create a temporary key for each (value,score) tuple
        base_tmp_key = self._unique_key()
        conn.set(base_tmp_key, 'working...')  # only to "reserve" the main tmp key
        tmp_keys = []
        # use a mapping dict (tmp_key_with_value=>score) to use in mset
        mapping = {}
        for value, score in result:
            tmp_key = '%s:%s' % (base_tmp_key, value)
            tmp_keys.append(tmp_key)
            mapping[tmp_key] = score
        # set all keys in one call
        conn.mset(mapping)

        return base_tmp_key, tmp_keys
def _zset_to_keys ( self , key , values = None , alpha = False ) : conn = self . cls . get_connection ( ) default = '' if alpha else '-inf' if values is None : result = conn . zrange ( key , start = 0 , end = - 1 , withscores = True ) values = list ( islice ( chain . from_iterable ( result ) , 0 , None , 2 ) ) else : if isinstance ( self . cls . database , PipelineDatabase ) : with self . cls . database . pipeline ( transaction = False ) as pipe : for value in values : pipe . zscore ( key , value ) scores = pipe . execute ( ) else : scores = [ ] for value in values : scores . append ( conn . zscore ( key , value ) ) result = [ ] for index , value in enumerate ( values ) : score = scores [ index ] if score is None : score = default result . append ( ( value , score ) ) base_tmp_key = self . _unique_key ( ) conn . set ( base_tmp_key , 'working...' ) tmp_keys = [ ] mapping = { } for value , score in result : tmp_key = '%s:%s' % ( base_tmp_key , value ) tmp_keys . append ( tmp_key ) mapping [ tmp_key ] = score conn . mset ( mapping ) return base_tmp_key , tmp_keys
Convert a redis sorted set to a list of keys, to be used by sort.
        Each key is on the following format, for each value in the sorted set:
            ramdom_string:value-in-the-sorted-set => score-of-the-value
        The random string is the same for all keys.
        If values is not None, only these values from the sorted set are saved
        as keys.
        If a value in values is not on the sorted set, it's still saved as a key
        but with a default value ('' is alpha is True, else '-inf')