def test_select(self): objs = TestLeague.db.select(League()) self.assertEqual(len(objs), 2) self.assertEqual(objs[0].getMnemonic(), 'league mnemonic TD') self.assertEqual(objs[0].getName(), 'league name TD') self.assertEqual(objs[0].getDesc(), 'league desc TD') self.assertEqual(objs[1].getMnemonic(), 'league mnemonic TD2') self.assertEqual(objs[1].getName(), 'league name TD2') self.assertEqual(objs[1].getDesc(), 'league desc TD2') objs = TestLeague.db.select(League('league mnemonic TD')) self.assertEqual(len(objs), 1) self.assertEqual(objs[0].getMnemonic(), 'league mnemonic TD') self.assertEqual(objs[0].getName(), 'league name TD') self.assertEqual(objs[0].getDesc(), 'league desc TD') objs = TestLeague.db.select( League.createAdhoc({ 'name': 'league name TD', 'desc': 'league desc TD' })) self.assertEqual(len(objs), 1) self.assertEqual(objs[0].getMnemonic(), 'league mnemonic TD') self.assertEqual(objs[0].getName(), 'league name TD') self.assertEqual(objs[0].getDesc(), 'league desc TD')
def test_insert(self): # Disable Foreign Keys checks for this test TestLeague.db.disableForeignKeys() with TestLeague.db.transaction() as t: TestLeague.db.upsert( League('league mnemonic TD INS', 'league name TD UPD', 'league desc TD UPD')) objs = TestLeague.db.select(League()) self.assertEqual(len(objs), 3) d = eval("{'mnemonic': 'league mnemonic TD INS'}") for k, v in d.items(): self.assertEqual(objs[2].__getattribute__('get' + k.title())(), v) d = eval( "{'name': 'league name TD UPD', 'desc': 'league desc TD UPD'}") for k, v in d.items(): self.assertEqual(objs[2].__getattribute__('get' + k.title())(), v) # force a rollback t.fail()
def test_delete(self): # Disable Foreign Keys checks for this test TestLeague.db.disableForeignKeys() with TestLeague.db.transaction() as t: TestLeague.db.delete(League('league mnemonic TD')) objs = TestLeague.db.select(League()) self.assertEqual(len(objs), 1) # force a rollback t.fail()
def test_create(self): obj = League.create( ('league mnemonic TD', 'league name TD', 'league desc TD')) self.assertEqual(obj.getMnemonic(), 'league mnemonic TD') self.assertEqual(obj.getName(), 'league name TD') self.assertEqual(obj.getDesc(), 'league desc TD')
def genFormTable(log:Logger, league:str, season:str, date:str=None, \ show:bool=False): ''' Generate a form 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 :returns: the league and form tables ''' log.info('Generating form 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(5) headers = ['Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'PTS'] schema = ['{:<20}', '{:>3}', '{:>3}', '{:>3}', '{:>3}', '{:>3}', \ '{:>3}', '{:>4}', '{:>4}'] formTable = Table(headers=headers, schema=schema, \ title='{} Form Table'.format(league.getDesc())) leagueTable = genLeagueTable(log, league.getMnemonic(), \ season.getName(), date, show) for team in leagueTable.getColumns()[0]: form, results = genForm(log, team, date) formTable.append(form.getRows()) formTable.setRows([row for row in \ sorted(sorted(formTable.getRows(), key=itemgetter(0)), \ key=lambda x : (x[8], x[7], x[5]), reverse=True)]) log.info(formTable) if show: formTable.asHTML(show) return leagueTable, formTable
def test_repr(self): obj = League('league mnemonic TD', 'league name TD', 'league desc TD') self.assertEqual( str(obj), "league : Keys {'mnemonic': 'league mnemonic TD'} : Values {'name': 'league name TD', 'desc': 'league desc TD'}" )
def test_keys_adhoc(self): l = League.createAdhoc(None) self.assertEqual(l.getTable(), 'league') self.assertTrue(l._keys.getFields() is None)
def test_isNullable(self): obj = League() self.assertTrue(True and obj.isNullable('desc'))
def genStats(log: Logger, algoId: int, league: str = None, backtest: bool = False): ''' Generate statistics on the marked matches :param log: a logging object :param algoId: the algo to apply :param league: the league to apply the algo over, all if unset :param backtest: run in backtest mode ''' log.info('Generating statistics for league <{}> with algo <{}> and ' \ 'backtest <{}>'.format(league if league 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(3) 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(4) for league in leagues: stats = {} 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 getStatisticsForResult('H', Statistics.setHome_Freq, Statistics.getHome_Freq) getStatisticsForResult('D', Statistics.setDraw_Freq, Statistics.getDraw_Freq) getStatisticsForResult('A', Statistics.setAway_Freq, Statistics.getAway_Freq) for k, v in stats.items(): db.upsert(v)
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
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))
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