from bs4 import BeautifulSoup as bs
import re
from contextlib import suppress
import traceback
from dateutil import parser
import requests
import xlsxwriter
from difflib import SequenceMatcher
from datetime import datetime, date
from datetime import timedelta
from webauto_base import webauto_base
import time
import math
import sqlite3
import logging
import os
espnArray =  []
collageArray = []
spreadArray = []

def get_espn():
    try:
        print("Request to https://www.espn.com/mens-college-basketball/standings...")
        url = "https://www.espn.com/mens-college-basketball/standings"
        #Get the Page Content String from the Espn
        pageString = requests.get(url).text
        soup = bs(pageString, 'html.parser')

        sections = soup.findAll('section', {'class':'ResponsiveTable'})

        for section in sections:
            conference_name = list(section.children)[0].get_text()

            tr = section.findChildren("tr", {'class','Table__TR--sm'})

            for i in range(int(len(tr)/2)):
                team_name = tr[i].find("abbr")
                point = tr[int(len(tr)/2)+i].findChildren("span", {'class', 'stat-cell'})
                if team_name is not None:
                    espnArray.append({
                        "conference_name" : conference_name,
                        "team_name" : team_name['title'],
                        "abbr" : team_name.get_text(),
                        "c_w_l": point[0].get_text(),
                        "c_gb": point[1].get_text(),
                        "c_pct": point[2].get_text(),
                        "o_w_l": point[3].get_text(),
                        "o_pct": point[4].get_text(),
                        "o_home": point[5].get_text(),
                        "o_away": point[6].get_text(),
                        "o_strk": point[7].get_text(),
                    })
    except Exception as e:
        print(str(e))
        logging.info(str(e))
        traceback.print_exc()

def get_colleage():
    try:
        print("Request to https://www.espn.com/mens-college-basketball/bpi/_/view/overview/sort/sospastrank/dir/asc...")
        url = "https://www.espn.com/mens-college-basketball/bpi/_/view/overview/sort/sospastrank/dir/asc"
        pg = 1
        while pg < 100:
            try:
                url = "https://www.espn.com/mens-college-basketball/bpi/_/view/overview/sort/sospastrank/page/" + str(pg) + "/dir/asc"
                page = requests.get(url).text
                soup = bs(page, 'html.parser')

                no_data = soup.find('div', {'class' : 'no-data-available'})
                if no_data is not None and no_data.get_text() == "No data available.":
                    break

                trs = soup.findAll('tr')

                for tr in trs:
                    try:
                        td = tr.findChildren('td')
                        if len(td) == 8:
                            collageArray.append({
                                'team_name' : td[1].find('span',{'class','team-names'}).get_text(),
                                'abbr' : td[1].find('abbr').get_text(),
                                'conf' : '',
                                'bpi_rk' : td[4].get_text(),
                                'sos_rk' : td[5].get_text(),
                                'sor_rk' : td[6].get_text(),
                            })
                    except:
                        pass

            except Exception as exx:
                print(str(exx))
                logging.info(str(exx))
                pass
            pg = pg + 1

    except Exception as e:
        print(str(e))
        logging.info(str(e))
        traceback.print_exc()

class get_spread(webauto_base):
    def __del__(self):
        super().__init__()

    def automate(self):
        try:
            print("Automation to get spreads...")

            url = "https://www.sportsbookreview.com/betting-odds/ncaa-basketball/"
            self.start_browser(False)
            self.navigate(url)

            self.delay_me(3)

            xpath = "//div[@id='bettingOddsGridContainer']/div[3]/*"
            bettingContainer = self.browser.find_elements_by_xpath(xpath)

            # #print(day)
            xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//div[@data-vertical-sbid='time']/div//span"
            p_time = self.browser.find_elements_by_xpath(xpath)
            #print(len(p_time))

            # #print(len(p_time))

            xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section//a//span"
            teams = self.browser.find_elements_by_xpath(xpath)

            #print(len(teams))
            xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='-1']//span[@data-cy='odd-grid-opener-league']"
            opener = self.browser.find_elements_by_xpath(xpath)

            bookmarker = None
            five_times = None
            bovada = None

            count = 0
            while True:
                xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='93']//span[@data-cy='odd-grid-league']"
                bookmarker = self.browser.find_elements_by_xpath(xpath)

                xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='19']//span[@data-cy='odd-grid-league']"
                five_times = self.browser.find_elements_by_xpath(xpath)

                xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='1618']//span[@data-cy='odd-grid-league']"
                bovada = self.browser.find_elements_by_xpath(xpath)

                if len(opener) == len(bookmarker) or len(opener) == len(five_times) or len(opener) == len(bovada):
                    break

                count = count + 1
                if count > 100 and (len(bookmarker) == 0 or len(five_times) == 0 or len(bovada) == 0):
                    break

            if len(bookmarker) == 0 or len(five_times) == 0 or len(bovada) == 0:
                xpath = "//i[@class='sbr-icon-chevron-right']"
                right = self.browser.find_element_by_xpath(xpath)
                right = right.find_element_by_xpath("..")
                right.click()

                self.delay_me(3)

                if len(bookmarker) == 0:
                    xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='93']//span[@data-cy='odd-grid-league']"
                    bookmarker = self.browser.find_elements_by_xpath(xpath)

                if len(five_times) == 0:
                    xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='19']//span[@data-cy='odd-grid-league']"
                    five_times = self.browser.find_elements_by_xpath(xpath)

                if len(bovada) == 0:
                    xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='1618']//span[@data-cy='odd-grid-league']"
                    bovada = self.browser.find_elements_by_xpath(xpath)

                right.click()

                self.delay_me(3)

                if len(bookmarker) == 0:
                    xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='93']//span[@data-cy='odd-grid-league']" #93
                    bookmarker = self.browser.find_elements_by_xpath(xpath)

                if len(five_times) == 0:
                    xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='19']//span[@data-cy='odd-grid-league']"
                    five_times = self.browser.find_elements_by_xpath(xpath)

                if len(bovada) == 0:
                    xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section[@data-vertical-sbid='1618']//span[@data-cy='odd-grid-league']"
                    bovada = self.browser.find_elements_by_xpath(xpath)

            print("Processing...")
            bettingDate = None
            i = 0
            for divs in bettingContainer:
                try:
                    bettingDate = divs.find_element_by_xpath("./div/div/span")
                    if bettingDate.text != "Box Scores":
                        bettingDate = bettingDate.text
                        continue

                    if bettingDate.text == "Box Scores":
                        continue

                except:
                    pass
                try:
                    length = len(divs.find_elements_by_xpath("./div"))
                    if length >= 2 and bettingDate is not None:

                        point_A_T = ""
                        point_B_T = ""
                        diff_A = 0
                        diff_B = 0
                        if length == 3:
                            points = divs.find_elements_by_xpath("./div[2]/div/div")
                            try:
                                point_A_T = int(points[len(points)-1].text.split("\n")[0])
                                point_B_T = int(points[len(points)-1].text.split("\n")[1])
                                diff_A = point_A_T - point_B_T
                                diff_B = point_B_T - point_A_T
                            except:
                                pass

                        opener_f = "-"
                        opener_s = "-"
                        try:
                            opener_val = opener[i].find_elements_by_xpath("./span")
                            opener_f = opener_val[0].text
                            opener_s = opener_val[1].text
                        except:
                            pass

                        bookmarker_f = "-"
                        bookmarker_s = "-"
                        try:
                            bookmarker_val = bookmarker[i].find_elements_by_xpath("./span")
                            bookmarker_f = bookmarker_val[0].text
                            bookmarker_s = bookmarker_val[1].text
                        except:
                            pass

                        five_times_f = "-"
                        five_times_s = "-"
                        try:
                            five_times_val = five_times[i].find_elements_by_xpath("./span")
                            five_times_f = five_times_val[0].text
                            five_times_s = five_times_val[1].text
                        except:
                            pass

                        bovada_f = "-"
                        bovada_s = "-"
                        try:
                            bovada_val = bovada[i].find_elements_by_xpath("./span")
                            bovada_f = bovada_val[0].text
                            bovada_s = bovada_val[1].text
                        except:
                            pass

                        spreadArray.append({
                            'team' : teams[i].text,
                            'opener' : getOdds(opener_f),
                            'opener_odds': getOdds(opener_s),
                            'bookmarker' : getOdds(bookmarker_f),
                            'bookmarker_odds' : getOdds(bookmarker_s),
                            'five_times' : getOdds(five_times_f),
                            'five_times_odds' : getOdds(five_times_s),
                            'bovada' : getOdds(bovada_f),
                            'bovada_odds' : getOdds(bovada_s),
                            'date': bettingDate,
                            'point': point_A_T,
                            'diff': diff_A
                        })

                        opener_f = "-"
                        opener_s = "-"
                        try:
                            opener_val = opener[i+1].find_elements_by_xpath("./span")
                            opener_f = opener_val[0].text
                            opener_s = opener_val[1].text
                        except:
                            pass

                        bookmarker_f = "-"
                        bookmarker_s = "-"
                        try:
                            bookmarker_val = bookmarker[i+1].find_elements_by_xpath("./span")
                            bookmarker_f = bookmarker_val[0].text
                            bookmarker_s = bookmarker_val[1].text
                        except:
                            pass

                        five_times_f = "-"
                        five_times_s = "-"
                        try:
                            five_times_val = five_times[i+1].find_elements_by_xpath("./span")
                            five_times_f = five_times_val[0].text
                            five_times_s = five_times_val[1].text
                        except:
                            pass

                        bovada_f = "-"
                        bovada_s = "-"
                        try:
                            bovada_val = bovada[i+1].find_elements_by_xpath("./span")
                            bovada_f = bovada_val[0].text
                            bovada_s = bovada_val[1].text
                        except:
                            pass

                        spreadArray.append({
                            'team' : teams[i+1].text,
                            'opener' : getOdds(opener_f),
                            'opener_odds': getOdds(opener_s),
                            'bookmarker' : getOdds(bookmarker_f),
                            'bookmarker_odds' : getOdds(bookmarker_s),
                            'five_times' : getOdds(five_times_f),
                            'five_times_odds' : getOdds(five_times_s),
                            'bovada' : getOdds(bovada_f),
                            'bovada_odds' : getOdds(bovada_s),
                            'date': bettingDate,
                            'point': point_B_T,
                            'diff': diff_B
                        })
                        i = i + 2
                except:
                    pass

            print("getting point ...")
            yesterday = datetime.strftime(datetime.now() - timedelta(1), '%Y%m%d')
            url = "https://www.sportsbookreview.com/betting-odds/ncaa-basketball/?date=" + yesterday

            xpath = "/html/body/div[1]/div/div/div/section/div/div[2]/div[1]/span[2]"
            self.click_element(xpath, 3, 0)

            self.delay_me(3)

            xpath = "//div[@id='bettingOddsGridContainer']/div[3]/*"
            self.wait_present(xpath, 30)
            bettingContainer = self.browser.find_elements_by_xpath(xpath)

            xpath = "//div[@id='bettingOddsGridContainer']//div//div//div//section//a//span"
            teams = self.browser.find_elements_by_xpath(xpath)

            bettingDate = None
            i = 0
            for divs in bettingContainer:
                try:
                    bettingDate = divs.find_element_by_xpath("./div/div/span")
                    if bettingDate.text != "Box Scores":
                        bettingDate = bettingDate.text
                        #print(i)
                        continue

                    if bettingDate.text == "Box Scores":
                        continue

                except:
                    pass
                try:
                    length = len(divs.find_elements_by_xpath("./div"))
                    if length >= 2 and bettingDate is not None:

                        point_A_T = ""
                        point_B_T = ""
                        diff_A = 0
                        diff_B = 0

                        teamNameA = ""
                        teamNameB = ""
                        if length == 3:
                            points = divs.find_elements_by_xpath("./div[2]/div/div")
                            teamNameA = divs.find_element_by_xpath("./div[1]/section/div[1]/div[1]/div/a/span").text
                            teamNameB = divs.find_element_by_xpath("./div[1]/section/div[1]/div[2]/div/a/span").text
                            try:
                                point_A_T = int(points[len(points)-1].text.split("\n")[0])
                                point_B_T = int(points[len(points)-1].text.split("\n")[1])
                                diff_A = point_A_T - point_B_T
                                diff_B = point_B_T - point_A_T
                            except:
                                pass

                        if spreadArray[i]["point"] == "" and teamNameA == spreadArray[i]["team"]:
                            spreadArray[i]["point"] = point_A_T
                            spreadArray[i]["diff"] = diff_A

                        if spreadArray[i+1]["point"] == "" and teamNameB == spreadArray[i+1]["team"]:
                            spreadArray[i+1]["point"] = point_B_T
                            spreadArray[i+1]["diff"] = diff_B
                        i = i + 2
                except:
                    pass
            self.quit_browser()
        except Exception as e:
            print(str(e))
            logging.info(str(e))
            traceback.print_exc()

