示例#1
0
    def test_insert(self):
        # Disable Foreign Keys checks for this test
        TestMatch.db.disableForeignKeys()

        with TestMatch.db.transaction() as t:
            TestMatch.db.upsert(
                Match('match date TD INS', 'league mnemonic TD INS',
                      'team name TD INS', 'team name TD INS', 'A', 5.6, 5.6,
                      5.6, 100, 100, 100, 100))
            objs = TestMatch.db.select(Match())

            self.assertEqual(len(objs), 3)

            d = eval(
                "{'date': 'match date TD INS', 'league': 'league mnemonic TD INS', 'home_team': 'team name TD INS', 'away_team': 'team name TD INS'}"
            )
            for k, v in d.items():
                self.assertEqual(objs[2].__getattribute__('get' + k.title())(),
                                 v)

            d = eval(
                "{'result': 'A', 'best_odds_h': 5.6, 'best_odds_d': 5.6, 'best_odds_a': 5.6, 'home_goals': 100, 'away_goals': 100, 'home_lp': 100, 'away_lp': 100}"
            )
            for k, v in d.items():
                self.assertEqual(objs[2].__getattribute__('get' + k.title())(),
                                 v)

            # force a rollback
            t.fail()
示例#2
0
 def test_isNullable(self):
     obj = Match()
     self.assertTrue(
         True and obj.isNullable('result') and obj.isNullable('best_odds_h')
         and obj.isNullable('best_odds_d') and obj.isNullable('best_odds_a')
         and obj.isNullable('home_goals') and obj.isNullable('away_goals')
         and obj.isNullable('home_lp') and obj.isNullable('away_lp'))
示例#3
0
    def test_update(self):
        # Disable Foreign Keys checks for this test
        TestMatch.db.disableForeignKeys()

        with TestMatch.db.transaction() as t:
            TestMatch.db.upsert(
                Match('match date TD', 'league mnemonic TD', 'team name TD',
                      'team name TD', 'A', 5.6, 5.6, 5.6, 100, 100, 100, 100))
            objs = TestMatch.db.select(
                Match('match date TD', 'league mnemonic TD', 'team name TD',
                      'team name TD'))

            self.assertEqual(len(objs), 1)
            self.assertEqual(objs[0].getDate(), 'match date TD')
            self.assertEqual(objs[0].getLeague(), 'league mnemonic TD')
            self.assertEqual(objs[0].getHome_Team(), 'team name TD')
            self.assertEqual(objs[0].getAway_Team(), 'team name TD')

            d = eval(
                "{'result': 'A', 'best_odds_h': 5.6, 'best_odds_d': 5.6, 'best_odds_a': 5.6, 'home_goals': 100, 'away_goals': 100, 'home_lp': 100, 'away_lp': 100}"
            )
            for k, v in d.items():
                self.assertEqual(objs[0].__getattribute__('get' + k.title())(),
                                 v)

            # force a rollback
            t.fail()

        with TestMatch.db.transaction() as t:
            match = TestMatch.db.select(
                Match('match date TD', 'league mnemonic TD', 'team name TD',
                      'team name TD'))[0]
            for k, v in d.items():
                match.__getattribute__('set' + k.title())(v)

            TestMatch.db.upsert(match)

            objs = TestMatch.db.select(
                Match('match date TD', 'league mnemonic TD', 'team name TD',
                      'team name TD'))
            self.assertEqual(len(objs), 1)
            self.assertEqual(objs[0].getDate(), 'match date TD')
            self.assertEqual(objs[0].getLeague(), 'league mnemonic TD')
            self.assertEqual(objs[0].getHome_Team(), 'team name TD')
            self.assertEqual(objs[0].getAway_Team(), 'team name TD')

            for k, v in d.items():
                self.assertEqual(objs[0].__getattribute__('get' + k.title())(),
                                 v)

            # force a rollback
            t.fail()
示例#4
0
    def test_delete(self):
        # Disable Foreign Keys checks for this test
        TestMatch.db.disableForeignKeys()

        with TestMatch.db.transaction() as t:
            TestMatch.db.delete(
                Match('match date TD', 'league mnemonic TD', 'team name TD',
                      'team name TD'))

            objs = TestMatch.db.select(Match())
            self.assertEqual(len(objs), 1)

            # force a rollback
            t.fail()
