Skip to content

Commit 9046884

Browse files
Add support to ANY to GEOSEARCHSTORE and to GEOSEARCH
1 parent 5cd878d commit 9046884

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
@@ -2908,7 +2908,7 @@ def geopos(self, name, *values):
29082908

29092909
def georadius(self, name, longitude, latitude, radius, unit=None,
29102910
withdist=False, withcoord=False, withhash=False, count=None,
2911-
sort=None, store=None, store_dist=None):
2911+
sort=None, store=None, store_dist=None, any=False):
29122912
"""
29132913
Return the members of the specified key identified by the
29142914
``name`` argument which are within the borders of the area specified
@@ -2942,11 +2942,12 @@ def georadius(self, name, longitude, latitude, radius, unit=None,
29422942
unit=unit, withdist=withdist,
29432943
withcoord=withcoord, withhash=withhash,
29442944
count=count, sort=sort, store=store,
2945-
store_dist=store_dist)
2945+
store_dist=store_dist, any=any)
29462946

29472947
def georadiusbymember(self, name, member, radius, unit=None,
29482948
withdist=False, withcoord=False, withhash=False,
2949-
count=None, sort=None, store=None, store_dist=None):
2949+
count=None, sort=None, store=None, store_dist=None,
2950+
any=False):
29502951
"""
29512952
This command is exactly like ``georadius`` with the sole difference
29522953
that instead of taking, as the center of the area to query, a longitude
@@ -2958,7 +2959,7 @@ def georadiusbymember(self, name, member, radius, unit=None,
29582959
withdist=withdist, withcoord=withcoord,
29592960
withhash=withhash, count=count,
29602961
sort=sort, store=store,
2961-
store_dist=store_dist)
2962+
store_dist=store_dist, any=any)
29622963

29632964
def _georadiusgeneric(self, command, *args, **kwargs):
29642965
pieces = list(args)
@@ -2969,21 +2970,26 @@ def _georadiusgeneric(self, command, *args, **kwargs):
29692970
else:
29702971
pieces.append('m',)
29712972

2973+
if kwargs['any'] and kwargs['count'] is None:
2974+
raise DataError("``any`` can't be provided without ``count``")
2975+
29722976
for arg_name, byte_repr in (
2973-
('withdist', b'WITHDIST'),
2974-
('withcoord', b'WITHCOORD'),
2975-
('withhash', b'WITHHASH')):
2977+
('withdist', 'WITHDIST'),
2978+
('withcoord', 'WITHCOORD'),
2979+
('withhash', 'WITHHASH')):
29762980
if kwargs[arg_name]:
29772981
pieces.append(byte_repr)
29782982

2979-
if kwargs['count']:
2980-
pieces.extend([b'COUNT', kwargs['count']])
2983+
if kwargs['count'] is not None:
2984+
pieces.extend(['COUNT', kwargs['count']])
2985+
if kwargs['any']:
2986+
pieces.append('ANY')
29812987

29822988
if kwargs['sort']:
29832989
if kwargs['sort'] == 'ASC':
2984-
pieces.append(b'ASC')
2990+
pieces.append('ASC')
29852991
elif kwargs['sort'] == 'DESC':
2986-
pieces.append(b'DESC')
2992+
pieces.append('DESC')
29872993
else:
29882994
raise DataError("GEORADIUS invalid sort")
29892995

@@ -3116,7 +3122,8 @@ def _geosearchgeneric(self, command, *args, **kwargs):
31163122
if kwargs['any']:
31173123
pieces.append(b'ANY')
31183124
elif kwargs['any']:
3119-
raise DataError("GEOSEARCH any can't be provided without count")
3125+
raise DataError("GEOSEARCH ``any`` can't be provided "
3126+
"without count")
31203127

31213128
# other properties
31223129
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)