def findDay():
    return date.today().weekday()

def getToday():
    return datetime.today().strftime("%Y-%m-%d")

def get_conf(team_name):

    try:
        tmp = espnArray[0]
        pt = 0
        for espn in espnArray:
            if pt < SequenceMatcher(None, espn['team_name'].lower(), team_name.lower()).ratio():
                pt = SequenceMatcher(None, espn['team_name'].lower(), team_name.lower()).ratio()
                tmp = espn

        return tmp
    except:
        logging.info("Not found the get_conf of team_name")
        return None

def get_coll(team_name):
    try:
        tmp = collageArray[0]
        pt = 0
        for collage in collageArray:
            if pt < SequenceMatcher(None, collage['team_name'].lower(), team_name.lower()).ratio():
                pt = SequenceMatcher(None, collage['team_name'].lower(), team_name.lower()).ratio()
                tmp = collage

        return tmp
    except:
        logging.info("Not found the coll of team_name")
        return None

def getCurrentDate():
    return datetime.today().day

def getCurrentMonth():
    return datetime.today().strftime("%b")

def getOdds(odds):
    try:
        odds = odds.replace('½','.5')
        if odds == 'PK':
            odds = '0'
        elif odds == '-':
            odds = '0'

        return odds

    except:
        return '0'

def make_data():
    print("Creating...")
    try:
        # insert the information into database

        today = getToday()

        conn = sqlite3.connect('my.db')
        c = conn.cursor()
        c.execute("delete from spread where update_time='{}'".format(today))
        conn.commit()

        index = 1
        flag = False

        todayData = []
        for spread in spreadArray:
            first_A = get_conf(spread['team'])
            first_B = get_coll(spread['team'])

            away_home = ""
            result = 0
            away_30 = ""
            home_70 = ""
            sharp = ""
            wager = ""
            p_d = ""
            if spread['diff'] > 0:
                p_d = spread['diff']

            if index % 2 == 1:
                away_home = first_A["o_away"]

                try:
                    array = first_A["o_away"].split('-')
                    result = round(int(array[0])/(int(array[0])+int(array[1])) * 100)
                except:
                    result = 0
                    pass

                if result <= 30:
                    flag = True
                else:
                    flag = False
            else:
                away_home = first_A["o_home"]

                result = 0
                try:
                    array = first_A["o_home"].split('-')
                    result = round(int(array[0])/(int(array[0])+int(array[1])) * 100)
                except:
                    result = 0
                    pass

                if result >= 70:
                    home_70 = 1
                else:
                    home_70 = 0

                try:
                    sharp = float(spread['bovada']) - float(spread['bookmarker'])
                except:
                    sharp = 0

                wager = "no"
                if flag == True:
                    away_30 = 1

                    if result >= 70 and sharp > 0:
                        wager = "yes"
                else:
                    away_30 = 0

            todayData.append(list({
                'date': spread['date'],
                'team': spread['team'],
                'conf': first_A["conference_name"],
                'spread': spread['opener'],
                'spread_odd': spread['opener_odds'],
                'bookmaker': spread['bookmarker'],
                'bootmaker_odd': spread['bookmarker_odds'],
                'fivetime': spread['five_times'],
                'fivetime_odd': spread['five_times_odds'],
                'bovada': spread['bovada'],
                'bovada_odd': spread['bovada_odds'],
                'away_home': away_home,
                'percentage': str(result) + "%",
                'w_l': first_A["o_pct"],
                'strk': first_A["o_strk"],
                'bpi_rank': first_B['bpi_rk'],
                'sos_rank': first_B['sos_rk'],
                'sor_rank': first_B['sor_rk'],
                'score': spread['point'],
                'p_d': p_d,
                'away_30': str(away_30),
                'home_70': str(home_70),
                'sharp': str(sharp),
                'wager': wager,
                'update_time': today
            }.values()))

            index = index + 1

        c.executemany("insert into spread('date','team','conf','spread','spread_odd','bookmaker','bootmaker_odd','fivetime','fivetime_odd','bovada','bovada_odd','away_home','percentage','w_l','strk','bpi_rank','sos_rank','sor_rank','score','p_d','away_30','home_70','sharp','wager','update_time') values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", todayData)
        conn.commit()
        conn.close()
    except Exception as e:
        logging.info(str(e))
        pass

def make_spread():
    print("Making...")
    try:
        today = datetime.now()

        dayNumber = findDay()
        filename = "result_" + today.strftime("%Y%m%d%H%M%S") + ".xlsx"

        workbook = xlsxwriter.Workbook(filename)
        worksheet = workbook.add_worksheet('NCAA Teams')

        bold = workbook.add_format({'bold': True})
        worksheet.write(0, 0, "Team", bold)
        worksheet.write(0, 1, "Conference", bold)
        worksheet.write(0, 2, "W-L (Conference)", bold)
        worksheet.write(0, 3, "GB", bold)
        worksheet.write(0, 4, "PCT", bold)
        worksheet.write(0, 5, "W-L (Overall)", bold)
        worksheet.write(0, 6, "PCT", bold)
        worksheet.write(0, 7, "Home", bold)
        worksheet.write(0, 8, "PCT", bold)
        worksheet.write(0, 9, "Away", bold)
        worksheet.write(0, 10, "PCT", bold)
        worksheet.write(0, 11, "STRK", bold)

        index = 1
        for espn in espnArray:
            worksheet.write(index, 0, espn['team_name'])
            worksheet.write(index, 1, espn['conference_name'])
            worksheet.write(index, 2, espn['c_w_l'])
            worksheet.write(index, 3, espn['c_gb'])
            worksheet.write(index, 4, espn['c_pct'])
            worksheet.write(index, 5, espn['o_w_l'])
            worksheet.write(index, 6, espn['o_pct'])
            worksheet.write(index, 7, espn['o_home'])
            worksheet.write(index, 9, espn['o_away'])
            worksheet.write(index, 11, espn['o_strk'])

            result = 0
            try:
                array = espn["o_home"].split('-')
                result = round(int(array[0])/(int(array[0])+int(array[1])) * 100)
            except:
                result = 0
                pass
            worksheet.write(index, 8, str(result) + "%")

            result = 0
            try:
                array = espn["o_away"].split('-')
                result = round(int(array[0])/(int(array[0])+int(array[1])) * 100)
            except:
                result = 0
                pass
            worksheet.write(index, 10, str(result) + "%")

            index = index + 1

        conn = sqlite3.connect('my.db')
        c = conn.cursor()

        weekday = findDay()

        bold = workbook.add_format({'bold': True})
        green_format1 = workbook.add_format()
        green_format1.set_pattern(1)  # This is optional when using a solid fill.
        green_format1.set_bg_color('green')

        tomato_format2 = workbook.add_format()
        tomato_format2.set_pattern(1)  # This is optional when using a solid fill.
        tomato_format2.set_bg_color('#ff6347')

        magenta_format = workbook.add_format()
        magenta_format.set_pattern(1)  # This is optional when using a solid fill.
        magenta_format.set_bg_color('magenta')

        for i in range(weekday+1):

            findex = weekday -i
            day = datetime.strftime(datetime.now() - timedelta(findex), '%Y-%m-%d')
            date = datetime.strftime(datetime.now() - timedelta(findex), '%d')
            month = datetime.strftime(datetime.now() - timedelta(findex), '%b')
            worksheet = workbook.add_worksheet(month + '.' + date)

            c.execute("select * from spread where update_time='" + day + "'")
            data = c.fetchall()
            if data is not None:

                worksheet.write(0, 0, "Date", bold)
                worksheet.write(0, 1, "Team", bold)
                worksheet.write(0, 2, "Conf", bold)
                worksheet.write(0, 3, "Spread (Opener)", bold)
                worksheet.write(0, 4, "Odds", bold)
                worksheet.write(0, 5, "Spread (BookMaker)", bold)
                worksheet.write(0, 6, "Odds", bold)
                worksheet.write(0, 7, "Spread (5 Dimes)", bold)
                worksheet.write(0, 8, "Odds", bold)
                worksheet.write(0, 9, "Spread (Bovada)", bold)
                worksheet.write(0, 10, "Odds", bold)
                worksheet.write(0, 11, "Away/Home Overall Record", bold)
                worksheet.write(0, 12, "Percentage", bold)
                worksheet.write(0, 13, "W-L (Overall) PCT", bold)
                worksheet.write(0, 14, "STRK", bold)
                worksheet.write(0, 15, "BPI Rank", bold)
                worksheet.write(0, 16, "SOS Rank", bold)
                worksheet.write(0, 17, "SOR Rank", bold)
                worksheet.write(0, 18, "Score", bold)
                worksheet.write(0, 19, "P.D", bold)
                worksheet.write(0, 20, "Away 30% below", bold)
                worksheet.write(0, 21, "Home 70% above", bold)
                worksheet.write(0, 22, "Sharp-Square", bold)
                worksheet.write(0, 23, "Wager", bold)

                index = 1
                for row in data:
                    worksheet.write(index, 0, row[1])
                    worksheet.write(index, 1, row[2])
                    worksheet.write(index, 2, row[3])
                    worksheet.write(index, 3, row[4])
                    worksheet.write(index, 4, row[5])

                    if row[6] != row[10]:
                        worksheet.write(index, 5, row[6], magenta_format)
                        worksheet.write(index, 9, row[10], magenta_format)
                    else:
                        worksheet.write(index, 5, row[6])
                        worksheet.write(index, 9, row[10])

                    worksheet.write(index, 6, row[7])
                    worksheet.write(index, 7, row[8])
                    worksheet.write(index, 8, row[9])
                    worksheet.write(index, 10, row[11])

                    worksheet.write_string(index, 11, row[12])

                    if index % 3 == 1:
                        if float(row[13].replace('%','')) <= 30:
                            worksheet.write(index, 12, row[13], tomato_format2)
                        else:
                            worksheet.write(index, 12, row[13])
                    else:
                        if float(row[13].replace('%','')) >= 70:
                            worksheet.write(index, 12, row[13], green_format1)
                        else:
                            worksheet.write(index, 12, row[13])

                    #worksheet.write(index, 12, row[13])
                    worksheet.write(index, 13, row[14])
                    worksheet.write(index, 14, row[15])
                    worksheet.write(index, 15, row[16])
                    worksheet.write(index, 16, row[17])
                    worksheet.write(index, 17, row[18])
                    worksheet.write(index, 18, row[19])
                    worksheet.write(index, 19, row[20])
                    worksheet.write(index, 20, row[21])
                    worksheet.write(index, 21, row[22])
                    worksheet.write(index, 22, row[23])
                    worksheet.write(index, 23, row[24])

                    index = index + 1
                    if index % 3 == 0:
                        index = index + 1

        worksheet = workbook.add_worksheet("Weekly Total")
        worksheet.write(0, 0, "Date", bold)
        worksheet.write(0, 1, "Matchcup", bold)
        worksheet.write(0, 2, "Result", bold)
        worksheet.write(0, 3, "P.D", bold)
        worksheet.write(0, 4, "Bookmaker", bold)
        worksheet.write(0, 5, "Away Record", bold)
        worksheet.write(0, 6, "Home Record", bold)
        worksheet.write(0, 7, "Wager", bold)
        worksheet.write(0, 8, "Bookmaker-P.D", bold)
        index = 1
        for i in range(weekday+1):

            findex = weekday -i
            day = datetime.strftime(datetime.now() - timedelta(findex), '%Y-%m-%d')
            date = datetime.strftime(datetime.now() - timedelta(findex), '%d')
            month = datetime.strftime(datetime.now() - timedelta(findex), '%b')

            c.execute("select * from spread where update_time='" + day + "'")
            data = c.fetchall()
            if data is not None:

                for tindex in range(int(len(data)/2)):
                    row = data[tindex*2]
                    nrow = data[tindex*2 + 1]
                    if nrow[24] == "yes":
                        away = "0"
                        home = "0"
                        naway = "0"
                        nhome = "0"
                        try:
                            h_a = row[12].split("-")
                            away = h_a[0]
                            home = h_a[1]
                        except:
                            pass

                        try:
                            nh_a = nrow[12].split("-")
                            naway = nh_a[0]
                            nhome = nh_a[1]
                        except:
                            pass

                        worksheet.write(index, 0, row[1])   #Date
                        worksheet.write(index, 1, row[2])   #Matchcup
                        worksheet.write(index, 2, row[19])   #Result
                        worksheet.write(index, 3, row[20])   #P.D
                        worksheet.write(index, 4, row[6])   #Bookmaker
                        worksheet.write(index, 5, away)   #Away Record
                        worksheet.write(index, 6, home)   #Home Record
                        worksheet.write(index, 7, row[24])   #Wager
                        worksheet.write(index, 8, row[23])   #Bookmaker-P.D

                        worksheet.write(index+1, 0, nrow[1])   #Date
                        worksheet.write(index+1, 1, nrow[2])   #Matchcup
                        worksheet.write(index+1, 2, nrow[19])   #Result
                        worksheet.write(index+1, 3, nrow[20])   #P.D
                        worksheet.write(index+1, 4, nrow[6])   #Bookmaker
                        worksheet.write(index+1, 5, naway)   #Away Record
                        worksheet.write(index+1, 6, nhome)   #Home Record
                        worksheet.write(index+1, 7, nrow[24])   #Wager
                        worksheet.write(index+1, 8, nrow[23])   #Bookmaker-P.D

                        index = index + 2
                        if index % 3 == 0:
                            index = index + 1
        conn.close()
        workbook.close()
    except Exception as e:
        logging.info(str(e))
        pass