示例#5
0
    def test_sourceData_CurrentSeason(self):
        with Database(self.dbName, SQLite3Impl()) as db, \
                db.transaction() as t:
            seasonMap = db.select(Source_Season_Map(98))[0]
            seasonMap.setActive(1)
            db.upsert(seasonMap)
            seasonMap = Source_Season_Map(98, 'season name TD2', \
                    'source_season_map moniker TD2', \
                    'source_season_map data_url TD2', 1)
            db.upsert(seasonMap)

        sourceData(self.mock_Logger, 'source name TD', True)

        self.mock_readCSVFileAsDict.assert_any_call( \
                'source_season_map data_url TD2')

        calls = (
                    call.info(
                        'Downloading data from source: source name TD'),
                    call.debug('Opening database: ./db/test.db'),
                    call.debug(
                        "source : Keys {'id': 98} : Values {'name': " \
                        "'source name TD', 'fixtures_url': 'source " \
                        "fixtures_url TD', 'url': 'source url TD'}"),
                    call.debug(
                        "[source_season_map : Keys {'source_id': 98, "\
                        "'season': 'season name TD2'} : Values {'moniker'" \
                        ": 'source_season_map moniker TD2', 'data_url': " \
                        "'source_season_map data_url TD2', 'active': 1}]"),
                    call.debug(
                        "[source_league_map : Keys {'source_id': 98, " \
                        "'league': 'league mnemonic TD'} : Values " \
                        "{'moniker': 'source_league_map moniker TD'}]"),
                    call.debug(
                        "[team : Keys {'name': 'team name TD'} : Values " \
                        "{'league': 'league mnemonic TD'}, " \
                        "team : Keys {'name': 'team name TD2'} : Values " \
                        "{'league': 'league mnemonic TD2'}]"),
                    call.info('Downloading...source_season_map data_url TD2'),
                )
        self.mock_Logger.assert_has_calls(calls)

        with Database(self.dbName, SQLite3Impl()) as db:
            ht = db.select(Team('Coventry', 'league mnemonic TD'))[0]
            at = db.select(Team('Cardiff', 'league mnemonic TD'))[0]
            match = db.select(Match(201010191))[0]

            self.assertTrue(ht and at and match)
            self.assertEquals(match.getDate(), '2010-10-19')
            self.assertEquals(match.getLeague(), 'league mnemonic TD')
            self.assertEquals(match.getHome_Team(), 'Coventry')
            self.assertEquals(match.getAway_Team(), 'Cardiff')
            self.assertEquals(match.getResult(), 'A')
            self.assertEquals(match.getBest_Odds_H(), 3.12)
            self.assertEquals(match.getBest_Odds_D(), 3.4)
            self.assertEquals(match.getBest_Odds_A(), 2.4)
            self.assertEquals(match.getHome_Goals(), 1)
            self.assertEquals(match.getAway_Goals(), 2)
            self.assertEquals(match.getHome_Lp(), None)
            self.assertEquals(match.getAway_Lp(), None)
示例#6
0
 def test_repr(self):
     obj = Match('match date TD', 'league mnemonic TD', 'team name TD',
                 'team name TD', 'X', 2.3, 2.3, 2.3, 98, 98, 98, 98)
     self.assertEqual(
         str(obj),
         "match : Keys {'date': 'match date TD', 'league': 'league mnemonic TD', 'home_team': 'team name TD', 'away_team': 'team name TD'} : Values {'result': 'X', 'best_odds_h': 2.3, 'best_odds_d': 2.3, 'best_odds_a': 2.3, 'home_goals': 98, 'away_goals': 98, 'home_lp': 98, 'away_lp': 98}"
     )
示例#7
0
 def getStatisticsForResult(result, setfn, getfn):
     keys = {'league': league.getMnemonic(), 'result': result}
     order = ['>date']
     for m in db.select(Match.createAdhoc(keys, order)):
         rating = db.select(
             Rating(m.getDate(), m.getLeague(), m.getHome_Team(),
                    m.getAway_Team(), algoId))
         if rating:
             mark = rating[0].getMark()
             s = stats.get(mark, Statistics(str( \
                     datetime.now().date()), algoId, \
                     league.getMnemonic(), mark, 0, 0, 0, 0))
             s.setMark_Freq(s.getMark_Freq() + 1)
             setfn(s, getfn(s) + 1)
             stats[mark] = s
示例#8
0
    def markMatch(self, match: Match, homePrev: list, awayPrev: list):
        '''
        Mark the match according to the data from the prior matches,
        assumes prior matches are sorted by date descending.

        :param match: the subject match to be marked
        :param homePrev: the previous matches for the home team
        :param awayPrev: the previous matches for the away team
        :returns: an integer representing the match mark or 99 if there are
        matches to make the calculation
        '''
        if len(homePrev) <= self.numMatches or len(
                awayPrev) <= self.numMatches:
            return 99
        ''' 
        print(match)
        print(homePrev[:self.numMatches])
        print(awayPrev[:self.numMatches])
        '''
        hTeamGoals = self.sumGoals(match.getHome_Team(), \
                homePrev[:self.numMatches])
        aTeamGoals = self.sumGoals(match.getAway_Team(), \
                awayPrev[:self.numMatches])
        return hTeamGoals - aTeamGoals
