Skip to content

Commit

Permalink
Issue #22: Add search_cities method and test
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed May 20, 2020
1 parent b158c9a commit dc7d765
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions geonamescache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ def get_us_counties(self):
self.us_counties, 'us_counties.json')
return self.us_counties

def search_cities(self, query, attribute='alternatenames'):
"""Search all city records and return list of records, that match query for given attribute."""

results = []
for key, record in self.get_cities().items():
if query in record[attribute]:
results.append(record)
return results

def _load_data(self, datadict, datafile):
if datadict is None:
with open(os.path.join(self.datadir, datafile), 'r') as f:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_geonamescache.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def test_cities_in_us_states(self):
self.assertEqual(name, cities[gid]['name'])
self.assertEqual(us_state, cities[gid]['admin1code'])

def test_search_cities(self):
cities = self.geonamescache.search_cities('Kiev')
self.assertGreaterEqual(len(cities), 1)

def test_us_counties_len(self):
# Make sure there are 3235 counties, which includes Puerto Rico etc.
us_counties = self.geonamescache.get_us_counties()
Expand Down

0 comments on commit dc7d765

Please sign in to comment.