SPOP Count?
See original GitHub issueHey andy/community,
Redis supports a count field when calling SPOP (http://redis.io/commands/spop), but I don’t think this framework has support for that.
Not sure if this was intentional (for some odd reason of which I’m unaware) or not, but it’d be useful to have.
I copy and pasted my patch below if you want to use it. I basically just duplicated the code for srandmember since even Redis docs mention they work the same. Although, it would’ve been nice to have the arg be count and not number.
diff --git a/redis/client.py b/redis/client.py
index 8b5a3fa..0f5ad93 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -1532,9 +1532,17 @@ class StrictRedis(object):
"Move ``value`` from set ``src`` to set ``dst`` atomically"
return self.execute_command('SMOVE', src, dst, value)
- def spop(self, name):
- "Remove and return a random member of set ``name``"
- return self.execute_command('SPOP', name)
+ def spop(self, name, number=None):
+ """
+ Remove and return a random member of set ``name``"
+
+ If ``number`` is None, pops a random member of set ``name``.
+
+ If ``number`` is supplied, pops a list of ``number`` random
+ members of set ``name``.
+ """
+ args = number and [number] or []
+ return self.execute_command('SPOP', name, *args)
def srandmember(self, name, number=None):
"""
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 286ea04..5c43feb 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -844,6 +844,13 @@ class TestRedisCommands(object):
assert value in s
assert r.smembers('a') == set(s) - set([value])
+ def test_spop_multi_value(self, r):
+ s = [b('1'), b('2'), b('3')]
+ r.sadd('a', *s)
+ values = r.spop('a', 2)
+ assert values in s
+ assert r.smembers('a') == set(values) < set(s)
+
def test_srandmember(self, r):
s = [b('1'), b('2'), b('3')]
r.sadd('a', *s)
[Edited: Added patch to tests in test_commands.py]
Ryan
Issue Analytics
- State:
- Created 9 years ago
- Comments:28 (2 by maintainers)
Top Results From Across the Web
SPOP - Redis
SPOP. Syntax. SPOP key [count]. Available since: 1.0.0; Time complexity: Without the count argument O(1), otherwise O(N) where N is the value of...
Read more >SPOP
SPOP key [count] ... Available since 1.0.0. ... Removes and returns one or more random elements from the set value store at key...
Read more >SPOP Count? · Issue #593 · redis/redis-py - GitHub
Hey andy/community, Redis supports a count field when calling SPOP (http://redis.io/commands/spop), but I don't think this framework has ...
Read more >how to use spop command with count if set have that much ...
What you want is to call SCARD myKey to test the number of members, and based on the result call SPOP . SPOP...
Read more >Redis SPOP Command Explained - Database.Guide
In Redis, the SPOP command removes and returns one or more random members from the set value store at the specified key ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

+1 Please add the missing count argument. Thank you.
Any update on the release timeline? It’s confusing having dissonance between the documentation on RTD and the latest pypi release…