示例#9
0
 def initialiseData(self):
     with Database(self.dbName, SQLite3Impl()) as db, db.transaction() as t:
         db.upsert(Match(201809011, '2018-09-01', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'H', 2.6, 3.1, 3.2, 2, 1))
         db.upsert(Match(201809021, '2018-09-02', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'D', 2.6, 3.1, 3.2, 1, 1))
         db.upsert(Match(201809031, '2018-09-03', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'A', 2.6, 3.1, 3.2, 0, 3))
         db.upsert(Match(201809041, '2018-09-04', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'A', 2.6, 3.1, 3.2, 2, 4))
         db.upsert(Match(201809051, '2018-09-05', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'H', 2.6, 3.1, 3.2, 2, 1))
         db.upsert(Match(201809061, '2018-09-06', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'D', 2.6, 3.1, 3.2, 3, 3))
         db.upsert(Match(201809071, '2018-09-07', 'league mnemonic TD', \
                 'team name TD', 'team name TD2', 'H', 2.6, 3.1, 3.2, 1, 0))
示例#10
0
    def test_create(self):
        obj = Match.create(
            ('match date TD', 'league mnemonic TD', 'team name TD',
             'team name TD', 'X', 2.3, 2.3, 2.3, 98, 98, 98, 98))

        self.assertEqual(obj.getDate(), 'match date TD')
        self.assertEqual(obj.getLeague(), 'league mnemonic TD')
        self.assertEqual(obj.getHome_Team(), 'team name TD')
        self.assertEqual(obj.getAway_Team(), 'team name TD')

        self.assertEqual(obj.getResult(), 'X')
        self.assertEqual(obj.getBest_Odds_H(), 2.3)
        self.assertEqual(obj.getBest_Odds_D(), 2.3)
        self.assertEqual(obj.getBest_Odds_A(), 2.3)
        self.assertEqual(obj.getHome_Goals(), 98)
        self.assertEqual(obj.getAway_Goals(), 98)
        self.assertEqual(obj.getHome_Lp(), 98)
        self.assertEqual(obj.getAway_Lp(), 98)
示例#11
0
def processMatchData(log, db, url, sourceId, season, leagueMap=None):
    log.info('Downloading...' + url)

    with readCSVFileAsDict(url) as resultsReader:
        for row in resultsReader:
            try:
                dt = datetime.strptime(row['Date'], '%d/%m/%y')
            except Exception as e:
                try:
                    dt = datetime.strptime(row['Date'], '%d/%m/%Y')
                except:
                    log.debug('Date conversion failed: {}'.format(e))
                    continue
            try:
                lge = row["b'Div"]
            except:
                lge = row['b"Div']

            if leagueMap and lge not in [l.getLeague() for l in leagueMap]:
                continue

            ht = row['HomeTeam']
            keys = {'source_id': sourceId, 'moniker': ht}
            teamMap = db.select(Source_Team_Map.createAdhoc(keys))
            if teamMap: ht = teamMap[0].getTeam()
            db.upsert(Team(ht, lge, season))

            at = row['AwayTeam']
            keys = {'source_id': sourceId, 'moniker': at}
            teamMap = db.select(Source_Team_Map.createAdhoc(keys))
            if teamMap: at = teamMap[0].getTeam()
            db.upsert(Team(at, lge, season))

            bestH, bestD, bestA = getBestOdds(log, row)
            match = Match(str(dt.date()), lge, ht, at, \
                    row['FTR'], bestH, bestD, bestA, \
                    row['FTHG'], row['FTAG'])
            log.debug(match)
            db.upsert(match)
