Get the length of a sorted set in Redis

    CLI Example:

    .. code-block:: bash

        salt '*' redis.zcard foo_sorted
491333
def zcard(key, host=None, port=None, db=None, password=None):
    '''
    
    '''
    server = _connect(host, port, db, password)
    return server.zcard(key)
776701
def length(self):
        
        total_length = 0
        for location_a, location_b in zip(
                self.locations[:-1], self.locations[1:]):
            total_length += Line(location_a, location_b).length
        return total_length
35963
def zrank(self, name, value):
        
        with self.pipe as pipe:
            value = self.valueparse.encode(value)
            return pipe.zrank(self.redis_key(name), value)
1161634
def get_rank(self, member, reverse=False, pipe=None):
        
        pipe = self.redis if pipe is None else pipe
        method = getattr(pipe, 'zrevrank' if reverse else 'zrank')
        rank = method(self.key, self._pickle(member))

        return rank
737883
function getWithLength(length) {
  var arrays = pool[length];
  var array;
  var i;

  // Create the first array for the specified length
  if (!arrays) {
    array = create(length);
  }

  // Find an unused array among the created arrays for the specified length
  if (!array) {
    for (i = arrays.length; i--;) {
      if (!arrays[i].inUse) {
        array = arrays[i];
        break;
      }
    }

    // If no array was found, create a new one
    if (!array) {
      array = create(length);
    }
  }

  array.inUse = true;
  return array;
}
1045511
def read_lengths_file( name ):
    

    chrom_to_length = {}
    f = file ( name, "rt" )
    for line in f:
        line = line.strip()
        if line == '' or line[0] == '#': continue
        try:
            fields = line.split()
            if len(fields) != 2: raise
            chrom = fields[0]
            length = int( fields[1] )
        except:
            raise ValueError("bad length file line: %s" % line)
        if chrom in chrom_to_length and length != chrom_to_length[chrom]:
            raise ValueError("%s has more than one length!" % chrom)
        chrom_to_length[chrom] = length
    f.close()
    return chrom_to_length
911347
def _get_final_set(self, sets, pk, sort_options):
        
        if self._lazy_collection['intersects']:
            # if the intersect method was called, we had new sets to intersect
            # to the global set of sets.
            # And it there is no real filters, we had the set of the whole
            # collection because we cannot be sure that entries in "intersects"
            # are all real primary keys
            sets = sets[::]
            sets.extend(self._lazy_collection['intersects'])
            if not self._lazy_collection['sets'] and not self.stored_key:
                sets.append(self.cls.get_field('pk').collection_key)

        final_set, keys_to_delete_later = super(ExtendedCollectionManager,
                                    self)._get_final_set(sets, pk, sort_options)

        # if we have a slice and we want to sort by the score of a sorted set,
        # as redis sort command doesn't handle this, we have to create keys for
        # each value of the sorted set and sort on them
        # @antirez, y u don't allow this !!??!!
        if final_set and self._sort_by_sortedset_before:
            # TODO: if we have filters, maybe apply _zet_to_keys to only
            #       intersected values
            base_tmp_key, tmp_keys = self._prepare_sort_by_score(None, sort_options)
            # new keys have to be deleted once the final sort is done
            if not keys_to_delete_later:
                keys_to_delete_later = []
            keys_to_delete_later.append(base_tmp_key)
            keys_to_delete_later += tmp_keys

        return final_set, keys_to_delete_later
298316
def key_value(minion_id,
              pillar,  # pylint: disable=W0613
              pillar_key='redis_pillar'):
    '''
    
    '''
    # Identify key type and process as needed based on that type
    key_type = __salt__['redis.key_type'](minion_id)
    if key_type == 'string':
        return {pillar_key: __salt__['redis.get_key'](minion_id)}
    elif key_type == 'hash':
        return {pillar_key: __salt__['redis.hgetall'](minion_id)}
    elif key_type == 'list':
        list_size = __salt__['redis.llen'](minion_id)
        if not list_size:
            return {}
        return {pillar_key: __salt__['redis.lrange'](minion_id, 0,
                                                     list_size - 1)}
    elif key_type == 'set':
        return {pillar_key: __salt__['redis.smembers'](minion_id)}
    elif key_type == 'zset':
        set_size = __salt__['redis.zcard'](minion_id)
        if not set_size:
            return {}
        return {pillar_key: __salt__['redis.zrange'](minion_id, 0,
                                                     set_size - 1)}
    # Return nothing for unhandled types
    return {}
234921
def zcount(self, name, min, max):
        
        with self.pipe as pipe:
            return pipe.zcount(self.redis_key(name), min, max)
