def get_rates(currencies): sources = [] targets = [] if CACHE_ENABLED: rate_map = get_rates_cached(currencies) for (source, target), rate in rate_map.items(): if not rate: sources.append(source) targets.append(target) else: rate_map = {c: None for c in currencies} sources = map(itemgetter(0), currencies) targets = map(itemgetter(1), currencies) rates = ExchangeRate.objects.filter( source__code__in=sources, target__code__in=targets).values_list( 'source__code', 'target__code', 'rate') for source, target, rate in rates: key = (source, target) # Some other combinations that are not in currencies originally # may have been fetched from the query if key in rate_map: rate_map[key] = rate return rate_map
def convert_values(args_list): value_map = {} rate_map = {} conversions = {args[1:3] for args in args_list} if CACHE_ENABLED: rate_map = get_rates_cached(conversions) for args in args_list: conversion = args[1:3] rate = rate_map.get(conversion) if not rate: rate = ExchangeRate.objects.get_rate(conversion[1], conversion[2]) value_map[args] = rate return value_map
def get_rates(currencies): sources = [] targets = [] if CACHE_ENABLED: rate_map = get_rates_cached(currencies) for (source, target), rate in rate_map.items(): if not rate: sources.append(source) targets.append(target) else: rate_map = {c: None for c in currencies} sources = map(itemgetter(0), currencies) targets = map(itemgetter(1), currencies) rates = ExchangeRate.objects.filter(source__code__in=sources, target__code__in=targets).values_list( 'source__code', 'target__code', 'rate') for source, target, rate in rates: key = (source, target) # Some other combinations that are not in currencies originally # may have been fetched from the query if key in rate_map: rate_map[key] = rate return rate_map