def pick_the_best(rates_records):
"""
Compare rates to each other and group then by absolute difference.
If there is group with minimal difference of two rates, choose one of them according the order of providers.
If there is group with minimal difference with more than two rates, choose rate in the middle / aka most common rate in the list.
:type rates_records: list[gold_digger.database.db_model.ExchangeRate]
:rtype: gold_digger.database.db_model.ExchangeRate
"""
if len(rates_records) in (1, 2):
return rates_records[0]
differences = defaultdict(list)
for a, b in combinations(rates_records, 2):
differences[abs(a.rate - b.rate)].extend((a, b)) # if (a,b)=1 and (b,c)=1 then differences[1]=[a,b,b,c]
minimal_difference, rates = min(differences.items())
if len(rates) == 2:
return rates[0]
else:
return Counter(rates).most_common(1)[0][0] # [(ExchangeRate, occurrences)]
exchange_rate_manager.py 文件源码
python
阅读 21
收藏 0
点赞 0
评论 0
评论列表
文章目录