The Python-side representaion of Redis set value. It behaves alike built-in Python set object. More exactly, it implements collections.MutableSet protocol.
Redis commands | Set methods |
---|---|
DEL | Set.clear() |
SADD | Set.add(), Set.update() |
SCARD | len() (Set.__len__()) |
SDIFF | Set.difference(), - (Set.__sub__()) |
SDIFFSTORE | Set.difference_update(), -= (Set.__isub__()) |
SINTER | Set.intersection(), & (Set.__and__()) |
SINTERSTORE | Set.intersection_update(), &= (Set.__iand__()) |
SISMEMBER | in (Set.__contains__()) |
SMEMBERS | iter() (Set.__iter__()) |
SMOVE | N/A |
SPOP | Set.pop() |
SRANDMEMBER | N/A |
SREM | Set.discard(), Set.remove() |
SUNION | Set.union(), | (Set.__or__()) |
SUNIONSTORE | Set.update(), |= (Set.__ior__()) |
N/A | Set.symmetric_difference(), ^ (Set.__xor__()) |
N/A | Set.symmetric_difference_update(), ^= (Set.__ixor__()) |
Bitwise and (&) operator. Gets the union of operands.
Mostly equivalent to intersection() method except it can take only one set-like operand. On the other hand intersection() can take zero or more iterable operands (not only set-like objects).
Parameters: | operand (collections.Set) – another set to get intersection |
---|---|
Returns: | the intersection |
Return type: | set |
in operator. Tests whether the set contains the given operand member.
Parameters: | member – the value to test |
---|---|
Returns: | True if the set contains the given operand member |
Return type: | bool |
Note
This method is directly mapped to SISMEMBER command.
Greater-than or equal to (>=) operator. Tests whether the set is a superset of the given operand.
It’s the same operation to issuperset() method except it can take a set-like operand only. On the other hand issuperset() can take an any iterable operand as well.
Parameters: | operand (collections.Set) – another set to test |
---|---|
Returns: | True if the set contains the operand |
Return type: | bool |
Greater-than (>) operator. Tests whether the set is a proper (or strict) superset of the given operand.
To eleborate, the key difference between this greater-than (>) operator and greater-than or equal-to (>=) operator, which is equivalent to issuperset() method, is that it returns False even if two sets are exactly the same.
Let this show a simple example:
>>> assert isinstance(s, sider.set.Set)
>>> set(s)
set([1, 2, 3])
>>> s > set([1, 2]), s >= set([1, 2])
(True, True)
>>> s > set([1, 2, 3]), s >= set([1, 2, 3])
(False, True)
>>> s > set([1, 2, 3, 4]), s >= set([1, 2, 3, 4])
(False, False)
Parameters: | operand (collections.Set) – another set to test |
---|---|
Returns: | True if the set is a proper superset of operand |
Return type: | bool |
Bitwise and (&=) assignment. Updates the set with the intersection of itself and the operand.
Mostly equivalent to intersection_update() method except it can take only one set-like operand. On the other hand intersection_update() can take zero or more iterable operands (not only set-like objects).
Parameters: | operand (collections.Set) – another set to intersection |
---|---|
Returns: | the set itself |
Return type: | Set |
Bitwise or (|=) assignment. Updates the set with the union of itself and the operand.
Mostly equivalent to update() method except it can take only one set-like operand. On the other hand update() can take zero or more iterable operands (not only set-like objects).
Parameters: | operand (collections.Set) – another set to union |
---|---|
Returns: | the set itself |
Return type: | Set |
Minus augmented assignment (-=). Removes all elements of the operand from this set.
Mostly equivalent to difference_update() method except it can take only one set-like operand. On the other hand difference_update() can take zero or more iterable operands (not only set-like objects).
Parameters: | operand (collections.Set) – another set which has elements to remove from this set |
---|---|
Returns: | the set itself |
Return type: | Set |
Bitwise exclusive argumented assignment (^=). Updates the set with the symmetric difference of itself and operand.
Mostly equivalent to symmetric_difference_update() method except it can take a set-like operand only. On the other hand symmetric_difference_update() can take an any iterable operand as well.
Parameters: | operand (collections.Set) – another set |
---|---|
Returns: | the set itself |
Return type: | Set |
Less-than or equal to (<=) operator. Tests whether the set is a subset of the given operand.
It’s the same operation to issubset() method except it can take a set-like operand only. On the other hand issubset() can take an any iterable operand as well.
Parameters: | operand (collections.Set) – another set to test |
---|---|
Returns: | True if the operand set contains the set |
Return type: | bool |
Gets the cardinality of the set.
Use this with the built-in len() function.
Returns: | the cardinality of the set |
---|---|
Return type: | numbers.Integral |
Note
This method is directly mapped to SCARD command.
Less-than (<) operator. Tests whether the set is a proper (or strict) subset of the given operand or not.
To eleborate, the key difference between this less-than (<) operator and less-than or equal-to (<=) operator, which is equivalent to issubset() method, is that it returns False even if two sets are exactly the same.
Let this show a simple example:
>>> assert isinstance(s, sider.set.Set)
>>> set(s)
set([1, 2, 3])
>>> s < set([1, 2]), s <= set([1, 2])
(False, False)
>>> s < set([1, 2, 3]), s <= set([1, 2, 3])
(False, True)
>>> s < set([1, 2, 3, 4]), s <= set([1, 2, 3, 4])
(True, True)
Parameters: | operand (collections.Set) – another set to test |
---|---|
Returns: | True if the set is a proper subset of operand |
Return type: | bool |
Bitwise or (|) operator. Gets the union of operands.
Mostly equivalent to union() method except it can take only one set-like operand. On the other hand union() can take zero or more iterable operands (not only set-like objects).
Parameters: | operand (collections.Set) – another set to union |
---|---|
Returns: | the union set |
Return type: | set |
Minus (-) operator. Gets the relative complement of the operand in the set.
Mostly equivalent to difference() method except it can take a set-like operand only. On the other hand difference() can take an any iterable operand as well.
Parameters: | operand (collections.Set) – another set to get the relative complement |
---|---|
Returns: | the relative complement |
Return type: | set |
Bitwise exclusive or (^) operator. Returns a new set with elements in either the set or the operand but not both.
Mostly equivalent to symmetric_difference() method except it can take a set-like operand only. On the other hand symmetric_difference() can take an any iterable operand as well.
Parameters: | operand (collections.Set) – other set |
---|---|
Returns: | a new set with elements in either the set or the operand but not both |
Return type: | set |
Adds an element to the set. This has no effect if the element is already present.
Parameters: | element – an element to add |
---|
Note
This method is a direct mapping to SADD comamnd.
Returns the difference of two or more sets as a new set i.e. all elements that are in this set but not the others.
Parameters: | sets – other iterables to get the difference |
---|---|
Returns: | the relative complement |
Return type: | set |
Note
This method is mapped to SDIFF command.
Removes all elements of other sets from this set.
Parameters: | *sets – other sets that have elements to remove from this set |
---|
Note
For Set objects of the same session it internally uses SDIFFSTORE command.
For other ordinary Python iterables, it uses SREM commands. If the version of Redis is less than 2.4, sends SREM multiple times. Because multiple operands of SREM command has been supported since Redis 2.4.
Removes an element from the set if it is a member. If the element is not a member, does nothing.
Parameters: | element – an element to remove |
---|
Note
This method is mapped to SREM command.
Gets the intersection of the given sets.
Parameters: | *sets – zero or more operand sets to get intersection. all these must be iterable |
---|---|
Returns: | the intersection |
Return type: | set |
Updates the set with the intersection of itself and other sets.
Parameters: | *sets – zero or more operand sets to intersection. all these must be iterable |
---|
Note
It sends a SINTERSTORE command for other Set objects and a SREM command for other ordinary Python iterables.
Multiple operands of SREM command has been supported since Redis 2.4.0, so it would send multiple SREM commands if the Redis version is less than 2.4.0.
Used commands: SINTERSTORE, SMEMBERS and SREM.
Tests whether two sets are disjoint or not.
Parameters: | operand (collections.Iterable) – another set to test |
---|---|
Returns: | True if two sets have a null intersection |
Return type: | bool |
Note
It internally uses SINTER command.
Tests whether the set is a subset of the given operand or not. To test proper (strict) subset, use < operator instead.
Parameters: | operand (collections.Iterable) – another set to test |
---|---|
Returns: | True if the operand set contains the set |
Return type: | bool |
Tests whether the set is a superset of the given operand. To test proper (strict) superset, use > operator instead.
Parameters: | operand (collections.Iterable) – another set to test |
---|---|
Returns: | True if the set contains operand |
Return type: | bool |
Removes an arbitrary element from the set and returns it. Raises KeyError if the set is empty.
Returns: | a removed arbitrary element |
---|---|
Raises exceptions.KeyError: | |
if the set is empty |
Note
This method is directly mapped to SPOP command.
Returns a new set with elements in either the set or the operand but not both.
Parameters: | operand (collections.Iterable) – other set |
---|---|
Returns: | a new set with elements in either the set or the operand but not both |
Return type: | set |
Updates the set with the symmetric difference of itself and operand.
Parameters: | operand (collections.Iterable) – another set to get symmetric difference |
---|
Note
This method consists of several Redis commands in a transaction: SINTER, SUNIONSTORE and SREM.
Gets the union of the given sets.
Parameters: | *sets – zero or more operand sets to union. all these must be iterable |
---|---|
Returns: | the union set |
Return type: | set |
Updates the set with union of itself and operands.
Parameters: | *sets – zero or more operand sets to union. all these must be iterable |
---|
Note
It sends a SUNIONSTORE command for other Set objects and a SADD command for other ordinary Python iterables.
Multiple operands of SADD command has been supported since Redis 2.4.0, so it would send multiple SADD commands if the Redis version is less than 2.4.0.