def main():
    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)
    filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ncaa.log')
    logging.basicConfig(filename=filename,level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',)
    logging.info("Start...")
    logging.info("Espn...")
    get_espn()

    logging.info("colleage...")
    get_colleage()

    logging.info("spread...")
    spread = get_spread()
    spread.automate()

    logging.info("making...")
    make_data()

    logging.info("creating...")
    make_spread()

    logging.info("End...")

main()


import pickle
import re
import itertools
import sys

thismodule = sys.modules[__name__]
cmu = {}
cmurev = {}
normaltrans = []
revtrans = []
untransre = None
normtransre = re.compile(r'(m|n|ŋ|p|t|k|b|d|ɡ|f|θ|s|ʃ|x|h|v|ð|z|ʒ|l|ɹ|j|w|ɪ|i|ʊ|u|e|ə|ɜ|ɔ|a|ɛ|æ|ʌ|ɒ|ɑ)')

with open("data/dictionary/compileddict.txt", 'rb') as f:
    cmu = pickle.load(f)
with open("data/dictionary/compileddictrev.txt", 'rb') as f:
    cmurev = pickle.load(f)

with open("data/dictionary/compiledipa.txt", 'rb') as f:
    normaltrans = pickle.load(f)
with open("data/dictionary/compiledeng.txt", 'rb') as f:
    revtrans = pickle.load(f)
    untransre = re.compile('|'.join(k for (k,v) in revtrans))
    revtrans = dict(revtrans)

with open("data/dictionary/kueler.txt", 'r') as f:
    kueler = dict((k, re.findall('/(.*?)/', v)) for (k, v) in list(itertools.zip_longest(*[iter([a.strip() for a in f.readlines()])] * 2, fillvalue="")))
    kuelerrev = dict((v,k) for k in kueler for v in kueler[k])

mods = ['', 'Y', 'O', 'OY']
def translate(sentence, f, t):
    return getattr(thismodule, f+t)(sentence)

def englishipa(sentence):
    def englishipaword(word):
        try:
            ud = cmu[word.upper()][0]
        except KeyError:
            ud = re.sub(r'(\w)\1+', r'\1\1', word).lower()
            for a in normaltrans:
                try:
                    ud = re.sub(a[0], a[1]+r'\1', ud)
                except Exception:
                    ud = re.sub(a[0], a[1], ud)
        return ud
    transsent = re.sub(r'[\w\']+', lambda x:englishipaword(x.group(0)), sentence)
    return transsent

def ipaenglish(sentence):
    def ipawordenglish(word):
        try:
            ud = cmurev[word]
        except KeyError:
            ud = untransre.sub(lambda x:revtrans[x.group(0)], word).upper()
        return ud
    transsent = re.sub(normtransre.pattern+r'+', lambda x:ipawordenglish(x.group(0)), sentence)
    return transsent

def kueleripa(sentence):
    return re.sub(r'[a-zA-Z](?:h|H)?', lambda x:(lambda k:(kueler[k][0] if k.isupper() else kueler[k.upper()][1]))(x.group(0)), sentence)

def ipakueler(sentence):
    return normtransre.sub(lambda x:(lambda c:kuelerrev[c].lower() if kueler[kuelerrev[c]].index(c)==1 else kuelerrev[c])(x.group(0)), sentence)

def kuelerenglish(sentence):
    return ipaenglish(kueleripa(sentence))

def englishkueler(sentence):
    return ipakueler(englishipa(sentence))


import json
import random
import time
import os

point = 0

def main():
    init()
    json_file = read()
    length = len(json_file["question"])
    global point

    #初期処理
    question_random = rand(0,length-1) #問題と正解を選ぶ
    print(str(json_file["question"][question_random][0])+"のポート番号は？")

    #選択肢の生成
    choice_list = []
    choice_list.append(json_file["question"][question_random])
    while len(choice_list) != 4:
        tmp = json_file["question"][rand(0,length-1)]
        if tmp not in choice_list:
            choice_list.append(tmp)
    random.shuffle(choice_list)
    for i in range(1,5):
        print(str(i) + ". " + str(choice_list[i-1][1]),end="     ")
    print()

    #入力受付
    answer_input = int(input("あなたの解答："))
    if answer_input < 0 or 4 < answer_input:
        print("不正な入力です。")
        fin()

    while answer_input != 0:
        gap()
        if choice_list[answer_input-1][1] == json_file["question"][question_random][1]:
            print("正解！！")
            point += 1
        else:
            print("不正解…………")
            print(str(json_file["question"][question_random][0])+"のポート番号は"+str(json_file["question"][question_random][1])+"です。")

        print()
        time.sleep(0.5)
        print("次へ行くにはエンターを押して下さい。")
        tmp2 = input()
        if tmp2 != "":
            print("不正な入力です。")
            fin()
        os.system("clear")

        #初期処理
        question_random = rand(0,length-1) #問題と正解を選ぶ
        print(str(json_file["question"][question_random][0])+"のポート番号は？")

        #選択肢の生成
        choice_list = []
        choice_list.append(json_file["question"][question_random])
        while len(choice_list) != 4:
            tmp = json_file["question"][rand(0,length-1)]
            if tmp not in choice_list:
                choice_list.append(tmp)
        random.shuffle(choice_list)
        for i in range(1,5):
            print(str(i) + ". " + str(choice_list[i-1][1]),end="     ")
        print()

        #入力受付
        answer_input = int(input("あなたの解答："))
        if answer_input < 0 or 4 < answer_input:
            print("不正な入力です。")
            fin()

    fin()

def init():
    os.system("clear")
    print("ウェルノウンポートクイズを始めるよ")
    print("半角数字1～4で解答して下さい。0を入力すると終了します。")
    time.sleep(0.5)
    print()
    return

def read():
    with open("data.json") as f:
        return json.load(f)

def rand(x,y):
    return random.randrange(x,y)

def gap():
    print()
    s = ""
    for i in range(8):
        s += "**"
        time.sleep(0.2)
        print("\r"+s,end="")
    print("\n")

def fin():
    global point
    time.sleep(0.5)
    print("終了します。")
    print("あなたの正解数は"+str(point)+"回でした！")
    exit()

if __name__ == "__main__":
    main()


import pygame
import time
import random
from metodos import endGame, initGame

# inicia el pygame para el juego
pygame.init()
sizeScreen = (470, 600)
screen = pygame.display.set_mode(sizeScreen)
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)  # Fuente para el texto
font2 = pygame.font.Font(None, 16)  # Fuente para el sub texto

# assets

# imgs
background = pygame.image.load("assets/calle.png")

moto = pygame.image.load("assets/moto.png")
motoScaled = pygame.transform.scale(moto, (20, 65))

carBlue = pygame.image.load("assets/carBlue.png")
carGreen = pygame.image.load("assets/carGreen.png")
carRed = pygame.image.load("assets/carRed.png")
carRose = pygame.image.load("assets/carRose.png")
carYellow = pygame.image.load("assets/carYellow.png")

