diff --git a/README.rst b/README.rst index 0e22799..f69da47 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/geonamescache/mappers.py b/geonamescache/mappers.py new file mode 100644 index 0000000..b5dfe9f --- /dev/null +++ b/geonamescache/mappers.py @@ -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 \ No newline at end of file