From fab1baa20143eb3b0bfa1fcdabf9594aff50a13e Mon Sep 17 00:00:00 2001 From: Alexander Jones Date: Mon, 4 Jul 2016 19:49:41 -0500 Subject: [PATCH] API and class hierarchy changes This commit adds automated walkover detection and read-only generic competitor fields for both player-based and team-based matches. A modified class hierarchy has been established below Match. Due to the significant class hierarchy and API changes relative to the existing API, the version has been bumped to 0.2. --- README.rst | 7 +++ competitions/match/Match.py | 72 +++++++++++++++++++++++ competitions/match/default/SimpleMatch.py | 7 +-- competitions/match/default/TestMatch.py | 7 +-- setup.py | 2 +- 5 files changed, 86 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 6a01a60..4fd490d 100644 --- a/README.rst +++ b/README.rst @@ -7,6 +7,13 @@ competitions series of packages. This package will include simple default match simulators to support basic usage of this system with competitions-cup and competitions-league, among others. +Changes in v0.2 +--------------- + +- Generic competitor fields +- Walkover detection for two-competitor matches +- Modified class hierarchy + .. image:: https://travis-ci.org/happy5214/competitions-match.svg?branch=master :alt: Build status :target: https://travis-ci.org/happy5214/competitions-match diff --git a/competitions/match/Match.py b/competitions/match/Match.py index 6b6b729..64f1c57 100644 --- a/competitions/match/Match.py +++ b/competitions/match/Match.py @@ -26,3 +26,75 @@ class Match(object): def play(self): """Play the match.""" raise NotImplementedError + + +class TwoCompetitorMatch(Match): + + """Base class for matches with exactly two competitors.""" + + @property + def competitor1(self): + """The first (usually home) competitor.""" + raise NotImplementedError + + @property + def competitor2(self): + """The second (usually away) competitor.""" + raise NotImplementedError + + @property + def is_walkover(self): + """Whether or not this match is a walkover/bye.""" + return self.competitor1 is None or self.competitor2 is None + + +class TwoTeamMatch(TwoCompetitorMatch): + + """Base class for two-team matches.""" + + def __init__(self, team1, team2): + """Constructor. + + @param team1: The first (home) team + @type team1: An object that can be converted to a string + @param team2: The second (away) team + @type team2: An object that can be converted to a string + """ + self.team1 = team1 + self.team2 = team2 + + @property + def competitor1(self): + """The first (usually home) competitor.""" + raise self.team1 + + @property + def competitor2(self): + """The second (usually away) competitor.""" + raise self.team2 + + +class TwoPlayerMatch(TwoCompetitorMatch): + + """Base class for two-player matches.""" + + def __init__(self, player1, player2): + """Constructor. + + @param player1: The first (home) player + @type player1: An object that can be converted to a string + @param player2: The second (away) player + @type player2: An object that can be converted to a string + """ + self.player1 = player1 + self.player2 = player2 + + @property + def competitor1(self): + """The first (usually home) competitor.""" + raise self.player1 + + @property + def competitor2(self): + """The second (usually away) competitor.""" + raise self.player2 diff --git a/competitions/match/default/SimpleMatch.py b/competitions/match/default/SimpleMatch.py index bb731ea..581e1c9 100644 --- a/competitions/match/default/SimpleMatch.py +++ b/competitions/match/default/SimpleMatch.py @@ -20,10 +20,10 @@ import random -from competitions.match.Match import Match +from competitions.match.Match import TwoTeamMatch -class SimpleMatch(Match): +class SimpleMatch(TwoTeamMatch): """A simple default match simulator based on the card game "War".""" @@ -35,8 +35,7 @@ def __init__(self, team1, team2): @param team2: The second (away) team @type team2: An object that can be converted to a string """ - self.team1 = team1 - self.team2 = team2 + super(SimpleMatch, self).__init__(team1, team2) self.score1 = '' """The score for team1.""" self.score2 = '' diff --git a/competitions/match/default/TestMatch.py b/competitions/match/default/TestMatch.py index 73a3dca..7d00ece 100644 --- a/competitions/match/default/TestMatch.py +++ b/competitions/match/default/TestMatch.py @@ -18,10 +18,10 @@ from __future__ import unicode_literals -from competitions.match.Match import Match +from competitions.match.Match import TwoTeamMatch -class TestMatch(Match): +class TestMatch(TwoTeamMatch): """A match simulator for tests.""" @@ -33,8 +33,7 @@ def __init__(self, team1, team2): @param team2: The second (away) team @type team2: An object that can be converted to a string """ - self.team1 = team1 - self.team2 = team2 + super(TestMatch, self).__init__(team1, team2) self.score1 = '' """The score for team1.""" self.score2 = '' diff --git a/setup.py b/setup.py index c9f527c..b15f80b 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setup( name='competitions-match', - version='0.1.3', + version='0.2', description='competitions support library for matches', long_description=long_description,