Skip to content

Commit

Permalink
Added support to retrieve the code associated with given US territory.
Browse files Browse the repository at this point in the history
Useful in cases where a US territory is given and the corresponding code
is required. For instance, one would receive the code "CA" in case
looking up the string "California".
  • Loading branch information
birkjernstrom committed Sep 10, 2012
1 parent e73bc93 commit 2d41017
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions paypal/countries.py
Expand Up @@ -1386,6 +1386,7 @@
)

_ordered_cache = {}
_name_lookup_cache = {}


def get_ordered():
Expand Down Expand Up @@ -1416,18 +1417,34 @@ def get_name(code):
return _get_name_from_dict(code, COUNTRIES)


def get_code(name):
return _get_code_from_dict(name, 'countries', COUNTRIES)


def get_us_state_name(code):
return _get_name_from_dict(code, US_STATES)


def get_us_state_code(name):
return _get_code_from_dict(name, 'us_states', US_TERRITORIES)


def get_us_province_name(code):
return _get_name_from_dict(code, US_PROVINCES)


def get_us_province_code(name):
return _get_code_from_dict(name, 'us_provinces', US_TERRITORIES)


def get_us_territory_name(code):
return _get_name_from_dict(code, US_TERRITORIES)


def get_us_territory_code(name):
return _get_code_from_dict(name, 'us_territories', US_TERRITORIES)


def _get_ordered_us_pairs(cache_key, source):
global _ordered_cache
cache = _ordered_cache.get(cache_key, None)
Expand All @@ -1441,3 +1458,20 @@ def _get_ordered_us_pairs(cache_key, source):

def _get_name_from_dict(code, source):
return source[code.upper()]['name']


def _get_code_lookup(cache_key, source):
global _name_lookup_cache
cache = _name_lookup_cache.get(cache_key, None)
if cache is not None:
return cache

lookup = dict((v['name'].lower(), v) for k, v in source.iteritems())
_name_lookup_cache[cache_key] = lookup
return lookup


def _get_code_from_dict(name, cache_key, source):
lookup = _get_code_lookup(cache_key, source)
result = lookup.get(name.lower(), None)
return result['code'] if result else None

0 comments on commit 2d41017

Please sign in to comment.