piston = pygame.image.load("assets/piston.png")
pistonScaled = pygame.transform.scale(piston, (40, 90))
tyre = pygame.image.load("assets/tyre.png")
tyreScaled = pygame.transform.scale(tyre, (60, 60))

# sounds
breakSound = pygame.mixer.Sound('assets/audio/break.wav')
runSound = pygame.mixer.Sound('assets/audio/runmoto.wav')
pistonSound = pygame.mixer.Sound('assets/audio/piston.wav')
pistonMommentSound = pygame.mixer.Sound('assets/audio/pistonMoment.wav')
crashSound = pygame.mixer.Sound('assets/audio/crash.wav')

# general variables
backgroundPosition = 0
fps = 60
xMoto = 180
yMoto = 480
velocidadMovXMoto = 0
velocidadMovYMoto = 0
initialTime = pygame.time.get_ticks()
currentSeconds = 0
secondsToSubtract = 0
velocityGame = 2
powers = [pistonScaled]
play = True
# Lista de posiciones de los Elementos(carros, llantas y pistones)
elementsPositions = []
powerActive = False #Para saber cuando el poder esta activo
timePowerActive = 0 #para contabilizar el tiempo que el poder esta activo

initGame(screen) # Primera pantalla para el juego

# Función para agregar un nuevo elemento a la lista
def addElements():
    lane = random.randint(0, 1)  # Selección aleatoria de un carril
    xPos = 170 + lane * 60  # Posición X según el carril seleccionado
    yPos = -100 * (len(elementsPositions) * 3)   # Empieza arriba de la pantalla la operación funciona para poner más separados los elementos en la pantalla
    selectedElement = selectElements(random.randint(0, 6)) # Selección aleatoria de un elemento

    # Para centrar el piston el carril
    if selectedElement == pistonScaled:
        xPos += 15

    elementsPositions.append((xPos, yPos, selectedElement))  # Agregar el elemento a la lista

# Función para mover los carros
def updateElements():
    for i, (x, y, element) in enumerate(elementsPositions):
        y += velocityGame  # Velocidad de movimiento del elemento
        elementsPositions[i] = (x, y, element)  # Actualizar posición del elemento

        # Eliminar elementos que hayan salido de la pantalla
        if y > 600:
            elementsPositions.pop(i)

# este metodo controla el tiempo o velovdad del juego
def addVelocityGame():
    global velocityGame
    if currentSeconds % 10 == 0 and currentSeconds != 0:
        velocityGame += 0.01

# función para retornar un elemento según la posición
def selectElements(position):
    if position == 0:
        return carBlue
    elif position == 1:
        return carGreen
    elif position == 2:
        return carRed
    elif position == 3:
        return carRose
    elif position == 4:
        return carYellow
    if position == 5:
        return pistonScaled
    elif position == 6:
        return tyreScaled

# Bucle principal del juego
runSound.play(-1)
while play:
    addVelocityGame()
    # Calcular el tiempo transcurrido
    currentSeconds = pygame.time.get_ticks() - initialTime

    # Formatear el tiempo en segundos
    currentSeconds = currentSeconds // 1000

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        # captura el evento de clickar una tecla
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                velocidadMovXMoto += -(velocityGame + 1)
            if event.key == pygame.K_RIGHT:
                velocidadMovXMoto += (velocityGame + 1)
            if event.key == pygame.K_UP:
                velocidadMovYMoto = -(velocityGame + 1)
            if event.key == pygame.K_DOWN:
                velocidadMovYMoto = (velocityGame + 1)
            if event.key == pygame.K_SPACE:
                if not powerActive:
                    if len(powers) > 0:
                        powers.pop(0)
                        timePowerActive = currentSeconds
                        powerActive = True
        # captura el evento de soltar una tecla
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                velocidadMovXMoto = 0
            if event.key == pygame.K_RIGHT:
                velocidadMovXMoto = 0
            if event.key == pygame.K_UP:
                velocidadMovYMoto = 0
            if event.key == pygame.K_DOWN:
                velocidadMovYMoto = 0

    backgroundPosition += velocityGame  # Velocidad de desplazamiento

    # Actualización de la posición de la moto con reglas para no salirse de la carretera
    xMoto += velocidadMovXMoto
    if xMoto <= 179 or xMoto >= 270:
        xMoto -= velocidadMovXMoto
        velocidadMovXMoto = 0

    yMoto += velocidadMovYMoto
    if yMoto >= 480 or yMoto <= 2:
        yMoto -= velocidadMovYMoto
        velocidadMovYMoto = 0

    # Dibujar fondo
    screen.blit(background, (0, backgroundPosition))
    screen.blit(background, (0, backgroundPosition - sizeScreen[1])) #este es para que se vea el fondo cuando se vaya acabando el primero

    # Dibujar elementos y controla colisiones
    for (x, y, element) in elementsPositions:
        elementDrawed = screen.blit(element, (x, y))
        # verificar colisiones con la moto, agrega sonidos y poderes
        if motoDrawed.colliderect(elementDrawed):
            if element == carBlue or element == carGreen or element == carRed or element == carRose or element == carYellow:
                runSound.stop()
                crashSound.play()
                play = False #para finalizar el juego
            elif element == pistonScaled:
                powers.append(element)
                pistonSound.play()
                elementsPositions.remove((x, y, element))
            elif element == tyreScaled:
                secondsToSubtract += 5
                breakSound.play()
                elementsPositions.remove((x, y, element))

    # Crea los textos para luego mostrarlos
    textTime = font.render("Time: " + str(currentSeconds) + " sec", True, (0, 0, 0))
    textTime2 = font.render("To subtract: " + str(secondsToSubtract) + " sec", True, (0, 0, 0))
    textTime3 = font.render("Power:" + str(len(powers)), True, (0, 0, 0))
    textTime4 = font2.render("press space for power", True, (0, 0, 0))
    textTime5 = font2.render("Power Active: " + str(powerActive), True, (0, 0, 0))

    # Mostrar el texto en la pantalla
    screen.blit(textTime, (10, 10))
    screen.blit(textTime2, (10, 40))
    screen.blit(textTime3, (10, 70))
    screen.blit(textTime4, (10, 100))
    screen.blit(textTime5, (10, 120))

    # muestra moto
    motoDrawed = screen.blit(motoScaled, (xMoto, yMoto))

# control del poder activado, si se encuentra activo vaciamos la lista de elementos, reproducimos sonido y lo desactivamos
    if not powerActive:
        # Generar nuevos carros aleatoriamente
        if random.random() < 0.05:  # Probabilidad de generar un nuevo carro en cada iteración
            addElements()   #agrega elementos al juego

        # Actualizar posición de los elementos
        updateElements()
    else:
        elementsPositions = []
        pistonMommentSound.play()
        if currentSeconds - timePowerActive >= 10:
            timePowerActive = 0 #reiniciamos el tiempo
            powerActive = False #apagamos el poder

    #reiniciamos posicion del fondo
    if backgroundPosition >= sizeScreen[1]:
        backgroundPosition = 0

    pygame.display.flip()
    clock.tick(fps)

# metodo para finalizar el juego
endGame(currentSeconds - secondsToSubtract)


import pandas as pd
import re
import nltk
from nltk.stem import PorterStemmer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk.corpus import stopwords
from pickle import load

import os
current_directory = os.getcwd()
print(current_directory)

df = pd.read_csv('data/dataset/suspicious-tweets.csv')
model_to_load_path = 'data/modelSave/nb_model.sav'

def load_model(path):
    model = load(open(path, 'rb'))
    return model

def preprocess_text(sen):
    sentence = re.sub('(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)', ' ', sen)  # Removing html tags
    sentence = re.sub('[^a-zA-Z]', ' ', sentence)  # Remove punctuations and numbers
    sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)  # Single character removal
    sentence = re.sub(r'\s+', ' ', sentence)  # Removing multiple spaces
    sentence = sentence.replace("ain't", "am not").replace("aren't", "are not")
    sentence = ' '.join(text.lower() for text in sentence.split(' '))  # Lowering cases
    sw = stopwords.words('english')
    sentence = ' '.join(text for text in sentence.split() if text not in sw)  # removing stopwords
    return sentence

stemmer = PorterStemmer()
count_vect = CountVectorizer()

df['message'] = df.message.apply(preprocess_text)
df['message'] = df['message'].apply(nltk.word_tokenize)
df['message'] = df['message'].apply(lambda x: [stemmer.stem(y) for y in x])
df['message'] = df['message'].apply(lambda x: ' '.join(x))

counts = count_vect.fit_transform(df['message'])
transformer = TfidfTransformer().fit(counts)
counts = transformer.fit_transform(counts)
print("End modelPrediction computation")

def prediction(stringa):
    model = load_model(model_to_load_path)
    stringa = preprocess_text(stringa)
    stringa = nltk.word_tokenize(stringa)
    stringa = ' '.join(stringa)
    stringa = count_vect.transform([stringa])
    stringa = transformer.transform(stringa)
    prediction = model.predict(stringa)
    return prediction

#MAKE PREDICTION
print(prediction("I am going to kill"))

print(prediction("Negro Under Sentence To Hang At Tallahassee."))
print(prediction("Unbelievable murder by an African community in New York"))
print(prediction("LGBQT rights parade attacked by white supremacists"))
print(prediction("Governor Brough Fired Upon By Negroes At Elaine"))
print(prediction("In a Turbulent Time for China, Xi Projects Unity at an Ex-Leader’s Funeral"))
print(prediction("Drone Attacks Hit Russia for 2nd Straight Day"))
print(prediction("pain Is Locked in a Stalemate With Morocco in the Second Half"))
print(prediction("Inside the Saudi Strategy to Keep the World Hooked on Oil"))
print(prediction("Biden Administration Expands Protections for Haitian Migrants"))
print(prediction("vegetarian mushroom shawarma pitas"))
print(prediction("Lebanese Stuffed Eggplant"))
print(prediction("Rosquillas maize biscuits from Nicaragua"))
print(prediction("Typical Polish dish - Zurek"))
print(prediction("Is This Elephant Bothering You?"))


# imports
from flask import Flask
from flask_restful import Api
from resources.items import Items
import temp_db

# initialization

app = Flask(__name__)
app.config["PROPOGATE_EXCEPTIONS"] = True
api = Api(app)

# endpoints
api.add_resource(Items, "/items")

# database initialization
temp_db.create_table()
if not temp_db.check_records():
    temp_db.insert_into_table()

temp_db.show_all()

# execution
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)


from picamera import PiCamera
from os import system
from time import sleep
import sys
import argparse

parser = argparse.ArgumentParser(description='Este programa permite tomar timelapses con una rPI Cam')
parser.add_argument('-tt','--totaltime', help='Tiempo total del timelapse en segundos', required=False, type=int)
parser.add_argument('-l','--lapse',help='Tiempo entre fotos en segndos', required=True, type=int)
parser.add_argument('-c', '--cantidad', help='Cantidad de imagenes', required=True, type=int)

args = parser.parse_args()

camera = PiCamera()
camera.resolution = (1024,768)
camera.vflip = True
camera.hflip = True

totaltime = args.totaltime
lapse = args.lapse
cantidad = args.cantidad

