-
Notifications
You must be signed in to change notification settings - Fork 65
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: adds GEODIST command #1260
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -149,6 +149,29 @@ def __init__(self, longitude: float, latitude: float): | |||||
self.latitude = latitude | ||||||
|
||||||
|
||||||
class GeoUnit(Enum): | ||||||
""" | ||||||
Enumeration representing distance units options for the `GEODIST` command. | ||||||
""" | ||||||
|
||||||
METERS = "m" | ||||||
""" | ||||||
Represents distance in meters. | ||||||
""" | ||||||
KILOMETERS = "km" | ||||||
""" | ||||||
Represents distance in kilometers. | ||||||
""" | ||||||
MILES = "mi" | ||||||
""" | ||||||
Represents distance in miles. | ||||||
""" | ||||||
FEET = "ft" | ||||||
""" | ||||||
Represents distance in feet. | ||||||
""" | ||||||
|
||||||
|
||||||
class ExpirySet: | ||||||
"""SET option: Represents the expiry type and value to be executed with "SET" command.""" | ||||||
|
||||||
|
@@ -1627,6 +1650,47 @@ async def geoadd( | |||||
await self._execute_command(RequestType.GeoAdd, args), | ||||||
) | ||||||
|
||||||
async def geodist( | ||||||
self, | ||||||
key: str, | ||||||
member1: str, | ||||||
member2: str, | ||||||
unit: Optional[GeoUnit] = None, | ||||||
) -> Optional[float]: | ||||||
""" | ||||||
Returns the distance between two members in the geospatial index stored at `key`. | ||||||
|
||||||
See https://valkey.io/commands/geodist for more details. | ||||||
|
||||||
Args: | ||||||
key (str): The key of the sorted set. | ||||||
member1 (str): The name of the first member. | ||||||
member2 (str): The name of the second member. | ||||||
unit (Optional[GeoUnit]): The unit of distance measurement. See `GeoUnit`. | ||||||
If not specified, the default unit is `METERS`. | ||||||
|
||||||
Returns: | ||||||
Optional[float]: The distance between `member1` and `member2`. | ||||||
If one or both members do not exist, or if the key does not exist, returns None. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Examples: | ||||||
>>> await client.geoadd("my_geo_set", {"Palermo": GeospatialData(13.361389, 38.115556), "Catania": GeospatialData(15.087269, 37.502669)}) | ||||||
>>> await client.geodist("my_geo_set", "Palermo", "Catania") | ||||||
166274.1516 # Indicates the distance between "Palermo" and "Catania" in meters. | ||||||
>>> await client.geodist("my_geo_set", "Palermo", "Palermo", unit=GeoUnit.KILOMETERS) | ||||||
166.2742 # Indicates the distance between "Palermo" and "Palermo" in kilometers. | ||||||
>>> await client.geodist("my_geo_set", "non-existing", "Palermo", unit=GeoUnit.KILOMETERS) | ||||||
None # Returns None for non-existing memeber. | ||||||
shohamazon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
args = [key, member1, member2] | ||||||
if unit: | ||||||
args.append(unit.value) | ||||||
|
||||||
return cast( | ||||||
Optional[float], | ||||||
await self._execute_command(RequestType.GeoDist, args), | ||||||
) | ||||||
|
||||||
async def geohash(self, key: str, members: List[str]) -> List[Optional[str]]: | ||||||
""" | ||||||
Returns the GeoHash strings representing the positions of all the specified members in the sorted set stored at | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||
ExpireOptions, | ||||||
ExpirySet, | ||||||
GeospatialData, | ||||||
GeoUnit, | ||||||
InfoSection, | ||||||
InsertPosition, | ||||||
UpdateOptions, | ||||||
|
@@ -1233,6 +1234,35 @@ def geoadd( | |||||
|
||||||
return self.append_command(RequestType.GeoAdd, args) | ||||||
|
||||||
def geodist( | ||||||
self: TTransaction, | ||||||
key: str, | ||||||
member1: str, | ||||||
member2: str, | ||||||
unit: Optional[GeoUnit] = None, | ||||||
) -> TTransaction: | ||||||
""" | ||||||
Returns the distance between two members in the geospatial index stored at `key`. | ||||||
|
||||||
See https://valkey.io/commands/geodist for more details. | ||||||
|
||||||
Args: | ||||||
key (str): The key of the sorted set. | ||||||
member1 (str): The name of the first member. | ||||||
member2 (str): The name of the second member. | ||||||
unit (Optional[GeoUnit]): The unit of distance measurement. See `GeoUnit`. | ||||||
If not specified, the default unit is meters. | ||||||
|
||||||
Returns: | ||||||
Yury-Fridlyand marked this conversation as resolved.
Show resolved
Hide resolved
shohamazon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Optional[float]: The distance between `member1` and `member2`. | ||||||
If one or both members do not exist, or if the key does not exist, returns None. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
args = [key, member1, member2] | ||||||
if unit: | ||||||
args.append(unit.value) | ||||||
|
||||||
return self.append_command(RequestType.GeoDist, args) | ||||||
|
||||||
def geohash(self: TTransaction, key: str, members: List[str]) -> TTransaction: | ||||||
""" | ||||||
Returns the GeoHash strings representing the positions of all the specified members in the sorted set stored at | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
ExpirySet, | ||
ExpiryType, | ||
GeospatialData, | ||
GeoUnit, | ||
InfBound, | ||
InfoSection, | ||
InsertPosition, | ||
|
@@ -1338,6 +1339,33 @@ async def test_geohash(self, redis_client: TRedisClient): | |
with pytest.raises(RequestError): | ||
await redis_client.geohash(key, ["Palermo", "Catania"]) | ||
|
||
@pytest.mark.parametrize("cluster_mode", [True, False]) | ||
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) | ||
async def test_geodist(self, redis_client: TRedisClient): | ||
key, key2 = get_random_string(10), get_random_string(10) | ||
members_coordinates = { | ||
"Palermo": GeospatialData(13.361389, 38.115556), | ||
"Catania": GeospatialData(15.087269, 37.502669), | ||
} | ||
assert await redis_client.geoadd(key, members_coordinates) == 2 | ||
|
||
assert await redis_client.geodist(key, "Palermo", "Catania") == 166274.1516 | ||
assert ( | ||
await redis_client.geodist(key, "Palermo", "Catania", GeoUnit.KILOMETERS) | ||
== 166.2742 | ||
) | ||
assert await redis_client.geodist(key, "Palermo", "Palermo", GeoUnit.MILES) == 0 | ||
assert ( | ||
Yury-Fridlyand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await redis_client.geodist( | ||
key, "Palermo", "non-existing-member", GeoUnit.FEET | ||
) | ||
== None | ||
) | ||
|
||
assert await redis_client.set(key2, "value") == OK | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment for that test |
||
with pytest.raises(RequestError): | ||
await redis_client.geodist(key2, "Palmero", "Catania") | ||
|
||
@pytest.mark.parametrize("cluster_mode", [True, False]) | ||
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) | ||
async def test_zadd_zaddincr(self, redis_client: TRedisClient): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to add a test?