示例#12
0
def testPredictions(log:Logger, algoId:int, season:str, predictions:Table, \
        show:bool=False):
    '''
    From the provided table of predictions, generate the actual betting
    outcome and display.

    :param log: a logging object
    :param algo: the algo under test
    :param season: the season under test
    :param predictions: a Table of match predictions with analytics
    :param show: displays any tables as HTML when True
    '''
    log.info('Running testPredictions for algo <{}> and season <{}>'.format( \
            algoId, season))

    config = getSweeperConfig()
    dbName = config['dbName']
    log.debug('Opening database: {}'.format(dbName))

    headers = ['Date', 'Match', 'Mark', 'RO', 'AO', 'Res', 'Stk', \
            'Win', 'Pot', 'Yld']
    schema = ['{:<12}', '{:<40}', '{:>4}', '{:>5.2f}', '{:>5.2f}', \
            '{:>3}', '{:>5}', '{:>5.2f}', '{:>5.2f}', '{:>5.2f}']
    startPot = 20.0
    winnings = Table(headers=headers, schema=schema,
            title='{}, starting pot {} units'.format( \
                    predictions.getTitle(), startPot))

    pot = startPot
    with Database(dbName, SQLite3Impl()) as db, db.transaction() as t:
        try:
            algo = db.select(Algo(algoId))[0]
            algo = AlgoFactory.create(algo.getName())
        except Exception as e:
            log.critical('No algo matching the provided id exists')
            log.critical('Because...%s' % e)
            sys.exit(1)
        try:
            season = db.select(Season(season))[0]
        except Exception as e:
            log.critical('No season matching the provided season exists')
            log.critical('Because...%s' % e)
            sys.exit(2)

        # Get the match result and best odds for each prediction and then
        # calculate winnings
        for p in predictions.getRows():
            dt = p[predictions.getHeaders().index('Date')]
            fix = p[predictions.getHeaders().index('Match')]
            ht, at = fix.split(' (vs) ')
            m = p[predictions.getHeaders().index('Mark')]
            ro = p[predictions.getHeaders().index('HO')]

            keys = {'date': dt, 'home_team': ht, 'away_team': at}
            match = db.select(Match.createAdhoc(keys))[0]
            ao = match.getBest_Odds_H()
            r = match.getResult()

            keys.update({'<date': keys.pop('date')})
            keys.update({'>date': season.getL_Bnd_Date()})
            del keys['home_team']
            del keys['away_team']
            priorMatches = db.select(Match.createAdhoc(keys))
            priorHTMatches = len([m for m in priorMatches \
                    if m.getHome_Team() == match.getHome_Team() \
                    or m.getAway_Team() == match.getHome_Team()])
            priorATMatches = len([m for m in priorMatches \
                    if m.getHome_Team() == match.getAway_Team() \
                    or m.getAway_Team() == match.getAway_Team()])

            incl = not (priorHTMatches < algo.numMatches or \
                    priorATMatches < algo.numMatches)
            colour = None
            row = [dt, fix, m, ro, ao, r]
            if incl and ao >= ro:
                if r == 'H':
                    pot += ao - 1
                    row += [1, ao]
                    colour = Table.Palette.GREEN
                else:
                    pot -= 1
                    row += [1, 0]
                    colour = Table.Palette.RED
            else:
                row += [0, 0]
            row += [pot, (pot - startPot) / startPot * 100.0]

            if colour:
                winnings.addHighlight(col='Match', pattern=fix, \
                        wholeRow=True, repeat=False, colour=colour)
            if incl:
                winnings.append([row])
    log.info(winnings)

    if show:
        winnings.asHTML(show)

    return winnings