for i in range(cantidad):
    name = 'image{0:04d}.jpg'.format(i)
    camera.capture(name)
    print('photo %s taken'%(name))
    sleep(lapse)

print('done')


import codecs
import json
from Netflix import netflix
from JLPT import jlpt
from WaniKani import wanikani
from Dialect import dialect

frequencyFileNumber = int(input("""
After execution, there will be a text file generated with the corresponding name of the frequency file(s)
with all the words that weren't in your known words list.

Which frequency file would you like to use to compare with your known words?
1. Narou
2. Netflix
3. Novel 5k
4. Slice of Life
5. Shonen
6. Visual Novel
7. JLPT
8. Wanikani
9. Dialect
"""))

stars = 0
level = 0
if frequencyFileNumber < 7:
    stars = int(input("Up to how many stars do you want to analyze? (e.g. \"4\") "))
elif frequencyFileNumber == 7 or frequencyFileNumber == 8:
    level = int(input("Up to which level do you want to analyze? (e.g. \"3\") "))
elif frequencyFileNumber == 9:
    pass
else:
    raise Exception("Choose a better number for the stars or levels!")

if frequencyFileNumber == 1:
    fileNameFrequency = "Narou.json"
elif frequencyFileNumber == 2:
    fileNameFrequency = "Netflix.json"
elif frequencyFileNumber == 3:
    fileNameFrequency = "Novel 5k.json"
elif frequencyFileNumber == 4:
    fileNameFrequency = "Slice of Life.json"
elif frequencyFileNumber == 5:
    fileNameFrequency = "Shonen.json"
elif frequencyFileNumber == 6:
    fileNameFrequency = "Visual Novel.json"
elif frequencyFileNumber == 7:
    fileNameFrequency = "JLPT.json"
elif frequencyFileNumber == 8:
    fileNameFrequency = "Wanikani.json"
elif frequencyFileNumber == 9:
    fileNameFrequency = "Dialect.json"
else:
    raise Exception("Choose an actual number that is displayed in the help menu!")

fileNameWords = "Words.json"

dataFrequency = json.load(codecs.open(fileNameFrequency, 'r', 'utf-8-sig'))

with open(fileNameWords, 'r', encoding='UTF-8') as file:
    dataWords = json.load(file)

if frequencyFileNumber < 7:
    netflix(dataWords, dataFrequency, stars)
if frequencyFileNumber == 7:
    jlpt(dataWords, dataFrequency, level)
if frequencyFileNumber == 8:
    wanikani(dataWords, dataFrequency, level)
if frequencyFileNumber == 9:
    dialect(dataWords, dataFrequency)


"""
This module intends to be a delegate for writing emails.

Its clients should only instantiate their writer of choice &
provide it with some basic data to include in the email.
They then get an email that's ready-to-send via taskcluster.Notify service.
"""

from dataclasses import dataclass, asdict
from abc import ABC, abstractmethod

from typing import List, Tuple, Union

from django.conf import settings
from treeherder.model.models import Push
from treeherder.perf.models import BackfillRecord, PerformanceSignature

FXPERF_TEST_ENG_EMAIL = "perftest-alerts@mozilla.com"  # team' s email

@dataclass
class Email:
    address: str = None
    content: str = None
    subject: str = None

    def as_payload(self) -> dict:
        return asdict(self)

class EmailWriter(ABC):
    def __init__(self):
        self._email = Email()

    def prepare_new_email(self, must_mention: Union[List[object], object]) -> dict:
        """
        Template method
        """
        must_mention = self.__ensure_its_list(must_mention)

        self._write_address()
        self._write_subject()
        self._write_content(must_mention)
        return self.email

    @property
    def email(self):
        return self._email.as_payload()

    @abstractmethod
    def _write_address(self):
        pass  # pragma: no cover

    @abstractmethod
    def _write_subject(self):
        pass  # pragma: no cover

    @abstractmethod
    def _write_content(self, must_mention: List[object]):
        pass  # pragma: no cover

    @staticmethod
    def __ensure_its_list(must_mention) -> List[object]:
        if not isinstance(must_mention, List):
            must_mention = [must_mention]
        return must_mention

# For automatically backfilling performance data
class BackfillReportContent:
    DESCRIPTION = """Perfherder automatically backfills performance jobs originating from Linux platforms.
     It does this every hour, as long as it doesn't exceed the daily limit.
    > __Here's a summary of latest backfills:__
    ---
        """

    TABLE_HEADERS = """
    | Alert summary | Alert | Job symbol | Total backfills (aprox.) | Push range |
    | :---: | :---: | :---: | :---: | :---: |
        """

    def __init__(self):
        self._raw_content = None

    def include_records(self, records: List[BackfillRecord]):
        self._initialize_report_intro()

        for record in records:
            self._include_in_report(record)

    def _initialize_report_intro(self):
        if self._raw_content is None:
            self._raw_content = self.DESCRIPTION + self.TABLE_HEADERS

    def _include_in_report(self, record: BackfillRecord):
        new_table_row = self._build_table_row(record)
        self._raw_content += f"{new_table_row}\n"

    def _build_table_row(self, record: BackfillRecord) -> str:
        alert_summary = record.alert.summary
        alert = record.alert
        job_type = record.job_type
        total_backfills = record.total_backfills_triggered
        push_range = self.__build_push_range(record.get_context())

        # some fields require adjustments
        summary_id = alert_summary.id
        alert_id = alert.id
        job_symbol = str(job_type)

        return f"| {summary_id} | {alert_id} | {job_symbol} | {total_backfills} | {push_range} |"

    def __build_push_range(self, backfill_context: List[dict]) -> str:
        """
        Provides link to Treeherder' s Job view
        """
        from_push, to_push = self.__fetch_border_pushes(backfill_context)  # suspect range as tuple

        # TODO: repository should be provided by upstream (AKA backfill context)
        #  to save some database calls
        repository = from_push.repository.name
        from_change = from_push.revision
        to_change = to_push.revision

        return f"{settings.SITE_URL}/jobs?repo={repository}&fromchange={from_change}&tochange={to_change}"

    def __fetch_border_pushes(self, backfill_context) -> Tuple[Push, Push]:
        from_datapoint = backfill_context[0]
        to_datapoint = backfill_context[-1]

        from_push = from_datapoint['push_id']
        to_push = to_datapoint['push_id']

        from_push = Push.objects.get(id=from_push)
        to_push = Push.objects.get(id=to_push)
        return from_push, to_push

    def __str__(self):
        if self._raw_content is None:
            raise ValueError("No content set")
        return self._raw_content

class BackfillNotificationWriter(EmailWriter):
    def _write_address(self):
        self._email.address = FXPERF_TEST_ENG_EMAIL

    def _write_subject(self):
        self._email.subject = "Automatic Backfilling Report"

    def _write_content(self, must_mention: List[BackfillRecord]):
        content = BackfillReportContent()
        content.include_records(must_mention)

        self._email.content = str(content)

# For performance data cycling
class DeletionReportContent:
    DESCRIPTION = """Perfherder removes performance data that is older than one year and in some cases even sooner, leaving behind performance signatures that aren't associated to any data point. These as well need to be removed.
    > __Here's a summary of recently deleted performance signatures:__
    ---
        """

    TABLE_HEADERS = """
    | Repository | Framework | Platform | Suite | Application |
    | :---: | :---: | :---: | :---: | :---: |
        """

    def __init__(self):
        self._raw_content = None

    def include_signatures(self, signatures: List[PerformanceSignature]):
        self._initialize_report_intro()

        for signature in signatures:
            self._include_in_report(signature)

    def _initialize_report_intro(self):
        if self._raw_content is None:
            self._raw_content = self.DESCRIPTION + self.TABLE_HEADERS

    def _include_in_report(self, signature: PerformanceSignature):
        new_table_row = self._build_table_row(signature)
        self._raw_content += f"{new_table_row}\n"

    def _build_table_row(self, signature: PerformanceSignature) -> str:
        props = self.__extract_properties(signature)

        return '| {repository} | {framework} | {platform} | {suite} | {application} |'.format(
            repository=props["repository"],
            framework=props["framework"],
            platform=props["platform"],
            suite=props["suite"],
            application=props["application"],
        )

    def __extract_properties(self, signature: PerformanceSignature) -> dict:
        return {
            "repository": signature.repository.name,
            "framework": signature.framework.name,
            "platform": signature.platform.platform,
            "suite": signature.suite,
            "application": signature.application,
        }

    def __str__(self):
        if self._raw_content is None:
            raise ValueError("No content set")
        return self._raw_content

class DeletionNotificationWriter(EmailWriter):
    def _write_address(self):
        self._email.address = FXPERF_TEST_ENG_EMAIL

    def _write_subject(self):
        self._email.subject = "Summary of deleted Performance Signatures"

    def _write_content(self, must_mention: List[PerformanceSignature]):
        content = DeletionReportContent()
        content.include_signatures(must_mention)

        self._email.content = str(content)


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("new-listing", views.new_listing, name="new-listing"),
    path("listings/<slug>", views.listing, name="listing"),
    path("add-or-delete-watchlist/<slug>", views.add_or_delete_watchlist, name="add-or-delete-watchlist"),
    path("listings/<slug>/bid", views.bid, name="bid"),
    path("listings/<slug>/close", views.close_listing, name="close-listing"),
    path("listings/<slug>/comment", views.comment, name="comment"),
    path("watchlist", views.watchlist, name="watchlist"),
    path("categories", views.categories, name="categories"),
    path("categories/<slug>", views.category, name="category")
]


#!/usr/bin/env python
# coding: utf-8

# Standard imports
import numpy as np
import pandas as pd

from limitless_scrape import *
from limitless_analysis import *

import textwrap

import plotly.express as px
import plotly.graph_objects as go
import plotly
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State

app = dash.Dash(__name__)
server = app.server

# Read in data
plot_df = pd.read_csv('data_collection/results/latest/scrape_results.csv')
set_calendar_df = pd.read_csv('set_release_calendar.csv')

# Make sure set_names don't have white space
set_calendar_df["set_name"] = set_calendar_df["set_name"].str.strip()

# Find latest set
LATEST_SET = set_calendar_df["set_name"].unique().tolist()[-1]

def textwrapper(s, width=50):
    return "
".join(textwrap.wrap(s,width=width))

app.layout = html.Div([

    html.Div(
        className="my_row",
        children=[
            html.Div(
                className="dropdown_card",
                children=[
                    html.Label(["Choose archetypes to display on the graph:"], style={'font-weight': 'bold', "display": "block", "text-align": "center"}),
                    dcc.Dropdown(
                        id='dropdown_format',
                        options=[{'label': x, 'value': x} for x in set_calendar_df['set_name'].unique()],
                        value=LATEST_SET,
                        multi=False,
                        disabled=False,
                        clearable=True,
                        searchable=True,
                        placeholder='Select a set',
                        persistence=True,
                        persistence_type='memory'),

                    dcc.Dropdown(
                        id='dropdown_deck',
                        options=[{'label': x, 'value': x} for x in plot_df.sort_values('deck')['deck'].unique()],
            #           value='Mew Genesect',
                        multi=False,
                        disabled=False,
                        clearable=True,
                        searchable=True,
                        placeholder='Select archetype',
                        persistence='string',
                        persistence_type='memory'),

                    dcc.Dropdown(
                        id='dropdown_opp_deck',
                        options=[{'label': x, 'value': x} for x in plot_df.sort_values('opposing_deck')['opposing_deck'].unique()],
                        value = ["Palkia Inteleon", "Mew Genesect", "Kyurem Palkia", "Giratina LZ Box", "Lost Zone Box"],
                        multi=True,
                        disabled=False,
                        clearable=True,
                        searchable=True,
                        placeholder='Show winrates against...',
                        persistence='string',
                        persistence_type='memory'),
                ])
            ]
    ),

    html.Div(
        className="my_row",
        children=[
            html.Div([
                dcc.Graph(id='our_graph')
                ], className='graph_card'),

            html.Div([
                dcc.Graph(id='heatmap')
                ], className='graph_card'),
        ]
    )
], className="create_container")

