Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Added SPOP command #1261

Merged
merged 22 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4c9e346
Added SPOP command to python
GilboaAWS Apr 10, 2024
a6b8fdf
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
e4c1c66
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
af7d82a
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
c7106ff
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
7467cce
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
53adc7d
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
66a3bc9
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
8c52b5e
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
5462669
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
f8d6046
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
581baca
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
ca4907f
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
93b7763
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
d55c5f8
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
820c3c5
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
75481c6
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
dd953b0
Update python/python/glide/async_commands/transaction.py
GilboaAWS Apr 11, 2024
af1a86a
Update python/python/glide/async_commands/core.py
GilboaAWS Apr 11, 2024
28af946
fix black errors
shohamazon Apr 11, 2024
1e95bd4
Apply suggestions from code review
GilboaAWS Apr 14, 2024
30c78d7
Apply suggestions from code review
shohamazon Apr 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Python: Added STRLEN command ([#1230](https:/aws/glide-for-redis/pull/1230))
* Python: Added HKEYS command ([#1228](https:/aws/glide-for-redis/pull/1228))
* Python: Added ZREMRANGEBYSCORE command ([#1151](https:/aws/glide-for-redis/pull/1151))
* Node: Added SPOP, SPOPCOUNT commands. ([#1117](https:/aws/glide-for-redis/pull/1117))
* Node, Python: Added SPOP, SPOPCOUNT commands. ([#1117](https:/aws/glide-for-redis/pull/1117), [#1261](https:/aws/glide-for-redis/pull/1261))
* Node: Added ZRANGE command ([#1115](https:/aws/glide-for-redis/pull/1115))
* Python: Added RENAME command ([#1252](https:/aws/glide-for-redis/pull/1252))

Expand Down
16 changes: 16 additions & 0 deletions glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
b"ZRANK" | b"ZREVRANK" => cmd
.position(b"WITHSCORE")
.map(|_| ExpectedReturnType::ZrankReturnType),
b"SPOP" => {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
if cmd.arg_idx(2).is_some() {
Some(ExpectedReturnType::Set)
} else {
None
}
}
_ => None,
}
}
Expand Down Expand Up @@ -479,4 +486,13 @@ mod tests {
)
.is_err());
}

#[test]
fn test_convert_spop_to_set_for_spop_count() {
assert!(matches!(
expected_type_for_cmd(redis::cmd("SPOP").arg("key1").arg("3")),
Some(ExpectedReturnType::Set)
));
assert!(expected_type_for_cmd(redis::cmd("SPOP").arg("key1")).is_none());
}
}
47 changes: 47 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,53 @@ async def scard(self, key: str) -> int:
"""
return cast(int, await self._execute_command(RequestType.SCard, [key]))

async def spop(self, key: str) -> str:
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
"""
Removes and returns one random member from the set stored at `key`.

See https://valkey-io.github.io/commands/spop/ for more details.
To pop multiple members, see `spop_count`.

Args:
key (str): The key of the set.

Returns:
str: The value of the popped member.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

Examples:
>>> await client.spop("my_set")
"value1" # Removes and returns a random member from the set "my_set".
>>> await client.spop("non_exiting_key")
None
"""
return cast(str, await self._execute_command(RequestType.Spop, [key]))
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

async def spop_count(self, key: str, count: int) -> Set[str]:
"""
Removes and returns up to `count` random members from the set stored at `key`, depending on the set's length.

See https://valkey-io.github.io/commands/spop/ for details.
shohamazon marked this conversation as resolved.
Show resolved Hide resolved
To pop a single member, see `spop`
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

Args:
key (str): The key of the set.
count (int): The count of the elements to pop from the set.

Returns:
Set[str]: A set of popped elements will be returned depending on the set's length.
If `key` does not exist, empty set will be returned.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

Examples:
>>> await client.spop_count("my_set", 2)
{"value1", "value2"} # Removes and returns 2 random members from the set "my_set".
>>> await client.spop_count("non_exiting_key", 2)
Set()
shohamazon marked this conversation as resolved.
Show resolved Hide resolved

GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
"""
return cast(
Set[str], await self._execute_command(RequestType.Spop, [key, str(count)])
)

async def sismember(
self,
key: str,
Expand Down
32 changes: 32 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,38 @@ def scard(self: TTransaction, key: str) -> TTransaction:
"""
return self.append_command(RequestType.SCard, [key])

def spop(self: TTransaction, key: str) -> TTransaction:
"""
Removes and returns one random member from the set value store at `key`.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
See https://valkey-io.github.io/commands/spop/ for details.
shohamazon marked this conversation as resolved.
Show resolved Hide resolved
To pop multiple members, see `spop_count`
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

Args:
key (str): The key of the set.

Commands response:
str: The value of the popped member.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
"""
return self.append_command(RequestType.Spop, [key])

def spop_count(self: TTransaction, key: str, count: int) -> TTransaction:
"""
Removes and returns up to `count` random members from the set stored at `key`, depending on the set's length.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
See https://valkey-io.github.io/commands/spop/ for details.
shohamazon marked this conversation as resolved.
Show resolved Hide resolved
To pop a single member, see `spop`
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

Args:
key (str): The key of the set.
count (int): The count of the elements to pop from the set.

Commands response:
Set[str]: A set of popped elements will be returned depending on the set's length.
If `key` does not exist, empty set will be returned.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
"""
return self.append_command(RequestType.Spop, [key, str(count)])

def sismember(
self: TTransaction,
key: str,
Expand Down
18 changes: 18 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,24 @@ async def test_sismember(self, redis_client: TRedisClient):
assert not await redis_client.sismember(key, get_random_string(5))
assert not await redis_client.sismember("non_existing_key", member)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_spop(self, redis_client: TRedisClient):
key = get_random_string(10)
member = get_random_string(5)
assert await redis_client.sadd(key, [member]) == 1
assert await redis_client.spop(key) == member

member2 = get_random_string(5)
member3 = get_random_string(5)
assert await redis_client.sadd(key, [member, member2, member3]) == 3
assert await redis_client.spop_count(key, 4) == {member, member2, member3}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check that nothing left in the set

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

assert await redis_client.scard(key) == 0

assert await redis_client.spop("non_existing_key") == None
assert await redis_client.spop_count("non_existing_key", 3) == set()

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_ltrim(self, redis_client: TRedisClient):
Expand Down
6 changes: 6 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ async def transaction_test(
args.append(1)
transaction.sismember(key7, "bar")
args.append(True)
transaction.spop(key7)
args.append("bar")
transaction.sadd(key7, ["foo", "bar"])
args.append(2)
transaction.spop_count(key7, 4)
args.append({"foo", "bar"})

transaction.zadd(key8, {"one": 1, "two": 2, "three": 3, "four": 4})
args.append(4)
Expand Down
Loading