diff --git a/geonamescache/__init__.py b/geonamescache/__init__.py index ec58e8c..6b9fe30 100644 --- a/geonamescache/__init__.py +++ b/geonamescache/__init__.py @@ -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: diff --git a/tests/test_geonamescache.py b/tests/test_geonamescache.py index 1c89c43..47add98 100644 --- a/tests/test_geonamescache.py +++ b/tests/test_geonamescache.py @@ -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()