# Filter what active decks are available in the dropdown based on format that is selected
@app.callback(
    Output('dropdown_deck', 'options'),
    Input('dropdown_format', 'value')
)
def available_active_decks(selected_format):
    # Filter by date
    set_df = set_calendar_df[set_calendar_df["set_name"]==selected_format]
    start_date = set_df.iloc[0]['start_date']
    end_date = set_df.iloc[0]['end_date']

    filtered_by_date_df = plot_df[(plot_df['date'] >= start_date) & (plot_df['date'] <= end_date)]
    return [{'label': x, 'value': x} for x in filtered_by_date_df.sort_values('deck')['deck'].unique()]

# Set active deck to most popular deck
@app.callback(
    Output('dropdown_deck', 'value'),
    Input('dropdown_format', 'value')
)
def set_active_deck(selected_format):
    # Filter by date
    set_df = set_calendar_df[set_calendar_df["set_name"]==selected_format]
    start_date = set_df.iloc[0]['start_date']
    end_date = set_df.iloc[0]['end_date']

    filtered_by_date_df = plot_df[(plot_df['date'] >= start_date) & (plot_df['date'] <= end_date)]

    # group by games played
    gp_df = filtered_by_date_df.groupby('deck').sum()['games_played'].reset_index()

    # Return top played deck
    return [{'label': x, 'value': x} for x in gp_df.sort_values('games_played', ascending=False)['deck'].unique()][0]['value']

# Filter what opposing decks are available in the dropdown based on the format and active deck selected
@app.callback(
    Output('dropdown_opp_deck', 'options'),
    [
        Input('dropdown_format', 'value'),
        Input('dropdown_deck', 'value')
    ]
)
def available_opp_decks(selected_format, selected_active_deck):
    # Filter by date
    set_df = set_calendar_df[set_calendar_df["set_name"]==selected_format]
    start_date = set_df.iloc[0]['start_date']
    end_date = set_df.iloc[0]['end_date']

    filtered_by_date_df = plot_df[(plot_df['date'] >= start_date) & (plot_df['date'] <= end_date)]

    # Filter for deck of interest
    filtered_df = filter_plot_df(filtered_by_date_df, selected_active_deck)

    # Return list of opposing decks after filters are applied
    return [{'label': x, 'value': x} for x in filtered_df.sort_values('opposing_deck')['opposing_deck'].unique()]

# Set opposing decks to top 5 most played in the selected format
@app.callback(
    Output('dropdown_opp_deck', 'value'),
    Input('dropdown_format', 'value'),
)
def available_opp_decks(selected_format):
    # Filter by date
    set_df = set_calendar_df[set_calendar_df["set_name"]==selected_format]
    start_date = set_df.iloc[0]['start_date']
    end_date = set_df.iloc[0]['end_date']

    filtered_by_date_df = plot_df[(plot_df['date'] >= start_date) & (plot_df['date'] <= end_date)]

    # group by games played
    gp_df = filtered_by_date_df.groupby('deck').sum('games_played').reset_index()

    # Return 5 most played deck
    return  [x['value'] for x in [{'label': x, 'value': x} for x in gp_df.sort_values('games_played', ascending=False)['deck'].unique()]][0:5]

# Plot our graph based on our selections in the dropdown
@app.callback(
    Output('our_graph', 'figure'),
    [
        Input('dropdown_format', 'value'),
        Input('dropdown_deck','value'),
        Input('dropdown_opp_deck', 'value')
     ]
     )

def build_graph(dropdown_format, dropdown_deck, dropdown_opp_deck):
    """Plot win rates for selected active deck and selected opposing decks in the selected format.

    Arguments:
        dropdown_format (str): Name of a Pokemon TCG expansion. Filters data for plotting
                               to only include data from the start of the expansion's release date,
                               til the day before the next expansion's release date.
        dropdown_deck (str): Name of a Pokemon TCG archetype. Filters the data for plotting. Show
                             this decks win rate against other archetypes.
        dropdown_opp_deck (list): Name(s) of Pokemon TCG archetype(s). Filters the data to show the
                                  win rates of the deck selected in dropdown_deck against the decks
                                  selected in dropdown_opp_deck.

    Returns:
        fig (plotly.express line graph): A line graph showing the data filtered by the three dropdown
                                         menus.
    """

    # Get a set's start and end date
    set_df = set_calendar_df[set_calendar_df["set_name"]==dropdown_format]
    start_date = set_df.iloc[0]['start_date']
    end_date = set_df.iloc[0]['end_date']

    # Filter dataframe by date
    format_df = plot_df[(plot_df['date'] >= start_date) & (plot_df['date'] <= end_date)]

    # Filter for deck of interest
    active_deck_df = filter_plot_df(format_df, dropdown_deck)

    # Filter the data for opposing deck
    opp_deck_df = active_deck_df[active_deck_df["opposing_deck"].isin(dropdown_opp_deck)]

    # Aggregate by day - need weighted win rates here
    gp_per_day = opp_deck_df.groupby(["deck", "opposing_deck", "date"]).sum().reset_index()
    gp_per_day["gp_per_day"] = gp_per_day["games_played"]

    join_conditions = ["deck", "opposing_deck", "date"]
    weighted_df = opp_deck_df.merge(gp_per_day[["deck", "opposing_deck", "date", "gp_per_day"]], how="left", on=join_conditions)

    weighted_df['wt'] = weighted_df["games_played"] / weighted_df["gp_per_day"]
    weighted_df['wt_wr'] = round(weighted_df['winrate'] * weighted_df['wt'], 2)

    weighted_df = weighted_df.groupby(["deck", "opposing_deck", "date"]).sum(["games_played", "wt_wr"]).reset_index()

    # Create line graph
    fig = px.scatter(weighted_df,
                     x='date',
                     y='wt_wr',
                     color='opposing_deck',
                     hover_data=["games_played"],
                     size='games_played',
                     labels=dict(date="Date",
                                 opposing_deck="Opposing Deck",
                                 wt_wr="Win Rate",
                                 games_played="Games Played")) \
                                 .update_traces(mode='lines+markers')

    # Update layout
    # fig.update_layout(width=1080, height=720)
    fig.update_layout(
        showlegend=True,
        yaxis_range=[-0.05,1.05],
        title_text=textwrapper(f"{dropdown_deck}'s win rate against opposing decks over time since {dropdown_format}'s release")
    )
    fig.update_layout(title={"x": 0.45, "y": 0.95, "xanchor": "center", "yanchor": "middle"})
    fig.add_hline(y=0.5)
    fig.update_xaxes(ticks="outside", ticklen=10)
    fig.update_yaxes(ticks="outside", dtick=0.1, ticklen=10)

    return fig

@app.callback(
    Output('heatmap', 'figure'),
    [
        Input('dropdown_format', 'value'),
        Input('dropdown_deck','value'),
        Input('dropdown_opp_deck', 'value')
     ]
     )
