Skip to content
Browse files

tests for cities methods, use json as data format for cities to avoid…

… python2/3 incompatibilities with pickle
  • Loading branch information...
1 parent e6bac35 commit 5c6c6a37d85d34e7e0c5567fbe9d0c624d0e1232 @yaph committed
Showing with 24 additions and 19 deletions.
  1. +9 −10 geonamescache/__init__.py
  2. BIN geonamescache/cities.pickle
  3. +5 −9 scripts/cities.py
  4. +10 −0 tests/test_geonamescache.py
View
19 geonamescache/__init__.py
@@ -29,6 +29,10 @@ class GeonamesCache:
cities_by_names = {}
+ def get_dataset_by_key(self, dataset, key):
+ return dict((d[key], d) for c, d in dataset.items())
+
+
def get_continents(self):
return self.continents
@@ -42,11 +46,11 @@ def get_us_states(self):
def get_countries_by_names(self):
- return dict((d['name'], d) for c, d in self.countries.items())
+ return self.get_dataset_by_key(self.countries, 'name')
def get_us_states_by_names(self):
- return dict((d['name'], d) for c, d in self.us_states.items())
+ return self.get_dataset_by_key(self.us_states, 'name')
def get_cities(self):
@@ -71,15 +75,10 @@ def get_cities_by_name(self, name):
def _load_cities(self):
if self.cities is None:
- import os
- try:
- import cPickle as pickle
- except:
- import pickle
+ import os, json
fc = open(os.path.join(
- os.path.dirname(os.path.abspath(__file__)),
- 'cities.pickle'), 'rb')
- self.cities = pickle.load(fc)
+ os.path.dirname(os.path.abspath(__file__)), 'cities.json'), 'r')
+ self.cities = json.load(fc)
fc.close()
return self.cities
View
BIN geonamescache/cities.pickle
Binary file not shown.
View
14 scripts/cities.py
@@ -1,14 +1,10 @@
# -*- coding: utf-8 -*-
-import csv
-try:
- import cPickle as pickle
-except:
- import pickle
+import csv, json
cities = {}
-fcsv = open('../data/cities15000.txt', 'rb')
+fcsv = open('../data/cities15000.txt', 'r')
reader = csv.reader(fcsv, 'excel-tab')
-headers = reader.next()
+headers = next(reader)
for record in reader:
(geonameid, name, asciiname, alternatenames, latitude, longitude, featureclass, featurecode, countrycode, cc2, admin1code, admin2code, admin3code, admin4code, population, elevation, dem, timezone, modificationdate) = record
# required because used as key
@@ -23,6 +19,6 @@
'timezone': timezone
}
-fcities = open('../geonamescache/cities.pickle', 'wb')
-pickle.dump(cities, fcities)
+fcities = open('../geonamescache/cities.json', 'w')
+json.dump(cities, fcities)
fcities.close()
View
10 tests/test_geonamescache.py
@@ -57,6 +57,16 @@ def test_get_countries_by_names(self):
len(self.geonamescache.get_countries()))
+ def test_get_cities_by_name(self):
+ cities = self.geonamescache.get_cities()
+ for gid, name in (('3191316', 'Samobor'), ('3107112', 'Vaciamadrid')):
+ self.assertEqual(name, cities[gid]['name'])
+
+
+ def test_get_cities_by_name(self):
+ self.assertEqual(2, len(self.geonamescache.get_cities_by_name('Madrid')))
+
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 5c6c6a3

Please sign in to comment.
Something went wrong with that request. Please try again.