Skip to content

Commit

Permalink
#4 added country mapper that allows to map arbitrary properties
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Aug 8, 2014
1 parent da81f57 commit 0f36e74
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,22 @@ dictionaries with the requested data:
- get_us_states_by_names()
- get_cities_by_name(name)


Mappers
-------

The mappers module provides function(s) to map data properties. Currently you can create a mapper that maps country properties, e. g. the `name` property to the `iso3`property, to do so you'd write the following code:

from geonamescache.mappers import country
mapper = country(from_key='name', to_key='iso3')

iso3 = mapper('Spain') # iso3 is assigned ESP


TODOs
-----

- create sphinx docs
- analyze performance of get_cities_by_name
- call get_dataset_by_key with name of dataset, so there is no need for \*_by_names methods

Expand Down
17 changes: 17 additions & 0 deletions geonamescache/mappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from geonamescache import GeonamesCache
from . import mappings


def country(from_key='name', to_key='iso'):
gc = GeonamesCache()
dataset = gc.get_dataset_by_key(gc.get_countries(), from_key)

def mapper(key):
if 'name' == from_key and key in mappings.country_names:
key = mappings.country_names[key]
item = dataset.get(key)
if item:
return item[to_key]

return mapper

0 comments on commit 0f36e74

Please sign in to comment.