示例#13
0
def genForm(log: Logger, team: str, date: str = None, show: bool = False):
    '''
    Generate form over the previous 6 matches for the team provided

    :param log: a logging object
    :param team: the subject team
    :param date: search date, today if None
    :param show: displays any tables as HTML when True
    '''
    log.info('Generating form for date <{}> and team <{}>'.format(date, team))

    config = getSweeperConfig()
    dbName = config['dbName']
    log.debug('Opening database: {}'.format(dbName))

    with Database(dbName, SQLite3Impl()) as db, db.transaction() as t:
        if date:
            dt = datetime.strptime(date, '%Y-%m-%d')
        else:
            dt = datetime.today().date()
        dt = dt + timedelta(days=1)

        try:
            keys = {'<date' : dt.strftime('%Y-%m-%d'), 'home_team' : team, \
                    '!result' : ''}
            order = {'>date'}
            matches1 = [m for m in db.select(Match.createAdhoc(keys, order))]
            del keys['home_team']
            keys['away_team'] = team
            matches2 = [m for m in db.select(Match.createAdhoc(keys, order))]
        except:
            log.critical("Couldn't find matches for team and date provided")
            sys.exit(2)

        matches = sorted(matches1 + matches2,
                         key=lambda m: m.getDate(),
                         reverse=True)[0:6]

        form = Form()
        for m in matches:
            form.played += 1
            if m.getHome_Team() == team:
                form.glfor += m.getHome_Goals()
                form.glagn += m.getAway_Goals()
            else:
                form.glfor += m.getAway_Goals()
                form.glagn += m.getHome_Goals()
            form.gldif = form.glfor - form.glagn
            if m.getResult() == 'H':
                if m.getHome_Team() == team:
                    form.won += 1
                    form.points += 3
                else:
                    form.lost += 1
            elif m.getResult() == 'D':
                form.drawn += 1
                form.points += 1
            elif m.getResult() == 'A':
                if m.getAway_Team() == team:
                    form.won += 1
                    form.points += 3
                else:
                    form.lost += 1
            else:
                raise Exception("Wasn't expecting that!")

        headers = ['Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'PTS']
        schema = ['{:<20}', '{:>3}', '{:>3}', '{:>3}', '{:>3}', '{:>3}', \
                '{:>3}', '{:>4}', '{:>4}']
        t1 = Table(headers=headers, schema=schema)
        t1.append([[team, *form.asList()]])
        log.info(t1)

        if show: t1.asHTML(show)

        headers = ['Date', 'Home Team', 'HTG', 'ATG', 'Away Team']
        schema = ['{:<12}', '{:<20}', '{:>3}', '{:>3}', '{:>20}']
        t2 = Table(headers=headers, schema=schema)
        t2.append([[m.getDate(), m.getHome_Team(), m.getHome_Goals(), \
                m.getAway_Goals(), m.getAway_Team()] for m in matches])
        t2.addHighlight('Home Team', team, False)
        log.info(t2)

        if show: t2.asHTML(show)

        return t1, t2
示例#14
0
def presentFixtures(log:Logger, algoId:int, league:str=None, show:bool=False, \
        mail:bool=False, backtest:bool=False, season:str=None):
    '''
    Present the latest set of fixtures with all the appropriate ratings.

    :param log: a logging object
    :param league: the subject league, None signifies all available leagues 
    :param show: displays any tables as HTML when True
    :param mail: send as email
    :param backtest: run in backtest mode
    :param season: season to run backtest for
    '''
    log.info('Presenting fixtures for algo <{}>, league <{}> and backtest ' \
            '<{}> for season <{}>'.format(algoId, league if league else 'ALL', \
            backtest, season))

    config = getSweeperConfig()
    dbName = config['dbName']
    log.debug('Opening database: {}'.format(dbName))

    with Database(dbName, SQLite3Impl()) as db, db.transaction() as t:
        date = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
        try:
            algo = db.select(Algo(algoId))[0]
            algo = AlgoFactory.create(algo.getName())
        except Exception as e:
            log.critical('No algo matching the provided id exists')
            log.critical('Because...%s' % e)
            sys.exit(2)

        if backtest:
            # In backtest mode use the inverse algoId to retrieve config,
            # ratings and stats and process all matches irrespective of
            # existing results
            algoId = -algoId
            if season:
                try:
                    season = db.select(Season(season))[0]
                except Exception as e:
                    log.critical( \
                            'No season matching the provided season exists')
                    sys.exit(3)
            else:
                log.critical('Must specify season with backtest')
                sys.exit(4)
            keys = {'>date' : season.getL_Bnd_Date(), \
                    '<date' : season.getU_Bnd_Date()}
        else:
            keys = {'>date': date, 'result': ''}

        try:
            if league: keys.update({'league': league})
            order = ['<league', '<date']
            fixtures = db.select(Match.createAdhoc(keys, order))
            if not fixtures: raise Exception('No fixtures')
        except Exception as e:
            log.critical("Couldn't find fixtures for league and date " \
                    "provided, run sourcedata?")
            log.critical('Because...{}'.format(e))
            sys.exit(5)

        try:
            if 'result' in keys: del keys['result']
            if '<date' in keys: keys.update({'<match_date': keys.pop('<date')})
            del keys['>date']
            dt = datetime.strptime(min(f.getDate() for f in fixtures), \
                    '%Y-%m-%d') - timedelta(days=1)
            keys.update({'>match_date': dt.strftime('%Y-%m-%d')})
            keys.update({'algo_id': algoId})
            order = ['<league', '<match_date']
            ratings = db.select(Rating.createAdhoc(keys, order))
            log.debug('Num fixtures {}, ratings {}'.format(len(fixtures), \
                    len(ratings)))
            if len(fixtures) != len(ratings):
                raise Exception('Mismatched ratings')
        except Exception as e:
            log.critical("Couldn't find algo ratings for all fixtures, " \
                    "run analysematches?")
            log.critical('Because...{}'.format(e))
            sys.exit(6)

        try:
            del keys['>match_date']
            if '<match_date' in keys: del keys['<match_date']
            keys.update({'>generation_date': date})
            order = ['>generation_date']
            stats = db.select(Statistics.createAdhoc(keys, order))
            if not stats: raise Exception('No statistics')
            lastGenDate = stats[0].getGeneration_Date()
            stats = [s for s in stats if s.getGeneration_Date() == lastGenDate]
        except Exception as e:
            log.critical("Couldn't find algo statistics for league and date, " \
                    "run genstats?")
            log.critical('Because...{}'.format(e))
            sys.exit(7)

        def statsSummary(s: Statistics):
            if s.getMark() == 99:
                return 0, 0, 0.0, 0.0, 0, 0.0, 0.0, 0, 0.0, 0.0
            markF = s.getMark_Freq()
            homeF = s.getHome_Freq()
            homeP = (homeF / markF) * 100.0 if markF else 0.0
            homeO = 100.0 / homeP if homeP else 99.99
            drawF = s.getDraw_Freq()
            drawP = (drawF / markF) * 100.0 if markF else 0.0
            drawO = 100.0 / drawP if drawP else 99.99
            awayF = s.getAway_Freq()
            awayP = (awayF / markF) * 100.0 if markF else 0.0
            awayO = 100.0 / awayP if awayP else 99.99
            return markF, homeF, homeP, homeO, drawF, drawP, drawO, awayF, \
                    awayP, awayO

        for r in itertools.filterfalse(lambda r : r.getMark() in \
                [s.getMark() for s in stats if r.getLeague() == s.getLeague()],\
                ratings):
            stats.append(Statistics(r.getMatch_Date(), r.getAlgo_Id(), \
                    r.getLeague(), r.getMark(), 0, 0, 0, 0))
        analytics = map(lambda r : [(r, statsSummary(s)) for s in stats \
                if r.getMark() == s.getMark() \
                and r.getLeague() == s.getLeague()], ratings)
        presentation = zip(fixtures, analytics)

        tables = {}
        mailText = 'Visit the website for more details - http://www.sweeperfootball.com<br/><br/>'
        for i, (league, group) in enumerate(itertools.groupby(presentation, \
                lambda x : x[0].getLeague())):
            try:
                leagueDesc = db.select(League(league))[0].getDesc()
            except Exception as e:
                log.critical("Couldn't find league")
                log.critical('Because..{}'.format(e))
                sys.exit(5)
            try:
                keys = {'league': league, 'algo_id': algoId}
                order = ['>config_date']
                algoCfg = db.select(Algo_Config.createAdhoc(keys, order))[0]
            except Exception as e:
                log.critical("Couldn't find algo config for league")
                log.critical('Because...{}'.format(e))
                sys.exit(6)

            presGrp = list(group)
            headers = ['Date', 'Match', 'Mark', 'M#', \
                    'H#', 'H%', 'HO', 'D#', 'D%', 'DO', 'A#', 'A%', 'AO']
            schema = ['{:<12}', '{:<40}', '{:>4}', '{:>4}', \
                    '{:>4}', '{:>5.2f}', '{:>5.2f}', '{:>4}', '{:>5.2f}', \
                    '{:>5.2f}', '{:>4}', '{:>5.2f}', '{:>5.2f}']
            t = Table(headers=headers, schema=schema, \
                    title='{} Fixtures'.format(leagueDesc))

            if backtest:
                # if we are backtesting then only return the predictions
                t.append([[f.getDate(), '{} (vs) {}'.format(f.getHome_Team(), \
                        f.getAway_Team()), r.getMark(), *a] \
                        for f, [(r, a)] in presGrp \
                        if r.getMark() > algoCfg.getL_Bnd_Mark() \
                        and r.getMark() < algoCfg.getU_Bnd_Mark()])
                return t

            t.append([[f.getDate(), '{} (vs) {}'.format(f.getHome_Team(), \
                    f.getAway_Team()), r.getMark(), *a] \
                    for f, [(r, a)] in presGrp])
            t.setHighlights([['Match', '{} (vs) {}'.format(f.getHome_Team(), \
                    f.getAway_Team()), False, False] \
                    for f, [(r, a)] in presGrp \
                    if r.getMark() > algoCfg.getL_Bnd_Mark() \
                    and r.getMark() < algoCfg.getU_Bnd_Mark()])
            t.htmlReplacements([['(vs)', '<br/>']])

            try:
                keys = {'>u_bnd_date': date, '<l_bnd_date': date}
                season = db.select(Season.createAdhoc(keys))[0].getName()
            except Exception as e:
                log.critical("Couldn't find season for date")
                log.critical('Because...{}'.format(e))
                sys.exit(6)

            mask = log.getMask()
            log.setMask(mask & ~Logger.INFO)
            leagueTable, formTable = genFormTable(log, league, season, date)
            log.setMask(mask)
            log.info(t)
            log.info(formTable)

            tables[leagueDesc] = (t, leagueTable, formTable)

            if show:
                t.asHTML(show)
                formTable.asHTML(show)

            if mail:
                if not i:
                    mailText += t.asHTML().replace('</body>', '') + '<br/>'
                else:
                    mailText += t.asHTML(fullyFormed=False) + '<br/>'
                mailText += formTable.asHTML(fullyFormed=False) + '<br/>'

        if mail:
            mailText = 'MIME-Version: 1.0\nContent-type: text/html\nSubject: Sweeper Football Predictions\n\n{}</body>'.format(
                mailText)
            #mailText = 'MIME-Version: 1.0\nContent-type: text/html\nSubject: Sweeper Football Predictions - PREDICTIONS AVAILABLE FROM THIS WEEK!\n\n{}</body>'.format(mailText)
            mailCfg = getSweeperConfig('mail.cfg')
            fromAddr = mailCfg['fromAddr']
            subs = db.select(Subscriber.createAdhoc({'include': 1}))
            toAddrs = [s.getEmail() for s in subs]
            server = smtplib.SMTP(mailCfg['svr'], int(mailCfg['port']))
            server.ehlo()
            server.starttls()
            server.ehlo()
            server.login(fromAddr, mailCfg['pwd'])
            server.sendmail(fromAddr, toAddrs, mailText)
            server.quit()
            log.info('email sent to: {!s}'.format(toAddrs))

        return tables
示例#15
0
    def test_select(self):
        objs = TestMatch.db.select(Match())
        self.assertEqual(len(objs), 2)
        self.assertEqual(objs[0].getDate(), 'match date TD')
        self.assertEqual(objs[0].getLeague(), 'league mnemonic TD')
        self.assertEqual(objs[0].getHome_Team(), 'team name TD')
        self.assertEqual(objs[0].getAway_Team(), 'team name TD')

        self.assertEqual(objs[0].getResult(), 'X')
        self.assertEqual(objs[0].getBest_Odds_H(), 2.3)
        self.assertEqual(objs[0].getBest_Odds_D(), 2.3)
        self.assertEqual(objs[0].getBest_Odds_A(), 2.3)
        self.assertEqual(objs[0].getHome_Goals(), 98)
        self.assertEqual(objs[0].getAway_Goals(), 98)
        self.assertEqual(objs[0].getHome_Lp(), 98)
        self.assertEqual(objs[0].getAway_Lp(), 98)

        self.assertEqual(objs[1].getDate(), 'match date TD2')
        self.assertEqual(objs[1].getLeague(), 'league mnemonic TD2')
        self.assertEqual(objs[1].getHome_Team(), 'team name TD2')
        self.assertEqual(objs[1].getAway_Team(), 'team name TD2')

        self.assertEqual(objs[1].getResult(), 'Z')
        self.assertEqual(objs[1].getBest_Odds_H(), 2.4)
        self.assertEqual(objs[1].getBest_Odds_D(), 2.4)
        self.assertEqual(objs[1].getBest_Odds_A(), 2.4)
        self.assertEqual(objs[1].getHome_Goals(), 99)
        self.assertEqual(objs[1].getAway_Goals(), 99)
        self.assertEqual(objs[1].getHome_Lp(), 99)
        self.assertEqual(objs[1].getAway_Lp(), 99)

        objs = TestMatch.db.select(
            Match('match date TD', 'league mnemonic TD', 'team name TD',
                  'team name TD'))
        self.assertEqual(len(objs), 1)
        self.assertEqual(objs[0].getDate(), 'match date TD')
        self.assertEqual(objs[0].getLeague(), 'league mnemonic TD')
        self.assertEqual(objs[0].getHome_Team(), 'team name TD')
        self.assertEqual(objs[0].getAway_Team(), 'team name TD')

        self.assertEqual(objs[0].getResult(), 'X')
        self.assertEqual(objs[0].getBest_Odds_H(), 2.3)
        self.assertEqual(objs[0].getBest_Odds_D(), 2.3)
        self.assertEqual(objs[0].getBest_Odds_A(), 2.3)
        self.assertEqual(objs[0].getHome_Goals(), 98)
        self.assertEqual(objs[0].getAway_Goals(), 98)
        self.assertEqual(objs[0].getHome_Lp(), 98)
        self.assertEqual(objs[0].getAway_Lp(), 98)

        objs = TestMatch.db.select(
            Match.createAdhoc({
                'result': 'X',
                'best_odds_h': 2.3,
                'best_odds_d': 2.3,
                'best_odds_a': 2.3,
                'home_goals': 98,
                'away_goals': 98,
                'home_lp': 98,
                'away_lp': 98
            }))
        self.assertEqual(len(objs), 1)
        self.assertEqual(objs[0].getDate(), 'match date TD')
        self.assertEqual(objs[0].getLeague(), 'league mnemonic TD')
        self.assertEqual(objs[0].getHome_Team(), 'team name TD')
        self.assertEqual(objs[0].getAway_Team(), 'team name TD')

        self.assertEqual(objs[0].getResult(), 'X')
        self.assertEqual(objs[0].getBest_Odds_H(), 2.3)
        self.assertEqual(objs[0].getBest_Odds_D(), 2.3)
        self.assertEqual(objs[0].getBest_Odds_A(), 2.3)
        self.assertEqual(objs[0].getHome_Goals(), 98)
        self.assertEqual(objs[0].getAway_Goals(), 98)
        self.assertEqual(objs[0].getHome_Lp(), 98)
        self.assertEqual(objs[0].getAway_Lp(), 98)
示例#16
0
 def test_keys_adhoc(self):
     l = Match.createAdhoc(None)
     self.assertEqual(l.getTable(), 'match')
     self.assertTrue(l._keys.getFields() is None)
示例#17
0
def analyseMatches(log:Logger, algoId:int, league:str=None, season:str=None, \
        backtest:bool=False):
    '''
    Mark all unmarked matches

    :param log: a logging object
    :param algoId: the algo to apply
    :param league: the league to apply the algo over, None means ALL
    :param season: the season to apply the algo over, None means ALL
    :param backtest: run in backtest mode
    '''
    log.info('Analysing matches for league <{}>, season <{}> with algo <{}> ' \
            'and backtest <{}>'.format(league if league else 'ALL', \
            season if season else 'ALL', algoId, backtest))

    config = getSweeperConfig()
    dbName = config['dbName']
    log.debug('Opening database: {}'.format(dbName))

    with Database(dbName, SQLite3Impl()) as db, db.transaction() as t:
        try:
            algo = db.select(Algo(algoId))[0]
            algo = AlgoFactory.create(algo.getName())
            # In backtest mode use the inverse algoId to retrieve config,
            # ratings and stats
            if backtest: algoId = -algoId
        except:
            log.critical('No algo matching the provided id exists')
            sys.exit(2)
        try:
            if league:
                leagues = db.select(League(league))
            else:
                leagues = db.select(League())
        except:
            log.critical('No league matching the provided mnemonic exists')
            sys.exit(3)
        try:
            if season: season = db.select(Season(season))[0]
        except:
            log.critical('No season matching the provided season exists')
            sys.exit(4)

        keys = {'algo_id': algoId, '<mark': 99}
        ratings = db.select(Rating.createAdhoc(keys))
        ratedMatchKeys = [MatchKeys(r.getMatch_Date(), r.getLeague(), \
                r.getHome_Team(), r.getAway_Team()) for r in ratings]
        log.info('Found {} ratings for algo {}'.format(len(ratedMatchKeys), \
                algoId))

        for league in leagues:
            if season:
                keys = {'league' : league.getMnemonic(), \
                        '>date' : season.getL_Bnd_Date(), \
                        '<date' : season.getU_Bnd_Date()}
            else:
                keys = {'league': league.getMnemonic()}
            order = ['>league', '>date']
            matches = db.select(Match.createAdhoc(keys, order))
            unmarked = list(filter(lambda x : x._keys not in ratedMatchKeys, \
                    matches))
            results = list(filter(lambda x: x.getResult() != '', matches))
            log.info('{} {} matches found unmarked'.format(len(unmarked), \
                    league.getMnemonic()))
            for m in unmarked:
                hTeamMatches = list(filter(lambda x : m.getHome_Team() in \
                        (x.getHome_Team(), x.getAway_Team()) and x.getDate() \
                        < m.getDate(), results))
                aTeamMatches = list(filter(lambda x : m.getAway_Team() in \
                        (x.getHome_Team(), x.getAway_Team()) and x.getDate() \
                        < m.getDate(), results))
                mark = algo.markMatch(m, hTeamMatches, aTeamMatches)
                if mark is not None:
                    db.upsert(Rating(m.getDate(), m.getLeague(), \
                            m.getHome_Team(), m.getAway_Team(), algoId, mark))
示例#18
0
def genLeagueTable(log:Logger, league:str, season:str, date:str=None, \
        show:bool=False):
    '''
    Generate a league table for the subject league and season

    :param log: a logging object
    :param league: the subject league
    :param season: the subject season
    :param date: the date string up to which to generate the league YYYY-MM-DD
    :param show: displays any tables as HTML when True
    '''
    log.info('Generating league table for ' \
            'league <{}> and season <{}>'.format(league, season))

    config = getSweeperConfig()
    dbName = config['dbName']
    log.debug('Opening database: {}'.format(dbName))

    with Database(dbName, SQLite3Impl()) as db, db.transaction() as t:     
        try:
            league = db.select(League(league))[0]
        except:
            log.critical('No league matching the provided mnemonic exists')
            sys.exit(3)
        try:
            season = db.select(Season(season))[0]
        except:
            log.critical('No season matching the provided string exists')
            sys.exit(4)

        ubnd =  date if date is not None else season.getU_Bnd_Date()
        keys = {'league' : league.getMnemonic(), '!result' : '', '>date' : \
                season.getL_Bnd_Date(), '<date' : ubnd} 
        matches = [m for m in db.select(Match.createAdhoc(keys))]
        log.info('{} {} matches found'.format(len(matches), \
                league.getMnemonic()))

        keys = {'league' : league.getMnemonic(), 'season' : season.getName()}
        teams = db.select(Team.createAdhoc(keys)) 

        table = dict([(t.getName(), Form()) for t in teams])
        for m in matches:
            table[m.getHome_Team()] = h = table.get(m.getHome_Team(), Form())
            table[m.getAway_Team()] = a = table.get(m.getAway_Team(), Form())
            h.played += 1
            a.played += 1
            h.glfor += m.getHome_Goals()
            h.glagn += m.getAway_Goals()
            h.gldif = h.glfor - h.glagn
            a.glfor += m.getAway_Goals()
            a.glagn += m.getHome_Goals()
            a.gldif = a.glfor - a.glagn
            if m.getResult() == 'H':
                h.won += 1
                h.points += 3
                a.lost += 1
            elif m.getResult() == 'D':
                h.drawn += 1
                h.points += 1
                a.drawn += 1
                a.points += 1
            elif m.getResult() == 'A':
                a.won += 1
                a.points += 3
                h.lost += 1
            else:
                raise Exception("Empty result, wasn't expecting that")

        headers = ['Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'PTS']
        schema = ['{:<20}', '{:>3}', '{:>3}', '{:>3}', '{:>3}', '{:>3}', \
                '{:>3}', '{:>4}', '{:>4}']
        t = Table(headers=headers, schema=schema, title='{} Table'.format(\
                league.getDesc()))
        t.append([[row[0], *row[1].asList()] for row in  \
                sorted(sorted(table.items(), key=itemgetter(0)), \
                key=lambda x : (x[1].points, x[1].gldif, x[1].glfor), \
                reverse=True)])
        log.info(t)

        if show: t.asHTML(show)

        return t