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
from geonamescache import GeonamesCache
from . import mappings


def country(from_key='name', to_key='iso'):
    """Creates and returns a mapper function to access country data.

    The mapper function that is returned must be called with one argument. In
    the default case you call it with a name and it returns a 3-letter
    ISO_3166-1 code, e. g. called with ``Spain`` it would return ``ESP``.

    :param from_key: (optional) the country attribute you give as input.
        Defaults to ``name``.
    :param to_key: (optional) the country attribute you want as output.
        Defaults to ``iso``.
    :return: mapper
    :rtype: function
    """

    gc = GeonamesCache()
    dataset = gc.get_dataset_by_key(gc.get_countries(), from_key)

    def mapper(input):
        # For country name inputs take the names mapping into account.
        if 'name' == from_key:
            input = mappings.country_names.get(input, input)
        # If there is a record return the demanded attribute.
        item = dataset.get(input)
        if item:
            return item[to_key]

    return mapper
Add documentation for country mapper
Add documentation for country mapper

  from geonamescache import GeonamesCache
  from . import mappings
  
  
  def country(from_key='name', to_key='iso'):
+     """Creates and returns a mapper function to access country data.
+ 
+     The mapper function that is returned must be called with one argument. In
+     the default case you call it with a name and it returns a 3-letter
+     ISO_3166-1 code, e. g. called with ``Spain`` it would return ``ESP``.
+ 
+     :param from_key: (optional) the country attribute you give as input.
+         Defaults to ``name``.
+     :param to_key: (optional) the country attribute you want as output.
+         Defaults to ``iso``.
+     :return: mapper
+     :rtype: function
+     """
+ 
      gc = GeonamesCache()
      dataset = gc.get_dataset_by_key(gc.get_countries(), from_key)
  
-     def mapper(key):
+     def mapper(input):
-         if 'name' == from_key and key in mappings.country_names:
-             key = mappings.country_names[key]
+         # For country name inputs take the names mapping into account.
+         if 'name' == from_key:
+             input = mappings.country_names.get(input, input)
+         # If there is a record return the demanded attribute.
-         item = dataset.get(key)
+         item = dataset.get(input)
          if item:
              return item[to_key]
  
      return mapper
Add documentation for country mapper
## Code Before:
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
## Instruction:
Add documentation for country mapper
## Code After:
from geonamescache import GeonamesCache
from . import mappings


def country(from_key='name', to_key='iso'):
    """Creates and returns a mapper function to access country data.

    The mapper function that is returned must be called with one argument. In
    the default case you call it with a name and it returns a 3-letter
    ISO_3166-1 code, e. g. called with ``Spain`` it would return ``ESP``.

    :param from_key: (optional) the country attribute you give as input.
        Defaults to ``name``.
    :param to_key: (optional) the country attribute you want as output.
        Defaults to ``iso``.
    :return: mapper
    :rtype: function
    """

    gc = GeonamesCache()
    dataset = gc.get_dataset_by_key(gc.get_countries(), from_key)

    def mapper(input):
        # For country name inputs take the names mapping into account.
        if 'name' == from_key:
            input = mappings.country_names.get(input, input)
        # If there is a record return the demanded attribute.
        item = dataset.get(input)
        if item:
            return item[to_key]

    return mapper