Skip to content

Commit

Permalink
Python: adds HSTRLEN command (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon authored Jun 18, 2024
1 parent 0369b9c commit 0dde48b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Node: Added XLEN command ([#1555](https://github.com/aws/glide-for-redis/pull/1555))
* Node: Added ZINTERCARD command ([#1553](https://github.com/aws/glide-for-redis/pull/1553))
* Python: Added LMPOP and BLMPOP commands ([#1547](https://github.com/aws/glide-for-redis/pull/1547))
* Python: Added HSTRLEN command ([#1564](https://github.com/aws/glide-for-redis/pull/1564))
* Python: Added MSETNX command ([#1565](https://github.com/aws/glide-for-redis/pull/1565))
* Python: Added MOVE command ([#1566](https://github.com/aws/glide-for-redis/pull/1566))
* Node: Added OBJECT IDLETIME command ([#1567](https://github.com/aws/glide-for-redis/pull/1567))
Expand Down
25 changes: 24 additions & 1 deletion python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ async def hget(self, key: str, field: str) -> Optional[str]:
Returns None if `field` is not presented in the hash or `key` does not exist.
Examples:
>>> await client.hset("my_hash", "field")
>>> await client.hset("my_hash", "field", "value")
>>> await client.hget("my_hash", "field")
"value"
>>> await client.hget("my_hash", "nonexistent_field")
Expand Down Expand Up @@ -1149,6 +1149,29 @@ async def hrandfield_withvalues(self, key: str, count: int) -> List[List[str]]:
),
)

async def hstrlen(self, key: str, field: str) -> int:
"""
Returns the string length of the value associated with `field` in the hash stored at `key`.
See https://valkey.io/commands/hstrlen/ for more details.
Args:
key (str): The key of the hash.
field (str): The field in the hash.
Returns:
int: The string length or 0 if `field` or `key` does not exist.
Examples:
>>> await client.hset("my_hash", "field", "value")
>>> await client.hstrlen("my_hash", "my_field")
5
"""
return cast(
int,
await self._execute_command(RequestType.HStrlen, [key, field]),
)

async def lpush(self, key: str, elements: List[str]) -> int:
"""
Insert all the specified values at the head of the list stored at `key`.
Expand Down
15 changes: 15 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,21 @@ def hrandfield_withvalues(self: TTransaction, key: str, count: int) -> TTransact
RequestType.HRandField, [key, str(count), "WITHVALUES"]
)

def hstrlen(self: TTransaction, key: str, field: str) -> TTransaction:
"""
Returns the string length of the value associated with `field` in the hash stored at `key`.
See https://valkey.io/commands/hstrlen/ for more details.
Args:
key (str): The key of the hash.
field (str): The field in the hash.
Commands response:
int: The string length or 0 if `field` or `key` does not exist.
"""
return self.append_command(RequestType.HStrlen, [key, field])

def lpush(self: TTransaction, key: str, elements: List[str]) -> TTransaction:
"""
Insert all the specified values at the head of the list stored at `key`.
Expand Down
15 changes: 15 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,21 @@ async def test_hrandfield_withvalues(self, redis_client: TRedisClient):
with pytest.raises(RequestError):
await redis_client.hrandfield_withvalues(key2, 5)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_hstrlen(self, redis_client: TRedisClient):
key = get_random_string(10)

assert await redis_client.hstrlen(key, "field") == 0
assert await redis_client.hset(key, {"field": "value"}) == 1
assert await redis_client.hstrlen(key, "field") == 5

assert await redis_client.hstrlen(key, "field2") == 0

await redis_client.set(key, "value")
with pytest.raises(RequestError):
await redis_client.hstrlen(key, "field")

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_lpush_lpop_lrange(self, redis_client: TRedisClient):
Expand Down
2 changes: 2 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ async def transaction_test(
args.append([key3])
transaction.hrandfield_withvalues(key4, 1)
args.append([[key3, "10.5"]])
transaction.hstrlen(key4, key3)
args.append(4)

transaction.client_getname()
args.append(None)
Expand Down

0 comments on commit 0dde48b

Please sign in to comment.