sider.set — Set objects

See also

Redis Data Types
The Redis documentation that explains about its data types: strings, lists, sets, sorted sets and hashes.
class sider.set.Set(session, key, value_type=<class 'sider.types.ByteString'>)

The Python-side representaion of Redis set value. It behaves alike built-in Python set object. More exactly, it implements collections.MutableSet protocol.

Mappings of Redis commands–Set methods
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__())
__and__(operand)

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
__contains__(*args, **kwargs)

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.

__ge__(operand)

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
__gt__(operand)

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
__iand__(operand)

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
__ior__(operand)

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
__isub__(operand)

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
__ixor__(operand)

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
__le__(operand)

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
__len__(*args, **kwargs)

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.

__lt__(operand)

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
__or__(operand)

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
__sub__(operand)

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
__xor__(operand)

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
add(*args, **kwargs)

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.

clear(*args, **kwargs)

Removes all elements from this set.

Note

Under the hood it simply DEL the key.

difference(*sets)

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.

difference_update(*sets)

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.

discard(*args, **kwargs)

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.

intersection(*sets)

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
intersection_update(*sets)

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.

isdisjoint(operand)

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.

issubset(operand)

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

Note

This method consists of following Redis commands:

  1. SDIFF for this set and operand
  2. SLEN for this set
  3. SLEN for operand

If the first SDIFF returns anything, it sends no commands of the rest and simply returns False.

issuperset(operand)

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
pop()

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.

symmetric_difference(operand)

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

Note

This method consists of following two commands:

  1. SUNION of this set and the operand
  2. SINTER of this set and the operand

and then makes a new set with elements in the first result are that are not in the second result.

symmetric_difference_update(operand)

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.

union(*sets)

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

Note

It sends a SUNION command for other Set objects. For other ordinary Python iterables, it unions all in the memory.

update(*sets)

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.

Previous topic

sider.list — List objects

Next topic

sider.sortedset — Sorted sets

This Page