作为我的中间尝试,没有尝试遍历不同的电路板。此数据仅由搜索所有查询生成。
@application.route('/', methods=['GET', 'POST'])
def index():
search = {"query": {"match_all": {}}}
resp = es.search(index="matchpoints", doc_type="score", body = search)
rows = extract_rows(resp)
for board in rows:
scores = score_board(board)
report(scores)
print(report(scores))
return 'ok'
def extract_rows(resp):
"""Extract the rows for the board from the query response."""
# Based on the data structure provided by the OP.
rows = [row["_source"] for row in resp["hits"]["hits"]]
# We want to return the group the data by board number
# so that we can score each board.
keyfunc = lambda row: int(row['board_number'])
rows.sort(key=keyfunc)
for _, group in itertools.groupby(rows, keyfunc):
yield list(group)
def compute_mp(scores, score):
"""Compute the match point score for a pair."""
mp_score = sum(v for k, v in scores.items() if score > k) * 2
# The pair's own score will always compare equal - remove it.
mp_score += sum(v for k, v in scores.items() if score == k) - 1
return mp_score
def score_board(tables):
"""Build the scores for each pair."""
scores = []
top = 2 * (len(tables) - 1)
# Store the scores for each N-S partnership.
ns_scores = collections.Counter(int(table["nsscore"]) for table in tables)
# Build the output for each pair.
与以前一样,它会产生正确的字典,其中评分正确,但结果重复且行数过多。
函数式编程
慕少森
相关分类