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 implementscollections.MutableSet
protocol.Mappings of Redis commands– Set
methods¶Redis commands Set
methodsDEL 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 handintersection()
can take zero or more iterable operands (not only set-like objects).Parameters: operand ( collections.Set
) – another set to get intersectionReturns: the intersection Return type: set
-
__contains__
(*args, **kwargs)¶ in
operator. Tests whether the set contains the given operandmember
.Parameters: member – the value to test Returns: True
if the set contains the given operandmember
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 givenoperand
.It’s the same operation to
issuperset()
method except it can take a set-like operand only. On the other handissuperset()
can take an any iterable operand as well.Parameters: operand ( collections.Set
) – another set to testReturns: True
if the set contains theoperand
Return type: bool
-
__gt__
(operand)¶ Greater-than (
>
) operator. Tests whether the set is a proper (or strict) superset of the givenoperand
.To eleborate, the key difference between this greater-than (
>
) operator and greater-than or equal-to (>=
) operator, which is equivalent toissuperset()
method, is that it returnsFalse
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 testReturns: True
if the set is a proper superset ofoperand
Return type: bool
-
__iand__
(operand)¶ Bitwise and (
&=
) assignment. Updates the set with the intersection of itself and theoperand
.Mostly equivalent to
intersection_update()
method except it can take only one set-like operand. On the other handintersection_update()
can take zero or more iterable operands (not only set-like objects).Parameters: operand ( collections.Set
) – another set to intersectionReturns: the set itself Return type: Set
-
__ior__
(operand)¶ Bitwise or (
|=
) assignment. Updates the set with the union of itself and theoperand
.Mostly equivalent to
update()
method except it can take only one set-like operand. On the other handupdate()
can take zero or more iterable operands (not only set-like objects).Parameters: operand ( collections.Set
) – another set to unionReturns: the set itself Return type: Set
-
__isub__
(operand)¶ Minus augmented assignment (
-=
). Removes all elements of theoperand
from this set.Mostly equivalent to
difference_update()
method except it can take only one set-like operand. On the other handdifference_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 setReturns: the set itself Return type: Set
-
__ixor__
(operand)¶ Bitwise exclusive argumented assignment (
^=
). Updates the set with the symmetric difference of itself andoperand
.Mostly equivalent to
symmetric_difference_update()
method except it can take a set-like operand only. On the other handsymmetric_difference_update()
can take an any iterable operand as well.Parameters: operand ( collections.Set
) – another setReturns: the set itself Return type: Set
-
__le__
(operand)¶ Less-than or equal to (
<=
) operator. Tests whether the set is a subset of the givenoperand
.It’s the same operation to
issubset()
method except it can take a set-like operand only. On the other handissubset()
can take an any iterable operand as well.Parameters: operand ( collections.Set
) – another set to testReturns: True
if theoperand
set contains the setReturn 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 givenoperand
or not.To eleborate, the key difference between this less-than (
<
) operator and less-than or equal-to (<=
) operator, which is equivalent toissubset()
method, is that it returnsFalse
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 testReturns: True
if the set is a proper subset ofoperand
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 handunion()
can take zero or more iterable operands (not only set-like objects).Parameters: operand ( collections.Set
) – another set to unionReturns: the union set Return type: set
-
__sub__
(operand)¶ Minus (
-
) operator. Gets the relative complement of theoperand
in the set.Mostly equivalent to
difference()
method except it can take a set-like operand only. On the other handdifference()
can take an any iterable operand as well.Parameters: operand ( collections.Set
) – another set to get the relative complementReturns: the relative complement Return type: set
-
__xor__
(operand)¶ Bitwise exclusive or (
^
) operator. Returns a new set with elements in either the set or theoperand
but not both.Mostly equivalent to
symmetric_difference()
method except it can take a set-like operand only. On the other handsymmetric_difference()
can take an any iterable operand as well.Parameters: operand ( collections.Set
) – other setReturns: a new set with elements in either the set or the operand
but not bothReturn type: set
-
add
(*args, **kwargs)¶ Adds an
element
to the set. This has no effect if theelement
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 newset
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 theelement
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 testReturns: True
if two sets have a null intersectionReturn 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 testReturns: True
if theoperand
set contains the setReturn type: bool
-
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 testReturns: True
if the set containsoperand
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 setReturns: a new set with elements in either the set or the operand
but not bothReturn type: set
-
symmetric_difference_update
(operand)¶ Updates the set with the symmetric difference of itself and
operand
.Parameters: operand ( collections.Iterable
) – another set to get symmetric differenceNote
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
-
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.
-