Skip to content

Commit 298d722

Browse files
Merge pull request #1606 from AvitalFineRedis/GEORADIUS_count_any
Add support to ANY to GEOSEARCHSTORE and to GEOSEARCH
2 parents eeac252 + 9046884 commit 298d722

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

redis/commands.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -2932,7 +2932,7 @@ def geopos(self, name, *values):
29322932

29332933
def georadius(self, name, longitude, latitude, radius, unit=None,
29342934
withdist=False, withcoord=False, withhash=False, count=None,
2935-
sort=None, store=None, store_dist=None):
2935+
sort=None, store=None, store_dist=None, any=False):
29362936
"""
29372937
Return the members of the specified key identified by the
29382938
``name`` argument which are within the borders of the area specified
@@ -2966,11 +2966,12 @@ def georadius(self, name, longitude, latitude, radius, unit=None,
29662966
unit=unit, withdist=withdist,
29672967
withcoord=withcoord, withhash=withhash,
29682968
count=count, sort=sort, store=store,
2969-
store_dist=store_dist)
2969+
store_dist=store_dist, any=any)
29702970

29712971
def georadiusbymember(self, name, member, radius, unit=None,
29722972
withdist=False, withcoord=False, withhash=False,
2973-
count=None, sort=None, store=None, store_dist=None):
2973+
count=None, sort=None, store=None, store_dist=None,
2974+
any=False):
29742975
"""
29752976
This command is exactly like ``georadius`` with the sole difference
29762977
that instead of taking, as the center of the area to query, a longitude
@@ -2982,7 +2983,7 @@ def georadiusbymember(self, name, member, radius, unit=None,
29822983
withdist=withdist, withcoord=withcoord,
29832984
withhash=withhash, count=count,
29842985
sort=sort, store=store,
2985-
store_dist=store_dist)
2986+
store_dist=store_dist, any=any)
29862987

29872988
def _georadiusgeneric(self, command, *args, **kwargs):
29882989
pieces = list(args)
@@ -2993,21 +2994,26 @@ def _georadiusgeneric(self, command, *args, **kwargs):
29932994
else:
29942995
pieces.append('m',)
29952996

2997+
if kwargs['any'] and kwargs['count'] is None:
2998+
raise DataError("``any`` can't be provided without ``count``")
2999+
29963000
for arg_name, byte_repr in (
2997-
('withdist', b'WITHDIST'),
2998-
('withcoord', b'WITHCOORD'),
2999-
('withhash', b'WITHHASH')):
3001+
('withdist', 'WITHDIST'),
3002+
('withcoord', 'WITHCOORD'),
3003+
('withhash', 'WITHHASH')):
30003004
if kwargs[arg_name]:
30013005
pieces.append(byte_repr)
30023006

3003-
if kwargs['count']:
3004-
pieces.extend([b'COUNT', kwargs['count']])
3007+
if kwargs['count'] is not None:
3008+
pieces.extend(['COUNT', kwargs['count']])
3009+
if kwargs['any']:
3010+
pieces.append('ANY')
30053011

30063012
if kwargs['sort']:
30073013
if kwargs['sort'] == 'ASC':
3008-
pieces.append(b'ASC')
3014+
pieces.append('ASC')
30093015
elif kwargs['sort'] == 'DESC':
3010-
pieces.append(b'DESC')
3016+
pieces.append('DESC')
30113017
else:
30123018
raise DataError("GEORADIUS invalid sort")
30133019

@@ -3140,7 +3146,8 @@ def _geosearchgeneric(self, command, *args, **kwargs):
31403146
if kwargs['any']:
31413147
pieces.append(b'ANY')
31423148
elif kwargs['any']:
3143-
raise DataError("GEOSEARCH any can't be provided without count")
3149+
raise DataError("GEOSEARCH ``any`` can't be provided "
3150+
"without count")
31443151

31453152
# other properties
31463153
for arg_name, byte_repr in (

tests/test_commands.py

+6
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,9 @@ def test_georadius_count(self, r):
26552655
r.geoadd('barcelona', *values)
26562656
assert r.georadius('barcelona', 2.191, 41.433, 3000, count=1) == \
26572657
[b'place1']
2658+
assert r.georadius('barcelona', 2.191, 41.433, 3000,
2659+
count=1, any=True) == \
2660+
[b'place2']
26582661

26592662
@skip_if_server_version_lt('3.2.0')
26602663
def test_georadius_sort(self, r):
@@ -2706,6 +2709,9 @@ def test_georadiusmember(self, r):
27062709
(2.187376320362091, 41.40634178640635)],
27072710
[b'place1', 0.0, 3471609698139488,
27082711
(2.1909382939338684, 41.433790281840835)]]
2712+
assert r.georadiusbymember('barcelona', 'place1', 4000,
2713+
count=1, any=True) == \
2714+
[b'\x80place2']
27092715

27102716
@skip_if_server_version_lt('5.0.0')
27112717
def test_xack(self, r):

0 commit comments

Comments
 (0)