See also
The Python-sider representation of Redis sorted set value. It behaves in similar way to collections.Counter object which became a part of standard library since Python 2.7.
It implements collections.MutableMapping and collections.MutableSet protocols.
Redis commands | SortedSet methods |
---|---|
DEL | SortedSet.clear() |
ZADD | = (SortedSet.__setitem__()) |
ZCARD | len() (SortedSet.__len__()) |
ZINCRBY | SortedSet.add(), SortedSet.discard(), SortedSet.update() |
ZRANGE | iter() (SortedSet.__iter__()) |
ZRANGE WITHSCORES | SortedSet.items(), SortedSet.most_common(), SortedSet.least_common() |
ZREM | del (SortedSet.__delitem__()), SortedSet.discard() |
ZSCORE | SortedSet.__getitem__(), in (SortedSet.__contains__()) |
ZUNIONSTORE | SortedSet.update() |
N/A | SortedSet.setdefault() |
N/A | SortedSet.pop() |
N/A | SortedSet.popitem() |
in operator. Tests whether the set contains the given operand member.
Parameters: | member – the value to test |
---|---|
Returns: | True if the sorted set contains the given operand member |
Return type: | bool |
Note
This method internally uses ZSCORE command.
Removes the member.
Parameters: | member – the member to delete |
---|---|
Raises: |
|
Gets the score of the given member.
Parameters: | member – the member to get its score |
---|---|
Returns: | the score of the member |
Return type: | |
Raises: |
|
Note
It is directly mapped to Redis ZSCORE command.
Gets the cardinality of the sorted set.
Returns: | the cardinality (the number of elements) of the sorted set |
---|---|
Return type: | numbers.Integral |
Note
It is directly mapped to Redis ZCARD command.
Sets the score of the member. Adds the member if it doesn’t exist.
Parameters: |
|
---|---|
Raises exceptions.TypeError: | |
if the given member is not acceptable by its value_type or the given score is not a numbers.Real object |
Note
It is directly mapped to Redis ZADD command.
Adds a new member or increases its score (default is 1).
Parameters: |
|
---|
Note
This method is directly mapped to ZINCRBY command.
Removes all values from this sorted set.
Note
Under the hood it simply DEL the key.
Opposite operation of add(). It decreases its score (default is 1). When its score get the remove number (default is 0) or less, it will be removed.
If you don’t want to remove it but only decrease its score, pass None into remove parameter.
If you want to remove member, not only decrease its score, use __delitem__() instead:
del sortedset[member]
Parameters: |
|
---|
Returns an ordered of pairs of elements and these scores.
Parameters: | reverse (bool) – order result descendingly if it’s True. default is False which means ascending order |
---|---|
Returns: | an ordered list of pairs. every pair looks like (element, score) |
Return type: | collections.Sequence |
Note
This method is directly mapped to ZRANGE command and WITHSCORES option.
Gets its all elements. Equivalent to __iter__() except it returns an ordered Sequence instead of iterable.
Parameters: | reverse (bool) – order result descendingly if it’s True. default is False which means ascending order |
---|---|
Returns: | the ordered list of its all keys |
Return type: | collections.Sequence |
Note
This method is directly mapped to Redis ZRANGE command.
Returns a list of the n least common (exactly, lowly scored) members and their counts (scores) from the least common to the most. If n is not specified, it returns all members in the set. Members with equal scores are ordered arbitarily.
Parameters: | n (numbers.Integral) – the number of members to get |
---|---|
Returns: | an ordered list of pairs. every pair looks like (element, score) |
Return type: | collections.Sequence |
Note
This method is directly mapped to ZREVRANGE command and WITHSCORES option.
Returns a list of the n most common (exactly, highly scored) members and their counts (scores) from the most common to the least. If n is not specified, it returns all members in the set. Members with equal scores are ordered arbitarily.
Parameters: | n (numbers.Integral) – the number of members to get |
---|---|
Returns: | an ordered list of pairs. every pair looks like (element, score) |
Return type: | collections.Sequence |
Note
This method is directly mapped to ZRANGE command and WITHSCORES option.
Populates a member of the set.
If key keyword argument or one or more positional arguments have present, it behaves like dict.pop() method:
Parameters: |
|
---|---|
Returns: | the score of the member before the operation has been committed |
Return type: |
If no positional arguments or no key keyword argument, it behaves like set.pop() method. Basically it does the same thing with popitem() except it returns just a popped value (while popitem() returns a pair of popped value and its score).
Parameters: | desc (bool) – keyword only. by default, it populates the member of the lowest score, but if you pass True to this it will populates the highest instead. default is False |
---|---|
Returns: | the populated member. it will be the lowest scored member or the highest scored member if desc is True |
Raises exceptions.KeyError: | |
when the set is empty |
See also
Method popitem()
If any case there are common keyword-only parameters:
Parameters: |
|
---|
Populates the lowest scored member (or the highest if desc is True) and its score.
It returns a pair of the populated member and its score. The score is a value before the operation has been committed.
Parameters: |
|
---|---|
Returns: | a pair of the populated member and its score. the first part of a pair will be the lowest scored member or the highest scored member if desc is True. the second part of a pair will be the score before the operation has been committed |
Return type: | tuple |
Raises exceptions.KeyError: | |
when the set is empty |
See also
Method pop()
Gets the score of the given key if it exists or adds key with default score.
Parameters: |
|
---|---|
Returns: | the score of the key after the operation has been committed |
Return type: |
Merge with passed sets and keywords. It’s behavior is almost equivalent to dict.update() and set.update() except it’s aware of scores.
For example, assume the initial elements and their scores of the set is (in notation of dictionary):
{'c': 1, 'a': 2, 'b': 3}
and you has updated it:
sortedset.update(set('acd'))
then it becomes (in notation of dictionary):
{'d': 1, 'c': 2, 'a': 3, 'b': 3}
You can pass mapping objects or keywords instead to specify scores to increment:
sortedset.update({'a': 1, 'b': 2})
sortedset.update(a=1, b=2)
sortedset.update(set('ab'), set('cd'),
{'a': 1, 'b': 2}, {'c': 1, 'd': 2},
a=1, b=2, c=1, d=2)
Parameters: |
|
---|
Note
There’s an incompatibility with dict.update(). It always treats iterable of pairs as set of pairs, not mapping pairs, unlike dict.update(). It is for resolving ambiguity (remember value_type can take tuples or such things).
Note
Under the hood it uses multiple ZINCRBY commands and ZUNIONSTORE if there are one or more SortedSet objects in operands.
(sider.types.Bulk) The type of set elements.
Returns an ordered list of scores.
Parameters: | reverse (bool) – order result descendingly if it’s True. default is False which means ascending order |
---|---|
Returns: | an ordered list of scores |
Return type: | collections.Sequence |
Note
This method internally uses ZRANGE command and WITHSCORES option.