@@ -45,21 +45,33 @@ def do_ls(self, name: str = ""):
45
45
query = qb .build ()
46
46
list_cities (query .find ())
47
47
48
- def do_city_neighbors (self , city : str ):
49
- """find five next neighbors to city <name>\n usage: city_neighbors <name>"""
50
- qb = self ._box .query ()
51
- qb .equals_string (self ._name_prop , city )
52
- query = qb .build ()
53
- cities = query .find ()
54
- if len (cities ) == 1 :
55
- location = cities [0 ].location
48
+ def do_city_neighbors (self , args : str ):
49
+ """find <count> (default: 5) next neighbors to city <name>\n usage: city_neighbors <name>, [,<count>]"""
50
+ try :
51
+ args = args .split (',' )
52
+ if len (args ) > 2 :
53
+ raise ValueError ()
54
+ city = args [0 ]
55
+ if len (city ) == 0 :
56
+ raise ValueError ()
57
+ count = 5
58
+ if len (args ) == 2 :
59
+ count = int (args [1 ])
56
60
qb = self ._box .query ()
57
- qb .nearest_neighbors_f32 (self ._location_prop , location , 6 )
58
- qb .not_equals_string (self ._name_prop , city )
59
- neighbors = qb .build ().find_with_scores ()
60
- list_cities_with_scores (neighbors )
61
- else :
62
- print (f"no city found named '{ city } '" )
61
+ qb .equals_string (self ._name_prop , city )
62
+ query = qb .build ()
63
+ cities = query .find ()
64
+ if len (cities ) == 1 :
65
+ location = cities [0 ].location
66
+ qb = self ._box .query ()
67
+ qb .nearest_neighbors_f32 (self ._location_prop , location , count + 1 ) # +1 for the city
68
+ qb .not_equals_string (self ._name_prop , city )
69
+ neighbors = qb .build ().find_with_scores ()
70
+ list_cities_with_scores (neighbors )
71
+ else :
72
+ print (f"no city found named '{ city } '" )
73
+ except ValueError :
74
+ print ("usage: city_neighbors <name>[,<count>]" )
63
75
64
76
def do_neighbors (self , args ):
65
77
"""find <count> neighbors to geo-coord <lat> <long>.\n usage: neighbors <count>,<latitude>,<longitude>"""
0 commit comments