def getMatchLine(self, match, teamId, leagueResLink): html = """ <tr> <td class="date">{date}</td> <td class="time">{time}</td> <td class="court">{court}</td> <td class="opponent"><a href="{oppfix.url}">{oppname}</a></td> <td class="homeAway">{homeaway}</td> <td class="result">{result}</td> </tr> """ mDate = DateFormatter.formatDate(match.datetime, False, False) mTime = DateFormatter.formatTime(match.datetime) mCourt = match.court mOppFixLink = PageLink("teamFixtures", self, {"team": match.opponentId}, True) mOppName = match.opponentName mHomeAway = "H" if match.home else "A" mOppId = match.opponentId mHomeId = teamId if match.home else mOppId mAwayId = mOppId if match.home else teamId if match.result: if match.margin: res = "{0} by {1}".format(match.result, match.margin) else: res = "{0}".format(match.result) mResult = "<a href=\"{leaguefix.url}#{homeid}{awayid}\">{result}</a>".format(leaguefix=leagueResLink, teamid=teamId, homeid=mHomeId, awayid=mAwayId, result=res) else: mResult = "" answer = html.format(date=mDate, time=mTime, court=mCourt, oppfix = mOppFixLink, oppname=mOppName, homeaway=mHomeAway, result=mResult) return answer;
def getOtherDateLinks(self, report): html = """ <ul id="datenav" class="noprint"> <li>Results on other dates:</li> {datelinks} </ul> """ linkHtml = """ <li> <a href="{datelink.url}">{date}</a> </li> """ dates = {} for d in report.otherDates: isoDate = DateFormatter.formatDateToISO(d) dateStr = DateFormatter.formatDate(d, False, None) dates[isoDate] = dateStr if len(dates) == 0: answer = "" else: linkData = [] for isoDate in sorted(dates.keys(), reverse=True): dateStr = dates[isoDate] if len(linkData) == 0 and self.allParams.get("date", None) is not None: link = PageLink("latestResults", self) else: link = PageLink("dateResults", self, {"date": isoDate}) linkData.append(linkHtml.format(datelink=link, date=dateStr)) answer = html.format(datelinks=string.join(linkData, "\n")) return answer
def getTitle(self): report = self.getReport() if report.date is None: answer = "SEHICL Results" else: answer = "SEHICL Results: {0}".format(DateFormatter.formatDate(report.date, True, True)) return answer
def loadReport(self, rootElement): dateStr = self.allParams.get('date', None) date = None if dateStr is None else DateFormatter.parseDateFromString( dateStr) answer = ResultsReportGenerator().getDateResultsReport( rootElement, date) return answer
def getHeading(self, report): if report.date is None: answer = "Results" else: answer = "Results: {0}".format( DateFormatter.formatDate(report.date, True, True)) return answer
def getMatchLine(self, match, prevTime): html = """ <tr> <td class="time">{time}</td> <td class="court">{court}</td> <td class="teams"> <a href="{homefix.url}">{homename}</a> v <a href="{awayfix.url}">{awayname}</a> </td> {leaguefix} </tr> """ mTime = DateFormatter.formatTime(match.time) if prevTime != match.time else "" mCourt = match.court mHomeFixLink = PageLink("teamFixtures", self, {"team": match.homeTeamId}, True) mHomeName = match.homeTeamName mAwayFixLink = PageLink("teamFixtures", self, {"team": match.awayTeamId}, True) mAwayName = match.awayTeamName mLeagueFix = "" if match.leagueId is not None: lfHtml = """ <td> <a href="{leaguefix.url}">{leaguename}</a> </td> """ lFixLink = PageLink("leagueFixtures", self, {"league": match.leagueId}, True) mLeagueFix = lfHtml.format(leaguefix=lFixLink, leaguename=match.leagueName) answer = html.format(time=mTime, court=mCourt, homefix = mHomeFixLink, homename=mHomeName, awayfix=mAwayFixLink, awayname=mAwayName, leaguefix=mLeagueFix) return answer;
def getStatusMessage(self, table): html = """ <p class="statusMessage">{status}.</p> """ if table.lastCompleteMatchDate is None: answer = "" elif self.allParams.get("archive", "no") == "yes": answer = "" else: if table.complete: message = "Final averages" else: dateStr = DateFormatter.formatDate(table.lastCompleteMatchDate, True, True) if table.toCome == 0: template = "Includes all games up to and including {0}" message = template.format(dateStr) else: template = "Date of last game included: {0} ({1} {2} to come)" message = template.format( dateStr, table.toCome, TextUtils.getGrammaticalNumber(table.toCome, "result", "results")) answer = html.format(status=message) return answer
def getMatch(self, matchElement, teams, players, leagueName): answer = MatchInReport() dateTime = DateFormatter.parseDateTimeFromString( matchElement.find("date").text) answer.date = dateTime.date() answer.time = dateTime.time() answer.court = matchElement.find("pitch").text htElement = matchElement.find("homeTeam") homeTeamId = htElement.get("id") answer.homeTeamId = homeTeamId answer.homeTeamName = teams.get(homeTeamId) awayTeamId = matchElement.find("awayTeam").get("id") answer.awayTeamId = awayTeamId answer.awayTeamName = teams.get(awayTeamId) answer.leagueName = leagueName playedMatchElement = matchElement.find("playedMatch") if playedMatchElement is not None: answer.innings = self.getInningsList(playedMatchElement, teams, players) awardedMatchElement = matchElement.find("awardedMatch") if awardedMatchElement is not None: answer.award = self.getAwardedMatchDetails(awardedMatchElement, homeTeamId, awayTeamId, teams) return answer
def getTitle(self): report = self.getReport() if report.date is None: answer = "SEHICL Results" else: answer = "SEHICL Results: {0}".format( DateFormatter.formatDate(report.date, True, True)) return answer
def getAllMatchDatesWithCompletedMatches(self, rootElement, dateToExclude=None): answer = set() for l in rootElement.findall("league"): for m in l.findall("match"): if self.isComplete(m): date = DateFormatter.parseDateTimeFromString(m.find("date").text).date() if date != dateToExclude: answer.add(date) return answer
def getMatchLine(self, match, teamId, leagueResLink): html = """ <tr> <td class="date">{date}</td> <td class="time">{time}</td> <td class="court">{court}</td> <td class="opponent"><a href="{oppfix.url}">{oppname}</a></td> <td class="homeAway">{homeaway}</td> <td class="result">{result}</td> </tr> """ mDate = DateFormatter.formatDate(match.datetime, False, False) mTime = DateFormatter.formatTime(match.datetime) mCourt = match.court mOppFixLink = PageLink("teamFixtures", self, {"team": match.opponentId}, True) mOppName = match.opponentName mHomeAway = "H" if match.home else "A" mOppId = match.opponentId mHomeId = teamId if match.home else mOppId mAwayId = mOppId if match.home else teamId if match.result: if match.margin: res = "{0} by {1}".format(match.result, match.margin) else: res = "{0}".format(match.result) mResult = "<a href=\"{leaguefix.url}#{homeid}{awayid}\">{result}</a>".format( leaguefix=leagueResLink, teamid=teamId, homeid=mHomeId, awayid=mAwayId, result=res) else: mResult = "" answer = html.format(date=mDate, time=mTime, court=mCourt, oppfix=mOppFixLink, oppname=mOppName, homeaway=mHomeAway, result=mResult) return answer
def getResultsForDate(self, theDate, matches): html = """ <tr> <td class="date" colspan="2">{date}</td> </tr> {results} """ resList = [] for m in sorted(matches, key=attrgetter("time", "court")): resList.append(self.getMatchResult(m)) answer = html.format(date=DateFormatter.formatDate(theDate, True, True), results=string.join(resList, "\n")) return answer
def getAllMatchDatesWithCompletedMatches(self, rootElement, dateToExclude=None): answer = set() for l in rootElement.findall("league"): for m in l.findall("match"): if self.isComplete(m): date = DateFormatter.parseDateTimeFromString( m.find("date").text).date() if date != dateToExclude: answer.add(date) return answer
def getMatch(self, matchElement, teams, league): answer = MatchInReport() datetime = DateFormatter.parseDateTimeFromString(matchElement.find("date").text) answer.date = datetime.date() answer.time = datetime.time() answer.court = matchElement.find("pitch").text answer.homeTeamId = matchElement.find("homeTeam").get("id") answer.homeTeamName = teams[answer.homeTeamId] answer.awayTeamId = matchElement.find("awayTeam").get("id") answer.awayTeamName = teams[answer.awayTeamId] if league is not None: answer.leagueId = league.get("id") answer.leagueName = league.find("name").text return answer
def getResultsForDate(self, theDate, matches): html = """ <tr> <td class="date" colspan="2">{date}</td> </tr> {results} """ resList = [] for m in sorted(matches, key=attrgetter("time", "court")): resList.append(self.getMatchResult(m)) answer = html.format(date=DateFormatter.formatDate( theDate, True, True), results=string.join(resList, "\n")) return answer
def getAverages(self, rootElement, leagues, teams, players, batting=False, bowling=False): answer = AveragesReport() lastCompleteMatchDate = None lastScheduledMatchDate = None incompleteMatchesByDate = {} battingAverages = None if not batting else {} bowlingAverages = None if not bowling else {} if batting or bowling: for leagueElement in rootElement.findall("league"): leagueId = leagueElement.get("id") if leagueId in leagues.keys(): for matchElement in leagueElement.findall("match"): relevant, complete = self.updateAveragesFromMatch( matchElement, teams, players, battingAverages, bowlingAverages) if relevant: date = DateFormatter.parseDateTimeFromString( matchElement.find("date").text).date() if lastScheduledMatchDate is None or lastScheduledMatchDate < date: lastScheduledMatchDate = date if complete: if lastCompleteMatchDate is None or lastCompleteMatchDate < date: lastCompleteMatchDate = date else: count = incompleteMatchesByDate.get(date, 0) incompleteMatchesByDate[date] = count + 1 answer.lastCompleteMatchDate = lastCompleteMatchDate answer.lastScheduledMatchDate = lastScheduledMatchDate answer.complete = len(incompleteMatchesByDate) == 0 if lastCompleteMatchDate is not None: toCome = 0 for d in sorted(incompleteMatchesByDate.keys()): if d > lastCompleteMatchDate: break toCome = toCome + incompleteMatchesByDate[d] answer.toCome = toCome if batting: answer.battingAverages = battingAverages.values() if bowling: answer.bowlingAverages = bowlingAverages.values() answer.leagueName = leagues.values()[0] if len(leagues) == 1 else None answer.teamName = teams.values()[0] if len(teams) == 1 else None return answer
def loadMatchInfo(self, rootElement, homeId, awayId): answer = None if homeId is not None and awayId is not None: for league in rootElement.findall("league"): teams = self.getTeams(league, homeId, awayId) if teams is not None: for match in league.findall("match"): if homeId == match.find("homeTeam").get("id") and awayId == match.find("awayTeam").get("id"): matchDate = DateFormatter.parseDateTimeFromString(match.find("date").text) court = match.find("pitch").text leagueId = league.get("id") leagueName = league.get("name").text answer = MatchInfo(matchDate, court, leagueId, leagueName, teams) break if answer is not None: break return answer
def getMatchLines(self, theDate, matches): html = """ <tbody class="nobreak"> <tr> <td class="date" colspan="3">{date}</td> </tr> {matchLines} </tbody> """ answer = "" if len(matches) > 0: prevTime = None mLines = [] for m in sorted(matches, key=attrgetter("time", "court")): mLines.append(self.getMatchLine(m, prevTime)) prevTime = m.time dateStr = DateFormatter.formatDate(theDate, True, True) answer = html.format(date=dateStr, matchLines=string.join(mLines, "\n")) return answer
def getStatusMessage(self, table): html = """ <p class="statusMessage">{status}.</p> """ if table.lastCompleteMatchDate is None: answer = "" elif self.allParams.get("archive", "no") == "yes": answer = "" else: if table.complete: message = "Final table" else: dateStr = DateFormatter.formatDate(table.lastCompleteMatchDate, True, True) if table.toCome == 0: template = "Includes all games up to and including {0}" message = template.format(dateStr) else: template = "Date of last game included: {0} ({1} {2} to come)" message = template.format(dateStr, table.toCome, TextUtils.getGrammaticalNumber(table.toCome, "result", "results")) answer = html.format(status=message) return answer
def getMatch(self, matchElement, teams, players, leagueName): answer = MatchInReport() dateTime = DateFormatter.parseDateTimeFromString(matchElement.find("date").text) answer.date = dateTime.date() answer.time = dateTime.time() answer.court = matchElement.find("pitch").text htElement = matchElement.find("homeTeam") homeTeamId = htElement.get("id") answer.homeTeamId = homeTeamId answer.homeTeamName = teams.get(homeTeamId) awayTeamId = matchElement.find("awayTeam").get("id") answer.awayTeamId = awayTeamId answer.awayTeamName = teams.get(awayTeamId) answer.leagueName = leagueName playedMatchElement = matchElement.find("playedMatch") if playedMatchElement is not None: answer.innings = self.getInningsList(playedMatchElement, teams, players) awardedMatchElement = matchElement.find("awardedMatch") if awardedMatchElement is not None: answer.award = self.getAwardedMatchDetails(awardedMatchElement, homeTeamId, awayTeamId, teams) return answer
def getAverages(self, rootElement, leagues, teams, players, batting=False, bowling=False): answer = AveragesReport() lastCompleteMatchDate = None lastScheduledMatchDate = None incompleteMatchesByDate = {} battingAverages = None if not batting else {} bowlingAverages = None if not bowling else {} if batting or bowling: for leagueElement in rootElement.findall("league"): leagueId = leagueElement.get("id") if leagueId in leagues.keys(): for matchElement in leagueElement.findall("match"): relevant, complete = self.updateAveragesFromMatch(matchElement, teams, players, battingAverages, bowlingAverages) if relevant: date = DateFormatter.parseDateTimeFromString(matchElement.find("date").text).date() if lastScheduledMatchDate is None or lastScheduledMatchDate < date: lastScheduledMatchDate = date if complete: if lastCompleteMatchDate is None or lastCompleteMatchDate < date: lastCompleteMatchDate = date else: count = incompleteMatchesByDate.get(date, 0) incompleteMatchesByDate[date] = count + 1 answer.lastCompleteMatchDate = lastCompleteMatchDate answer.lastScheduledMatchDate = lastScheduledMatchDate answer.complete = len(incompleteMatchesByDate) == 0 if lastCompleteMatchDate is not None: toCome = 0 for d in sorted(incompleteMatchesByDate.keys()): if d > lastCompleteMatchDate: break toCome = toCome + incompleteMatchesByDate[d] answer.toCome = toCome if batting: answer.battingAverages = battingAverages.values() if bowling: answer.bowlingAverages = bowlingAverages.values() answer.leagueName = leagues.values()[0] if len(leagues) == 1 else None answer.teamName = teams.values()[0] if len(teams) == 1 else None return answer
def getLeagueTable(self, leagueElement): answer = LeagueTableInReport() answer.leagueName = leagueElement.find("name").text elem = leagueElement.find("teamsPromoted") answer.promoted = 0 if elem is None else int(elem.text) elem = leagueElement.find("teamsRelegated") answer.relegated = 0 if elem is None else int(elem.text) answer.tableRows = self.createTableRows(leagueElement) lastCompleteMatchDate = None lastScheduledMatchDate = None incompleteMatchesByDate = {} for m in leagueElement.findall("match"): complete = self.updateTableRowsFromMatch(m, answer.tableRows) date = DateFormatter.parseDateTimeFromString(m.find("date").text).date() if lastScheduledMatchDate is None or lastScheduledMatchDate < date: lastScheduledMatchDate = date if complete: if lastCompleteMatchDate is None or lastCompleteMatchDate < date: lastCompleteMatchDate = date else: incompleteMatchesByDate[date] = incompleteMatchesByDate.get(date, 0) + 1 answer.lastCompleteMatchDate = lastCompleteMatchDate answer.lastScheduledMatchDate = lastScheduledMatchDate answer.complete = len(incompleteMatchesByDate) == 0 if lastCompleteMatchDate is not None: toCome = 0 for d in sorted(incompleteMatchesByDate.keys()): if d > lastCompleteMatchDate: break toCome = toCome + incompleteMatchesByDate[d] answer.toCome = toCome self.markPromotedRelegatedChampions(answer) tableNotesElement = leagueElement.find("tableNotes") tableNotesText = None if tableNotesElement is None else tableNotesElement.text if tableNotesText is not None: tableNotesText = tableNotesText.strip() answer.notes = [s.strip() for s in string.split(tableNotesText, "\n")] return answer
def testFormatDateDatePassedMonthShortYearLong(self): date = datetime.date(2013, 12, 22) result = DateFormatter.formatDate(date, False, True) expectedResult = "22nd Dec 2013" self.assertEqual(expectedResult, result)
def isOnDate(self, matchElement, date): mDate = DateFormatter.parseDateTimeFromString(matchElement.find("date").text).date() answer = mDate == date return answer
def testFormatDateYearOmitted(self): date = datetime.datetime(2013, 7, 31, 14, 13) result = DateFormatter.formatDate(date, False, None) expectedResult = "31st Jul" self.assertEqual(expectedResult, result)
def testGetOrdinalSuffix(self): expectedResults = "st nd rd th th th th th th th th th th th th th th th th th st nd rd th th th th th th th st".split() count = len(expectedResults) self.assertEquals(31, count) results = [DateFormatter.getOrdinalSuffix(day) for day in range(1, count + 1)] self.assertEquals(expectedResults, results)
def testFormatDateToISO(self): date = datetime.datetime(2013, 4, 7, 16, 25) result = DateFormatter.formatDateToISO(date) expectedResult = "2013-04-07" self.assertEqual(expectedResult, result)
def testFormatDateDateTimePassed(self): date = datetime.datetime(2013, 7, 31, 14, 13) result = DateFormatter.formatDate(date, True, True) expectedResult = "31st July 2013" self.assertEqual(expectedResult, result)
def loadReport(self, rootElement): dateStr = self.allParams.get('date', None) date = None if dateStr is None else DateFormatter.parseDateFromString(dateStr) answer = ResultsReportGenerator().getDateResultsReport(rootElement, date) return answer
def testFormatDateDatePassedMonthLongYearShort(self): date = datetime.date(2013, 4, 12) result = DateFormatter.formatDate(date, True, False) expectedResult = "12th April 13" self.assertEqual(expectedResult, result)
def testParseDateFromString(self): theString = "2013-07-31" expectedResult = datetime.date(2013, 7, 31) result = DateFormatter.parseDateFromString(theString) self.assertEqual(expectedResult, result)
def testFormatTimeDateTimePassed(self): time = datetime.datetime(2013, 7, 26, 12, 13) result = DateFormatter.formatTime(time) expectedResult = "12:13" self.assertEqual(expectedResult, result)
def isOnDate(self, matchElement, date): mDate = DateFormatter.parseDateTimeFromString( matchElement.find("date").text).date() answer = mDate == date return answer
def getHeading(self, report): if report.date is None: answer = "Results" else: answer = "Results: {0}".format(DateFormatter.formatDate(report.date, True, True)) return answer
def testFormatDateDatePassedMonthShortYearShort(self): date = datetime.date(2013, 1, 3) result = DateFormatter.formatDate(date, False, False) expectedResult = "3rd Jan 13" self.assertEqual(expectedResult, result)
def testFormatTimeTimePassed(self): time = datetime.time(14, 13) result = DateFormatter.formatTime(time) expectedResult = "2:13" self.assertEqual(expectedResult, result)
def testFormatDateDatePassedMonthLongYearLong(self): date = datetime.date(2013, 7, 1) result = DateFormatter.formatDate(date, True, True) expectedResult = "1st July 2013" self.assertEqual(expectedResult, result)