def build_heatmap(selected_format, selected_active_deck, selected_opp_deck):
    # Filter by selected format
    set_df = set_calendar_df[set_calendar_df["set_name"]==selected_format]
    start_date = set_df.iloc[0]['start_date']
    end_date = set_df.iloc[0]['end_date']

    # Filter dataframe by date
    format_df = plot_df[(plot_df['date'] >= start_date) & (plot_df['date'] <= end_date)]

    # Create list of decks selected in dropdown
    selected_opp_deck.append(selected_active_deck)
    unique_decks = []
    for deck in selected_opp_deck:
        if deck not in unique_decks:
            unique_decks.append(deck)

    # Filter active deck by decks in unique decks, filter again on opposing deck by unique decks also
    active_deck_df = format_df[format_df['deck'].isin(unique_decks)]
    filtered_df = active_deck_df[active_deck_df['opposing_deck'].isin(unique_decks)]

    # blank heatmaps for wins and games played
    heatmap_gp = pd.DataFrame(columns=unique_decks, index=unique_decks)
    heatmap_wr = pd.DataFrame(columns=unique_decks, index=unique_decks)

    # Get total games played and weighted win rates
    filtered_df["total_games"] = filtered_df.apply(lambda x: total_gp(filtered_df, x), axis=1)
    filtered_df["weight"] = filtered_df["games_played"]/filtered_df["total_games"]
    filtered_df["wt_wr"] = filtered_df["winrate"] * filtered_df["weight"]

    # Create aggregated df
    agg_df = filtered_df.groupby(['deck', 'opposing_deck']).agg({"wt_wr": "sum", "games_played": "sum"}).reset_index()

    # Loop through active decks to fill in columns in blank heatmaps
    for active_deck in heatmap_wr:
        filtered_df = agg_df[agg_df['deck']==active_deck]

        # Find win rate for every deck in index (opposing deck)
        wr_ls = []
        gp_ls = []
        for deck in heatmap_wr.index:
            if deck in filtered_df['opposing_deck'].unique():
                winrate = filtered_df[filtered_df['opposing_deck']==deck].iloc[0]['wt_wr']
                gp = filtered_df[filtered_df['opposing_deck']==deck].iloc[0]['games_played']
                wr_ls.append(round(winrate,2))
                gp_ls.append(gp)
            else:
                winrate = None
                wr_ls.append(winrate)
                gp_ls.append(None)

        # set column to mu
        heatmap_wr[active_deck] = wr_ls
        heatmap_gp[active_deck] = gp_ls

    # Heatmap hovertext
    hovertext = []
    for xi, xx in enumerate(heatmap_wr.columns):
        hovertext.append(list())
        for yi, yy in enumerate(heatmap_wr.index):
            hovertext[-1].append(f"Active Deck: {yy}
Opposing Deck: {xx}
Win Rate: {heatmap_wr.values[xi][yi]}
Games Played: {heatmap_gp.values[xi][yi]}")

    fig = go.Figure(data=go.Heatmap(
                   z=heatmap_wr.values,
                   x=heatmap_wr.columns.tolist(),
                   y=heatmap_wr.index.tolist(),
                   hoverinfo='text',
                   text=hovertext,
                   texttemplate="%{z}",
                   textfont={"size": 12},
                   hoverongaps = False,
                   xgap=3,
                   ygap=3,
                #    zmin=0,
                #    zmax=1
                   ))

    # Layout
    title_text = f"Average win rates since {selected_format}'s release"
    xaxis_title = "Active Decks"
    yaxis_title = "Opposing Decks"
    fig.update_layout(
                      title_text=title_text,
                      title={"x": 0.5, "y": 0.9, "xanchor": "center", "yanchor": "bottom"},
                      xaxis_title=xaxis_title,
                      yaxis_title=yaxis_title
                    )

    return fig

if __name__ == '__main__':
    app.run_server(debug=True, port=8080)


import os
import pickle
from datetime import datetime
import tensorflow as tf
from tf_fourier_features.fourier_features_mlp import FourierFeatureMLP

SAMPLING_RATIO = 0.3
BATCH_SIZE = 8192
EPOCHS = 2000

IMAGE_SIZE = 800
IMAGE_EMBED = 8

img_filepath_1 = '../data/blue_flower.jpg'
img_filepath_2 = '../data/fur-style.jpg'
img_filepath_3 = '../data/celtic_spiral_knot.jpg'

images_paths = [img_filepath_1, img_filepath_2, img_filepath_3]
image_ground_truths = []

for img_filepath in images_paths:
    img_raw = tf.io.read_file(img_filepath)
    img_ground_truth = tf.io.decode_image(img_raw, channels=3, dtype=tf.float32)
    img_ground_truth = tf.image.resize(img_ground_truth, [IMAGE_SIZE, IMAGE_SIZE], method=tf.image.ResizeMethod.BICUBIC)
    image_ground_truths.append(img_ground_truth)

print("Decoded {} images of shape {}".format(len(image_ground_truths), image_ground_truths[0].shape))

rows, cols, channels = image_ground_truths[0].shape
pixel_count = rows * cols
sampled_pixel_count = int(pixel_count * SAMPLING_RATIO)

def build_train_tensors():
    img_mask_x = tf.random.uniform([sampled_pixel_count], maxval=rows, seed=0, dtype=tf.int32)
    img_mask_y = tf.random.uniform([sampled_pixel_count], maxval=cols, seed=1, dtype=tf.int32)

    img_mask_x = tf.expand_dims(img_mask_x, axis=-1)
    img_mask_y = tf.expand_dims(img_mask_y, axis=-1)

    img_mask_idx = tf.concat([img_mask_x, img_mask_y], axis=-1)

    train_images = []
    for image in image_ground_truths:
        img_train = tf.gather_nd(image, img_mask_idx, batch_dims=0)
        train_images.append(img_train)

    img_mask_x = tf.cast(img_mask_x, tf.float32) / rows
    img_mask_y = tf.cast(img_mask_y, tf.float32) / cols

    img_mask = tf.concat([img_mask_x, img_mask_y], axis=-1)
    image_masks = []
    image_contexts = []

    tf.random.set_seed(0)
    for ix in range(len(image_ground_truths)):
        img_context = tf.random.normal([1, IMAGE_EMBED])
        image_contexts.append(img_context)

        img_context = tf.broadcast_to(img_context, [img_mask.shape[0], IMAGE_EMBED])
        context_mask = tf.concat([img_mask, img_context], axis=-1)
        image_masks.append(context_mask)

    return image_masks, train_images, image_contexts

image_masks, train_images, image_contexts = build_train_tensors()
print("Number of masks and train images : ", len(image_masks))

img_mask = tf.concat(image_masks, axis=0)
img_train = tf.concat(train_images, axis=0)
print("Number of train samples : ", len(img_mask))

train_dataset = tf.data.Dataset.from_tensor_slices((img_mask, img_train))
train_dataset = train_dataset.shuffle(10000).batch(BATCH_SIZE).cache()
train_dataset = train_dataset.prefetch(tf.data.experimental.AUTOTUNE)

# Build model
model = FourierFeatureMLP(units=256, final_units=3, final_activation='sigmoid', num_layers=4,
                          gaussian_projection=256, gaussian_scale=20.0)

# instantiate model
_ = model(tf.zeros([1, 2 + IMAGE_EMBED]))

model.summary()

BATCH_SIZE = min(BATCH_SIZE, len(img_mask))
num_steps = int(len(img_mask) * EPOCHS / BATCH_SIZE)
print("Total training steps : ", num_steps)
learning_rate = tf.keras.optimizers.schedules.PolynomialDecay(0.0005, decay_steps=num_steps, end_learning_rate=0.0001,
                                                              power=2.0)

optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
loss = tf.keras.losses.MeanSquaredError(reduction=tf.keras.losses.Reduction.NONE)  # Sum of squared error
model.compile(optimizer, loss=loss)

checkpoint_dir = 'checkpoints/multi_fourier_features/inpainting/'
if not os.path.exists(checkpoint_dir):
    os.makedirs(checkpoint_dir)

# Serailize the context vectors
with open(checkpoint_dir + 'image_contexts.pkl', 'wb') as f:
    pickle.dump(image_contexts, f)
print("Serialized the context embeddings !")

timestamp = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
logdir = os.path.join('../logs/multi_fourier_features/inpainting/', timestamp)

if not os.path.exists(logdir):
    os.makedirs(logdir)

callbacks = [
    tf.keras.callbacks.ModelCheckpoint(checkpoint_dir + 'model', monitor='loss', verbose=0,
                                       save_best_only=True, save_weights_only=True, mode='min'),
    tf.keras.callbacks.TensorBoard(logdir, update_freq='batch', profile_batch=20)
]

model.fit(train_dataset, epochs=EPOCHS, callbacks=callbacks, verbose=2)


from datetime import datetime

import boto3
from botocore.exceptions import ClientError

from stream_alert_cli.logger import LOGGER_CLI

class LambdaVersion(object):
    """Publish new versions of the StreamAlert Lambda functions.

    Each Lambda function is configured with a production alias.
    This alias points to the latest published version of the
    Lambda function.

    After a Lambda package is versioned, the StreamAlert config
    is updated, and then Terraform runs to update the alias with
    the new version number.

    All StreamAlert setups should start with "$LATEST".
    """

    def __init__(self, **kwargs):
        """Initialize the version publishing

        Keyword Args:
            config (CLIConfig): Loaded StreamAlert CLI Config
            package (LambdaPackage): The created Lambda Package
            clustered_deploy (bool): Identifies cluster based Lambdas
        """
        self.config = kwargs['config']
        self.package = kwargs['package']
        self.clustered_deploy = kwargs.get('clustered_deploy', True)

    @staticmethod
    def _version_helper(**kwargs):
        """Make the API call to publish the Lambda function

        Keyword Arguments:
            client (boto3.client): Lambda boto3 client
            function_name (str): Lambda function name to publish
            code_sha_256 (str): The SHA256 of the current $LATEST package
            date (datetime): Current time

        Returns:
            int: Version OR False if the publish fails
        """
        client = kwargs.get('client')
        if not client:
            LOGGER_CLI.error('No AWS client provided')
            return False

        try:
            version = client.publish_version(
                FunctionName=kwargs['function_name'],
                CodeSha256=kwargs['code_sha_256'],
                Description='Publish Lambda {} on {}'.format(kwargs['function_name'],
                                                             kwargs['date'])
            )['Version']
        except ClientError as err:
            LOGGER_CLI.error(err)
            return False

        return int(version)

    def _publish_helper(self, **kwargs):
        """Handle clustered or single Lambda function publishing

        Keyword Arguments:
            cluster (str): The cluster to deploy to, this is optional

        Returns:
            bool: Result of the function publishes
        """
        cluster = kwargs.get('cluster')
        # Clustered Lambda functions have a different naming pattern
        if cluster:
            region = self.config['clusters'][cluster]['region']
            function_name = '{}_{}_streamalert_{}'.format(
                self.config['global']['account']['prefix'],
                cluster,
                self.package.package_name
            )
        else:
            region = self.config['global']['account']['region']
            function_name = '{}_streamalert_{}'.format(
                self.config['global']['account']['prefix'],
                self.package.package_name
            )

        # Configure the Lambda client
        client = boto3.client('lambda', region_name=region)
        code_sha_256 = self.config['lambda'][self.package.config_key]['source_current_hash']

        # Publish the function(s)
        # TODO: move the extra logic into the LambdaPackage subclasses instead of this
        if self.package.package_name == 'stream_alert_app':
            if not 'stream_alert_apps' in self.config['clusters'][cluster]['modules']:
                return True # nothing to publish for this cluster

            for app_name, app_info in self.config['clusters'][cluster]['modules'] \
                ['stream_alert_apps'].iteritems():
                # Name follows format: '<prefix>_<cluster>_<service>_<app_name>_app'
                function_name = '_'.join([self.config['global']['account']['prefix'], cluster,
                                          app_info['type'], app_name, 'app'])
                new_version = self._publish(client, function_name, code_sha_256)
                if not new_version:
                    continue

                LOGGER_CLI.info('Published version %s for %s:%s',
                                new_version, cluster, function_name)

                app_info['current_version'] = new_version

        else:

            new_version = self._publish(client, function_name, code_sha_256)
            if not new_version:
                return False

            # Update the config
            if cluster:
                LOGGER_CLI.info('Published version %s for %s:%s',
                                new_version, cluster, function_name)
                self.config['clusters'][cluster]['modules']['stream_alert'] \
                    [self.package.package_name]['current_version'] = new_version
            else:
                LOGGER_CLI.info('Published version %s for %s',
                                new_version, function_name)
                self.config['lambda'][self.package.config_key]['current_version'] = new_version
        self.config.write()

        return True

    def _publish(self, client, function_name, code_sha_256):
        """Publish the function"""
        date = datetime.utcnow().strftime("%Y%m%d_T%H%M%S")
        LOGGER_CLI.debug('Publishing %s', function_name)
        new_version = self._version_helper(
            client=client,
            function_name=function_name,
            code_sha_256=code_sha_256,
            date=date)

        return new_version

    def publish_function(self):
        """Main Publish Function method"""
        if self.clustered_deploy:
            for cluster in self.config.clusters():
                if not self._publish_helper(cluster=cluster):
                    return False
        else:
            if not self._publish_helper():
                return False

        return True


#!/usr/bin/env python3
import argparse
import sql_metadata
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='hoge')
    parser.add_argument('filename', type=str)
    args = parser.parse_args()
    with open(args.filename) as f:
        read_data = f.read()
    f.close()
    for name in sql_metadata.get_query_tables(read_data):
        print(name)


from flask import jsonify, make_response, request
from flask_restful import Resource
from ..models.sale_model import *

class Sales(Resource):
    def __init__(self):
        self.sale = Sale()

    def post(self):
        data = request.get_json()
        user_id = data['user_id']
        product_id = data['product_id']
        quantity = data['quantity']
        unit_price = data['unit_price']
        total_cost = int(data['quantity']) * int(data['unit_price'])

        sale = self.sale.create_sale(user_id, product_id, quantity, unit_price)
        return make_response(jsonify({
            "message": "success",
            "sale": sale,
            "total_cost": total_cost
        }), 201)

    def get(self):
        sales = self.sale.get_all_sales()
        return make_response(jsonify({
            "status": "OK",
            "message": "success",
            "products": sales
        }), 200)

class FilterRecordsByAttendant(Resource):
    def __init__(self):
        self.sales = Sale()

    def get(self, user_id):
        sale_records = self.sales.filter_sales_records_by_attendant(user_id)
        return make_response(jsonify({
            "status": "OK",
            "message": "success",
            "product": sale_records
        }), 200)