1238874
def _get_key_value(self, key, is_hll=False):
        '''
        
        '''
        if is_hll:
            # get hll value
            return self.redis_conn.execute_command("PFCOUNT", key)
        else:
            # get zcard value
            return self.redis_conn.zcard(key)
185416
def readlength(self):
        """"""
        logging.info('Estimating read lengths of FASTQ files')
        # Iterate through the samples
        for sample in self.samples:
            sample.run.Date = 'NA'
            sample.run.InvestigatorName = 'NA'
            sample.run.TotalClustersinRun = 'NA'
            sample.run.NumberofClustersPF = 'NA'
            sample.run.PercentOfClusters = 'NA'
            sample.run.SampleProject = 'NA'
            # Only perform this step if the forward and reverse lengths have not been loaded into the metadata
            if not GenObject.isattr(sample.run, 'forwardlength') and not GenObject.isattr(sample.run, 'reverselength'):
                # Initialise the .header attribute for each sample
                sample.header = GenObject()
                sample.commands = GenObject()
                # Set /dev/null
                devnull = open(os.devnull, 'wb')
                # Only process the samples if the file type is a list
                if type(sample.general.fastqfiles) is list:
                    # Set the forward fastq to be the first entry in the list
                    forwardfastq = sorted(sample.general.fastqfiles)[0]
                    # If the files are gzipped, then zcat must be used instead of cat
                    if '.gz' in forwardfastq:
                        command = 'zcat'
                    else:
                        command = 'cat'
                    # Read in the output of the (z)cat of the fastq file piped through head to read only the first 1000
                    # lines. Will make a string of the first 1000 lines in the file
                    forwardreads = subprocess.Popen("{} {} | head -n 1000".format(command, forwardfastq),
                                                    shell=True,
                                                    stdout=subprocess.PIPE,
                                                    stderr=devnull).communicate()[0].rstrip()
                    # Set the length of the reads as follows: the highest value (max) of the length of the sequence. The
                    # sequence was extracted from the rest of the lines in the fastq file. Example of first four lines:
                    """
                    @M02466:126:000000000-AKF4P:1:1101:11875:1838 1:N:0:1
                    TCATAACGCAGTGAAACGCTTTAACAAAAGCGGAGACACGCCACTATTTGTCAATATTTCGTATGATACATTTTTAGAAAATCAAGAAGAGTTGCACGA
                    +
                    AA,B89C,@++B,,,,C,:BFF9,C,,,,,6+++++:,C,8C+BE,EFF9FC,6E,EFGF@F<F@9F9E<FFGGGC8,,,,CC<,,,,,,6CE,C<C,,
                    """
                    # The line with the sequence information occurs every four lines (1, 5, 9, etc). This can be
                    # represented by linenumber % 4 == 1
                    try:
                        # Added due to weird 2to3 conversion issues, was coming
                        forwardreads = forwardreads.decode('utf-8')
                    except UnicodeDecodeError:
                        sample.run.forwardlength = 0
                    # up as a bytes object when we need it as a string.
                    try:
                        forwardlength = max([len(sequence) for iterator, sequence in enumerate(forwardreads.split('\n'))
                                             if iterator % 4 == 1])
                        sample.run.forwardlength = forwardlength
                    except (ValueError, TypeError):
                        sample.run.forwardlength = 0
                    # For paired end analyses, also calculate the length of the reverse reads
                    if len(sample.general.fastqfiles) == 2:
                        reversefastq = sorted(sample.general.fastqfiles)[1]
                        reversereads = subprocess.Popen("{} {} | head -n 1000".format(command, reversefastq),
                                                        shell=True,
                                                        stdout=subprocess.PIPE,
                                                        stderr=devnull).communicate()[0].rstrip()
                        try:
                            reversereads = reversereads.decode('utf-8')
                        except UnicodeDecodeError:
                            sample.run.reverselength = 0
                        try:
                            sample.run.reverselength = max([len(sequence) for iterator, sequence in
                                                            enumerate(reversereads.split('\n')) if iterator % 4 == 1])
                        except (ValueError, TypeError):
                            sample.run.reverselength = 0
                    # Populate metadata of single end reads with 'NA'
                    else:
                        sample.run.reverselength = 0
193186
def _final_redis_call(self, final_set, sort_options):
        

        conn = self.cls.get_connection()
        if sort_options is not None:
            # a sort, or values, call the SORT command on the set
            return conn.sort(final_set, **sort_options)
        else:
            # no sort, nor values, simply return the full set
            return conn.smembers(final_set)
170165
def closest_length(refs, pred):
    '''
    
    '''
    smallest_diff = float('inf')
    closest_length = float('inf')
    for ref in refs:
        diff = abs(len(ref) - len(pred))
        if diff < smallest_diff or (diff == smallest_diff and len(ref) < closest_length):
            smallest_diff = diff
            closest_length = len(ref)
    return closest_length
