#! python3
import webbrowser
import sys
import bs4
import requests
import openpyxl
import os
import xlsxwriter
import re

rootSite = 'http://wiki.wargaming.net/en/World_of_Warships'

#shipclasses = ['DD', 'CA', 'BB', 'CV']
shipclasses = ['DD', 'CA', 'BB']
# shipclasses = ['CA', 'BB']
# skiplist = ['St. Louis']

for shipclass in shipclasses:

    if shipclass == 'DD':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Destroyers'
    elif shipclass == 'CA':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Cruisers'
    elif shipclass == 'BB':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Battleships'
    elif shipclass == 'CV':
        shipsite = 'http://wiki.wargaming.net/en/Ship:Aircraft_Carriers'

    ##### creating the resultfile

    shipfilename = shipclass+'results.xlsx'
    relpath = os.path.abspath(os.path.dirname(__file__))
    resultfile = relpath+"\\"+shipfilename

    if os.path.exists(resultfile):
        os.remove(resultfile)
        print('deleted '+resultfile)
    print('creating file with xlswriter')
    xlswritership = xlsxwriter.Workbook(shipfilename)
    xlswritership.close()
    shipsheet = openpyxl.load_workbook(shipfilename)
    firstsheet = shipsheet['Sheet1']
    firstsheet.title = 'BattleLevel 1'
    shipsheet.create_sheet('BattleLevel 2')
    shipsheet.create_sheet('BattleLevel 3')
    shipsheet.create_sheet('BattleLevel 4')
    shipsheet.create_sheet('BattleLevel 5')
    shipsheet.create_sheet('BattleLevel 6')
    shipsheet.create_sheet('BattleLevel 7')
    shipsheet.create_sheet('BattleLevel 8')
    shipsheet.create_sheet('BattleLevel 9')
    shipsheet.create_sheet('BattleLevel 10')
    #resultfile created

    ##### setting the top row of each sheet

    toprow = [
        'shipName',
        'shipClass',
        'shipCountry',
        'shipTier',
        'shipCurrency',
        'shipCost',
        'shipHitPoints',
        'shipResearch',
        'shipMainGun',
        'shipMainGunCount',
        'shipMainGunROF',
        'shipMainGunReload',
        'shipMainGunRotate',
        'shipMainGun180',
        'shipMainGunRange',
        'shipMainGunDispersion',
        'shipMainGunHEShell',
        'shipMainGunHEDam',
        'shipMainGunHEFire',
        'shipMainGunHEVel',
        'shipMainGunHEWeight',
        'shipMainGunAPShell',
        'shipMainGunAPDam',
        'shipMainGunAPVel',
        'shipMainGunAPWeight'
    ]
    for sheet in shipsheet.worksheets:
        # print(toprow)
        for n in range(1,len(toprow)+1):
            sheet.cell(row=1,column=n).value = toprow[n-1]

    #testing the root site

    # rootres = requests.get(rootsite)
    # rootres.raise_for_status()
    # rootSoup = bs4.BeautifulSoup(rootres.text)

    ##TODO - turn this into something that isn't restricted to DD
    #get destroyer site
    pagereq = requests.get(shipsite)
    pagereq.raise_for_status()
    pagesoup = bs4.BeautifulSoup(pagereq.text, "html.parser")

    #print japanese DD names
    nationDivs = pagesoup.find_all("div",class_="wot-frame-1")

    for nation in nationDivs:

        print('====================')
        print(nation.find("h2").text)
        print('====================')

        nationShips = nation.find_all("div",class_="tleft")
        for thisShip in nationShips:
            fulllink = 'http://wiki.wargaming.net'+(thisShip.find_all("a"))[1].attrs['href']
            getshipname = (thisShip.find_all("a"))[1].text
            print('regex time')
            if (re.search("St.*Louis",getshipname)) or (re.search("Nueve.de.Julio",getshipname)):
                print('found it')
            else:

                shiparray = {}

                shiparray['shipName'] = getshipname

                print('----- ' + shiparray['shipName'])
                # print(fulllink)
                thisShipPage = requests.get(fulllink)
                thisShipPage.raise_for_status()
                thisShipSoup = bs4.BeautifulSoup(thisShipPage.text, "html.parser")

                #gets class, country, tier
                perfdiv = thisShipSoup.find('div',class_='b-performance_position')
                shiparray['shipClass'] = perfdiv.text.split(' | ')[0]
                shiparray['shipCountry'] = perfdiv.text.split(' | ')[1]
                shiparray['shipTier'] = perfdiv.text.split(' | ')[2]
                # print(shiparray)
                #gets general, armament, toobs, maneuverability, concealment
                shipstats = thisShipSoup.find('div',class_='b-performance_border').find_all('div',class_='gw-popup-card b-tech-nav_item__opened')
                for statgroup in shipstats:
                    groupName = statgroup.find('div',class_='b-performance_title gw-popup-card_head js-tech-nav_head').text
                    # print('--')
                    # print(groupName)
                    # print('--')
                    stats = statgroup.find('div',class_='gw-popup-card_content').find_all('tr')

                    ###GENERAL STAT GROUP

                    if groupName == 'General':

                        # shipCurrency = ''
                        # shipCost = ''
                        # shipHitPoints = ''
                        # shipResearch = ''
                        for stat in stats:
                            # print('--------------')
                            ##TODO - strip whitespace from variables
                            if stat.find('span',class_='t-performance_left').text == 'Purchase price':
                                shiparray['shipCurrency'] = stat.find('img')['alt']
                                shiparray['shipCost'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Hit Points':
                                shiparray['shipHitPoints'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Research price':
                                shiparray['shipResearch'] = stat.find('span',class_='t-performance_right').text

                        # if shipCost != 'promo':
                        #     print('Currency: '+shipCurrency)
                        # print('Cost: '+shipCost)
                        # print('HP: '+shipHitPoints)
                        # if shipResearch != '':
                        #     print('ResearchXP: '+shipResearch)
                        #print(stats)

                    ###MAIN BATTERY

                    elif groupName == 'Main Battery':
                        # shipMainGun = ''
                        # shipMainGunCount = ''
                        # shipMainGunROF = ''
                        # shipMainGunReload = ''
                        # shipMainGunRotate = ''
                        # shipMainGun180 = ''
                        # shipMainGunRange = ''
                        # shipMainGunDispersion = ''
                        # shipMainGunHEShell = ''
                        # shipMainGunHEDam = ''
                        # shipMainGunHEFire = ''
                        # shipMainGunHEVel = ''
                        # shipMainGunHEWeight = ''
                        # shipMainGunAPShell = ''
                        # shipMainGunAPDam = ''
                        # shipMainGunAPVel = ''
                        # shipMainGunAPWeight = ''

                        shiparray['shipMainGun'] = stats[0].find('span',class_='t-performance_left').text
                        shiparray['shipMainGunCount'] = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shiparray['shipMainGunROF'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shiparray['shipMainGunReload'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shiparray['shipMainGunRotate'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shiparray['shipMainGun180'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shiparray['shipMainGunRange'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shiparray['shipMainGunDispersion'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shiparray['shipMainGunHEShell'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shiparray['shipMainGunHEDam'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shiparray['shipMainGunHEFire'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shiparray['shipMainGunHEVel'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shiparray['shipMainGunHEWeight'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shiparray['shipMainGunAPShell'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shiparray['shipMainGunAPDam'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shiparray['shipMainGunAPVel'] = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shiparray['shipMainGunAPWeight'] = stat.find('span',class_='t-performance_right').text

                        # print(shipMainGun)
                        # print(shipMainGunCount)
                        # print(shipMainGunROF)
                        # print(shipMainGunReload)
                        # print(shipMainGunRotate)
                        # print(shipMainGun180)
                        # print(shipMainGunRange)
                        # print(shipMainGunDispersion)
                        # print(shipMainGunHEShell)
                        # print(shipMainGunHEDam)
                        # print(shipMainGunHEFire)
                        # print(shipMainGunHEVel)
                        # print(shipMainGunHEWeight)
                        # print(shipMainGunAPShell)
                        # print(shipMainGunAPDam)
                        # print(shipMainGunAPVel)
                        # print(shipMainGunAPWeight)

                    ###Secondary Batteries

                    elif groupName == 'Secondary Armament #1':
                        shipSec1Gun = ''
                        shipSec1GunCount = ''
                        shipSec1GunROF = ''
                        shipSec1GunReload = ''
                        shipSec1GunRotate = ''
                        shipSec1Gun180 = ''
                        shipSec1GunRange = ''
                        shipSec1GunDispersion = ''
                        shipSec1GunHEShell = ''
                        shipSec1GunHEDam = ''
                        shipSec1GunHEFire = ''
                        shipSec1GunHEVel = ''
                        shipSec1GunHEWeight = ''
                        shipSec1GunAPShell = ''
                        shipSec1GunAPDam = ''
                        shipSec1GunAPVel = ''
                        shipSec1GunAPWeight = ''

                        shipSec1Gun = stats[0].find('span',class_='t-performance_left').text
                        shipSec1GunCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shipSec1GunROF = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shipSec1GunReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shipSec1GunRotate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shipSec1Gun180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shipSec1GunRange = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shipSec1GunDispersion = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shipSec1GunHEShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shipSec1GunHEDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shipSec1GunHEFire = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipSec1GunHEVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipSec1GunHEWeight = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipSec1GunAPShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipSec1GunAPDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipSec1GunAPVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipSec1GunAPWeight = stat.find('span',class_='t-performance_right').text

                    elif groupName == 'Secondary Armament #2':
                        shipSec2Gun = ''
                        shipSec2GunCount = ''
                        shipSec2GunROF = ''
                        shipSec2GunReload = ''
                        shipSec2GunRotate = ''
                        shipSec2Gun180 = ''
                        shipSec2GunRange = ''
                        shipSec2GunDispersion = ''
                        shipSec2GunHEShell = ''
                        shipSec2GunHEDam = ''
                        shipSec2GunHEFire = ''
                        shipSec2GunHEVel = ''
                        shipSec2GunHEWeight = ''
                        shipSec2GunAPShell = ''
                        shipSec2GunAPDam = ''
                        shipSec2GunAPVel = ''
                        shipSec2GunAPWeight = ''

                        shipSec2Gun = stats[0].find('span',class_='t-performance_left').text
                        shipSec2GunCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shipSec2GunROF = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shipSec2GunReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shipSec2GunRotate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shipSec2Gun180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shipSec2GunRange = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shipSec2GunDispersion = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shipSec2GunHEShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shipSec2GunHEDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shipSec2GunHEFire = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipSec2GunHEVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipSec2GunHEWeight = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipSec2GunAPShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipSec2GunAPDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipSec2GunAPVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipSec2GunAPWeight = stat.find('span',class_='t-performance_right').text

                    elif groupName == 'Secondary Armament #3':
                        shipSec3Gun = ''
                        shipSec3GunCount = ''
                        shipSec3GunROF = ''
                        shipSec3GunReload = ''
                        shipSec3GunRotate = ''
                        shipSec3Gun180 = ''
                        shipSec3GunRange = ''
                        shipSec3GunDispersion = ''
                        shipSec3GunHEShell = ''
                        shipSec3GunHEDam = ''
                        shipSec3GunHEFire = ''
                        shipSec3GunHEVel = ''
                        shipSec3GunHEWeight = ''
                        shipSec3GunAPShell = ''
                        shipSec3GunAPDam = ''
                        shipSec3GunAPVel = ''
                        shipSec3GunAPWeight = ''

                        shipSec3Gun = stats[0].find('span',class_='t-performance_left').text
                        shipSec3GunCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Rate of Fire':
                                shipSec3GunROF = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Reload Time':
                                shipSec3GunReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rotation Speed':
                                shipSec3GunRotate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == '180 Degree Turn Time':
                                shipSec3Gun180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Firing Range':
                                shipSec3GunRange = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Dispersion':
                                shipSec3GunDispersion = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell':
                                shipSec3GunHEShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum HE Shell Damage':
                                shipSec3GunHEDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Chance of Fire on Target Caused by HE Shell':
                                shipSec3GunHEFire = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipSec3GunHEVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipSec3GunHEWeight = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipSec3GunAPShell = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipSec3GunAPDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipSec3GunAPVel = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipSec3GunAPWeight = stat.find('span',class_='t-performance_right').text

                    ### Torpedo Tubes

                    elif groupName == 'Torpedo Tubes':
                        shipTorpTube = ''
                        shipTorpCount = ''
                        shipTorpReload = ''
                        shipTorpRorate = ''
                        shipTorp180 = ''
                        shipTorp = ''
                        shipTorpDam = ''
                        shipTorpSpeed = ''
                        shipTorpRange = ''

                        shipTorpTube = stats[0].find('span',class_='t-performance_left').text
                        shipTorpCount = stats[0].find('span',class_='t-performance_right').text

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Initial HE Shell Velocity':
                                shipTorpReload = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'HE Shell Weight':
                                shipTorpRorate = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell':
                                shipTorp180 = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Maximum AP Shell Damage':
                                shipTorp = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Initial AP Shell Velocity':
                                shipTorpDam = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipTorpSpeed = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'AP Shell Weight':
                                shipTorpRange = stat.find('span',class_='t-performance_right').text

                    elif groupName == 'AA Defense':
                        ####FUCKING AA GUNS
                        #### for each ship's count of AA guns, add them to an array. When it's time to print, take the highest number of AA guns and make that the number of AA guns in the sheet

                        ship0AA = ''
                        ship0AACount = ''
                        ship0AADps = ''
                        ship0AArange = ''
                        ship1AA = ''
                        ship1AACount = ''
                        ship1AADps = ''
                        ship1AArange = ''
                        ship2AA = ''
                        ship2AACount = ''
                        ship2AADps = ''
                        ship2AArange = ''
                        ship3AA = ''
                        ship3AACount = ''
                        ship3AADps = ''
                        ship3AArange = ''
                        ship4AA = ''
                        ship4AACount = ''
                        ship4AADps = ''
                        ship4AArange = ''

                        class AAgun:
                            gun = ""
                            guncount = ""
                            gunrange = ""
                            gundps = ""

                            def __init__(self,gunname,count,grange,dps):
                                self.gun = gunname
                                self.guncount = count
                                self.gunrange = grange
                                self.gundps = dps

                            def calculatedps(self):
                                turrets = self.guncount.split('x')[0]
                                barrelsper = self.guncount.split('x')[1]
                                totaldps = float(turrets)*float(barrelsper)*float(self.gundps)
                                return totaldps
                            def print (self):
                                print("this is my fun gun")
                                print(self.gun)
                                print(self.guncount)
                                print(self.gunrange)
                                print(self.gundps)

                        shipAAguns = []
                        statcount = range(0,len(stats),1)
                        thegun = ""
                        thecount = ""
                        therange = ""
                        thedps = ""
                        for i in statcount:
                            # print(i)
                            if i%3 == 0:
                                # print(str(i)+"%3=0")
                                # print("turn this into the gun name and count")
                                thegun = stats[0].find('span',class_='t-performance_left').text
                                thecount = stats[0].find('span',class_='t-performance_right').text.replace("pcs.","").replace(' ','')
                            if i%3 ==1:
                                # print(str(i)+"%3=1")
                                # print("this is going to be the dps")
                                thedps = stats[1].find('span',class_='t-performance_right').text
                            if i%3 == 2:
                                # print(str(i)+"%3=2")
                                # print("this is going to be the range")
                                therange = stats[2].find('span',class_='t-performance_right').text
                                newgun = AAgun(thegun,thecount,thedps,therange)
                                shipAAguns.append(newgun)
                                thegun = ""
                                thecount = ""
                                thedps = ""
                                therange = ""
                        # for gun in shipAAguns:
                            # gun.print()

                        # for stat in stats:
                        #     print(stat)
                        #     # if line doesn't start with '...'

                    ##TODO - multiple AA Defense

                    ### Maneuverability

                    elif groupName == 'Maneuverability':
                        shipSpeed = ''
                        shipTurnRadius = ''
                        shipRudderShift = ''

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Maximum Speed':
                                shipSpeed = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Turning Circle Radius':
                                shipTurnRadius = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Rudder Shift Time':
                                shipRudderShift = stat.find('span',class_='t-performance_right').text

                    ### Concealment

                    elif groupName == 'Concealment':
                        shipSurfaceDetect = ''
                        shipAirDetect = ''

                        for stat in stats:
                            if stat.find('span',class_='t-performance_left').text == 'Surface Detectability Range':
                                shipSurfaceDetect = stat.find('span',class_='t-performance_right').text
                            if stat.find('span',class_='t-performance_left').text == 'Air Detectability Range':
                                shipAirDetect = stat.find('span',class_='t-performance_right').text

                shipBattleLevels = ""
                for shipBattleLevel in thisShipSoup.find('span',class_='b-battles-levels_interval'):
                    if shipBattleLevels == "":
                        shipBattleLevels = shipBattleLevel.text
                    else:
                        shipBattleLevels = shipBattleLevels + "," + shipBattleLevel.text
                print("levels:"+shipBattleLevels)
                for level in shipBattleLevels.split(','):
                    # print('battle level '+level)
                    thesheet = shipsheet['BattleLevel '+level]
                    thisrow = thesheet.max_row+1
                    # print('next free row is '+str(thisrow))
                    for shipvalue in shiparray:

                        # print('shipvalue we are writing is '+shipvalue)

                        for i in range(1,thesheet.max_column+1):
                            if thesheet.cell(row=1,column=i).value == shipvalue:
                                # print('the column for this value is '+str(i))
                                # print('saving ')
                                thesheet.cell(row=thisrow,column=i).value = shiparray[shipvalue]

                # input("Press a key to continue...")
    shipsheet.save(shipfilename)


import xml.etree.ElementTree as ElementTree
import numpy as np
import cv2
# from xmlDictConfig import XmlDictConfig
from utils.dataframe import XmlDictConfig

def get_bndbox(tree, image_size=None):
    root = tree.getroot()
    xmldict = XmlDictConfig(root)
    bndbox = []
    classes = []
    if type(xmldict['object']) == type([]):
        for elem in xmldict['object']:
            xmin,ymin,xmax,ymax = elem['bndbox']['xmin'],elem['bndbox']['ymin'],elem['bndbox']['xmax'],elem['bndbox']['ymax']
            class_name = elem['name']
            bndbox.append((int(xmin), int(ymin), int(xmax), int(ymax)))
            classes.append(class_name)
    else:
        xmin,ymin,xmax,ymax = xmldict['object']['bndbox']['xmin'],xmldict['object']['bndbox']['ymin'],xmldict['object']['bndbox']['xmax'],xmldict['object']['bndbox']['ymax']
        class_name = xmldict['object']['name']
        bndbox.append((int(xmin), int(ymin), int(xmax), int(ymax)))
        classes.append(class_name)

    if image_size is not None:
        scaled_boxes = []
        for box in bndbox:
            scaled_boxes.append(scale_bndbox(tree=tree, bndbox=box, image_size=image_size))
        return scaled_boxes, classes
    else:
        return bndbox, classes

def update_bndbox(tree, bndbox):
    root = tree.getroot()
    object_count = len(bndbox)
    for i in range(object_count):
        xmin, ymin, xmax, ymax = bndbox[i]
        root[6+i][4][0].text = str(xmin)
        root[6+i][4][1].text = str(ymin)
        root[6+i][4][2].text = str(xmax)
        root[6+i][4][3].text = str(ymax)

def draw_bndbox(img, xmin, ymin, xmax, ymax, cls, color):
    cv2.rectangle(img=img, pt1=(xmin,ymin), pt2=(xmax,ymax), color=color, thickness=1)
    font                   = cv2.FONT_HERSHEY_SIMPLEX
    bottomLeftCornerOfText = (int(xmin),int(ymin-2))
    fontScale              = 0.2
    fontColor              = (255,255,255)
    lineType               = 1

    if cls == 1:
        label = '1'
    elif cls == 2:
        label = '2'
    elif cls == 3:
        label = '3'
    elif cls == 4:
        label = '4'
    cv2.putText(img, label, bottomLeftCornerOfText, font, fontScale, fontColor, lineType)

def iou(box_a, box_b):
    x_a = max(box_a[0], box_b[0])
    y_a = max(box_a[1], box_b[1])
    x_b = min(box_a[2], box_b[2])
    y_b = min(box_a[3], box_b[3])
    intersection = max(0, x_b - x_a) * max(0,y_b - y_a)
    area_a = (box_a[2] - box_a[0]) * (box_a[3] - box_a[1])
    area_b = (box_b[2] - box_b[0]) * (box_b[3] - box_b[1])
    return intersection / float(area_a + area_b - intersection)

def rotate_bounding_box(tree, M):
    bndbox,_ = get_bndbox(tree, )
    new_bndbox = []
    for elem in bndbox:
        xmin, ymin, xmax, ymax = elem

        x1,y1 = xmin, ymin
        x2,y2 = xmin, ymax
        x3,y3 = xmax, ymax
        x4,y4 = xmax, ymin

        corners = np.hstack((x1,y1,x2,y2,x3,y3,x4,y4))
        corners = corners.reshape(-1,2)
        corners = np.hstack((corners, np.ones((corners.shape[0],1), dtype=type(corners[0][0]))))

        calculated = np.dot(M,corners.T).T
        calculated = calculated.reshape(-1,8)

        x_ = calculated[:,[0,2,4,6]]
        y_ = calculated[:,[1,3,5,7]]

        xmin_new = int(np.min(x_,1).reshape(-1,1))
        ymin_new = int(np.min(y_,1).reshape(-1,1))
        xmax_new = int(np.max(x_,1).reshape(-1,1))
        ymax_new = int(np.max(y_,1).reshape(-1,1))

        new_bndbox.append((xmin_new, ymin_new, xmax_new, ymax_new))

    update_bndbox(tree, new_bndbox)

def scale_bndbox(tree, bndbox, image_size):
    img_h, img_w = image_size
    root = tree.getroot()
    current_w = int(root[4][0].text)
    current_h = int(root[4][1].text)
    current_channels = int(root[4][2].text)

    ratio_w = img_w / current_w
    ratio_h = img_h / current_h
    #bndboxes = get_bndbox(tree)
    #new_bndbox = []
    xmin, ymin, xmax, ymax = bndbox
    scaled_xmin = xmin*ratio_w
    scaled_xmax = xmax*ratio_w
    scaled_ymin = ymin*ratio_h
    scaled_ymax = ymax*ratio_h
    #gt_box = [int(i) for i in gt_box]
    #new_bndbox.append((scaled_xmin, scaled_ymin, scaled_xmax, scaled_ymax))
    return [int(scaled_xmin), int(scaled_ymin), int(scaled_xmax), int(scaled_ymax)]
    #update_bndbox(tree, new_bndbox)


import asyncio
import websockets
import json
import pprint
from source.services.print_to_console import dashed_printer
from time import time
from source.settings.configurations import PAYLOAD_FILE_PATH, PAYLOAD_MEASUREMENT_SERVER_PORT, \
    PAYLOAD_MEASUREMENT_SERVER_ADDRESS

counter = 0
packages_to_write = []
time_length_of_probe = 60
time_to_finish = 0
socked_connected = False
time_0 = None

async def handler(websocket, _):
    global counter
    global packages_to_write
    global time_0
    global time_length_of_probe
    global socked_connected
    global time_to_finish
    if not socked_connected:
        time_0 = time()
        time_to_finish = int(time_0) + time_length_of_probe
        socked_connected = True
    time_counter = time()
    if time_counter < time_to_finish:
        with open(PAYLOAD_FILE_PATH, 'a') as f:
            payload = await websocket.recv()
            time_1 = time()
            if payload:
                counter += 1
                data_package = json.loads(payload)
                frame_time_separation = time_1 - time_0
                time_0 = time_1
                dashed_printer('Payload frame number {}'.format(counter))
                dashed_printer(' Received package format:')
                pprint.pprint(data_package)
                print('Package time separation is: {}'.format(frame_time_separation))
                dashed_printer(' -- -- -- end of payload frame -- -- --')
                packages_to_write.append({
                    "time": time(),
                    "frame": payload,
                    "frame_time_separation": frame_time_separation
                })
                if counter % 30 == 0:
                    dashed_printer('Writing to a file')
                    f.write(str(json.dumps(packages_to_write)))
                    f.write('\n')
                    del packages_to_write[:]
    else:
        if len(packages_to_write) > 0:
            with open(PAYLOAD_FILE_PATH, 'a') as f:
                dashed_printer('Writing to a file')
                f.write(str(json.dumps(packages_to_write)))
                f.write('\n')
                del packages_to_write[:]
            dashed_printer('PROBE TIME IS REACHED, WRITING TO A FILE IS STOPPED, YOU CAN STOP THE SERVER WITH ctrl + c')

if __name__ == '__main__':
    time_length_of_probe = int(input('Specify time span of this probe: '))
    asyncio.get_event_loop().run_until_complete(
        websockets.serve(handler, PAYLOAD_MEASUREMENT_SERVER_ADDRESS, PAYLOAD_MEASUREMENT_SERVER_PORT))
    asyncio.get_event_loop().run_forever()


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import time as tm
import math
import sys
import pickle as pkl
import matplotlib.pyplot as plt
from numpy import linalg as LA
from MMNet_base import *
from HyperNetwork_base import *

class MMNet(nn.Module):

    def __init__(self, num_layers, NT, NR, constellation, device='cuda'):
        super(MMNet, self).__init__()
        self.num_layers = num_layers
        self.device = device
        self.MMNetbase = MMNet_base(NT, NR, constellation, self.device)
        self.NT = NT
        self.NR = NR
        self.HyperNet = HyperNetwork(self.NT,self.NR, device)

    def forwardHyperNet(self, H):
        batch_size = H.shape[0]

        Theta = self.HyperNet.forward(H)
        Theta = torch.reshape(Theta, (batch_size, self.num_layers,  2 * self.NT + (2 * self.NT ) * (2 * self.NR)) )
        W_theta = Theta[:,:,:-(2 * self.NT)]
        theta_vec = Theta[:,:,-(2 * self.NT):]

        return W_theta, theta_vec

    def forward(self, H, y, noise_sigma):
        batch_size = H.shape[0]
        x_size = H.shape[-1]

        x_prev = torch.zeros(batch_size, x_size, dtype=torch.float64).to(device=self.device)
        x_list=[x_prev]
        Theta = self.HyperNet.forward(H)
        Theta = torch.reshape(Theta, (batch_size, self.num_layers,  2 * self.NT + (2 * self.NT ) * (2 * self.NR)) )
        W_theta = Theta[:,:,:-(2 * self.NT)]
        theta_vec = Theta[:,:,-(2 * self.NT):]

        for index in range(self.num_layers):
            xout = self.MMNetbase.forward(H, W_theta[:,index,:], theta_vec[:,index,:], y, x_list[-1].float(), noise_sigma, batch_size)
            x_list.append(xout.double())

        return (x_list[1:])


# This file is part of beets.
# Copyright 2011, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Facilities for automatically determining files' correct metadata.
"""
import os
import logging
import re

from beets import library, mediafile
from beets.util import sorted_walk, ancestry

# Parts of external interface.
from .hooks import AlbumInfo, TrackInfo
from .match import AutotagError
from .match import tag_item, tag_album
from .match import RECOMMEND_STRONG, RECOMMEND_MEDIUM, RECOMMEND_NONE
from .match import STRONG_REC_THRESH, MEDIUM_REC_THRESH, REC_GAP_THRESH

# Global logger.
log = logging.getLogger('beets')

# Constants for directory walker.
MULTIDISC_MARKERS = (r'part', r'volume', r'vol\.', r'disc', r'cd')
MULTIDISC_PAT_FMT = r'%s\s*\d'

# Additional utilities for the main interface.

def albums_in_dir(path, ignore=()):
    """Recursively searches the given directory and returns an iterable
    of (path, items) where path is a containing directory and items is
    a list of Items that is probably an album. Specifically, any folder
    containing any media files is an album. Directories and file names
    that match the glob patterns in ``ignore`` are skipped.
    """
    collapse_root = None
    collapse_items = None

    for root, dirs, files in sorted_walk(path, ignore):
        # Get a list of items in the directory.
        items = []
        for filename in files:
            try:
                i = library.Item.from_path(os.path.join(root, filename))
            except mediafile.FileTypeError:
                pass
            except mediafile.UnreadableFileError:
                log.warn('unreadable file: ' + filename)
            else:
                items.append(i)

        # If we're collapsing, test to see whether we should continue to
        # collapse. If so, just add to the collapsed item set;
        # otherwise, end the collapse and continue as normal.
        if collapse_root is not None:
            if collapse_root in ancestry(root):
                # Still collapsing.
                collapse_items += items
                continue
            else:
                # Collapse finished. Yield the collapsed directory and
                # proceed to process the current one.
                if collapse_items:
                    yield collapse_root, collapse_items
                collapse_root = collapse_items = None

        # Does the current directory look like a multi-disc album? If
        # so, begin collapsing here.
        if dirs and not items: # Must be only directories.
            multidisc = False
            for marker in MULTIDISC_MARKERS:
                pat = MULTIDISC_PAT_FMT % marker
                if all(re.search(pat, dirname, re.I) for dirname in dirs):
                    multidisc = True
                    break

            # This becomes True only when all directories match a
            # pattern for a single marker.
            if multidisc:
                # Start collapsing; continue to the next iteration.
                collapse_root = root
                collapse_items = []
                continue

        # If it's nonempty, yield it.
        if items:
            yield root, items

    # Clear out any unfinished collapse.
    if collapse_root is not None and collapse_items:
        yield collapse_root, collapse_items

def apply_item_metadata(item, track_info):
    """Set an item's metadata from its matched TrackInfo object.
    """
    item.artist = track_info.artist
    item.title = track_info.title
    item.mb_trackid = track_info.track_id
    if track_info.artist_id:
        item.mb_artistid = track_info.artist_id
    # At the moment, the other metadata is left intact (including album
    # and track number). Perhaps these should be emptied?

def apply_metadata(items, album_info):
    """Set the items' metadata to match an AlbumInfo object. The list
    of items must be ordered.
    """
    for index, (item, track_info) in enumerate(zip(items, album_info.tracks)):
        # Album, artist, track count.
        if not item:
            continue
        if track_info.artist:
            item.artist = track_info.artist
        else:
            item.artist = album_info.artist
        item.albumartist = album_info.artist
        item.album = album_info.album
        item.tracktotal = len(items)

        # Release date.
        if album_info.year:
            item.year = album_info.year
        if album_info.month:
            item.month = album_info.month
        if album_info.day:
            item.day = album_info.day

        # Title and track index.
        item.title = track_info.title
        item.track = index + 1

        # Disc and disc count.
        item.disc = track_info.medium
        item.disctotal = album_info.mediums

        # MusicBrainz IDs.
        item.mb_trackid = track_info.track_id
        item.mb_albumid = album_info.album_id
        if track_info.artist_id:
            item.mb_artistid = track_info.artist_id
        else:
            item.mb_artistid = album_info.artist_id
        item.mb_albumartistid = album_info.artist_id
        item.albumtype = album_info.albumtype
        if album_info.label:
            item.label = album_info.label

        # Compilation flag.
        item.comp = album_info.va


# -*- coding: utf-8 -*-
"""
deepecoach
~~~~~~~~~~~~~~~~~~~

Deep neural approach to Boundary and Disfluency Detection.

:copyright: (c) 2017 by Marcos Treviso
:licence: MIT, see LICENSE for more details
"""
from __future__ import absolute_import, unicode_literals
import logging

# Generate your own AsciiArt at:
# patorjk.com/software/taag/#f=Calvin%20S&t=deepecoach
__banner__ = r"""
╔╦╗┌─┐┌─┐┌─┐
 ║║├┤ ├┤ ├─┘ by Mehedi Hasan
═╩╝└─┘└─┘┴
"""

__prog__ = "deepecoach"
__title__ = 'DeepEcoach'
__summary__ = 'Deep neural approach for text segmentation'
__uri__ = 'https://github.com/teanalab/eCoaching-Text-Segmentation'

__version__ = '0.0.1-alpha'

__author__ = 'Mehedi Hasan'
__email__ = 'mehedi@wayne.edu'

__license__ = 'MIT'
__copyright__ = 'Copyright 2018 Mehedi Hasan'

# the user should dictate what happens when a logging event occurs
logging.getLogger(__name__).addHandler(logging.NullHandler())

# imports
from . import error_analysis
from . import train
from .pipeline import Pipeline


from chainer import cuda
import numpy as np
from dqn import environment
from dqn.agent import create_agent
import argparse

def run():
    parser = argparse.ArgumentParser()
    parser.add_argument('--comment', '-c', default='', type=str, help='comment to distinguish output')
    parser.add_argument('--gpu', '-g', default= -1, type=int, help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--net_type', '-n', type=str, default="full_connect", help='network type')
    args = parser.parse_args()

    comment = args.comment
    gpu = args.gpu
    log_path = './result/{}/log/test.log'.format(comment)

    if gpu >= 0:
    	cuda.get_device(gpu).use()

    actions = ["up", "down", "right", "left"]
    n_actions = len(actions)
    agent = create_agent(n_actions, gpu, exp_policy='const0.0', decay_steps=1,
    							q_update_freq=1, fixed_q_update_freq=1,
    							memory_size=100, net_type=args.net_type, discount=0)
    agent.load('./result/{}/network/agent_final/'.format(comment))
    env = environment.Environment(1000, comment, test=True)

    with open(log_path, 'w') as f:
        pass

    episode_pics = []
    for i in range(10000):
        obs = env.reset()
        done = False
        R = 0
        t = 0
        while not done and t < 10:
            action = agent.act(obs)
            #action_value = agent.model(agent.batch_states([obs], agent.xp, agent.phi))
            #print(action_value)
            obs, r, done, _ = env.step(action)
            R += r
            t += 1
            if r == 1:
                episode_pics.append(env.episode_pics)

        with open(log_path, 'a') as f:
            f.write('test episode: {}, R: {}, step: {}\n'.format(i, R, t))
        print('test episode: {}, R: {}, step: {}'.format(i, R, t))

        agent.stop_episode()
    np.save('./result/{}/episode_pics.npy'.format(comment), np.array(episode_pics))

    #c = 0
    #for i, obs in enumerate(env.pics[9]):
    #    action_value = agent.model(agent.batch_states([obs], agent.xp, agent.phi))
    #    print(i, actions[cuda.to_cpu(action_value.greedy_actions.data)[0]], action_value)
    #    if actions[cuda.to_cpu(action_value.greedy_actions.data)[0]] == 'up':
    #        c += 1
    #print(c)

if __name__ == '__main__':
    run()


from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, mixins, status
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

from notice.models import MainPageDodExplanation, FAQLink, NoticeLink, SuggestionLink, PrivacyPolicyLink, \
    TermsOfServiceLink, ContactLink
from notice.serializers import DodExplanationSerializer, ThirdPartyMenuListSerializer, FAQMenuSerializer, \
    NoticeMenuSerializer, SuggestionMenuSerializer, ContactMenuSerializer

class DodExplanationAPIView(viewsets.GenericViewSet,
                            mixins.ListModelMixin,):
    permission_classes = [AllowAny]
    queryset = MainPageDodExplanation.objects.filter(is_active=True).order_by('order')
    serializer_class = DodExplanationSerializer
    pagination_class = None
    """
    대시보드 없는 메인페이지의 dod 설명 api 입니다.
    """
    def list(self, request, *args, **kwargs):
        """
        api : api/v1/dod-explanation/
        """

        return super(DodExplanationAPIView, self).list(request, args, kwargs)

class ThirdPartyMenuListAPIView(viewsets.GenericViewSet,
                                mixins.ListModelMixin):
    queryset = None
    serializer_class = None
    permission_classes = [IsAuthenticated]

    def list(self, request, *args, **kwargs):
        faq = None if not FAQLink.objects.filter(is_active=True).exists()\
            else FAQLink.objects.filter(is_active=True).last()

        notice = None if not NoticeLink.objects.filter(is_active=True).exists()\
            else NoticeLink.objects.filter(is_active=True).last()

        suggestion = None if not SuggestionLink.objects.filter(is_active=True).exists()\
            else SuggestionLink.objects.filter(is_active=True).last()

        contact = None if not ContactLink.objects.filter(is_active=True).exists() \
            else ContactLink.objects.filter(is_active=True).last()

        menu_list = [faq, contact, suggestion, notice]
        menu_data = []
        for menu in menu_list:
            if menu:
                menu_data.append(self._meta_serializer(menu))

        return Response(menu_data, status=status.HTTP_200_OK)

    def _meta_serializer(self, obj):
        val = {'icon_src': obj.icon.url,
               'link': obj.link}
        obj_name = obj._meta.model_name
        if obj_name == 'faqlink':
            title = '자주 묻는 질문'
        elif obj_name == 'contactlink':
            title = '문의·상담하기'
        elif obj_name == 'suggestionlink':
            title = '건의하기'
        elif obj_name == 'noticelink':
            title = '공지사항'
        else:
            title = None
        val['title'] = title
        return val


#!/usr/bin/env python
# Copyright 2014 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.

import sys
import unittest

from test_support import test_env
test_env.setup_test_env()

from google.appengine.ext import ndb

from components.datastore_utils import mapping
from test_support import test_case

class EntityX(ndb.Model):
  a = ndb.IntegerProperty()

def int_ceil_div(value, divisor):
  """Returns the ceil() value of a integer based division."""
  return (value + divisor - 1) / divisor

class MappingTest(test_case.TestCase):
  def test_pop_future(self):
    items = [ndb.Future() for _ in xrange(5)]
    items[1].set_result(None)
    items[3].set_result('foo')
    inputs = items[:]
    mapping.pop_future_done(inputs)
    self.assertEqual([items[0], items[2], items[4]], inputs)

  def test_page_queries(self):
    for i in range(40):
      EntityX(id=i, a=i%4).put()
    queries = [
      EntityX.query(),
      EntityX.query(EntityX.a == 1),
      EntityX.query(EntityX.a == 2),
    ]
    actual = list(mapping.page_queries(queries))

    # The order won't be deterministic. The only important this is that exactly
    # all the items are returned as chunks.
    expected = [
      [EntityX(id=i, a=1) for i in xrange(1, 40, 4)],
      [EntityX(id=i, a=2) for i in xrange(2, 42, 4)],
      [EntityX(id=i, a=i%4) for i in xrange(1, 21)],
      [EntityX(id=i, a=i%4) for i in xrange(21, 40)],
    ]
    self.assertEqual(len(expected), len(actual))
    for line in actual:
      # Items may be returned out of order.
      try:
        i = expected.index(line)
      except ValueError:
        self.fail('%s not found in %s' % (line, actual))
      self.assertEqual(expected.pop(i), line)

  def test_incremental_map(self):
    for i in range(40):
      EntityX(id=i, a=i%4).put()
    queries = [
      EntityX.query(),
      EntityX.query(EntityX.a == 1),
      EntityX.query(EntityX.a == 2),
    ]
    actual = []
    # Use as much default arguments as possible.
    mapping.incremental_map(queries, actual.append)

    # The order won't be deterministic. The only important this is that exactly
    # all the items are returned as chunks and there is 3 chunks.
    expected = sorted(
        [EntityX(id=i, a=1) for i in xrange(1, 40, 4)] +
        [EntityX(id=i, a=2) for i in xrange(2, 42, 4)] +
        [EntityX(id=i, a=i%4) for i in xrange(1, 21)] +
        [EntityX(id=i, a=i%4) for i in xrange(21, 40)],
        key=lambda x: (x.key.id, x.to_dict()))
    map_page_size = 20
    self.assertEqual(int_ceil_div(len(expected), map_page_size), len(actual))
    actual = sorted(sum(actual, []), key=lambda x: (x.key.id, x.to_dict()))
    self.assertEqual(expected, actual)

  def test_incremental_map_throttling(self):
    for i in range(40):
      EntityX(id=i, a=i%4).put()
    queries = [
      EntityX.query(),
      EntityX.query(EntityX.a == 1),
      EntityX.query(EntityX.a == 2),
    ]
    actual = []
    def map_fn(items):
      actual.extend(items)
      # Note that it is returning more Future than what is called. It's fine.
      for _ in xrange(len(items) * 5):
        n = ndb.Future('yo dawg')
        # TODO(maruel): It'd be nice to not set them completed right away to
        # have better code coverage but I'm not sure how to do this.
        n.set_result('yo')
        yield n

    def filter_fn(item):
      return item.a == 2

    mapping.incremental_map(
        queries=queries,
        map_fn=map_fn,
        filter_fn=filter_fn,
        max_inflight=1,
        map_page_size=2,
        fetch_page_size=3)

    # The order won't be deterministic so sort it.
    expected = sorted(
        [EntityX(id=i, a=2) for i in xrange(2, 42, 4)] * 2,
        key=lambda x: (x.key.id, x.to_dict()))
    actual.sort(key=lambda x: (x.key.id, x.to_dict()))
    self.assertEqual(expected, actual)

if __name__ == '__main__':
  if '-v' in sys.argv:
    unittest.TestCase.maxDiff = None
  unittest.main()


#!/usr/local/bin/python3.4

import sys
import re
import sqlparse
import string
from sqlparse.sql import IdentifierList, Identifier
from sqlparse.tokens import Keyword, DML,DDL,Whitespace,Newline,String

class sqlParser:

    def __init__(self):
        self.fromList = []
        self.toList = []
        self.oddList = []
        self.db ={'agg','crawler','dds','pgtemp','rpt','hivetemp','logs','ods','pag','shots'}

    def is_subselect(self,parsed):
        if not parsed.is_group():
            return False
        for item in parsed.tokens:
            if item.ttype is DML and item.value.upper() == 'SELECT':
                return True
            if self.is_subselect(item):
                return True
        return False

    # return if is a sub_group other than "SELECT"
    def is_subchange(self,parsed):
        if parsed.is_group():
            for item in parsed.tokens:
                if (item.ttype is DML or item.ttype is DDL) and item.value.upper()!='SELECT':
                    return True
                if self.is_subchange(item):
                    return True
        return False

    def find_keyword(self,sql,keyword):
        keyword = keyword.upper()
        keyword = re.compile(r'\b%s\b' %keyword)
        match = re.search(keyword,sql)
        if match:
            return match.start()
        else:
            return -1

    def extract_to_part(self,parsed):
        if self.is_subchange(parsed.tokens[0]):
            for x in self.extract_to_part(parsed.tokens[0]):
                yield x
        to_seen = False
        for item in parsed.tokens:
            if to_seen:
                if item is None:
                    raise StopIteration
                elif item.ttype is Whitespace or item.ttype is Newline:
                    pass
                elif item.ttype is Keyword and item.value.upper() == 'IF':
                    post_item = parsed.token_next(parsed.token_index(item))
                    if post_item is not None and post_item.ttype is Keyword and post_item.value.upper() == 'EXISTS':
                        to_seen = False
                        yield parsed.token_next(parsed.token_index(post_item))
                elif item.ttype is Keyword and item.value.upper() == 'TABLE':
                    to_seen = False
                    yield parsed.token_next(parsed.token_index(item))
                else:
                    to_seen = False
                    yield item
            elif item.ttype is Keyword and re.match(r"(^.*TABLE$)|(^.*INTO$)",item.value.upper()):
                pre_item = parsed.token_prev(parsed.token_index(item))
                if pre_item.ttype is Keyword and re.match(r"(^.*TEMP$)",pre_item.value.upper()):
                        pass#the create temp table case,ignored..
                else:
                    to_seen = True

    def extract_from_part(self,parsed):
        from_seen = False
        for item in parsed.tokens:
            if self.is_subselect(item):
                for x in self.extract_from_part(item):
                    yield x

            if item.ttype is None:
                idx = self.find_keyword(str(item),'IN')
                if idx!=-1:
                    subsql = str(item)[idx+3:len(str(item))]
                    stmt = sqlparse.parse(subsql)[0]
                    for x in self.extract_from_part(stmt):
                        yield x
                idx = self.find_keyword(str(item),'EXISTS')
                if idx != -1:
                    subsql = str(item)[idx+6:len(str(item))]
                    stmt = sqlparse.parse(subsql)[0]
                    for x in self.extract_from_part(stmt):
                        yield x

            if from_seen:
                if self.is_subselect(item):
                    for x in self.extract_from_part(item):
                        yield x
                    from_seen = False
                elif item is None:
                    raise StopIteration
                elif item.ttype is Whitespace or item.ttype is Newline:
                    pass
                else:
                    yield item
                    from_seen = False
            elif item.ttype is Keyword and re.match(r"(^.*FROM$)|(^.*JOIN$)|(\bUSING\b)",item.value.upper()):
                pre_item = parsed.token_prev(parsed.token_index(item))
                if pre_item.ttype is DML and re.match(r"(^.*DELETE$)",pre_item.value.upper()):
                    self.oddList.append(parsed.token_next(parsed.token_index(item)).value)#the delete from case,put the delete table to oddList
                else:
                    from_seen = True

    def extract_table_names(self,token_stream):
        for item in token_stream:
            if isinstance(item,IdentifierList):
                for identifier in item.get_identifiers():
                    tables = str.split(str(identifier))
                    for i in tables:
                        if re.match(r"(^.+\..+$)",str(i)):
                            if i[len(i)-1]=='(':
                                i = i[0:len(i)-1]
                            yield i
            else:
                tables = str.split(str(item))
                for i in tables:
                    if re.match(r"(^.+\..+$)",str(i)):
                        if i[len(i)-1]=='(':
                            i = i[0:len(i)-1]
                        yield i

    def extract_from_tables(self,stmt):
        streamFrom = self.extract_from_part(stmt)
        return list(self.extract_table_names(streamFrom))

    def extract_to_tables(self,stmt):
        streamTo = self.extract_to_part(stmt)
        return list(self.extract_table_names(streamTo))

    def extractSingleSql(self,sql):
        sql = sqlparse.format(sql,reindent=True,keyword_case='upper')
        strinfo = re.compile(r'\bAS\b')
        sql = strinfo.sub(' ON ',sql) #temp convert as to on since splparse lib is not split 'as' keyword well
        stmt = sqlparse.parse(sql)[0]
        self.fromList.extend(self.extract_from_tables(stmt))
        self.toList.extend(self.extract_to_tables(stmt))

    def validateTables(self,table):
        db = re.split('\W+',table)[0]
        if db in self.db:
            return True
        else:
            return False

    def extractSql(self,text):
        self.toList=[]
        self.fromList=[]
        self.oddList=[]
        sqls = sqlparse.split(text)
        for sql in sqls:
            if len(sql)>0:
                self.extractSingleSql(sql)

        self.toList.extend(self.oddList)
        self.fromList = list(set(self.fromList))
        self.toList = list(set(self.toList))
        self.fromList = list(filter(self.validateTables,self.fromList))
        self.toList = list(filter(self.validateTables,self.toList))
        return [self.fromList,self.toList]

if __name__ == '__main__':
    f = open(sys.argv[1],'r')
    sqls_text = f.read()
    x = sqlParser()
    print(x.extractSql(sqls_text))


from google.cloud import texttospeech

class GoogleSpeech:

    def __init__(self):
        self.client = texttospeech.TextToSpeechClient()

    def say(self, text):
        print("Google Say {}".format(text))
        synthesis_input = texttospeech.types.SynthesisInput(text=text)

        voice = texttospeech.types.VoiceSelectionParams(
            name='en-US-Wavenet-C',
            language_code='en-US'
        )

        audio_config = texttospeech.types.AudioConfig(
            audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16,
            pitch=-2.8,
            speaking_rate = 0.8
        )

        print("Synthesizing")
        response = self.client.synthesize_speech(synthesis_input, voice, audio_config)
        print("Done")

        with open('/tmp/love.wav', 'wb') as fd:
            fd.write(response.audio_content)
            print("Wrote to file")

    def list_voices(self):
        """Lists the available voices."""

        # Performs the list voices request
        voices = self.client.list_voices()

        for voice in voices.voices:
            # Display the voice's name. Example: tpc-vocoded
            print('Name: {}'.format(voice.name))

            # Display the supported language codes for this voice. Example: "en-US"
            for language_code in voice.language_codes:
                print('Supported language: {}'.format(language_code))

            # SSML Voice Gender values from google.cloud.texttospeech.enums
            ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE',
                                  'FEMALE', 'NEUTRAL']

            # Display the SSML Voice Gender
            print('SSML Voice Gender: {}'.format(
                ssml_voice_genders[voice.ssml_gender]))

            # Display the natural sample rate hertz for this voice. Example: 24000
            print('Natural Sample Rate Hertz: {}\n'.format(
                voice.natural_sample_rate_hertz))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/11/17 21:02
# @Author : Dendi
# @contact: msexuan@163.com
# @File : Layers.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformer.SubLayers import MultiHeadAttention, PositionwiseFeedForward

class EncoderLayer(nn.Module):
    '''编码层'''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        '''

        :param d_model:     模型输入维度
        :param d_inner:     前馈层隐层维度
        :param n_head:      多头
        :param d_k:         键向量维度
        :param d_v:         值向量维度
        :param dropout:
        '''
        super(EncoderLayer, self).__init__()

        self.sef_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)

        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)

    def forward(self, enc_input, non_pad_mask = None, slf_attn_mask= None):

        enc_output, enc_slf_attn = self.sef_attn(enc_input, enc_input, enc_input, mask=slf_attn_mask)
        # if non_pad_mask != None:
        #     enc_output *= non_pad_mask
        enc_output *= non_pad_mask
        enc_output = self.pos_ffn(enc_output)
        # if non_pad_mask != None:
        #     enc_output *= non_pad_mask
        enc_output *= non_pad_mask
        return enc_output, enc_slf_attn

class DecoderLayer(nn.Module):
    '''解码层'''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        super(DecoderLayer, self).__init__()
        self.slf_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.enc_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner,dropout=dropout)

    def forward(self, dec_input, enc_output, non_pad_mask=None, slf_attn_mask=None, dec_enc_attn_mask=None):
        # dec_output shape [batch_size, len_q, n_head * d_v]
        # dec_slf_attn shape [n_head * batch_size, len_q, d_v]
        dec_output, dec_slf_attn = self.slf_attn(
            dec_input, dec_input, dec_input, mask=slf_attn_mask)
        dec_output *= non_pad_mask

        dec_output, dec_enc_attn = self.enc_attn(
            dec_output, enc_output, enc_output, mask=dec_enc_attn_mask)
        dec_output *= non_pad_mask

        dec_output = self.pos_ffn(dec_output)
        dec_output *= non_pad_mask

        return dec_output, dec_slf_attn, dec_enc_attn


# -*- coding:utf-8 -*-
"""Example of I/O plugin for loading DICOM files"""

import os.path as osp

try:
    try:
        # pydicom 0.9
        import dicom as dicomio
    except ImportError:
        # pydicom 1.0
        from pydicom import dicomio
    def load_dicom(filename):
        try:
            name = osp.splitext(osp.basename(filename))[0]
            try:
                data = dicomio.read_file(filename, force=True)
            except TypeError:
                data = dicomio.read_file(filename)
            arr = data.pixel_array
            return {name: arr}, None
        except Exception as error:
            return None, str(error)
except ImportError:
    load_dicom = None


#-*- coding: utf-8 -*-

#!/usr/bin/env python
#coding=UTF-8
import multiprocessing
from multiprocessing import Process
from threading import Thread
from time import sleep
import string
import time
import sys
import logging
import random
import os

reload(sys)
sys.setdefaultencoding('utf8')
logging.basicConfig(level=logging.DEBUG,
        format='%(asctime)s %(filename)s[line:%(lineno)d] %(funcName)s %(levelname)s %(message)s',
        datefmt='%a, %d %b %Y %H:%M:%S',
        filename='log.log',
        filemode='a')
#num是并发线程总数
processNum = 50
threadNum = 50

def processWorking(userlist):
    threads = []
    for i in range(threadNum):

        t = Thread(target=theadWorking,args=(i,))
        t.setDaemon(True)
        t.start() #把JOBS排入队列
        threads.append(t)
    for t in threads:
        t.join()
    #time.sleep(10)
def theadWorking(num):
    #print num
    logging.error(num)

if __name__ == '__main__':

    processs = []
    for num in range(processNum):
        p = Process(target=processWorking, args=('2',))
        processs.append(p)
        p.start()
    for p in processs:
        p.join()
    #print u'执行完毕'


#!/usr/bin/python3.6
# coding: utf-8

import logging

from neverland.exceptions import ArgumentError
from neverland.protocol.crypto.mode import Modes
from neverland.protocol.crypto.openssl import OpenSSLCryptor, load_libcrypto
from neverland.protocol.crypto.kc.aead.gcm import GCMKernelCryptor

logger = logging.getLogger('Crypto')

openssl_ciphers = {
    cipher: OpenSSLCryptor for cipher in OpenSSLCryptor.supported_ciphers
}

kc_aead_ciphers = {
    cipher: GCMKernelCryptor for cipher in GCMKernelCryptor.supported_ciphers
}

supported_ciphers = dict()
supported_ciphers.update(openssl_ciphers)
supported_ciphers.update(kc_aead_ciphers)

openssl_preload_func_map = {
    cipher: load_libcrypto for cipher in OpenSSLCryptor.supported_ciphers
}

preload_funcs = {}
preload_funcs.update(openssl_preload_func_map)

ALL_CIPHERS = list(supported_ciphers.keys())

def preload_crypto_lib(cipher_name, libpath=None):
    preload_func = preload_funcs.get(cipher_name)
    if preload_func is not None:
        preload_func(libpath)

class Cryptor():

    # we tag the cryptor with a label of the socket address of the remote node
    # type: str, format: "ip:port"
    attribution = None

    def __init__(self, config, key=None, iv=None, attribution=None):
        ''' Constructor

        :param config: the config
        :param key: optional, if this argument is not given, then the default
                    key derived by neverland.utils.HashTools.hkdf will be used
        :param iv: optional, if this argument is not given, then the default
                   IV derived by neverland.utils.HashTools.hdivdf will be used
        :param attribution: a tag about which remote node that this cryptor
                            belongs to.
        '''

        self.config = config
        self.attribution = attribution

        self._cipher_name = self.config.net.crypto.cipher
        self._cipher_cls = supported_ciphers.get(self._cipher_name)

        if self._cipher_cls is None:
            raise Exception('unsupported cipher')

        if self._cipher_name.startswith('kc-'):
            self._cipher_cls.check_kernel_version()

        self._init_ciphers(key, iv)

        logger.info(
            f'Loadded crypto implementation {self._cipher_cls.__name__} '
            f'with cipher {self._cipher_name}'
        )

    def _init_ciphers(self, key, iv):
        self._cipher = self._cipher_cls(self.config, Modes.ENCRYPTING, key, iv)
        self._decipher = self._cipher_cls(self.config, Modes.DECRYPTING, key, iv)

    def reset(self):
        self._cipher.reset()
        self._decipher.reset()

    def encrypt(self, data):
        return self._cipher.update(data)

    def decrypt(self, data):
        return self._decipher.update(data)

    @property
    def iv(self):
        return self._cipher._iv


from playsound import playsound

def clear_button():
    # Clear the button status after it's read
    file = open('data/button_status.txt', 'w')
    file.write('None')
    file.close()

class Sensor:

    def __init__(self):

        # Initialize the data files
        file = open('data/led_status.txt', 'w')
        file.write('0 0 0 0')
        file.close()

        file = open('data/button_status.txt', 'w')
        file.write('None')
        file.close()

        file = open('data/temperature.txt', 'w')
        file.write('0')
        file.close()

        file = open('data/light.txt', 'w')
        file.write('on')
        file.close()

    # Send data to the leds
    @staticmethod
    def output(led, status):

        # Sound the buzzer if requested
        if led == 'buzzer' or led == 'buzz':
            playsound('src/buzzer.wav')
            return

        # Get the current led status
        file = open('data/led_status.txt', 'r')
        led_status = file.readline().split()
        file.close()

        # Change led status as required
        if led == 'rled' and status == 'off':
            led_status[0] = '0'
        elif led == 'rled' and status == 'on':
            led_status[0] = '1'

        if led == 'yled' and status == 'off':
            led_status[1] = '0'
        elif led == 'yled' and status == 'on':
            led_status[1] = '1'

        if led == 'gled' and status == 'off':
            led_status[2] = '0'
        elif led == 'gled' and status == 'on':
            led_status[2] = '1'

        if led == 'bled' and status == 'off':
            led_status[3] = '0'
        elif led == 'bled' and status == 'on':
            led_status[3] = '1'

        # Write the new led status to the data file
        file = open('data/led_status.txt', 'w')
        file.writelines(led_status[0] + ' ' +
                        led_status[1] + ' ' +
                        led_status[2] + ' ' +
                        led_status[3])
        file.close()

    @staticmethod
    def input(phy_obj):

        # Get the name of the most recent button clicked
        file = open('data/button_status.txt', 'r')
        button_status = file.readline()
        file.close()

        # Return the status if a button query
        if phy_obj == button_status:
            clear_button()
            return "pressed"

        elif phy_obj == button_status:
            clear_button()
            return "pressed"

        elif phy_obj == button_status:
            clear_button()
            return "pressed"

        elif phy_obj == button_status:
            clear_button()
            return "pressed"

        # Return the temperature if a temperature query
        elif phy_obj == 'temperature' or phy_obj == 'temp':
            file = open('data/temperature.txt', 'r')
            reading = file.readline()
            file.close()
            return reading

        # Return the light status if a light query
        elif phy_obj == 'light':
            file = open('data/light.txt', 'r')
            reading = file.readline()
            file.close()
            return reading


#!/usr/bin/env python
from setuptools import setup
import os
import sys

if os.geteuid() == 0:
    data_files = [('/etc/bash_completion.d', ['pythonpy/pycompletion.sh']),]
else:
    if sys.argv[1] == 'install':
        print(
'''******************************************************************************
Looks like you didn't run this command using sudo.
Pythonpy needs root privileges to copy pycompletion.sh to /etc/bash_completion.d
1) If you are in a virtualenv, you can configure tab completion without root using:
    source /path/to/virtualenv/bash_completion.d/pycompletion.sh
2) If you aren't using virtualenv, remember that pip requires sudo by default
    on most systems. py is a simple python script that does not require any
    root access or special privileges. If you don't like using root,
    learn virtualenv and refer to 1).
Installation proceeding without root access...
******************************************************************************''')
    data_files = [('bash_completion.d', ['pythonpy/pycompletion.sh']),]

py_entry = 'py%s = pythonpy.__main__:main'
pycompleter_entry = 'pycompleter%s = pythonpy.pycompleter:main'
endings = ('', sys.version[:1], sys.version[:3])
entry_points_scripts = []
for e in endings:
    entry_points_scripts.append(py_entry % e)
    entry_points_scripts.append(pycompleter_entry % e)

setup(
    name='pythonpy',
    version='0.3.5',
    description='python -c, with tab completion and shorthand',
    data_files=data_files,
    license='MIT',
    url='https://github.com/Russell91/pythonpy',
    long_description='https://github.com/Russell91/pythonpy',
    packages = ['pythonpy'],
    entry_points = {
        'console_scripts': entry_points_scripts
    },
)


from flask import Flask,render_template,request, redirect,url_for
from database import *
app = Flask(__name__)

@app.route('/add_event',methods=["POST","GET"])
def addevent():
	if request.method=="POST":
		nameOfEvent = request.form['nameOfEvent']
		date = request.form['date']
		discription=request.form['discription']
		email=request.form['email']
		nameOfCreater=request.form['nameOfCreater']
		requirements=request.form['requirements']
		phoneNumber=request.form['phoneNumber']
		numberOfVolunteers=request.form['numberOfVolunteers']
		pic=request.form['pic']
		add_event(nameOfEvent,date,email, discription, nameOfCreater, phoneNumber, numberOfVolunteers, requirements, pic)
		return render_template("addevent.html")

	else:
		return render_template("addevent.html")

@app.route('/')
def home():
    return redirect(url_for("events"))

@app.route('/about')
def about():
    return render_template("about.html")

@app.route('/events')
def events():
	events=get_all_events()
	return render_template("events.html",events=events)

@app.route('/viewevent/<int:event_id>')
def viewevent(event_id):
	event=get_all_events_by_id(event_id)
	return render_template("viewevent.html",event=event)

@app.route('/volunform/<int:event_id>', methods=["POST"])
def volunform(event_id):
	event = get_all_events_by_id(event_id)
	volunteer(event.nameOfEvent)
	return redirect(url_for("viewevent",event_id=event_id))
if __name__ == '__main__':
    app.run(debug=True)


"""Templates for pose estimators, or posers."""
import asyncio
import numbers
import warnings

from ..util import utilz as u

class PoseEuler(object):
    """Struct containing all variables needed for instantaneous pose."""

    __slots__ = [
        "x",
        "y",
        "z",
        "vel_x",
        "vel_y",
        "vel_z",
        "acc_x",
        "acc_y",
        "acc_z",
        "ang_x",
        "ang_y",
        "ang_z",
        "vel_ang_x",
        "vel_ang_y",
        "vel_ang_z",
        "acc_ang_x",
        "acc_ang_y",
        "acc_ang_z",
    ]

    def __init__(
        self,
        x: float = 0,
        y: float = 0,
        z: float = 0,
        vel_x: float = 0,
        vel_y: float = 0,
        vel_z: float = 0,
        acc_x: float = 0,
        acc_y: float = 0,
        acc_z: float = 0,
        ang_x: float = 0,
        ang_y: float = 0,
        ang_z: float = 0,
        vel_ang_x: float = 0,
        vel_ang_y: float = 0,
        vel_ang_z: float = 0,
        acc_ang_x: float = 0,
        acc_ang_y: float = 0,
        acc_ang_z: float = 0,
    ):
        """
        Instantaneous pose and velocity of an object.

        :param pose: location in meters and orientation in quaternion
        :param velocity: velocity in meters/second
                         Angular velocity of the pose in axis-angle representation. The direction is the angle of
                         rotation and the magnitude is the angle around that axis in radians/second.
        """
        self.x = x
        self.y = y
        self.z = z
        self.vel_x = vel_x
        self.vel_y = vel_y
        self.vel_z = vel_z
        self.acc_x = acc_x
        self.acc_y = acc_y
        self.acc_z = acc_z
        self.ang_x = ang_x
        self.ang_y = ang_y
        self.ang_z = ang_z
        self.vel_ang_x = vel_ang_x
        self.vel_ang_y = vel_ang_y
        self.vel_ang_z = vel_ang_z
        self.acc_ang_x = acc_ang_x
        self.acc_ang_y = acc_ang_y
        self.acc_ang_z = acc_ang_z

    def __getitem__(self, key):
        if key in self.__slots__:
            return getattr(self, key)

        raise KeyError(key)

    def __setitem__(self, key, val):
        assert isinstance(val, numbers.Number), "value should be numeric"

        if key in self.__slots__:
            setattr(self, key, val)
            return

        raise KeyError(key)

    def __repr__(self):
        return "<{}.{} [{}\n] object at {}>".format(
            self.__class__.__module__,
            self.__class__.__name__,
            "\n\t".join([f"{key}={self[key]}" for key in self.__slots__]),
            hex(id(self)),
        )

class Pose(object):
    """Struct containing all variables needed for instantaneous pose."""

    __slots__ = [
        "x",
        "y",
        "z",
        "r_w",
        "r_x",
        "r_y",
        "r_z",
        "vel_x",
        "vel_y",
        "vel_z",
        "ang_vel_x",
        "ang_vel_y",
        "ang_vel_z",
    ]

    def __init__(self, pose=(0, 0, 0, 1, 0, 0, 0), velocity=(0,) * 6):
        """
        Instantaneous pose and velocity of an object.

        :param pose: location in meters and orientation in quaternion
        :param velocity: velocity in meters/second
                         Angular velocity of the pose in axis-angle representation. The direction is the angle of
                         rotation and the magnitude is the angle around that axis in radians/second.
        """
        self.x: float = pose[0]  # +(x) is right in meters
        self.y: float = pose[1]  # +(y) is up in meters
        self.z: float = pose[2]  # -(z) is forward in meters
        self.r_w: float = pose[3]  # from 0 to 1
        self.r_x: float = pose[4]  # from -1 to 1
        self.r_y: float = pose[5]  # from -1 to 1
        self.r_z: float = pose[6]  # from -1 to 1

        self.vel_x: float = velocity[0]  # +(x) is right in meters/second
        self.vel_y: float = velocity[1]  # +(y) is up in meters/second
        self.vel_z: float = velocity[2]  # -(z) is forward in meters/second
        self.ang_vel_x: float = velocity[3]
        self.ang_vel_y: float = velocity[4]
        self.ang_vel_z: float = velocity[5]

    def __getitem__(self, key):
        if key in self.__slots__:
            return getattr(self, key)

        raise KeyError(key)

    def __setitem__(self, key, val):
        assert isinstance(val, numbers.Number), "value should be numeric"

        if key in self.__slots__:
            setattr(self, key, val)
            return

        raise KeyError(key)

    def __repr__(self):
        return "<{}.{} [{}] object at {}>".format(
            self.__class__.__module__,
            self.__class__.__name__,
            ", ".join([f"{key}={self[key]}" for key in self.__slots__]),
            hex(id(self)),
        )

"""class CoroutineKeepAlive(object):
    def __init__(self, send_delay, receive_delay, close_delay=0.1):
        self.send = [True, send_delay]
        self.receive = [True, receive_delay]
        self.close = [True, close_delay]"""

class ControllerState(Pose):
    """Struct containing all variables needed for instantaneous controller state."""

    __slots__ = [
        "x",
        "y",
        "z",
        "r_w",
        "r_x",
        "r_y",
        "r_z",
        "vel_x",
        "vel_y",
        "vel_z",
        "ang_vel_x",
        "ang_vel_y",
        "ang_vel_z",
        "grip",
        "system",
        "menu",
        "trackpad_click",
        "trigger_value",
        "trackpad_x",
        "trackpad_y",
        "trackpad_touch",
        "trigger_click",
    ]

    def __init__(
        self,
        pose=(0, 0, 0, 1, 0, 0, 0),
        velocity=(0,) * 6,
        grip=0,
        system=0,
        menu=0,
        trackpad_click=0,
        trigger_value=0,
        trackpad_x=0,
        trackpad_y=0,
        trackpad_touch=0,
        trigger_click=0,
    ):
        """
        Instantaneous controller state.

        :param pose: location in meters and orientation in quaternion
        :param velocity: velocity in meters/second
                         Angular velocity of the pose in axis-angle representation. The direction is the angle of
                         rotation and the magnitude is the angle around that axis in radians/second.
        :param grip: is grip down
        :param system: is system button down
        :param menu: is menu button down
        :param trackpad_click: is trackpad clicked
        :param trigger_value: trigger pressure value
        :param trackpad_x: trackpad touch x position
        :param trackpad_y: trackpad touch y position
        :param trackpad_touch: is trackpad being touched
        :param trigger_click: is trigger clicked
        """
        super().__init__(pose, velocity)
        self.grip: int = grip  # 0 or 1
        self.system: int = system  # 0 or 1
        self.menu: int = menu  # 0 or 1
        self.trackpad_click: int = trackpad_click  # 0 or 1
        self.trigger_value: int = trigger_value  # from 0 to 1
        self.trackpad_x: float = trackpad_x  # from -1 to 1
        self.trackpad_y: float = trackpad_y  # from -1 to 1
        self.trackpad_touch: int = trackpad_touch  # 0 or 1
        self.trigger_click: int = trigger_click  # 0 or 1

def get_slot_names(slotted_instance):
    """Get all slot names in a class with slots."""
    # thanks: https://stackoverflow.com/a/6720815/782170
    return slotted_instance.__slots__

def get_slot_values(slotted_instance):
    """Get all slot values in a class with slots."""
    # thanks: https://stackoverflow.com/a/6720815/782170
    return [getattr(slotted_instance, slot) for slot in get_slot_names(slotted_instance)]

class PoserTemplate:
    """
    Poser base class.

    supplies 3 tracked device pose dicts:
        self.pose - hmd pose, position, orientation, velocity and angular velocity only
        self.pose_controller_r - same as hmd pose +controller inputs for controller 1
        self.pose_controller_l - same as hmd pose +controller inputs for controller 2

    more info on poses/message format:
        https://github.com/okawo80085/hobo_vr/wiki/poser-message-format

    supplies a last message from server buffer:
        self.last_read - string containing the last message from the server

    supplies threading vars:
        self.coro_list - list of all methods recognized as threads
        self.coro_keep_alive - dict of all registered threads, containing self.coro_keepAlive['threadMethodName'] = [KeepAliveBool, SleepDelay], the dict is populated at self.main() call

    this base class also has 3 built in threads, it is not recommended you override any of them:
        self.send - sends all pose data to the server
        self.recv - receives messages from the server, the last message is stored in self.lastRead
        self.close - closes the connection to the server and ends all threads that where registered by thread_register

    this base class will assume that every
    child method without '_' as a first character
    in the name is a thread, unless the name of that method has been added to self._coro_name_exceptions

    every child class also needs to register it's thread
    methods with the thread_register decorator

    Example:
        class MyPoser(PoserTemplate):
            def __init__(self, *args, **kwargs):
                super().__init(**kwargs)

            @thread_register(0.5)
            async def example_thread(self):
                while self.coro_keep_alive['example_thread'][0]:
                    self.pose['x'] += 0.04

                    await asyncio.sleep(self.coro_keep_alive['example_thread'][1])

        poser = MyPoser()

        asyncio.run(poser.main())

    more examples:
        https://github.com/okawo80085/hobo_vr/blob/master/virtualreality/trackers/color_tracker.py
        https://github.com/okawo80085/hobo_vr/blob/master/examples/poserTemplate.py

    """

    def __init__(
        self, *, addr="127.0.0.1", port=6969, send_delay=1 / 100, recv_delay=1 / 1000, **kwargs,
    ):
        """
        Create the poser template.

        :param addr: is the address of the server to connect to, stored in self.addr
        :param port: is the port of the server to connect to, stored in self.port
        :param send_delay: sleep delay for the self.send thread(in seconds)
        :param recv_delay: sleep delay for the self.recv thread(in seconds)
        """
        self.addr = addr
        self.port = port

        self._coro_name_exceptions = ["main"]

        self.coro_list = [
            method_name
            for method_name in dir(self)
            if callable(getattr(self, method_name))
            and method_name[0] != "_"
            and method_name not in self._coro_name_exceptions
        ]

        self.coro_keep_alive = {
            "close": [True, 0.1],
            "send": [True, send_delay],
            "recv": [True, recv_delay],
        }

        self.pose = Pose()
        self.pose_controller_r = ControllerState(pose=(0.5, 1, -1, 1, 0, 0, 0))
        self.pose_controller_l = ControllerState(pose=(0.5, 1.1, -1, 1, 0, 0, 0))
        self.last_read = ""

    async def _socket_init(self):
        """
        Connect to the server using self.addr and self.port and send the id message.

        Also store socket reader and writer in self.reader and self.writer.
        It is not recommended you override this method

        send id message
        more on id messages: https://github.com/okawo80085/hobo_vr/wiki/server-id-messages
        """
        print(f'connecting to the server at "{self.addr}:{self.port}"...')
        # connect to the server
        self.reader, self.writer = await asyncio.open_connection(self.addr, self.port, loop=asyncio.get_event_loop())
        # send poser id message
        self.writer.write(u.format_str_for_write("poser here"))

    async def send(self):
        """Send all poses thread."""
        while self.coro_keep_alive["send"][0]:
            try:
                msg = u.format_str_for_write(
                    " ".join(
                        [str(i) for i in get_slot_values(self.pose)]
                        + [str(i) for i in get_slot_values(self.pose_controller_r)]
                        + [str(i) for i in get_slot_values(self.pose_controller_l)]
                    )
                )

                self.writer.write(msg)

                await asyncio.sleep(self.coro_keep_alive["send"][1])
            except Exception as e:
                print(f"send failed: {e}")
                self.coro_keep_alive["send"][0] = False
                break

    async def recv(self):
        """Receive messages thread."""
        while self.coro_keep_alive["recv"][0]:
            try:
                data = await u.read(self.reader)
                self.last_read = data

                await asyncio.sleep(self.coro_keep_alive["recv"][1])
            except Exception as e:
                print(f"recv failed: {e}")
                self.coro_keep_alive["recv"][0] = False
                break

    async def close(self):
        """Await close thread, press "q" to kill all registered threads."""
        try:
            import keyboard

            while self.coro_keep_alive["close"][0]:
                if keyboard.is_pressed("q"):
                    self.coro_keep_alive["close"][0] = False
                    break

                await asyncio.sleep(self.coro_keep_alive["close"][1])

        except ImportError as e:
            print(f"close: failed to import keyboard, poser will close in 10 seconds: {e}")
            await asyncio.sleep(10)

        print("closing...")
        for key in self.coro_keep_alive:
            if self.coro_keep_alive[key][0]:
                self.coro_keep_alive[key][0] = False
                print(f"{key} stop sent...")
            else:
                print(f"{key} already stopped")

        await asyncio.sleep(1)

        self.writer.write(u.format_str_for_write("CLOSE"))
        self.writer.close()

        print("done")

    async def main(self):
        """
        Run the main async method.

        Gathers all recognized threads and runs them.
        It is not recommended you override this method.
        """
        await self._socket_init()

        await asyncio.gather(
            *[getattr(self, coro_name)() for coro_name in self.coro_list if coro_name not in self._coro_name_exceptions]
        )

def thread_register(sleepDelay, runInDefaultExecutor=False):
    """
    Register thread for PoserTemplate base class.

    sleepDelay - sleep delay in seconds
    runInDefaultExecutor - bool, set True if you want the function to be executed in asyncio's default pool executor
    """

    def _thread_reg(func):
        def _thread_reg_wrapper(self, *args, **kwargs):

            if not asyncio.iscoroutinefunction(func) and not runInDefaultExecutor:
                raise ValueError(f"{repr(func)} is not a coroutine function and runInDefaultExecutor is set to False")

            if func.__name__ not in self.coro_keep_alive and func.__name__ in self.coro_list:
                self.coro_keep_alive[func.__name__] = [True, sleepDelay]

            else:
                warnings.warn("thread register ignored, thread already exists", RuntimeWarning)

            if runInDefaultExecutor:
                loop = asyncio.get_running_loop()

                return loop.run_in_executor(None, func, self, *args, **kwargs)

            return func(self, *args, **kwargs)

        setattr(_thread_reg_wrapper, "__name__", func.__name__)

        return _thread_reg_wrapper

    return _thread_reg

class PoserClient(PoserTemplate):
    """
    PoserClient.

    example usage:
        poser = PoserClient()

        @poser.thread_register(1)
        async def lol():
            while poser.coro_keepAlive['lol'][0]:
                poser.pose['x'] += 0.2

                await asyncio.sleep(poser.coro_keepAlive['lol'][1])

        asyncio.run(poser.main())
    """

    def __init__(self, *args, **kwargs):
        """Create the poser client."""
        super().__init__(*args, **kwargs)
        self._coro_name_exceptions.append("thread_register")

    def thread_register(self, sleep_delay, runInDefaultExecutor=False):
        """
        Register a thread for PoserClient.

        sleepDelay - sleep delay in seconds
        runInDefaultExecutor - bool, set True if you want the function to be executed in asyncio's default pool executor
        """

        def _thread_register(coro):
            if not asyncio.iscoroutinefunction(coro) and not runInDefaultExecutor:
                raise ValueError(f"{repr(coro)} is not a coroutine function and runInDefaultExecutor is set to False")

            if coro.__name__ not in self.coro_keep_alive and coro.__name__ not in self.coro_list:
                self.coro_keep_alive[coro.__name__] = [True, sleep_delay]
                self.coro_list.append(coro.__name__)

                if runInDefaultExecutor:

                    def _wrapper(*args, **kwargs):
                        setattr(_wrapper, "__name__", f"{coro.__name__} _decorated")
                        loop = asyncio.get_running_loop()

                        return loop.run_in_executor(None, coro, *args, **kwargs)

                    setattr(_wrapper, "__name__", coro.__name__)
                    setattr(self, coro.__name__, _wrapper)
                    return _wrapper

                else:
                    setattr(self, coro.__name__, coro)

            else:
                raise NameError(
                    f"trying to register existing thread, thread with name {repr(coro.__name__)} already exists"
                )

            return coro

        return _thread_register


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

from bs4 import BeautifulSoup
import boto3

# Get the service resource.
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('book')

response = table.get_item(
    Key={
        'id_title': '1'
    }
)
item = response['Item']
print(item)


from django.urls import path

from kahawa import views

app_name = 'kahawa'

urlpatterns = [
    path('', views.index, name='index'),
    path('check-credentials',
         views.check_credentials,
         name='check-credentials'),
    path('signup', views.signup, name='signup'),
    path('verify', views.verify, name='verify'),
    path('success', views.success, name='success'),
    path('terms', views.terms, name='terms'),
]