from django.conf.urls import include, url
from . import views

urlpatterns = [
    url(r'^$', views.post_list),
    url(r'^reclamacoes/$',views.reclamacoes),
    url(r'^linhas/$',views.linhas),
    url(r'^linhas/(?P<pk>[0-9]+)/$', views.veiculo_especifico),
    url(r'^linhasestaticas/$',views.linhas_estaticas),
    url(r'^todasaslinhasestaticas/$',views.todas_linhas_estaticas),
    url(r'^linha/zona/(?P<pk>[^/]+)/$', views.linhas_por_zona),
    url(r'^distanciaonibus/$',views.distancia_onibus_user),
    url(r'^adicionarparadas/$',views.preecher_pardas),
    url(r'^paradas/$',views.mostrar_paradas),
    url(r'^distancia_raio/$',views.distancia_raio),
    url(r'^parada_proxima/$',views.qualquer_distancia_dois_pontos),
    url(r'^excluir/$',views.excluir_parada),
    url(r'^parada_especifica/(?P<pk>[0-9]+)/$', views.parada_especifica),
    url(r'^trans_lat_long_in_end/', views.converter_lat_long_in_address),
    url(r'^login/',views.loginpage),
    url(r'^validarlogin/', views.validarlogin),
    url(r'^administracao/', views.administracao),
    url(r'^resetar/', views.resetar),
    url(r'^sair/', views.sair),
    url(r'^addonibusadpt/', views.adicionar_vec_adpt)

]


from kivy.clock import Clock
from kivy.base import EventLoop
from kivy.uix.button import Button

from src.monitor import kivy_timing

from src.common import (
    GRID_HINT,
    DEFAULT_ATLAS,
    get,
    generate_grid_id
)

from src.save import (
    GRID,
    load_level,
    save_level,
    validate_character
)

# All the grid custom widget ids will be store here. This is because the ids
# are not stored in kivy layout ids because there were dynamically updated so
# we manually store the id reference of all the word button object
grid_ptr: list = []

# The custom widget which will use to construct grids for the cross word
# levels. This is actually a button widget with some custom logic for the game
class WordButton(Button):
    # To prevent mulitple selection we use a static variable to lock the grid
    lock: bool = False

    # @kivy_timing -> Slow
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        grid_ptr.append(self)
        self.opacity: float = 0.0
        self.id: str = generate_grid_id()
        self.next_auto_selection_id = self.id
        self.disabled: bool = True
        self.text: str = str(GRID.popleft()) if len(GRID) >= 0 else 'X'
        self.level_logic()
        self.bind(on_press=self.on_click)
        self.fade_effect_ptr = Clock.schedule_interval(self.fade_effect, 0.001)
        EventLoop.window.bind(on_keyboard=self.event_keyboard)

    # @kivy_timing -> Slow
    def level_logic(self) -> None:
        if self.text == "0":
            self.text = ""
        else:
            self.disabled = False
            if self.text.islower():
                self.text = ""

    @kivy_timing
    def on_click(self, *_) -> None:
        if self.text == "" and not self.disabled and not WordButton.lock:
            self.text = "?"
            WordButton.lock = True
            self.background_normal = ""
            self.background_color = 0, 1, 1, 1
        if self.text == "" and not self.disabled and WordButton.lock:
            self.text = "?"
            # remove previous selection
            for widget in grid_ptr:
                if widget.text == "?" and widget != self:
                    widget.background_normal = DEFAULT_ATLAS
                    widget.background_color = 1, 1, 1, 1
                    widget.text = ""
            self.background_normal = ""
            self.background_color = 0, 1, 1, 1

    # @kivy_timing ->
    def event_keyboard(self, __, key: int, *_):
        if self.text == "?":
            # NOTE: Reload the level data again to check validation This needs
            # to be done because while building the grid all the elements are
            # popped out. Thus the GRID deque was empty.
            load_level(get(level_number=True))
            WordButton.lock = False

            # set the grid block to it's original form
            def set_grid_block_to_default(*_):
                self.background_normal = DEFAULT_ATLAS
                self.background_color = 1, 1, 1, 1
                try:
                    selection = GRID_HINT[int(self.id)].split()[1]
                except IndexError:
                    selection = '0'
                if selection == 'd':
                    self.next_auto_selection_id = str(int(self.id) + 14)
                if selection == 'dd':
                    self.next_auto_selection_id = str(int(self.id) + 28)

                elif selection == 'u':
                    self.next_auto_selection_id = str(int(self.id) - 14)
                if selection == 'uu':
                    self.next_auto_selection_id = str(int(self.id) - 28)

                elif selection == 'l':
                    self.next_auto_selection_id = str(int(self.id) - 1)
                elif selection == 'll':
                    self.next_auto_selection_id = str(int(self.id) - 2)

                elif selection == 'r':
                    self.next_auto_selection_id = str(int(self.id) + 1)
                elif selection == 'rr':
                    self.next_auto_selection_id = str(int(self.id) + 2)

                elif selection == 'dr':
                    x = str(int(self.id) + 14)
                    self.next_auto_selection_id = str(int(x) + 1)
                elif selection == 'ur':
                    x = str(int(self.id) - 14)
                    self.next_auto_selection_id = str(int(x) + 1)

                elif selection == 'dl':
                    x = str(int(self.id) + 14)
                    self.next_auto_selection_id = str(int(x) - 1)
                elif selection == 'ul':
                    x = str(int(self.id) - 14)
                    self.next_auto_selection_id = str(int(x) - 1)

                elif selection == '0':
                    self.next_auto_selection_id = '0'

                for widget in grid_ptr:
                    try:
                        if widget.id == self.next_auto_selection_id != self.id and not widget.text.upper() and self.text != "":
                            widget.background_normal = ""
                            widget.background_color = 0, 1, 1, 1
                            widget.text = '?'
                    except AttributeError:
                        pass
                WordButton.lock = True

            def not_correct_letter(*_):
                self.text = ""
                set_grid_block_to_default(None)

            # Android Back button or Esc key
            if key == 27:
                Clock.schedule_once(not_correct_letter, 1)

            # The key mush be the ascii value of the lowercase character as
            # the save file contains the lowercase chararcter to validate with
            if validate_character(chr(key).lower(), self.id):
                self.text = chr(key).upper()
                save_level(int(self.id), chr(key).upper())
                Clock.schedule_once(set_grid_block_to_default, 1)
            else:
                self.text = "X"
                self.background_normal = DEFAULT_ATLAS
                self.background_color = 1, 0, 0, 1
                Clock.schedule_once(not_correct_letter, 1)

    # @kivy_timing -> Slow
    def fade_effect(self, *_) -> None:
        if self.opacity < 1:
            self.opacity += 0.1
        # Unregister event after use
        elif self.opacity > 1:
            Clock.unschedule(self.fade_effect_ptr)


import argparse

import backtrader as bt
import backtrader.feeds as btfeeds
from MyPandasDataFrame import MyPandasData

import pandas

def runstrat():
    args = parse_args()

    # Create a cerebro entity
    cerebro = bt.Cerebro(stdstats=False)

    # Add a strategy
    cerebro.addstrategy(bt.Strategy)

    # Get a pandas dataframe
    datapath = ('/Users/hits/Dropbox (Personal)/notebooks/data/AAPL_1day.h5')

    dataframe = pandas.read_hdf(datapath)
    dataframe = dataframe.sort_index()

    if not args.noprint:
        print('--------------------------------------------------')
        print(dataframe)
        print('--------------------------------------------------')

    # Pass it to the backtrader datafeed and add it to the cerebro
    data = MyPandasData(dataname=dataframe)

    cerebro.adddata(data)

    # Run over everything
    cerebro.run()

    # Plot the result
    cerebro.plot(style='bar')

def parse_args():
    parser = argparse.ArgumentParser(
        description='Pandas test script')

    parser.add_argument('--noheaders', action='store_true', default=False,
                        required=False,
                        help='Do not use header rows')

    parser.add_argument('--noprint', action='store_true', default=False,
                        help='Print the dataframe')

    return parser.parse_args()

if __name__ == '__main__':
    runstrat()


import cv2
import numpy as np

img = cv2.imread('./imagenes/Madotsuki.jpg',1)
cv2.imshow('imagenRGB',img)
cv2.waitKey(0)

gris = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('imagenGRAY',gris)
cv2.waitKey(0)

hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow('imagenHSV',hsv)
cv2.waitKey(0)

yuyv=cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
cv2.imshow('imagenYUV',yuyv)
cv2.waitKey(0)

Tcrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
cv2.imshow('imagenYCrCb',Tcrcb)
cv2.waitKey(0)

b, g, r = cv2.split(img)
cv2.imshow('blue',b)
cv2.imshow('green',g)
cv2.imshow('red',r)

#src = cv2.merge([r,g,b])
#src = cv2.merge([g,r,b])
#src = cv2.merge([b,g,r])
#src = cv2.merge([b,r,g])
src = cv2.merge([r,b,g])
cv2.imshow('Change Image',src)
cv2.waitKey(0)

cv2.destroyAllWindows(0)


import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1208)
def get_noise_index(size=20,ratio=0.2):
	noise_indx = np.arange(size)
	np.random.shuffle(noise_indx)
	noise_indx = noise_indx[:int(size*ratio)]
	return noise_indx

size = 20
avg_Ein = []
avg_Eout = []
for times in range(1000):
	data = np.random.uniform(-1,1,size)
	noise_indx = get_noise_index()
	label = np.sign(data)
	newlabel = label.copy()
	np.negative.at(newlabel, noise_indx) # flip noise data
	# sort data & label
	sorted_indx = data.argsort()
	newlabel = newlabel[sorted_indx]
	data = data[sorted_indx]
	# get the best H
	min_Ein, best_s, best_theta = 1, 1, 1
	for s in [1,-1]:
		for i in range(size+1):
			# the first and last interval
			if i == 0:
				theta = data[0] - 1
			elif i == size:
				theta = data[-1] + 1
			else:
				theta = (data[i] + data[i-1])/2
			err = 0
			for x,y in zip(data, newlabel):
				if s*np.sign(x-theta) != y:
					err += 1
			err /= size
			if err <= min_Ein:
				min_Ein = err
				best_s = s
				best_theta = theta
	print('Ein = {}, s = {}, theta = {}'.format(min_Ein, best_s, best_theta))
	Eout = 0.5 + 0.3*best_s*(np.absolute(best_theta)-1)
	avg_Ein.append(min_Ein)
	avg_Eout.append(Eout)
print('avg_Ein = {}'.format(np.average(avg_Ein)))
print('avg_Eout = {}'.format(np.average(avg_Eout)))
avg_Ein = np.array(avg_Ein)
avg_Eout = np.array(avg_Eout)
avg_diff = avg_Ein-avg_Eout
plt.hist(avg_diff,bins=int((max(avg_diff)-min(avg_diff))/0.01))#, bins=range(min(avg_diff), max(avg_diff),0.2))
plt.xlabel('Ein-Eout')
plt.ylabel('Frequency')
plt.savefig('hw2_7.png')