Permalink
Browse files

Merge branch 'develop'

  • Loading branch information...
2 parents a49ded4 + 93c83b1 commit 1891f443157282ee2dcca177e151736d4c1a676b @yaph committed Jun 17, 2012
Showing with 49 additions and 277 deletions.
  1. +5 −2 README.rst
  2. +17 −15 geonamescache/__init__.py
  3. +1 −0 geonamescache/countries.json
  4. +0 −255 geonamescache/geonamesdata.py
  5. +26 −5 scripts/countries.py
View
@@ -41,13 +41,16 @@ Currently geonamescache provides the following methods, that all return dictiona
- get_continents()
- get_countries()
- get_us_states()
-
+- get_cities()
+- get_countries_by_names()
+- get_us_states_by_names()
+- get_cities_by_name(name)
TODOs
-----
- analyze performance of get_cities_by_name
-
+- rather call get_dataset_by_key with name of dataset, so there is no need for *_by_names methods
Contributing
------------
View
@@ -10,23 +10,24 @@
"""
__title__ = 'geonamescache'
-__version__ = '0.6'
+__version__ = '0.7'
__author__ = 'Ramiro Gómez'
__license__ = 'MIT'
__copyright__ = 'Copyright 2012 Ramiro Gómez'
+import os, json
from . import geonamesdata
class GeonamesCache:
continents = geonamesdata.continents
- countries = geonamesdata.countries
us_states = geonamesdata.us_states
-
+ countries = None
cities = None
cities_items = None
cities_by_names = {}
+ datadir = os.path.dirname(os.path.abspath(__file__))
def get_dataset_by_key(self, dataset, key):
@@ -38,6 +39,8 @@ def get_continents(self):
def get_countries(self):
+ if self.countries is None:
+ self.countries = self._load_data(self.countries, 'countries.json')
return self.countries
@@ -46,17 +49,19 @@ def get_us_states(self):
def get_countries_by_names(self):
- return self.get_dataset_by_key(self.countries, 'name')
+ return self.get_dataset_by_key(self.get_countries(), 'name')
def get_us_states_by_names(self):
- return self.get_dataset_by_key(self.us_states, 'name')
+ return self.get_dataset_by_key(self.get_us_states(), 'name')
def get_cities(self):
"""Get a dictionary of cities keyed by geonameid."""
- return self._load_cities()
+ if self.cities is None:
+ self.cities = self._load_data(self.cities, 'cities.json')
+ return self.cities
def get_cities_by_name(self, name):
@@ -73,12 +78,9 @@ def get_cities_by_name(self, name):
return self.cities_by_names[name]
- def _load_cities(self):
- if self.cities is None:
- import os, json
- fc = open(os.path.join(
- os.path.dirname(os.path.abspath(__file__)), 'cities.json'), 'r')
- self.cities = json.load(fc)
- fc.close()
- return self.cities
-
+ def _load_data(self, datadict, datafile):
+ if datadict is None:
+ f = open(os.path.join(self.datadir, datafile), 'r')
+ datadict = json.load(f)
+ f.close()
+ return datadict

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 1891f44

Please sign in to comment.