908577
def _check_key_lengths(self, conn, instance, tags):
        
        key_list = instance.get('keys')

        if key_list is None:
            return

        if not isinstance(key_list, list) or len(key_list) == 0:
            self.warning("keys in redis configuration is either not a list or empty")
            return

        # get all the available databases
        databases = list(conn.info('keyspace'))
        if not databases:
            self.warning("Redis database is empty")
            return

        # convert to integer the output of `keyspace`, from `db0` to `0`
        # and store items in a set
        databases = [int(dbstring[2:]) for dbstring in databases]

        # user might have configured the instance to target one specific db
        if 'db' in instance:
            db = instance['db']
            if db not in databases:
                self.warning("Cannot find database {}".format(instance['db']))
                return
            databases = [db]

        # maps a key to the total length across databases
        lengths_overall = defaultdict(int)

        # don't overwrite the configured instance, use a copy
        tmp_instance = deepcopy(instance)

        for db in databases:
            lengths = defaultdict(lambda: defaultdict(int))
            tmp_instance['db'] = db
            db_conn = self._get_conn(tmp_instance)

            for key_pattern in key_list:
                if re.search(r"(?<!\\)[*?[]", key_pattern):
                    keys = db_conn.scan_iter(match=key_pattern)
                else:
                    keys = [key_pattern]

                for key in keys:
                    text_key = ensure_unicode(key)
                    try:
                        key_type = ensure_unicode(db_conn.type(key))
                    except redis.ResponseError:
                        self.log.info("key {} on remote server; skipping".format(text_key))
                        continue

                    if key_type == 'list':
                        keylen = db_conn.llen(key)
                        lengths[text_key]["length"] += keylen
                        lengths_overall[text_key] += keylen
                    elif key_type == 'set':
                        keylen = db_conn.scard(key)
                        lengths[text_key]["length"] += keylen
                        lengths_overall[text_key] += keylen
                    elif key_type == 'zset':
                        keylen = db_conn.zcard(key)
                        lengths[text_key]["length"] += keylen
                        lengths_overall[text_key] += keylen
                    elif key_type == 'hash':
                        keylen = db_conn.hlen(key)
                        lengths[text_key]["length"] += keylen
                        lengths_overall[text_key] += keylen
                    elif key_type == 'string':
                        # Send 1 if the key exists as a string
                        lengths[text_key]["length"] += 1
                        lengths_overall[text_key] += 1
                    else:
                        # If the type is unknown, it might be because the key doesn't exist,
                        # which can be because the list is empty. So always send 0 in that case.
                        lengths[text_key]["length"] += 0
                        lengths_overall[text_key] += 0

                    # Tagging with key_type since the same key can exist with a
                    # different key_type in another db
                    lengths[text_key]["key_type"] = key_type

            # Send the metrics for each db in the redis instance.
            for key, total in iteritems(lengths):
                # Only send non-zeros if tagged per db.
                if total["length"] > 0:
                    self.gauge(
                        'redis.key.length',
                        total["length"],
                        tags=tags
                        + ['key:{}'.format(key), 'key_type:{}'.format(total["key_type"]), 'redis_db:db{}'.format(db)],
                    )

        # Warn if a key is missing from the entire redis instance.
        # Send 0 if the key is missing/empty from the entire redis instance.
        for key, total in iteritems(lengths_overall):
            if total == 0 and instance.get("warn_on_missing_keys", True):
                self.gauge('redis.key.length', total, tags=tags + ['key:{}'.format(key)])
                self.warning("{0} key not found in redis".format(key))
908796
def length(self):
        
        if not self.connected:
            raise ConnectionError('Queue is not connected')

        try:
            length = self.rdb.llen(self._name)
        except redis.exceptions.ConnectionError as err:
            raise ConnectionError(str(err))

        return length
649345
def getIndexedLength(self) :
		""""""
		if self.x1 != None and self.x2 != None:
			return self.x2 - self.x1
		else :
			if len(self.children) == 0 :
				return 0
			else :
				l = self.children[0].x2 - self.children[0].x1
				for i in range(1, len(self.children)) :
					l += self.children[i].x2 - self.children[i].x1 - max(0, self.children[i-1].x2 - self.children[i].x1)
				return l
664480
def determine_lengths(dtrajs):
    
    """
    if (isinstance(dtrajs[0], (int))):
        return len(dtrajs) * np.ones((1))
    lengths = np.zeros((len(dtrajs)))
    for i in range(len(dtrajs)):
        lengths[i] = len(dtrajs[i])
    return lengths