def test_past_monday_date(self): monday_date = datetime.date(year=2013, month=3, day=4) self.assertEqual(past_monday_date(monday_date), monday_date) tuesday_date = datetime.date(year=2013, month=3, day=6) self.assertEqual(past_monday_date(tuesday_date), monday_date) sunday_date = datetime.date(year=2013, month=3, day=10) self.assertEqual(past_monday_date(sunday_date), monday_date)
def _default_inited_dict(min_date, max_date): """return OrderedDict where values are inited with []""" dct = OrderedDict() first_monday = tt.past_monday_date(min_date) if max_date is None: max_date = tt.past_monday_date( datetime.date.today()) + datetime.timedelta(days=7) last_monday = tt.past_monday_date(max_date) monday = first_monday while monday <= last_monday: year_weeknum = tt.get_year_weeknum(monday) dct[year_weeknum] = [] monday = monday + datetime.timedelta(days=7) return dct
def __init_data(self, sex, player_id, weeks_ago): result = [] monday_date = tt.past_monday_date(datetime.date.today()) start_ywn = tt.get_year_weeknum(monday_date) for ywn in tt.year_weeknum_reversed(start_ywn, weeks_ago): result.append(self.__week_results(player_id, weeked_tours.tours(sex, ywn))) return result
def initialize(sex=None, yearsnum=2): min_date = tt.past_monday_date(datetime.date.today()) - datetime.timedelta( days=int(365.25 * yearsnum)) sexes = ("wta", "atp") if sex is None else (sex, ) for sex in sexes: dct = fu.json_load_intkey_dict(dict_filename(sex)) read_players_from_db(sex, __players_from_sex[sex], min_date, dct)
def test_common(self): def write_tours( sex, fname, todaymode, min_date, max_date, with_paired, with_mix, rnd_detailing, ): tours = list( tours_generator( sex, todaymode=todaymode, min_date=min_date, max_date=max_date, with_paired=with_paired, with_mix=with_mix, rnd_detailing=rnd_detailing, )) if tours: dirname = "./tmp/debug" fu.ensure_folder(dirname) self.tours_matches_ordering(tours) tours_write_file(tours, os.path.join(dirname, fname)) self.assertTrue(len(tours) > 0) sex = "atp" min_date = tt.past_monday_date(datetime.date.today()) max_date = min_date + datetime.timedelta(days=7) write_tours( sex, "tours.txt", todaymode=False, min_date=min_date, max_date=max_date, with_paired=True, with_mix=False, rnd_detailing=True, ) write_tours( sex, "today_tours.txt", todaymode=True, min_date=min_date, max_date=max_date, with_paired=True, with_mix=False, rnd_detailing=True, )
def read_rating(self, sex, date=None, surfaces=("all", )): if not self.ident: return if date is None: rating_date = datetime.date.today() if rating_date.isoweekday() == 1: # в понед-к (по крайней мере утром) db еще не иммеет свежие рейтинги rating_date = rating_date - datetime.timedelta(days=7) else: rating_date = tt.past_monday_date(rating_date) else: rating_date = date self.rating.read(sex, self.ident, surfaces=surfaces, date=rating_date)
def initialize(prev_week=False): """must be called AFTER weeked_tours::initialize. init wta_chal_tour_surf, and add to sex_tourname_surf_map""" wta_chal_tour_surf.clear() monday = tt.past_monday_date(datetime.date.today()) if prev_week: monday = monday - datetime.timedelta(days=7) year_weeknum = tt.get_year_weeknum(monday) for tour in weeked_tours.tours("wta", year_weeknum): if tour.level == "teamworld": sex_tourname_surf_map[("wta", tour.name, None)] = tour.surface if tour.level == "chal": wta_chal_tour_surf.add((tour.name, tour.surface)) log.info("FS::init prev_week {} wta_chal {} {}".format( prev_week, tour.name, tour.surface))
def process(sex: str, min_date, max_date): from tournament import tours_generator if not dba.initialized(): dba.open_connect() if max_date.isoweekday() == 1: # for including qualification matches with match.date < max_date max_tour_date = max_date + datetime.timedelta(days=7) else: max_tour_date = max_date for tour in tours_generator(sex, min_date=min_date, max_date=max_tour_date): for match, rnd in tour.match_rnd_list(): if match.date and match.date > max_date: continue if match.date is None and tour.date >= tt.past_monday_date( max_date): continue put_match(sex, tour, rnd, match)
def initialize(): date = tt.past_monday_date(datetime.date.today()) min_date = date - datetime.timedelta(days=7 * matchstat.HIST_WEEKS_NUM) matchstat.initialize(min_date=min_date, time_reverse=True, tour_surface=True) decided_set.initialize_results(sex=None, min_date=LiveMatch.min_decset_date, max_date=datetime.date.today()) bet_coefs.initialize(sex="wta", min_date=min_date) bet_coefs.initialize(sex="atp", min_date=min_date) # for fatigue, live-matches details(rnd-details, tour.level, tour.surface): weeked_tours.initialize_sex( "wta", min_date=date - datetime.timedelta(days=7 * 55), max_date=date + datetime.timedelta(days=11), with_today=True, with_paired=True, with_ratings=True, with_bets=True, with_stat=True, rnd_detailing=True, ) weeked_tours.initialize_sex( "atp", min_date=date - datetime.timedelta(days=7 * 55), max_date=date + datetime.timedelta(days=11), with_today=True, with_paired=True, with_ratings=True, with_bets=True, with_stat=True, rnd_detailing=True, ) weeked_tours.use_tail_tours_cache = True
def fetch_initialize(sex=None, yearsnum=2): dba.open_connect() oncourt_players.initialize(yearsnum=yearsnum) now_date = datetime.datetime.now().date() cur_week_monday = tt.past_monday_date(now_date) if sex in (None, "wta"): weeked_tours.initialize_sex( "wta", min_date=cur_week_monday - datetime.timedelta(days=14), max_date=None, with_today=True, ) if sex in (None, "atp"): weeked_tours.initialize_sex( "atp", min_date=cur_week_monday - datetime.timedelta(days=14), max_date=None, with_today=True, ) fsdrv = common_wdriver.wdriver(company_name="FS") fsdrv.start() time.sleep(5) return fsdrv
def dates_range_for_query(self, match_date): """return min_date, max_date for sql query in semiopen style: min_date <= x < max_date""" if match_date is None: match_date = datetime.date.today() match_past_monday = tt.past_monday_date(match_date) match_further_monday = match_past_monday + datetime.timedelta(days=7) if self.itf: if self.qualification and match_date.isoweekday() in (6, 7): min_date = match_further_monday max_date = min_date + datetime.timedelta(days=7) else: min_date = match_past_monday max_date = match_further_monday else: if self.grand_slam: # самый широкий интервал min_date = match_past_monday - datetime.timedelta(days=14) max_date = match_further_monday + datetime.timedelta(days=14) else: # напр. 2-х недельный турнир играется (поэтому интервал чуть шире) min_date = match_past_monday - datetime.timedelta(days=7) max_date = match_further_monday + datetime.timedelta(days=7) return min_date, max_date
def split_tour_by_weeks(tour): def get_qual_match_date_shifted(tour_date, match_date, rnd): if match_date is not None or not rnd.qualification(): return None # qualifying -> sunday, q-Second -> saturday, q-First -> friday if rnd == "Qualifying": return tour_date + datetime.timedelta(days=6) elif rnd == "q-Second": return tour_date + datetime.timedelta(days=5) elif rnd == "q-First": return tour_date + datetime.timedelta(days=4) def get_mdraw_match_date(tour_date, match_date, rnd): if match_date is not None: return None # qualifying->monday, 1st->tuesday, 2nd->wednesday, 1/4->thursday, 1/2->friday if rnd == "Qualifying": return tour_date elif rnd == "First": return tour_date + datetime.timedelta(days=1) elif rnd == "Second": return tour_date + datetime.timedelta(days=2) elif rnd == "1/4": return tour_date + datetime.timedelta(days=3) elif rnd == "1/2": return tour_date + datetime.timedelta(days=4) elif rnd == "Final": return tour_date + datetime.timedelta(days=5) prevweek_matches_from_rnd = defaultdict(list) curweek_matches_from_rnd = defaultdict(list) nextweek_matches_from_rnd = defaultdict(list) curweek_date = tt.past_monday_date(tour.date) prevweek_date = curweek_date - datetime.timedelta(days=7) nextweek_date = curweek_date + datetime.timedelta(days=7) for rnd, matches in tour.matches_from_rnd.items(): for m in matches: if m.date is not None: if prevweek_date <= m.date < curweek_date: prevweek_matches_from_rnd[rnd].append(m) elif curweek_date <= m.date < nextweek_date: curweek_matches_from_rnd[rnd].append(m) elif nextweek_date <= m.date: nextweek_matches_from_rnd[rnd].append(m) else: raise co.TennisError( "match date {} is not binded with tour: {} match: {}".format( m.date, str(tour), str(m) ) ) else: week_shift = round_week_shift_by_struct(tour, rnd) if week_shift == -1: if rnd.qualification(): m.date = get_qual_match_date_shifted(prevweek_date, m.date, rnd) prevweek_matches_from_rnd[rnd].append(m) elif week_shift == 0: m.date = get_mdraw_match_date(curweek_date, m.date, rnd) curweek_matches_from_rnd[rnd].append(m) else: nextweek_matches_from_rnd[rnd].append(m) result = [] if len(prevweek_matches_from_rnd) > 0: prevweek_tour = copy.deepcopy(tour) prevweek_tour.date = prevweek_date prevweek_tour.matches_from_rnd = prevweek_matches_from_rnd result.append(prevweek_tour) if len(curweek_matches_from_rnd) > 0: # м.б. пусто если онкорт еще не прислал матчи основной сетки curweek_tour = copy.deepcopy(tour) curweek_tour.matches_from_rnd = curweek_matches_from_rnd result.append(curweek_tour) if len(nextweek_matches_from_rnd) > 0: nextweek_tour = copy.deepcopy(tour) nextweek_tour.date = nextweek_date nextweek_tour.matches_from_rnd = nextweek_matches_from_rnd result.append(nextweek_tour) return result
def is_prev_week(in_date): if args.current_week: return False cur_monday = tt.past_monday_date(datetime.date.today()) in_monday = tt.past_monday_date(in_date) return in_monday <= cur_monday
def _initialize_results_sex(sex, max_rating, max_rating_dif, min_date=None, max_date=None): sql = """select tours.DATE_T, tours.NAME_T, tours.RANK_T, tours.PRIZE_T, games.ID_R_G, games.RESULT_G, games.ID1_G, games.ID2_G from Tours_{0} AS tours, games_{0} AS games, Players_{0} AS fst_plr where games.ID_T_G = tours.ID_T and games.ID1_G = fst_plr.ID_P and (tours.NAME_T Not Like '%juniors%') and (fst_plr.NAME_P Not Like '%/%') """.format(sex) sql += dba.sql_dates_condition(min_date, max_date) sql += " order by tours.DATE_T;" with closing(dba.get_connect().cursor()) as cursor: for ( tour_dt, tour_name, db_rank, db_money, rnd_id, score_txt, fst_id, snd_id, ) in cursor.execute(sql): date = tour_dt.date() if tour_dt else None if date is None: raise co.TennisScoreError("none date {}".format(tour_name)) if not score_txt: continue scr = sc.Score(score_txt) if scr.retired: continue sets_count = scr.sets_count(full=True) if sets_count != 3 or scr.best_of_five(): continue set3_score = scr[2] if set3_score[0] < set3_score[1]: raise co.TennisScoreError( "right winner unexpected {}".format(scr)) money = oncourt_db.get_money(db_money) rank = None if db_rank is None else int(db_rank) if rank is None: log.error("none rank date: {} scr: {} name: {}".format( date, scr, tour_name)) if not isinstance(rank, int): raise co.TennisError( "not int rank '{}' date: {} scr: {} name: {}".format( rank, date, scr, tour_name)) rawname, level = oncourt_db.get_name_level(sex, tour_name.strip(), rank, money, date) if level in DISABLE_LEVELS: continue if level is None: raise co.TennisError( "none level date: {} scr: {} name: {}".format( date, scr, tour_name)) rnd = tennis.Round.from_oncourt_id(rnd_id) soft_level = tennis.soft_level(level, rnd) if soft_level is None: raise co.TennisError( "none soft_level date: {} scr: {} name: {}".format( date, scr, tour_name)) mdata = _get_match_data(sex, date, fst_id, snd_id, scr, max_rating, max_rating_dif) if mdata is not None: past_monday = tt.past_monday_date(date) ywn = tt.get_year_weeknum(past_monday) data_dict[(sex, soft_level)][ywn][(mdata.set1_score, mdata.set2_score)].hit( mdata.decided_win)
class SplitOntwoEnclosedTest(unittest.TestCase): def test_split_ontwo_enclosed(self): text = "ab(c)-d(ef)" p1, p2 = split_ontwo_enclosed(text, delim_op="(", delim_cl=")") self.assertTrue(p1 == "ab(c)-d" and p2 == "ef") if __name__ == "__main__": log.initialize(co.logname(__file__, test=True), file_level="info", console_level="info") dba.open_connect() oncourt_players.initialize(yearsnum=1.2) min_date = tt.past_monday_date( datetime.date.today()) - datetime.timedelta(days=7) weeked_tours.initialize_sex("wta", min_date=min_date, max_date=None, with_today=True, with_bets=True) weeked_tours.initialize_sex("atp", min_date=min_date, max_date=None, with_today=True, with_bets=True) initialize() unittest.main() dba.close_connect()
def _make(self, sex, match, completed_only): from tournament import Tournament if (not match.first_player or not match.second_player or not match.first_player.ident or not match.second_player.ident or not match.date): return tour_date_max = tt.past_monday_date(match.date) sql = """select Rounds.NAME_R, Courts.NAME_C, tours.ID_T, tours.NAME_T, tours.RANK_T, tours.DATE_T, tours.PRIZE_T, tours.COUNTRY_T, games.ID1_G, games.ID2_G, games.RESULT_G, games.DATE_G from Tours_{0} AS tours, Games_{0} AS games, Rounds, Courts where games.ID_T_G = tours.ID_T and Rounds.ID_R = games.ID_R_G and tours.ID_C_T = Courts.ID_C and tours.DATE_T < {1} and ((games.ID1_G = {2} and games.ID2_G = {3}) or (games.ID2_G = {2} and games.ID1_G = {3}) ) and (tours.NAME_T Not Like '%juniors%') ;""".format( sex, dba.msaccess_date(tour_date_max), match.first_player.ident, match.second_player.ident, ) with closing(dba.get_connect().cursor()) as cursor: for ( rnd_name, surf_name, tour_id, tour_name, tour_rank, tour_time, money, cou, p1st_id, p2nd_id, score_name, match_dtime, ) in cursor.execute(sql): score = sc.Score(score_name) if score.retired and completed_only: continue tour = Tournament( ident=tour_id, name=tour_name, sex=sex, surface=Surface(surf_name), rank=tour_rank, date=tour_time.date() if tour_time else None, money=money, cou=cou, ) if tour.level == "future": continue match_date = match_dtime.date( ) if match_dtime is not None else None if (p1st_id == match.first_player.ident and p2nd_id == match.second_player.ident): m = Match( match.first_player, match.second_player, score=score, rnd=Round(rnd_name), date=match_date, ) else: m = Match( match.second_player, match.first_player, score=score, rnd=Round(rnd_name), date=match_date, ) avgset = self._get_avgset(tour, m) self.tour_match_aset.append((tour, m, avgset)) self.tour_match_aset.sort(key=lambda i: i[0].date, reverse=True) self._remove_current_match(match.date)
parser.add_argument("--dump_tail_tours", action="store_true") return parser.parse_args() if __name__ == "__main__": args = parse_command_line_args() log.initialize(co.logname(__file__), file_level="info", console_level="info") log.info("started with company_name {}".format(args.company_name)) dba.open_connect() oncourt_players.initialize(yearsnum=1.2) ratings.initialize( sex=None, rtg_names=("std", "elo"), min_date=datetime.date.today() - datetime.timedelta(days=21), ) ratings.Rating.register_rtg_name("elo") ratings.Rating.register_rtg_name("elo_alt") mon_date = tt.past_monday_date(datetime.date.today()) initialize() live_initialize() if args.cleardir: pre_live_dir.remove_all() if args.dump_tail_tours: weeked_tours.dump_tail_tours(sex="wta", tail_weeks=2) weeked_tours.dump_tail_tours(sex="atp", tail_weeks=2) main(make_scripts()) dba.close_connect() sys.exit(0)