<p>另一个答案,反映了我对优雅的看法(我同意,这是一个相当主观的参数)。我希望我的对象由类定义,在构建时考虑到<a href="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="nofollow">OOP</a>,并使用管理对象之间关系的<a href="http://en.wikipedia.org/wiki/Object-relational_mapping" rel="nofollow">ORM</a>。这带来了许多优点和更清晰的代码。在</p>
<p>我在这里使用的是<a href="http://ponyorm.com/" rel="nofollow">pony ORM</a>,但是还有很多其他优秀的选项(最终会有更多的许可证),比如<a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>或{a5}。在</p>
<p><strong>下面是一个完整的示例</strong>—首先我们定义模型:</p>
<pre><code>from pony.orm import *
class Player(db.Entity):
"""A player is somebody who place a bet, identified by its name."""
name = Required(unicode)
score = Required(int, default=0)
bets = Set('Bet', reverse='player')
# any other player's info can be stored here
class Match(db.Entity):
"""A Match is a game, played or not yet played."""
ended = Required(bool, default=False)
home_score = Required(int, default=0)
visitors_score = Required(int, default=0)
bets = Set('Bet', reverse='match')
class Bet(db.Entity):
"""A class that stores a bet for a specific game"""
match = Required(Match, reverse="bets")
home_score = Required(int, default=0)
visitors_score = Required(int, default=0)
player = Required(Player, reverse="bets")
@db_session
def calculate_wins(match):
bets = select(b for b in Bet if b.match == match)[:]
for bet in bets:
if (match.home_score == bet.home_score) and (match.visitors_score == bet.visitors_score):
bet.player.score += 3 # exact
elif (match.home_score - match.visitors_score) == (bet.home_score - bet.visitors_score):
bet.player.score += 2 # goal differences
elif ((match.home_score > match.visitors_score) == (bet.home_score > bet.visitors_score)) and \
(match.home_score != match.visitors_score) and (bet.home_score != bet.visitors_score):
bet.player.score += 1 # tendency
else:
bet.player.score += 0 # wrong
</code></pre>
<p>使用这些类,您可以创建和更新您的比赛,球员,赌注数据库。
如果需要统计和数据聚合/排序,可以根据需要查询数据库。在</p>
^{pr2}$
<p>如果你愿意的话,你最终甚至可以使用numpy整合非常复杂的时间序列数据分析,就像Carst建议的那样,但是我相信这些添加的内容——尽管非常有趣——对你最初的问题来说有点不太好。在</p>