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

import logging
logger = logging.getLogger(__name__)

import os
import re
import time
import errno
import socket
import random
import subprocess
from abc import ABCMeta, abstractmethod

from simg.util import sstring
from base import BaseDevAdapter, VendorMsg
from base import DevAdapterError, DevAdapterCmdError

class SWAM3Connection(BaseDevAdapter):
    RECV_BUFSIZE = 4096

    def __init__(self, addr, name=""):
        logger.debug("create ss_server connection with addr: %s", addr)
        self.logname = None
        self._name = name
        self._addr = addr
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.connect(self._addr)
        self._sock.setblocking(0)
        self._device_type = None

    def close(self):
        if self._sock:
            self._sock.close()

    def sendcmd(self, cmd, timeout=5.0, interval=0.5, trim=True, wakeup=True):
        logger.info("send: '%s' to %s %s", cmd, self._addr, self._name)
        # Clear any data in the receive buffer first before issuing the command
        while True:
            # if a recv() call doesn’t find any data, an error exception is raised
            try:
                self._sock.recv(self.RECV_BUFSIZE)
            except socket.error:
                break

        data = ""
        starttime = time.time()
        try:
            self._sock.send(str(cmd + "<EOF>").encode("ascii"))
            # self._sock.send(str(cmd + "<EOF>"))
        except socket.error:
            logger.exception("")
        else:
            while True:
                try:
                    newdata = self._sock.recv(self.RECV_BUFSIZE)
                    logger.debug("recv: %s", newdata)
                    data += newdata.decode()
                except socket.error:
                    pass
                finally:
                    if "<EOF>" in data:
                        if "Warning:BB is in Sleep Mode" in data and wakeup is True:
                            logger.debug("Module is in sleep mode, force to wake up it")
                            self.sendcmd("pwr_set_mode 3")
                            logger.debug("retry cmd: %s", cmd)
                            data = self.sendcmd(cmd, timeout, interval, trim=False)
                        break
                    if time.time() - starttime > timeout:
                        logger.debug("recv timeout")
                        break
                    if interval:
                        time.sleep(interval)

        if trim:
            data = data.strip()
            data = data.replace("<EOF>", "")
            data = data.replace("\r", "")
        logger.info("Response: %s", data)
        return data

    def setLogFilename(self, newname):
        logger.info("change ss_server log to '%s' of the SWAM3 server on %s", newname, self._addr)
        try:
            self._sock.send(str("ss_server logfilename=%s<EOF>" % newname).encode("ascii"))
        except socket.error:
            logger.exception("change ss_server log path failed")
            raise
        else:
            logger.info("change ss_server log path done")
            self.logname = newname

    def nvramreset(self):
        self.sendcmd("nvram_set_defaults")
        self.reset()

    @abstractmethod
    def reset(self):
        raise NotImplementedError

    @abstractmethod
    def upgradeFirmware(self, filename):
        raise NotImplementedError

    def getMacAddress(self):
        mac = self.sendcmd("custparamget 0")
        if re.search("OK", mac, re.I):
            #for B&A
            mac = mac.split("OK")[0].split("=")[1]
        else:
            #for Gen3
            mac = mac.split("<")[0]
        return sstring.trimMacAddress(mac)

    def setMacAddress(self, mac):
        self.sendcmd("custparamset 0 " + mac)

    def getDeviceName(self):
        devname = self.sendcmd("custparamget 2")
        if re.search("OK", devname, re.I):
            #for B&A
            devname = devname.split("OK")[0].split("=")[1]
        else:
            #for Gen3
            devname = devname.split(" ")[0]
        return devname

    def setDeviceName(self,devname):
        self.sendcmd("custparamset 2 "+ devname)

    def connect(self, dstMacAddr=None):
        if dstMacAddr:
            dstMacAddr = dstMacAddr.lower() + ":00"
            macAddrs = self.sendcmd("dev_table_dump 2")
            R = re.compile(r"\(([^\)]*)\)")
            deviceList = re.findall(R, macAddrs)
            if deviceList:
                if dstMacAddr in deviceList:
                    index = deviceList.index(dstMacAddr)
                    self.sendcmd("connect_device " + index)
                    logger.debug("Connecting to device " + index)
                else:
                    logger.debug("The given device not in the wvan")
            else:
                logger.debug("No device in the wvan")
        else:
            self.sendcmd("connect_device 0")
            logger.debug("Connecting to device 0")

    def disconnect(self):
        self.sendcmd("disconnect_device 0")
        logger.debug("Disconnecting to device 0")

    def setVendorMsgFilter(self, msgFilter):
        return self.sendcmd("vendor_msg_set_filter %s" % msgFilter)

    def sendVendorMsg(self, msg):
        if not isinstance(msg, VendorMsg):
            raise TypeError("msg is not a VendorMsg")
        return self.sendcmd("vendor_msg_send %s %s %s %s" % (msg.vendorID, msg.dstMacAddr, msg.length, msg.data))

    def recvVendorMsg(self):
        """<15:40:52.319> 11:11:11 11:22:33:44:55:66 03 AA:BB:CC 0"""
        resp = self.sendcmd("ss_server getvendormsg")
        l = resp.split(" ")
        l = l[1:-1]
        vendorID = l[0].lower()
        dstMacAddr = sstring.trimMacAddress(l[1].lower())
        length = int(l[2])
        data = l[3].lower()
        return VendorMsg(vendorID, dstMacAddr, length, data)

    def getFwVersion(self):
        return self.sendcmd("show_version")

    def getFwBuild(self):
        fw_ver = self.getFwVersion()
        num_index = fw_ver.find('SVN') + 3
        return 'svn' + fw_ver[num_index:num_index+4]

    def getDevState(self):
        return self.sendcmd("dev_get_state")

    def getLinkQuality(self):
        link_qua = self.sendcmd("get_hr_link_quality")
        qua_index = link_qua.find('=') + 1
        return int(link_qua[qua_index:])

    def getSoftPairingStatus(self):
        resp = self.sendcmd("soft_pairing_info")
        mode = re.search(r"Soft pairing status=(\d{1})", resp).group(1)
        return int(mode)

    def getProductId(self):
        resp = self.sendcmd("custparamget 3")
        pid = re.search(r"cfgget id=([\w ]+)", resp).group(1)
        return pid

    def srvexit(self):
        logger.info("exit ss_server")
        self.sendcmd("ss_server exit", timeout=1.0)
        self.close()

class GEN3Connection(SWAM3Connection):
    def upgradeFirmware(self, filename):
        if not os.path.exists(filename):
            raise OSError(errno.ENOENT, "Firmware '%s' not exists" % filename)

        resetflag = False
        logger.debug("Querying the Enable Host bootup with BB in resetmode NVRAM (0x67) value on Gen3...")
        val = self.sendcmd("nvramget 0x67")
        if re.search("Parameter 0x67=0x01", val, re.I):
            logger.debug("Setting the Enable Host bootup with BB in resetmode NVRAM (0x67) value on Gen3 to 0...")
            self.sendcmd("nvramset 0x67 0x0")
            resetflag = True

        logger.debug("Querying the Sleep duration on inactivity (0x66) value on Gen3...")
        val = self.sendcmd("nvramget 0x66")
        if not re.search("Parameter 0x67=0x0", val, re.I):
            logger.debug("Setting the Sleep duration on inactivity (0x66) value on Gen3 to 0...")
            self.sendcmd("nvramset 0x66 0x0")
            resetflag = True

        if resetflag:
            self.reset()
            time.sleep(15)

        self.sendcmd("nvram_update_flag 0 1")
        self.sendcmd("ss_server embeddedupgrade=%s" % filename)
        self.sendcmd("nvram_update_flag 0 0")
        self.reset()
        time.sleep(15)

    def reset(self):
        return self.sendcmd("reset")

class BAConnection(SWAM3Connection):
    def upgradeFirmware(self, filename):
        """
        ERROR: AP port not opened<EOF>
        ERROR: Module not supported<EOF>
        ERROR: File not exists<EOF>
        ERROR: Open file failed<EOF>
        ERROR: Upgrade firmware failed<EOF>
        OK: Upgrade firmware succeeded<EOF>
        """
        if not os.path.exists(filename):
            raise OSError(errno.ENOENT, "Firmware '%s' not exists" % filename)

        resp = self.sendcmd("svr_apcommand full_upgrade=%s" % filename, timeout=120.0)
        try:
            match = re.search(r"ERROR: (.*)", resp)
            if match:
                raise DevAdapterCmdError(match.group(1))
        finally:
            self.reset()

    def reset(self):
        return self.sendcmd("svr_apcommand reset")

class SWAM3Server(object):
    BINPATH = "C:/Program Files (x86)/Silicon Image/SWAM3/SWAM3.exe"

    def __init__(self, moduleid, tcpport, ap_moduleid=None, logname=None):
        self._moduleid = moduleid
        self._tcpport = int(tcpport)
        self._logname = logname
        self._ap_moduleid = ap_moduleid
        self._srvproc = None    # will be set when calling self.start

    def __check_addr_not_in_use(self):
        addr = ("0.0.0.0", self._tcpport)
        logger.info("check addr <%s:%s> is not in use before startup ss_server" % addr)
        sock = socket.socket()
        try:
            sock.bind(addr)
        finally:
            sock.close()

    def __check_ss_server_avaiable(self):
        logger.debug("check ss_server is avaiable with cmd: show_version ")
        Connection = BAConnection if self._ap_moduleid else GEN3Connection
        conn = Connection(addr=("127.0.0.1", self._tcpport))
        try:
            if not conn.sendcmd("show_version"):
                conn.sendcmd("ss_server exit", timeout=1.0)
                raise DevAdapterError("startup ss_server failed")
        finally:
            conn.close()

    def start(self):
        self.__check_addr_not_in_use()

        cmd = '"%s" server --moduleid=%s --portno=%s --logsendtoclient=0 --logtoconsole=0 --start=1' % (self.BINPATH, self._moduleid, self._tcpport)
        if self._logname:
            cmd += " --logfilename=%s" % self._logname
        if self._ap_moduleid:
            cmd += " --ap_moduleid=%s" % self._ap_moduleid
        logger.info("startup ss_server with cmd: %s", cmd)
        self._srvproc = subprocess.Popen(cmd)
        time.sleep(15)

        self.__check_ss_server_avaiable()

    def stop(self):
        conn = SWAM3Connection(addr=("127.0.0.1", self._tcpport))
        conn.srvexit()
        if self._srvproc:
            self._srvproc.terminate()

class LocalSWAM3Adapter(object):
    def __init__(self, moduleid, ap_moduleid=None, tcpport=None, logname=None, name=""):
        if not tcpport:
            while True:
                sock = socket.socket()
                port = random.randint(40000, 65535)
                addr = ("0.0.0.0", port)
                try:
                    sock.bind(addr)
                except socket.error as err:
                    if err.errno == errno.EADDRINUSE:
                        logger.debug("addr <%s:%s> is in use, random another port")
                        continue
                    else:
                        raise
                else:
                    tcpport = port
                    break
                finally:
                    sock.close()

        tcpport = int(tcpport)
        srv = SWAM3Server(moduleid, tcpport, ap_moduleid, logname)
        srv.start()

        Connection = BAConnection if ap_moduleid else GEN3Connection
        self.conn = Connection(("127.0.0.1", tcpport), name)

    def __getattribute__(self, name):
        """
        Check the calling method name:
        If it is 'conn' or 'close', return self.conn or self.close; otherwise redirect it to self.conn
        This is an implementation of <Proxy> design pattern
        See <Proxy> design pattern for detail: http://www.oodesign.com/proxy-pattern.html
        """
        if name == "conn":
            return object.__getattribute__(self, "conn")
        elif name == "close":
            return object.__getattribute__(self, "close")
        else:
            conn = object.__getattribute__(self, "conn")
            return getattr(conn, name)

    def close(self):
        logger.info("exit ss_server")
        self.sendcmd("ss_server exit", timeout=1.0)
        self.conn.close()

if __name__ == "__main__":
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)-15s [%(levelname)-8s] - %(message)s'
    )

    adapter = LocalSWAM3Adapter("ba", "ba_ap", 40000, name="BA SRC1")
    adapter.close()
    # filename = r"H:\flash_wihd_mhl_int.bin"
    # adapter.upgradeFirmware(filename)


from __future__ import print_function
from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import imutils
import cv2
from timeit import default_timer as timer

start = timer()

name = 'match louis 1 2017.mp4'
cap = cv2.VideoCapture(name)
Width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
Height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS ))
fourcc = cv2.VideoWriter_fourcc('H','2','6','4')
out = cv2.VideoWriter("cut-hog-"+name+".avi", fourcc, np.double(fps), (Width,Height), True)
winSize = (64,128)
blockSize = (16,16)
blockStride = (8,8)
cellSize = (8,8)
nbins = 9
derivAperture = 1
winSigma = -1.
histogramNormType = 0
L2HysThreshold = 2.0000000000000001e-01
gammaCorrection = True
nlevels = 16
hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,
                        histogramNormType,L2HysThreshold,gammaCorrection,nlevels)
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
fgbg = cv2.createBackgroundSubtractorMOG2(history=50000, varThreshold=200)
i = 0
zer = 0
mnogo = 0
to = 0
proplay = (0, 0, 0, 0)
speed = 0
xp , yp = 0, 0
xpp , ypp = 0, 0
no_player = 1
slow=0
sec = 12
D = []
curr = 1
while(1):
    ret, frame = cap.read()
    if not(ret):
        break
    to+=1
    i+=1
    if to < fps/3:
        D.append(curr)
        continue
    else:
        to = 0
    print(i)
    image = frame[200:,:,:] #cv2.resize(frame,None,fx=0.5, fy=0.5,interpolation = cv2.INTER_CUBIC)
##    orig = np.copy(image)
    loc_frame = np.copy(image)
    fgmask = fgbg.apply(loc_frame)
    fgmask = fgmask * (fgmask >200)
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
            padding=(8, 8), scale=1.1)
    # draw the original bounding boxes
##    for (x, y, w, h) in rects:
##            cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # apply non-maxima suppression to the bounding boxes using a
    # fairly large overlap threshold to try to maintain overlapping
    # boxes that are still people
    rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
    pick = rects #non_max_suppression(rects, probs=None, overlapThresh=0.65)
    # draw the final bounding boxes
    j = 0
    mind = 120000000
    xc = image.shape[1]/2
    yc = image.shape[0]
    cv2.circle(image, (int(xpp),int(ypp)), 5, (255,0,0), -1)
    for (xA, yA, xB, yB) in pick:
        if (xB - xA) < 50 or (yB - yA) < 75 or ((xB - xA) < 150 and yB > 3*image.shape[0]/4) or yB < image.shape[0]/6:
            continue
        cv2.rectangle(image, (xA, yA), (xB, yB), (0, 0, 255), 2)
        dst = min((xA-xc)**2 + (yB-yc)**2,(xB-xc)**2 + (yB-yc)**2)
        pers = np.sum(fgmask[yA:yB,xA:xB])/255 / np.sum(fgmask[yA:yB,xA:xB] > -10)
##        if dst < mind:
##            proplay = (xA, yA, xB, yB)
##            xp = (xA+xB)/2
##            yp = (yA+yB)/2
##            mind = dst
        if dst < mind and pers > 0.02:
            j+=1
            if dst + 150 < mind:
                proplay = (xA, yA, xB, yB)
                xp = (xA+xB)/2
                yp = (yA+yB)/2
                mind = dst
            else:
                if yB > yp:
                    proplay = (xA, yA, xB, yB)
                    xp = (xA+xB)/2
                    yp = (yA+yB)/2
                    mind = dst
    speed = np.sqrt((xp-xpp)**2 + (yp-ypp)**2) / no_player
    pers = np.sum(fgmask[proplay[1]:proplay[3],proplay[0]:proplay[2]])/255 / np.sum(fgmask[proplay[1]:proplay[3],proplay[0]:proplay[2]] > -10)
    if j < 1:
        no_player += 1
        speed = 0
    else:
        no_player = 1
    xpp = xp
    ypp = yp
    if j < 1 or speed > 125:
        speed = 0
    if speed < 15:
        slow+=1
    else:
        slow = 0
    if no_player > 3*sec or slow > 3*sec:
        text = "slow"
        curr = 0
    else:
        text = "G"
        curr = 1
##    cv2.circle(image, (int(xpp),int(ypp)), 5, (0,0,255), -1)
##    cv2.rectangle(image, (0,0), (400, 60), (0,0,0), -1)
##    cv2.putText(image, text, (30, 15), cv2.FONT_HERSHEY_SIMPLEX,
##                0.5, (255, 255, 255), 2)
##    cv2.putText(image, str(speed), (00, 30), cv2.FONT_HERSHEY_SIMPLEX,
##                0.5, (255, 255, 255), 2)
##    cv2.putText(image, str(no_player), (00, 45), cv2.FONT_HERSHEY_SIMPLEX,
##                0.5, (255, 255, 255), 2)
##    cv2.rectangle(image, (proplay[0], proplay[1]), (proplay[2], proplay[3]), (0, 255, 0), 2)
##    cv2.imshow('image', image)
##    print(pers)
##    cv2.waitKey()
##    out.write(image)
    D.append(curr)
cap.release()
cap = cv2.VideoCapture(name)
# Edit the flags
F = np.copy(D)
for i in range(len(D)-1): # if it is a start, go back for 2 sec and 10 frames forward
    if D[i] == 0:
        for j in range(i-fps*(sec+3), i-4*fps):
            F[j] = 0
        for r in range(i-4*fps, i):
            F[r] = 1

##    text = str(speed)
##    cv2.circle(image, (int(xpp),int(ypp)), 5, (0,0,255), -1)
##    cv2.rectangle(image, (0,0), (400, 80), (0,0,0), -1)
##    cv2.putText(image, text, (30, 30), cv2.FONT_HERSHEY_SIMPLEX,
##                1.0, (255, 255, 255), 4)
##    cv2.putText(image, str(speed), (00, 60), cv2.FONT_HERSHEY_SIMPLEX,
##                1.0, (255, 255, 255), 4)
##    cv2.rectangle(image, (proplay[0], proplay[1]), (proplay[2], proplay[3]), (0, 255, 0), 2)
##    out.write(image)
##    if j !=1:
##    cv2.imshow('image', image)
##    cv2.waitKey()
##    k = cv2.waitKey(1) & 0xff
##    if k == ord('q'):
##        break
##        out.write(image)
##        if j < 1:
##            zer+=1
##        else:
##            mnogo+=1

for i in range(len(F)-1):
    ret, frame = cap.read()
    if not(ret):
        break
    if F[i] > 0:
        out.write(frame)
##            text = text_to_put[F[i]]
##            if F[i] != 2:
##                cv2.rectangle(frame, (0,0), (400, 60), (0,0,0), -1)
##                cv2.putText(frame, text, (30, 30), cv2.FONT_HERSHEY_SIMPLEX,
##                            1.0, (255, 255, 255), 4)
##            out.write(frame)
cap.release()
out.release()
end = timer()
print("\nProcessing completed successfully!")
minutes = np.int(np.floor((end - start)/60))
sec = np.int(np.round((end - start) - np.int(np.floor((end - start)/60)*60)))
if sec < 10:
    sec = "0"+str(sec)
if minutes < 10:
    print("Total time: ", "0"+str(minutes)+":"+str(sec))
else:
    print("Total time: ", str(minutes)+":"+str(sec))
cv2.destroyAllWindows()
print("zero: ", zer, "; mnogo: ", mnogo)


# import libraries
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import re
import random

# specify the url
#food_page = 'http://caddyshackkc.com/Happy_Hour_.html' #bad
#food_page = 'https://therecordbar.com'
#food_page = 'https://www.johnnystavern.com/locations/johnnys-olathe/'
food_page = 'https://www.trezomare.com/'
parsed_uri = urlparse(food_page)
domain = '{uri.scheme}://{uri.netloc}'.format(uri=parsed_uri)

searched = []

def getTimes(page_text):
    hhour_index = [m.start() for m in re.finditer('happy hour', page_text)]

    DAY = ['mon', 'tue', 'wed', 'thur', 'fri', 'sat', 'sun', 'every']
    TIME = ['am', 'pm', 'a.m', 'p.m']

    time_possibilities = []

    for integer in range(0, 13):
        time_possibilities.append(str(integer))
        time_possibilities.append(str(integer) + 'am')
        time_possibilities.append(str(integer) + ':30' + 'am')
        time_possibilities.append(str(integer) + ':00' + 'am')

        time_possibilities.append(str(integer) + 'a.m.')
        time_possibilities.append(str(integer) + ':30' + 'a.m.')
        time_possibilities.append(str(integer) + ':00' + 'a.m.')

        time_possibilities.append(str(integer) + 'pm')
        time_possibilities.append(str(integer) + ':30' + 'pm')
        time_possibilities.append(str(integer) + ':00' + 'pm')

        time_possibilities.append(str(integer) + 'p.m.')
        time_possibilities.append(str(integer) + ':30' + 'p.m.')
        time_possibilities.append(str(integer) + ':00' + 'p.m.')

        time_possibilities.append(str(integer) + ' am')
        time_possibilities.append(str(integer) + ':30' + ' am')
        time_possibilities.append(str(integer) + ':00' + ' am')

        time_possibilities.append(str(integer) + ' a.m.')
        time_possibilities.append(str(integer) + ':30' + ' a.m.')
        time_possibilities.append(str(integer) + ':00' + ' a.m.')

        time_possibilities.append(str(integer) + ' pm')
        time_possibilities.append(str(integer) + ':30' + ' pm')
        time_possibilities.append(str(integer) + ':00' + ' pm')

        time_possibilities.append(str(integer) + ' p.m.')
        time_possibilities.append(str(integer) + ':30' + ' p.m.')
        time_possibilities.append(str(integer) + ':00' + ' p.m.')

    day_indices = []
    time_indices = []

    for day in DAY:
        dayIndices = [m.start() for m in re.finditer(day, page_text)]
        for index in dayIndices:
            day_indices.append(index)

    for time in time_possibilities:
        timeIndices = [m.start() for m in re.finditer(time, page_text)]
        for index in timeIndices:
            time_indices.append(index)

    day_indices.sort()
    time_indices.sort()
    #print(hhour_index)
    #print(day_indices)
    #print(time_indices)

    day_index = -1
    time_index = -1
    main_index = -1

    for index in day_indices:
        if index > hhour_index[0]:
            day_index = index
            break

    for index in time_indices:
        if index > hhour_index[0]:
            time_index = index
            break

    if day_index > time_index:
        main_index = day_index

    else:
        main_index = time_index

    word = ''
    word2 = ''
    time = ''

    if page_text[day_index] is 'm' and page_text[day_index + 1] is 'o':
        word = 'Monday'
    elif page_text[day_index] is 't' and page_text[day_index + 1] is 'u':
        word = 'Tuesday'
    elif page_text[day_index] is 'w' and page_text[day_index + 1] is 'e':
        word = 'Wednesday'
    elif page_text[day_index] is 't' and page_text[day_index + 1] is 'h':
        word = 'Thursday'
    elif page_text[day_index] is 'f' and page_text[day_index + 1] is 'r':
        word = 'Friday'
    elif page_text[day_index] is 's' and page_text[day_index + 1] is 'a':
        word = 'Saturday'
    elif page_text[day_index] is 's' and page_text[day_index + 1] is 'u':
        word = 'Sunday'
    elif page_text[day_index] is 'e' and page_text[day_index + 1] is 'v':
        word = 'Everyday'

    if page_text[day_indices[day_indices.index(day_index) + 1]] is 'm' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'o':
        word2 = 'Monday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 't' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'u':
        word2 = 'Tuesday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 'w' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'e':
        word2 = 'Wednesday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 't' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'h':
        word2 = 'Thursday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 'f' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'r':
        word2 = 'Friday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 's' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'a':
        word2 = 'Saturday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 's' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'u':
        word2 = 'Sunday'
    elif page_text[day_indices[day_indices.index(day_index) + 1]] is 'e' and page_text[day_indices[day_indices.index(day_index) + 1] + 1] is 'v':
        word2 = 'Everyday'

    #print(page_text[day_indices[day_indices.index(day_index) + 1]])
    #print(page_text[day_indices[day_index+1]])

    allTimes = []

    #print(page_text[(time_index-8):(time_index+16)])

    for time in time_possibilities:
        if time in page_text[(time_index-8):(time_index+16)]:
            allTimes.append(time)
    #print(allTimes)
    reducedTimes = allTimes
    #print(reducedTimes)
    finalTimes = []

    # for i in range(0, len(reducedTimes)):
    #     for j in range(0, len(reducedTimes)):
    #         if reducedTimes[i] in reducedTimes[j] and len(reducedTimes[i]) < len(reducedTimes[j]):
    #             reducedTimes.pop(i)

    #startTime = random.randint(1,4)
    #endTime = random.randint(startTime + 1, 8)

    for time in reducedTimes:
        for oTime in reducedTimes:
            if time in oTime and len(time) < len(oTime):
                reducedTimes.remove(time)
    print(word, word2, reducedTimes)

def recURLSearch(url):
    page = requests.get(url)
    pass_page = page.text
    soup = BeautifulSoup(pass_page, 'html.parser')
    page_text = str(soup).lower()
    print(url)
    urls = {}

    threshold = 0

    for url1 in soup.find_all('a', href=True):
        urls[url1['href']] = url1.getText()

    for url1, text in urls.items():
        if "happy hour" in text.lower():
            threshold = threshold + 1
    #print(page_text)
    #print("////////////")
    #print(page_text.count('happy hour'))
    if page_text.count('happy hour') > threshold:
        getTimes(page_text)

    else:
        for url2, text in urls.items():
            if url2 not in searched and "http" not in url2 and '@' not in url2:
                print(url2)
                searched.append(url2)
                recURLSearch(domain + url2)

def initSearch():
    recURLSearch(food_page)

initSearch()


import math

__author__ = 'ilyaa'

def merge(first, second):
    fp = 0
    sp = 0
    mp = 0

    first, finv = first
    second, sinv = second

    lf = len(first)
    ls = len(second)

    inversions = finv + sinv

    merged = [0] * (lf + ls)

    while fp < lf and sp < ls:

        if first[fp] > second[sp]:
            merged[mp] = second[sp]
            sp += 1
            inversions += (lf - fp)
        else:
            merged[mp] = first[fp]
            fp += 1

        mp += 1

    if fp < lf:
        for item in first[fp:]:
            merged[mp] = item
            mp += 1

    if sp < ls:
        for item in second[sp:]:
            merged[mp] = item
            mp += 1

    return merged, inversions

def recursive_inversions(numbers, left, right):
    if (right + 1) - left <= 1:
        return numbers[left:right + 1], 0

    middle = int(math.floor((left + right) / 2))

    return merge(recursive_inversions(numbers, left, middle), recursive_inversions(numbers, middle + 1, right))

def naive_inversions(numbers):
    inversions = 0

    for i in range(0, len(numbers)):
        for j in range(i, len(numbers)):
            if numbers[i] > numbers[j]:
                inversions += 1

    return inversions

def main():
    _ = int(input())
    numbers = list(map(int, input().split()))

    _, inversions = recursive_inversions(numbers, 0, len(numbers) - 1)

    print(inversions)

if __name__ == '__main__':
    main()


#Computer Algorithm 061 class
#Homework 1 sequence_alignment
#201524582 JeongHeeSeok

fileInput = open("input.txt", 'r') #file open read mode
fileOutput = open("output.txt", 'w') #file open write mode

sequence1 = fileInput.readline() #ATCCTAG-
sequence1 = sequence1[:-1] + '-' #last index not include
sequence2 = fileInput.readline() + '-' #TACGT-
fileInput.close()                      #result : -TAC-GT & penalty = 7

##Make 2-division Matrix##
matrix = [[0]*len(sequence2) for i in range(len(sequence1))]
##matrix initialization process##
matrix[-1][-1] = 0
for i in range(0,len(sequence2)-1):
    matrix[-1][i] = 2*(len(sequence2)-1-i)
for j in range(0,len(sequence1)-1):
    matrix[j][-1] = 2*(len(sequence1)-1-j)
##matrix filled with penalty##
for l in range(2,len(sequence1)+1):
    for m in range(2,len(sequence2)+1) :
        if(sequence1[-l]==sequence2[-m]) :
            a = 0
        else :
            a = 1
        matrix[-l][-m] = min([matrix[-l][-m+1]+2,matrix[-l+1][-m]+2,matrix[-l+1][-m+1] + a])

for k in range(0,len(sequence1)):
    print(matrix[k])
##find shortest path with down or below diagonal##
i = 0
j = 0
sequenceResult = ""
result = ""
while((i != len(sequence1)-1)&(j != len(sequence2)-1)):
    if(matrix[i][j] - 2 == matrix[i+1][j]):
        i += 1
        result += "%d" % 2
        sequenceResult += "-"
    elif(matrix[i][j] -2 == matrix[i][j+1]):
        pass
        #j += 1
        #sequenceResult += "-"
    else:
        sequenceResult += sequence2[j]
        result += "%d" % (matrix[i][j]-matrix[i+1][j+1])
        i += 1
        j += 1

##print, write to file##
out = "Sequence penalty score = %d" % matrix[0][0]
print(out)
out += "\n"
fileOutput.write(out)

sequence1 = sequence1[:-1]
print(sequence1)
sequence1 += "\n"
fileOutput.write(sequence1)

print(sequenceResult)
sequenceResult += "\n"
fileOutput.write(sequenceResult)

print(result)
result += "\n"
fileOutput.write(result)

fileOutput.close()


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

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^xbd/$','xbd.views.geneIndex',name='geneIndex'),
    url(r'^xbd/admin/', include(admin.site.urls)),
    url(r'^xbd/transcript/(.+)/$', 'xbd.views.drawABox', name='drawABox'),
    url(r'^xbd/exons/(.+)/$', 'xbd.views.drawExonBoxes', name='drawExonBoxes'),
    url(r'^xbd/autocomplete/(.*)', 'xbd.views.getAutocompleteData', name='getAutocompleteData'),
    url(r'^xbd/json/(.+)/$','xbd.views.getJson',name='getJson'),
    url(r'^xbd/json/(.+)/(.+)/$','xbd.views.getJson',name='getJson'),
##    url(r'^xbd/model/(.*)/$', 'xbd.views.fitABox', name='fitABox'),
##    url(r'^xbd/gene/(.*)/$', 'xbd.views.transcriptTrajectory', name='transcriptTrajectory'),
##    url(r'^xbd/genes/(.*)/$', 'xbd.views.multipleTrajectories', name='multipleTrajectories'),   ##    url(r'','xbd.views.geneIndex',name='geneIndex'), #only accessed if tmp webserver

)


import flask
from webapp import authentication
import webapp.api.dashboard as api
from webapp.decorators import login_required
from webapp.api.exceptions import (
    AgreementNotSigned,
    ApiCircuitBreaker,
    ApiError,
    ApiResponseError,
    ApiResponseErrorList,
    ApiTimeoutError,
    MacaroonRefreshRequired,
    MissingUsername,
)

account = flask.Blueprint(
    "account", __name__, template_folder="/templates", static_folder="/static"
)

def refresh_redirect(path):
    try:
        macaroon_discharge = authentication.get_refreshed_discharge(
            flask.session["macaroon_discharge"]
        )
    except ApiResponseError as api_response_error:
        if api_response_error.status_code == 401:
            return flask.redirect(flask.url_for("login.logout"))
        else:
            return flask.abort(502, str(api_response_error))
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    flask.session["macaroon_discharge"] = macaroon_discharge
    return flask.redirect(path)

def _handle_errors(api_error: ApiError):
    if type(api_error) is ApiTimeoutError:
        return flask.abort(504, str(api_error))
    elif type(api_error) is MissingUsername:
        return flask.redirect(flask.url_for(".get_account_name"))
    elif type(api_error) is AgreementNotSigned:
        return flask.redirect(flask.url_for(".get_agreement"))
    elif type(api_error) is MacaroonRefreshRequired:
        return refresh_redirect(flask.request.path)
    elif type(api_error) is ApiCircuitBreaker:
        return flask.abort(503)
    else:
        return flask.abort(502, str(api_error))

def _handle_error_list(errors):
    codes = [error["code"] for error in errors]

    error_messages = ", ".join(codes)
    return flask.abort(502, error_messages)

@account.route("/")
@login_required
def get_account():
    return flask.redirect(flask.url_for("publisher_snaps.get_account_snaps"))

@account.route("/details")
@login_required
def get_account_details():
    try:
        # We don't use the data from this endpoint.
        # It is mostly used to make sure the user has signed
        # the terms and conditions.
        api.get_account(flask.session)
    except ApiResponseErrorList as api_response_error_list:
        return _handle_error_list(api_response_error_list.errors)
    except ApiError as api_error:
        return _handle_errors(api_error)

    flask_user = flask.session["openid"]
    context = {
        "image": flask_user["image"],
        "username": flask_user["nickname"],
        "displayname": flask_user["fullname"],
        "email": flask_user["email"],
    }

    return flask.render_template("publisher/account-details.html", **context)

@account.route("/agreement")
@login_required
def get_agreement():
    return flask.render_template(
        "publisher/developer_programme_agreement.html"
    )

@account.route("/agreement", methods=["POST"])
@login_required
def post_agreement():
    agreed = flask.request.form.get("i_agree")
    if agreed == "on":
        try:
            api.post_agreement(flask.session, True)
        except ApiResponseErrorList as api_response_error_list:
            codes = [error["code"] for error in api_response_error_list.errors]
            error_messages = ", ".join(codes)
            flask.abort(502, error_messages)
        except ApiError as api_error:
            return _handle_errors(api_error)

        return flask.redirect(flask.url_for(".get_account"))
    else:
        return flask.redirect(flask.url_for(".get_agreement"))

@account.route("/username")
@login_required
def get_account_name():
    return flask.render_template("publisher/username.html")

@account.route("/username", methods=["POST"])
@login_required
def post_account_name():
    username = flask.request.form.get("username")

    if username:
        errors = []
        try:
            api.post_username(flask.session, username)
        except ApiResponseErrorList as api_response_error_list:
            errors = errors + api_response_error_list.errors
        except ApiError as api_error:
            return _handle_errors(api_error)

        if errors:
            return flask.render_template(
                "publisher/username.html", username=username, error_list=errors
            )

        return flask.redirect(flask.url_for(".get_account"))
    else:
        return flask.redirect(flask.url_for(".get_account_name"))


import sys
input = sys.stdin.readline
INF = int(1e9) #무한을 의마하는 값으로 10억 설정

n,m = map(int, input().split()) #노드 간선의 갯수 입력 받기

start = int(input()) #시작 노드 번호 입력

graph = [[] for i in range(n+1)] # 각 노드에 연결되어 이는 노드에 대한 정보를 담는 리스트
visited = [False] * (n+1) # 방문한 적이 있는지 체크하는 리스트
distance = [INF] * (n+1) # 최단 거리 테이블을 모두 무한으로 초기화

# 모든 간선 정보 입력 받기
for _ in range(m):
    a,b,c = map(int,input().split())   #a번 노드에서 b번 노드로 가는 비용이 c
    graph[a].append((b,c))

# 방문하지 않은 노드 중에서, 가장 최단 거리가 짧은 노드의 번호 반환
def get_smallest_node():
    min_value=INF
    index = 0 #가장 최단 거리가 짧은 노드의 인덱스
    for i in range(1,n+1):
        if distance[i] < min_value and not visited[i]:
            min_value = distance[i]
            index = i
    return index

def dijkstra(start):
    distance[start] = 0
    visited[start] = True
    for j in graph[start]: # 시작 노드를 제외한 전체 노드에 대해 반복
        distance[j[0]] = j[1]
    for i in range(n-1):  # 현재 최단 거리가 가장 짧은 노드를 꺼내서 방문 처리
        now = get_smallest_node()
        visited[now] = True
        for j in graph[now]: # 현재 노드 노드와 연결된 다른 노드 확인
            cost = distance[now]+j[1]
            if cost < distance[j[0]]: # 현재 노드를 거쳐서 다른 노드로 이동하는 거리가 더 짧은 경우
                distance[j[0]] = cost

dijkstra(start) #다익스트라 알고리즘 수행

for i in range(1,n+1):
    if distance[i]==INF: #모든 노드로 가기 위한 최단 거리를 출력
        print("INFINITY")
    else: # 도달할 수 있는 경우 거리를 출력
        print(distance[i])


import auxiliary.processthread as process
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import json
import keywords

sns.set(style="whitegrid", rc={'figure.figsize':(10,5)})

def select(timestep, time):
    dctin = json.loads(timestep)
    if dctin['type'] == 'mito':
        return []
    dctin.update(dctin['evolvables'])
    dctin['evolvables'] = None
    return [dctin]

options = keywords.getarguments()

dfs = process.get(picklefname=keywords.nfile('./longevity.pickle'),fname='deaths.txt',runs=keywords.getruns(),force=options.f, folder=keywords.getfoldername(), selector=select,  verbose=options.v,   sortbykeywordix=keywords.getkeywordix())

# dfs = process.get(force=False, folder='./fisfus1', reverse=True,stop=1, selector=selectOnlyTime,  verbose=True,   sortbykeywordix=[("fission_rate", -1), ("fusion_rate", -1), ("deprecation_rate", -1)])
# print (dfs.keys())
processed = {}
for path in dfs:
    pre_df = []
    params = {}
    # entry = {}
    for key, val in dfs[path].items():
        if key != 'data':
            params[key]=val
    for dct in dfs[path]['data']:
        # print(dct)
        dct.update(params)
        # for key, val in dct.items():
        #     dct[key] = float(val)
        pre_df.append(dct)
        # print(dct)s
    processed[path] = pd.DataFrame(pre_df)

# print(df)
# facecolor=plt.gcf().get_facecolor()
alldf = []
for path in processed:
    if options.v:
        print("making  " + path)
    df = processed[path]
    # df = df[(df['V']  < 30)]
    df = df[(df['type'] == 'host')]
    df['10loglongevity'] = np.log10(df['time'] - df['time of birth'])
    end = df['time'].iloc[-1]
    print(end)
    if end < 50000:
        if options.v:
            print("short run, only making with flag -c")
        if not options.c:
            continue
    df = df[(df['time'] > 5000)]
    # df = df[(df['dna'] == True)]
    # df['time'] = df['time'].round(decimals=-4)
    bins = 6
    # print(df['time'])
    # end = df['time'].iloc[-1]
    df['time'] = np.digitize(df['time'], np.linspace(0, end, bins+1), right=True)
    df['time'] = df['time'] * end/bins
    # print(df['time'])
    # df['time'] = np.digitize(df['time'], [range(0, df['time'].iloc[-1], 6)])
    # df = df.groupby(by='time').reset_index()
    df['path'] = path
    alldf.append(df)
    # if True:
    #     continue
    if df.empty:
        continue
    fig, ax = plt.subplots()
    # g = sns.scatterplot(data=df, x='time', y='longevity', ax=ax)
    # df = df[(df['time of birth'] > 0)]
    # df= df[(df['time'] > 200000)]
    # df = pd.melt(df, id_vars=['time'], value_vars=["10loglongevity", 'fission_rate', "fusion_rate", 'rep', 'HOST_V_PER_OXPHOS', 'host_division_volume'])
    # print(df)
    g = sns.pairplot(data=df, vars=["10loglongevity", 'fission_rate', "fusion_rate", 'rep', 'HOST_V_PER_OXPHOS', 'host_division_volume'], kind='kde', hue='time', palette='viridis')
    # g = sns.FacetGrid(df, col="variable", hue="time", sharex=False,sharey=False, palette='viridis')
    # g.map(sns.kdeplot, "value", shade=True)
    # g.add_legend()
    # g = sns.pairplot(data=df, vars=["10loglongevity", 'fission_rate', "fusion_rate", 'rep', 'rep2', 'HOST_V_PER_OXPHOS', 'host_division_volume'], kind='hist', hue='time', palette='plasma')
    # title = "time of life of hosts through time\n"
    title = ''
    for kw, ix in keywords.getkeywordix():
        # print(title, kw, df[kw][0])
        title += (kw + " = " + str(processed[path][kw][0]) + '\n')
    g.fig.suptitle(title, y=1.)
    # g.fig.subplots_adjust(top0.9)
    fig.tight_layout()
    # ax.title.set_y(0.5)
    g.fig.subplots_adjust(top=0.8)
    # plt.savefig(keywords.nfile("pairplots/kde"+ path[-4:] +".png"))
    plt.savefig(keywords.nfile("pairplots/pair"+ path[-4:] +".png"))
    plt.close(fig)
    plt.close('all')

exit(0)
df = pd.concat(alldf, ignore_index=True)
if options.v:
    print(df)
# print(df[df.index.duplicated()])
fig, ax = plt.subplots()
g = sns.pairplot(data=df, vars=["10loglongevity", 'fission_rate', "fusion_rate", 'rep', 'HOST_V_PER_OXPHOS', 'host_division_volume'], kind='hist', hue='path')
# df = df[(df['time of birth'] > 0)]
# df = df[(df['longevity'] > 20000)]
# df= df[(df['time'] > 270000)]
# g = sns.scatterplot(data=df, x='time', y='longevity', hue='rep', palette='plasma', linewidth=0, ax=ax, alpha=0.5, s=5)
# g = sns.jointplot(x='rep', y='longevity', data=df, kind='kde')
# df['fission rate'] *= 1000
# df['fusion_rate'] *= 1000
# df= df[(df['time'] > 270000)]
# g = sns.pairplot(data=df, vars=["longevity", 'fission rate', "fusion_rate", 'rep'], kind='hist')
# g = sns.pairplot(data=df, vars=["longevity", 'fission rate', "fusion_rate", 'rep'], plot_kws={'linewidth':0, 'alpha':0.6, 's':3}, hue='path')
# df['time'] = df['time'].round(decimals=-3)
# df = df.groupby(by='time').mean().reset_index()
# g = sns.pairplot(data=df, vars=['fission events', "fusion events", 'fission rate', "fusion_rate"], kind='hist')
# title = "time of life of hosts through time\n"
# for kw, ix in keywords.getkeywordix():
#     # print(title, kw, df[kw][0])
#     title += (kw + " = " + str(processed[path][kw][0]) + '\n')
# g.set_title(title, y=0.9)
fig.tight_layout()
# ax.title.set_y(0.5)
# fig.subplots_adjust(top=0.8)
plt.savefig(keywords.nfile("allpair.png"))
plt.close()


"""  This will take data from the Canadian Henley Regatta and do various stuff with it
Program by Alexander Condello
Data from RegattaMaster
Regatta is the Canadian Henley Regatta
"""

# Downloaded from RegattaMaster, then delimted by semi-colons. Assumes that header format is the same for all
REGATTA_LIST = ["132nd_Royal_Canadian_Henley_Regatta_ResultsByAthletes.csv",\
                "131th_Royal_Canadian_Henley_Regatta_ResultsByAthletes.csv"]

import numpy as np

def _AC_gen_from_text(data):
    """Helper function for opening the csv files"""
    return np.genfromtxt(open(data,'r'), delimiter=';', skip_header=1, dtype=str)

def _AC_gen_date_playerdict(dataset, headers):
    """Generates a dictionary with the form:
    dict['Date']['Player Name'] = (Event Time, Event Name)"""

    name_i = headers.index('Name')
    date_i = headers.index('Date')
    event_i = headers.index('Event Name')
    time_i = headers.index('Time')
    date_dict = {}

    for entry in dataset :
        if entry[date_i] in date_dict :
            if entry[name_i] in date_dict[entry[date_i]] :
                date_dict[entry[date_i]][entry[name_i]].append((entry[time_i],entry[event_i]))
            else :
                date_dict[entry[date_i]][entry[name_i]]= [(entry[time_i],entry[event_i])]
        else :
            date_dict[entry[date_i]] = {entry[name_i]:[(entry[time_i],entry[event_i])]}
    return date_dict

def _AC_gen_date_boatdict(playerdict):
    """Generates a dictionary with the form:
    dict['Date'][('Time','Event Name')] = [Player1,Player2,...,PlayerN]"""

    boatdict = {}
    for date in playerdict :
        boatdict[date]={}
        for player in playerdict[date] :
            for (time, boat) in playerdict[date][player] :
                if (boat,time) not in boatdict[date] :
                    boatdict[date][(time,boat)]=[player]
                else :
                    boatdict[date][(time,boat)].append(player)
    return boatdict

def _format_date(date) :
    date_list = date.split('/')
    if len(date_list[0])==1: date_list[0] = '0'+date_list[0]
    if len(date_list[1])==1: date_list[1] = '0'+date_list[1]
    date_str = date_list[2]+date_list[0]+date_list[1]
    return int(date_str)

def _format_racetime(time):
    time_split = time.split('.')
    try : decimal = float('.'+ time_split[1])
    except : decimal = 0.0
    seconds = float(time_split[0][-2:])
    try :minutes = float(time_split[0][:-2])
    except : minutes = 0
    return 60*minutes+seconds+decimal

def _AC_format_dataset (entry, et_i, rt_i, dt_i) :
 # format time
   # et_i = self.headers.index('Time')
   # rt_i = self.headers.index('Adj. Time\n')
   # dt_i = self.headers.index('Date')

    event_time_list = entry[et_i][-5:].split(':')
    event_time = int(event_time_list[0])*60+int(event_time_list[1])
    race_time = _format_racetime(entry[rt_i])
    date = _format_date(entry[dt_i])

    entry[et_i] = event_time
    entry[rt_i] = race_time
    entry[dt_i] = date

    return entry

def import_dataset(regatta_list, previous_race_percentage=True, name=False, return_dicts=False) :
    ##NOT FUNCTIONAL
    """Generates the nice dataset in various forms.
    Inputs:
        previous_race_percentage : True/False
        name : True/False
"""
    assert type(previous_race_percentage)==bool \
           and type(name)==bool \
           and type(return_dicts)==bool, \
           ValueError('Test')

    data_set_fields = [('Date','int'), # YYYYMMDD
                       ('ID','int'), # ?
                       ('Event Time','int'), # in minutes since midnight
                       ('Time','float')] # in seconds
    if previous_race_percentage : data_set_fields.append(('Previous Race %','int'))
    if name : data_set_fields.insert(1,('Name','str'))

    raw_dataset = reduce(lambda x, y: np.concatenate((x,y)),
                         map(_AC_gen_from_text, regatta_list))
    with open(regatta_list[0],'r') as f : headers = f.readline().split(';')
    # ['Name', 'Event Name', 'Distance', 'Date', 'Time', 'Race Status', '', 'Level', 'Race Id', "Athlete's Club", 'Entry Club', 'Place', 'Seat No.', 'Seat', 'Status', 'Adj. Time\n']

    # Format the dataset
    #    Year -> YYYYMMDD
    #    Race Time -> seconds
    #    Event Time -> minutes since midnight
    #    Event Name -> (Event)-(Club)-(A)        will update the A/B later somehow
    raw_dataset = map(lambda x : _AC_format_dataset(x, et_i=headers.index('Time'),
                                         rt_i=headers.index('Adj. Time\n'),
                                         dt_i=headers.index('Date')), raw_dataset)

    date_playerdict = _AC_gen_date_playerdict(raw_dataset, headers)
    date_boatdict = _AC_gen_date_boatdict(date_playerdict)

    if return_dicts : return [], {}, date_playerdict
    else : return []

if __name__ == '__main__' :
    raw_dataset, boat_dict, player_dict  = import_dataset(REGATTA_LIST, return_dicts = True)


from geo_dict.postgis.connection import connect

def process_street(street_name):
    conn = connect()

    cur = conn.cursor()

    cur.execute("""SELECT node_id, sequence_id FROM way_nodes
WHERE way_id IN (SELECT way_id FROM way_tags WHERE k LIKE 'name' AND lower(v) LIKE '""" +
                street_name.lower() + """');""")

    row = cur.fetchall()

    way_parts, l, i = [], [], 0

    for r in row:
        l.append(r[0])
        if i + 1 < len(row) and row[i + 1][1] == 0:
            way_parts.append(l)
            l = []
        i += 1
    way_parts.append(l)

    # szukam rozwidlen, czyli innych odcinkow, ktore tez mozna dolaczyc do obecnego fragmentu w rozwidleniach takich, koniec dodatkowej drogi jest taki jak koniec obecnego fragmentu przed dodaniem nowego

    rozwidlenia = []  #jest to lista list - kazda lista w srodku to grupy rozwidlen w tym samym punkcie
    for i in range(len(way_parts) - 1):
        r = []
        for j in range(i + 1, len(way_parts)):
            if way_parts[i][-1] == way_parts[j][-1]:
                if len(
                        r) == 0:  #to jest po to, zeby way_parts[i] nie mogl byc dodany kilka razy (przy kilku rozwidleniach)
                    r.append(way_parts[i])
                r.append(way_parts[j])
        if len(r) > 0:
            rozwidlenia.append(r)

    #usuwam rozwidlenia - wywalam te fragmenty, ktore sa krotsze
    for r in rozwidlenia:
        while len(r) > 1:
            cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(r[0][0]) + """;""")
            geom1 = cur.fetchone()
            cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(r[0][-1]) + """;""")
            geom2 = cur.fetchone()
            cur.execute("""SELECT ST_Distance(ST_Transform('""" + geom1[0] + """'::text,26986),ST_Transform('""" + geom2[
                0] + """'::text,26986));""")
            dist1 = cur.fetchone()

            cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(r[1][0]) + """;""")
            geom1 = cur.fetchone()
            cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(r[1][-1]) + """;""")
            geom2 = cur.fetchone()
            cur.execute("""SELECT ST_Distance(ST_Transform('""" + geom1[0] + """'::text,26986),ST_Transform('""" + geom2[
                0] + """'::text,26986));""")
            dist2 = cur.fetchone()
            if dist1[0] > dist2[0]:
                way_parts.remove(r[1])
                r.remove(r[1])
            else:
                way_parts.remove(r[0])
                r.remove(r[0])

    #lacze poczatki odcinkow drog z koncami kolejnych odcinkow
    ways = []
    w = way_parts[0]
    way_parts.remove(w)
    while len(way_parts) > 0:
        last = w[-1]
        first = w[0]
        found = False
        for i in range(len(way_parts)):
            if way_parts[i][0] == last:  #wiaze poczatek jakiegos fragmentu z koncem obecnego
                w.extend(way_parts[i][1:])  #jak znalazlem, to rozszerzam obecny fragment
                found = True
                break
        if not found:
            for i in range(len(way_parts)):
                if way_parts[i][-1] == first:  #wiaze koniec jakiegos fragmentu z poczatkiem obecnego
                    w1 = way_parts[i]
                    w1.extend(w[1:])  #lacze te fragmenty
                    w = w1
                    found = True
                    break

        if found:
            way_parts.remove(way_parts[i])  #usuwam znaleziony fragment
        else:
            ways.append(w)
            w = way_parts[0]
            way_parts.remove(w)

    ways.append(w)

    #lacze poprzerywane odcinki
    while len(ways) > 1:
        d = {}  #slownik: klucz: odleglosc miedzy dwoma odcinkami (miedzy koncem pierwszego i poczatkiem drugiego)
        #wartosc: tuple(indeks odcinka 1, indeks odcinka 2, odcinek1_inversed (boolean), odcinek2_inversed (boolean)
        #trzecia i czwarta wartosc tupli jest potrzebna, bo niektore odcinki sa odwrocone wzgledem siebie
        for i in range(len(ways)):
            for j in range(len(ways)):
                if i != j:
                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[i][-1]) + """;""")
                    geom1 = cur.fetchone()
                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[j][0]) + """;""")
                    geom2 = cur.fetchone()
                    cur.execute(
                        """SELECT ST_Distance(ST_Transform('""" + geom1[0] + """'::text,26986),ST_Transform('""" + geom2[
                            0] + """'::text,26986));""")
                    dist = cur.fetchone()
                    d[dist] = (i, j, False, False)

                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[i][-1]) + """;""")
                    geom1 = cur.fetchone()
                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[j][-1]) + """;""")
                    geom2 = cur.fetchone()
                    cur.execute(
                        """SELECT ST_Distance(ST_Transform('""" + geom1[0] + """'::text,26986),ST_Transform('""" + geom2[
                            0] + """'::text,26986));""")
                    dist = cur.fetchone()
                    d[dist] = (i, j, False, True)

                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[i][0]) + """;""")
                    geom1 = cur.fetchone()
                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[j][0]) + """;""")
                    geom2 = cur.fetchone()
                    cur.execute(
                        """SELECT ST_Distance(ST_Transform('""" + geom1[0] + """'::text,26986),ST_Transform('""" + geom2[
                            0] + """'::text,26986));""")
                    dist = cur.fetchone()
                    d[dist] = (i, j, True, False)

                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[i][0]) + """;""")
                    geom1 = cur.fetchone()
                    cur.execute("""SELECT geom FROM nodes WHERE id = """ + str(ways[j][-1]) + """;""")
                    geom2 = cur.fetchone()
                    cur.execute(
                        """SELECT ST_Distance(ST_Transform('""" + geom1[0] + """'::text,26986),ST_Transform('""" + geom2[
                            0] + """'::text,26986));""")
                    dist = cur.fetchone()
                    d[dist] = (i, j, True, True)

        min_dist = min(d.keys())
        w1_ind = d[min_dist][0]
        w2_ind = d[min_dist][1]
        w1_inv = d[min_dist][2]
        w2_inv = d[min_dist][3]

        if w1_inv:
            ways[w1_ind].reverse()
        if w2_inv:
            ways[w2_ind].reverse()
        ways[w1_ind].extend(ways[w2_ind])
        ways.remove(ways[w2_ind])

    cur.close()
    conn.close()

    return ways[0]


import ansible.utils as utils
import ansible.errors as errors

import pprint

class LookupModule(object):

    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir

    def run(self, terms, **kwargs):

        if not isinstance(terms, list):
            raise errors.AnsibleError("with_ini_configs expects a list of dicts")

        ret = []
        for item in terms:
            for ini in item:
                for section in ini["sections"]:
                    for directive in section["options"]:
                        ret.append({'name': ini["name"], 'path': ini["path"], 'section': section["name"], 'option': directive.keys()[0], 'value': directive[directive.keys()[0]] })

        return ret


# !usr/bin/python3
# -*- coding: utf-8 -*-

'''
3. Создайте список, содержащий элементы целочисленного типа, затем с помощью цикла перебора измените тип данных элементов на числа с плавающей точкой. (Подсказка: используйте встроенную функцию float().)

'''

sequence = [10, 20, 30, 40, 50]
i = 0
for item in sequence:
    sequence[i] = float(item)
    i += 1

print(*sequence)


import os
import sys
project_path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(project_path)
# print(sys.path)
# print(os.path.dirname(__file__))
# print(sys.modules)
from model import pool
from model import logging
from model import oracle
from exception.statuCode import StatuCode
from exception.tools import ToolsHelp

def getCompanys():
    comStr = 'select * from zf_company' # 查询字符串
    try:
        connection = pool.acquire() # 对oracle连接对象池中获取连接
        cursor = connection.cursor() # 获取游标
        cursor.execute(comStr) # 执行SQL语句
        content = cursor.fetchall() # 获取行数据,返回一个元组类型的list列表
        cloumns = cursor.description # 获取当前表查询的列名
        return ToolsHelp.formateData(content,cloumns) # 调用formateData()工具方法格式化数据
    except oracle.DatabaseError as msg:
        logging.info(msg)
        return StatuCode.selectTabelError.value
    except Exception as e:
        logging.info(e)
        return StatuCode.unknowError.value
    finally:
        cursor.close() # 关闭游标
        pool.release(connection) # 释放连接对象回连接池

def insertCompany(companyName,mcNum):
    comStr = 'insert into zf_company (c_name,c_mc_num) values (:name,:mc_num)' # 带参数的插入语句
    try:
        connection = pool.acquire()
        cursor = connection.cursor()
        cursor.execute(comStr,{'name':companyName,'mc_num':mcNum}) # 带参数的执行语句
        connection.commit() # 通过连接对象提交
        return StatuCode.successCode.value
    except oracle.DatabaseError as msg:
        logging.info(msg)
        return StatuCode.insertDataError.value
    except Exception as e:
        logging.info(e)
        return StatuCode.unknowError.value
    finally:
        cursor.close() # 关闭游标
        pool.release(connection) # 释放连接对象回连接池

def delCompany(id:int = None,companyName:str = None):
    comStr = ''
    paramers = None
    if id == None and companyName != None:
        comStr = 'delete from zf_company where c_name = :arg'
        paramers = (companyName,)
    elif id !=None and companyName == None:
        comStr = 'delete from zf_company where c_id = :arg'
        paramers = (id,)
    elif id != None and companyName != None:
        comStr = 'delete from zf_company where c_id = :id and c_name = :name'
        paramers = {'id':id,'name':companyName}
    else:
        logging.info('未指定查询语句')
    try:
        connection = pool.acquire()
        cursor = connection.cursor()
        cursor.execute(comStr,paramers) # 带参数的执行语句
        connection.commit() # 通过连接对象提交
        return StatuCode.successCode.value
    except oracle.DatabaseError as msg:
        logging.info(msg)
        return StatuCode.deleteDataError.value
    except Exception as e:
        logging.info(e)
        return StatuCode.unknowError.value
    finally:
        cursor.close() # 关闭游标
        pool.release(connection) # 释放连接对象回连接池

def updateCompany(id,newCompanyName):
    comStr = 'update zf_company t set t.c_name = :name where t.c_id = :id' # 更新语句
    try:
        connection = pool.acquire()
        cursor = connection.cursor()
        cursor.execute(comStr,{'name':newCompanyName,'id':id}) # 参数为dict的执行语句,通过key匹配值
        connection.commit() # 通过连接对象提交
        return StatuCode.successCode.value
    except oracle.DatabaseError as msg:
        logging.info(msg)
        return StatuCode.insertDataError.value
    except Exception as e:
        logging.info(e)
        return StatuCode.unknowError.value
    finally:
        cursor.close() # 关闭游标
        pool.release(connection) # 释放连接对象回连接池

if __name__ == "__main__":
    # insertCompany('石门县住建局',2)
    # updateCompany(21,'石门县住保办')
    # ret = delCompany('石门县主板宝')
    # ret = delCompany(companyName='石门县住保办')
    # ret = updateCompany(9,'石门县文学艺术联合会')
    res = getCompanys()
    print(res)


"""
Django settings for tsr project on Heroku. Fore more info, see:
https://github.com/heroku/heroku-django-template

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import dj_database_url

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "*-4na9^d7c6l51me3+@3%#sbs#vjoc0k)w*3=i65&ped4(9t#o"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.sites',
    # Disable Django's own staticfiles handling in favour of WhiteNoise, for
    # greater consistency between gunicorn and `./manage.py runserver`. See:
    # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',

    #theme
    'bootstrap3',

    #external
#    'notification',
    'account',
    'pinax.eventlog',
    'pinax.webanalytics',
    'avatar',
    'localflavor',
    'storages',
    'actstream',
    'formtools',
    'ajax_select',
    'postman',
    'rest_framework',

    #tsr
    'common',
    'tsr_accounts',
    'tsr_venues',
    'tsr_events',

    #dev
    'django_extensions',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    "account.middleware.LocaleMiddleware",
    "account.middleware.TimezoneMiddleware",
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tsr.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT, "templates"),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'pinax_theme_bootstrap.context_processors.theme',
                'account.context_processors.account',
            ],

            'debug': DEBUG,
        },
    },
]

WSGI_APPLICATION = 'tsr.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

SITE_ID = 1

#email

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

#storages

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_S3_SECURE_URLS = False
AWS_QUERYSTRING_AUTH = False
AWS_S3_ACCESS_KEY_ID = ''
AWS_S3_SECRET_ACCESS_KEY = ''
AWS_STORAGE_BUCKET_NAME = 'tsrlocal'

#accounts settings
ACCOUNT_SIGNUP_REDIRECT_URL = "/account/signup/preference"

#postman
POSTMAN_AUTO_MODERATE_AS = True
POSTMAN_DISALLOW_ANONYMOUS = True
POSTMAN_NOTIFIER_APP = None

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

ACTSTREAM_SETTINGS = {
    'MANAGER': 'tsr.managers.TSRManager',
    'FETCH_RELATIONS': True,
    'USE_PREFETCH': True,
    'USE_JSONFIELD': True,
    'GFK_FETCH_DEPTH': 1,
}


import abc
import logging
import numpy as np
import os
import os.path
import pandas as pd
import shutil
import six
import tensorflow as tf

logger = logging.getLogger(__name__)

class BaseModel(object):
    def __init__(
        self, model_type, model_path, input_nodes, output_nodes
    ):
        self.model_type = model_type
        self.model_path = model_path
        self.input_nodes = input_nodes
        self.output_nodes = output_nodes
        self.features = [
            tf.contrib.layers.real_valued_column(
                'inputs', dimension=len(self.input_nodes)
            )
        ]
        logger.debug('features: %s', self.features)
        self.estimators = self.create_estimators()

    def get_test_result_mse(self, prediction, output):
        logger.debug('prediction: %s', prediction)
        logger.debug('ouput: %s', output)
        return np.mean(np.square(prediction - output))

    def get_test_result_rsquare(self, prediction, output):
        output_mean = np.mean(output)
        ss_tot = np.sum(np.square(output - output_mean))
        ss_reg = np.sum(np.square(prediction - output))
        if ss_tot < 0.01:
            return 0
        return 1 - ss_reg / ss_tot

    def test_data(
        self, input_data, output_data
    ):
        x = self.get_inputs(input_data)
        predictions = {}
        expectations = {}
        statistics = {}
        for column, estimator in six.iteritems(self.get_estimators()):
            prediction = np.array(list(estimator.predict(x=x)))
            expected = self.get_output(output_data, column)
            predictions[column] = prediction
            expectations[column] = expected
            statistics[column] = {
                'MSE': self.get_test_result_mse(
                    prediction, expected
                ),
                'rsquare': self.get_test_result_rsquare(
                    prediction, expected
                )
            }
        return {
            'predictions': pd.DataFrame(predictions, index=output_data.index),
            'expectations': pd.DataFrame(
                expectations, index=output_data.index
            ),
            'statistics': statistics
        }

    def create_estimator(self, model_path):
        return None

    def create_estimators(self):
        estimators = {}
        for node in self.output_nodes:
            path = os.path.join(self.model_path, '.'.join(node))
            if os.path.exists(path):
                shutil.rmtree(path)
            estimators[node] = self.create_estimator(path)
        return estimators

    def get_estimator(self, node):
        return self.estimators[node]

    def get_estimators(self):
        return self.estimators

    def get_outputs(self, output_data):
        return {
            'outputs': output_data[self.output_nodes].values
        }

    def get_output(self, output_data, column):
        return output_data[column].values

    def get_inputs(self, input_data):
        return {
            'inputs': input_data[self.input_nodes].values
        }

    def get_input(self, input_data, column):
        return input_data[column].value

    def train(
        self, input_data, output_data
    ):
        x = self.get_inputs(input_data)
        for column, estimator in six.iteritems(self.get_estimators()):
            y = self.get_output(output_data, column)
            input_fn = tf.contrib.learn.io.numpy_input_fn(
                x,
                y,
                batch_size=100,
                num_epochs=1000
            )
            estimator.fit(input_fn=input_fn, steps=1000)
        return self.test_data(
            input_data, output_data
        )

    def test(self, input_data, output_data):
        return self.test_data(
            input_data, output_data
        )

    def apply(self, input_data, output_data):
        pass

    def get_estimator_variables(self, estimator):
        estimator_variables = {}
        for variable_name in estimator.get_variable_names():
            variable = estimator.get_variable_value(variable_name)
            logger.debug('variable %s value %r', variable_name, variable)
            estimator_variables[variable_name] = variable.tolist()
        return estimator_variables

    def save(self):
        estimators_variables = {}
        for column, estimator in six.iteritems(self.get_estimators()):
            column_name = '.'.join(column)
            estimators_variables[column_name] = self.get_estimator_variables(
                estimator
            )
        return estimators_variables

    def load(self):
        self.estimators = self.create_estimators()

    def __str__(self):
        return '%s[model_type=%s]' % (
            self.__class__.__name__, self.model_type
        )

@six.add_metaclass(abc.ABCMeta)
class BaseModelBuilder(object):
    def create_model(
        self, model_type, model_path, input_nodes, output_nodes
    ):
        return BaseModel(
            model_type, model_path, input_nodes, output_nodes
        )

    def get_model(
        self, model_type, model_path, input_nodes, output_nodes
    ):
        return self.create_model(
            model_type, model_path, input_nodes, output_nodes
        )

    def __str__(self):
        return '%s' % self.__class__.__name__


import datetime
from datetime import datetime as dt, timedelta
import boto3
import time
import sys

def check_dt_hour(key, date_hours_list):
    path = key.split("/")[-6] + "/" + key.split("/")[-5] + "/" + key.split("/")[-4] + "/" + key.split("/")[-3]
    if path in date_hours_list:
        return True
    else:
        return False

def get_date_lists(start_hour, end_hour):
    date_hours_list = []
    date_list = []
    while start_hour <= end_hour:
        date_hours_list.append(start_hour.strftime('%Y/%m/%d/%H'))
        if start_hour.date().strftime('%Y/%m/%d/') not in date_list:
            date_list.append(start_hour.date().strftime('%Y/%m/%d/'))
        start_hour += timedelta(hours=1)
    return date_hours_list, date_list

def check_folder_existence(s3_client, bucket, location):
    try:
        s3_client.list_objects(Bucket=bucket, Prefix=location)['Contents']
    except KeyError as e:
        return False
    return True

def get_list_of_folders(s3_client, bucket, prefix, date_list, date_hours_list):
    list_of_folders = []
    paginator = s3_client.get_paginator('list_objects')
    for date in date_list:
        location = prefix + date
        operation_parameters = {'Bucket': bucket,
                                'Prefix': location}
        if check_folder_existence(s3_client, bucket, location):
            page_iterator = paginator.paginate(**operation_parameters)
            for page in page_iterator:
                all_folders = page['Contents']
                for key in all_folders:
                    if key['Key'][-1:] != '/' and check_dt_hour(key['Key'], date_hours_list):
                        files_location = 's3://' + bucket + '/' + '/'.join(key['Key'].split('/')[0:-1])
                        if files_location not in list_of_folders:
                            list_of_folders.append(files_location)
        else:
            continue
    return list_of_folders

def create_file(s3_client, bucket, output_file_location, output_file, files_list):
    f = open(output_file, "w+")
    for item in files_list:
        f.write(str(item) + '\n')
    f.close()
    s3_client.upload_file(output_file, bucket, output_file_location)

if __name__ == "__main__":
    # read input params
    bucket = sys.argv[1].replace('\r', '')
    prefix = sys.argv[2].replace('\r', '')
    lookback = int(sys.argv[3].replace('\r', ''))
    start = sys.argv[4].replace('\r', '')
    bookmark_file_bucket = sys.argv[5].replace('\r', '')
    bookmark_file_location = sys.argv[6].replace('\r', '')

    # calculate parameters
    s3_client = boto3.client('s3')
    start_hour = dt.strptime(start, "%Y-%m-%dT%H:%M:%S")
    # create end hour by substracting 2h from the current timestamp(last hourly folder from which we gona read data)
    end_hour = (datetime.datetime.now() - timedelta(hours=lookback)).replace(microsecond=0, second=0, minute=0)
    next_load_start_hour = end_hour + timedelta(hours=1)
    output_file = str(int(time.time())) + "_" + str(end_hour.strftime("%Y_%m_%dT%H_%M_%S")) + ".txt"
    output_file_location = bookmark_file_location + output_file
    output_file_path = 's3://' + bookmark_file_bucket + '/' + output_file_location

    date_hours_list, date_list = get_date_lists(start_hour, end_hour)
    locations = get_list_of_folders(s3_client, bucket, prefix, date_list, date_hours_list)
    if not locations:
        print("FILE_NAME=NO_FILES_TO_LOAD")
    else:
        create_file(s3_client, bookmark_file_bucket, output_file_location, output_file, locations)
        print("FILE_NAME=" + output_file_path)
    print("NEXT_LOAD_START_HOUR=" + str(next_load_start_hour.strftime("%Y-%m-%dT%H:%M:%S")))


# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

import configparser

from UM.Settings import SettingsError
from UM.Logger import Logger
from UM.Signal import Signal, SignalEmitter
from UM.SaveFile import SaveFile
from UM.Settings.Profile import Profile

##    A machine instance is a sort of wrapper for a machine definition.
#     Where machine defintion defines base values of a machine
#     The machine instance defines specific overrides of certain settings that are only
#     valid for this -single instance- of the machine (eg; Some after market modification)
class MachineInstance(SignalEmitter):
    MachineInstanceVersion = 1

    def __init__(self, machine_manager, **kwargs):
        super().__init__()

        self._machine_manager = machine_manager
        self._key = kwargs.get("key")
        self._name = kwargs.get("name", "")
        self._machine_definition = kwargs.get("definition", None)
        if self._machine_definition:
            self._machine_definition.loadAll()
        self._machine_setting_overrides = {}

        self._active_profile_name = None
        self._active_material_name = None

        self._working_profile = Profile(machine_manager)
        self._working_profile.setType("machine_instance_profile")

    nameChanged = Signal()

    ##  Get key of this machine instance.
    #   This is different from the name in respect that it need not be human readable
    #   The difference is simmilar with that of key & label for the settings.
    #   \sa setKey
    def getKey(self):
        return self._key

    ##  Set key of this machine instance.
    #   This is different from the name in respect that it need not be human readable
    #   The difference is simmilar with that of key & label for the settings.
    #   \sa getKey
    def setKey(self, key):
        self._key = key

    def getName(self):
        return self._name

    def setName(self, name):
        if name != self._name:
            old_name = self._name
            self._name = self._machine_manager.makeUniqueMachineInstanceName(name, self._machine_definition.getName(), old_name)
            self.nameChanged.emit(self, old_name)

    def getWorkingProfile(self):
        return self._working_profile

    def getActiveProfileName(self):
        return self._active_profile_name

    def setActiveProfileName(self, active_profile_name):
        self._active_profile_name = active_profile_name

    def getMaterialName(self):
        return self._active_material_name

    def setMaterialName(self, material_name):
        self._active_material_name = material_name

    def hasMaterials(self):
        return len(self._machine_manager.getAllMachineMaterials(self._name)) > 0

    def getMachineDefinition(self):
        return self._machine_definition

    def setMachineDefinition(self, definition):
        if not definition:
            return

        definition.loadAll()
        self._machine_definition = definition

    def setMachineSettingValue(self, setting, value):
        if not self._machine_definition.isMachineSetting(setting):
            Logger.log("w", "Tried to override setting %s that is not a machine setting", setting)
            return

        self._machine_setting_overrides[setting] = value

    def getMachineSettingValue(self, setting):
        if not self._machine_definition.isMachineSetting(setting):
            return

        if setting in self._machine_setting_overrides:
            return self._machine_setting_overrides[setting]

        return self._machine_definition.getSetting(setting).getDefaultValue()

    def getSettingValue(self, key):
        if not self._machine_definition.isSetting(key):
            return None

        if key in self._machine_setting_overrides:
            return self._machine_setting_overrides[key]

        return self._machine_definition.getSetting(key).getDefaultValue()

    def hasMachineSettingValue(self, key):
        return key in self._machine_setting_overrides

    def loadFromFile(self, path):
        config = configparser.ConfigParser(interpolation = None)
        config.read(path, "utf-8")

        if not config.has_section("general"):
            raise SettingsError.InvalidFileError(path)

        if not config.has_option("general", "version"):
            raise SettingsError.InvalidFileError(path)

        if not config.has_option("general", "name") or not config.has_option("general", "type"):
            raise SettingsError.InvalidFileError(path)

        if int(config.get("general", "version")) != self.MachineInstanceVersion:
            raise SettingsError.InvalidVersionError(path)

        type_name = config.get("general", "type")
        variant_name = config.get("general", "variant", fallback = "")

        self._machine_definition = self._machine_manager.findMachineDefinition(type_name, variant_name)
        if not self._machine_definition:
            raise SettingsError.DefinitionNotFoundError(type_name)
        self._machine_definition.loadAll()

        self._name = config.get("general", "name")
        self._key = config.get("general", "key", fallback = None)

        self._active_profile_name = config.get("general", "active_profile", fallback="")
        self._active_material_name = config.get("general", "material", fallback = "")

        for key, value in config["machine_settings"].items():
            self._machine_setting_overrides[key] = value

    def saveToFile(self, path):
        config = configparser.ConfigParser(interpolation = None)

        config.add_section("general")
        config["general"]["name"] = self._name
        config["general"]["type"] = self._machine_definition.getId()
        config["general"]["active_profile"] = str(self._active_profile_name)
        config["general"]["version"] = str(self.MachineInstanceVersion)

        if self._key:
            config["general"]["key"] = str(self._key)
        if self._machine_definition.getVariantName():
            config["general"]["variant"] = self._machine_definition.getVariantName()
        if self._active_material_name and self._active_material_name != "":
            config["general"]["material"] = self._active_material_name

        config.add_section("machine_settings")
        for key, value in self._machine_setting_overrides.items():
            config["machine_settings"][key] = str(value)

        try:
            with SaveFile(path, "wt", -1, "utf-8") as f:
                config.write(f)
        except Exception as e:
            Logger.log("e", "Failed to write Instance to %s: %s", path, str(e))


"""
Actual skill behaviour
"""

import random
from alexa_helper import *

KEY_SIDES_DICE = "KEY_SIDES_DICE"

def get_welcome_response():
    """
    Should output welcome message to the user
    """

    session_attributes = {KEY_SIDES_DICE: "6"}
    card_title = "Welcome"
    speech_output = "Hallo Eric. Geht es dir gut? Willkommen bei Wuerfelbox"
    reprompt_text = None
    should_end_session = False

    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def get_goodbye_response():
    """
    Should output a goodbye message
    """

    card_title = "Goodbye"
    speech_output = "Wiedersehen Eric. Habe noch einen guten Tag!"
    # Setting this to true ends the session and exits the skill.
    should_end_session = True
    reprompt_text = None

    return build_response({}, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def handle_intent_choose_dice(intent, session):
    """
    Should set the number of dice sides for this session
    """

    session_attributes = {}
    speech_output = "Ich habe dicht nicht gut verstanden. Wieviele Seiten soll der Wuerfel haben?" \
                    "Sag: Alexa nimm einen acht seitigen Wuerfel."
    reprompt_text = "Ich habe dicht nicht gut verstanden. Wieviele Seiten soll der Wuerfel haben?" \
                    "Sag: Alexa nimm einen acht seitigen Wuerfel."

    dice_sides = get_slot_value_from_intent(intent, KEY_SIDES_DICE)
    if dice_sides:
        session_attributes[KEY_SIDES_DICE] = dice_sides
        speech_output = "In Ordnung. Ich werde einen " + dice_sides + " seitigen Wuerfel verwenden."
        reprompt_text = None

    card_title = intent['name']
    should_end_session = False

    return build_response(
        session_attributes,
        build_speechlet_response(card_title, speech_output, reprompt_text, should_end_session)
    )

def handle_intent_roll_dice(intent, session):
    """Should throw a random dice and return result"""

    speech_output = "Wieviele Seiten soll der Wuerfel haben?" \
                    "Sag: Alexa wirf einen Wuerfel."
    reprompt_text = "Ich habe dicht nicht gut verstanden. Soll ich einen Wuerfel werfen?" \
                    "Sag: Alexa wirf einen Wuerfel."

    dice_sides = get_value_from_session(session, KEY_SIDES_DICE)
    if dice_sides:
        throw_result = random.randint(1, int(dice_sides))
        speech_output = "In Ordnung. Ich werfe einen "  + dice_sides + " seitigen Wuerfel." \
                        "Das Ergebnis des Wurfes ist " + str(throw_result)
        reprompt_text = None

    card_title = intent['name']
    session_attributes = {}
    should_end_session = False

    return build_response(
        session_attributes,
        build_speechlet_response(card_title, speech_output, reprompt_text, should_end_session)
    )


from meta.types import (
    UInt32, RecordType, SetType, VectorType, MapType
)
from linkedin.common import (
    fields, verify_type_names, URL, Name
)

#
# Root
#

Root = RecordType("Root", fields(
    base_url = URL,
    job_pages = MapType(URL, Name),
    queue = VectorType(URL),
    active_requests = SetType(URL),
    completed = UInt32
))

#
# verify_type_names
#

verify_type_names(globals())


"""
IO

End-Points (sources and sinks) for streaming data processing.
"""

json_parser = None
try:
    import orjson as json

    json_parser = "orjson"
except ImportError:
    pass
if not json_parser:
    try:
        import ujson

        json_parser = "ujson"
    except ImportError:
        import json
import csv
from pathlib import Path
from ._datasets import select_fields

def to_csv(dataset, filename, columns=["first_row"]):
    """
    Saves a dataset as a CSV
    """
    import csv

    with open(filename, "w", encoding="utf8", newline="") as file:

        # get the first record
        row = dataset.__next__()

        # get the columns from the record
        if columns == ["first_row"]:
            columns = dataset.keys()

        # write the headers
        csv_file = csv.DictWriter(file, fieldnames=columns)
        csv_file.writeheader()

        # cycle the rest of the file
        while row:
            row = select_fields(row, columns)
            csv_file.writerow(row)
            row = dataset.__next__()

def read_jsonl(filename, limit=-1, chunk_size=16*1024*1024, delimiter="\n"):
    """"""
    file_reader = read_file(filename, chunk_size=chunk_size, delimiter=delimiter)
    line = next(file_reader)
    while line:
        yield json.loads(line)
        limit -= 1
        if limit == 0:
            return
        try:
            line = next(file_reader)
        except StopIteration:
            return

def read_file(filename, chunk_size=16*1024*1024, delimiter="\n"):
    """
    Reads an arbitrarily long file, line by line
    """
    with open(filename, "r", encoding="utf8") as f:
        carry_forward = ""
        chunk = "INITIALIZED"
        while len(chunk) > 0:
            chunk = f.read(chunk_size)
            augmented_chunk = carry_forward + chunk
            lines = augmented_chunk.split(delimiter)
            carry_forward = lines.pop()
            yield from lines
        if carry_forward:
            yield carry_forward

def read_csv_lines(filename):
    with open(filename, "r", encoding="utf-8") as csvfile:
        datareader = csv.reader(csvfile)
        headers = next(datareader)
        row = next(datareader)
        while row:
            yield dict(zip(headers, row))
            try:
                row = next(datareader)
            except StopIteration:
                row = None

def write_jsonl(filename, data):
    with open(filename, "w", encoding="utf-8") as jsonfile:
        for r in data:
            try:
                jsonfile.write(json.dumps(r) + "\n")
            except ValueError:
                jsonfile.write("*****" + "\n")

# https://gist.github.com/nwjlyons/621fabfc0d4c1119b2ad338f615ce4ef#file-chunks-py
def generator_chunker(generator, chunk_size):
    """Yield successive chunks from a generator"""
    chunk = []

    for item in generator:
        if len(chunk) >= chunk_size:
            yield chunk
            chunk = [item]
        else:
            chunk.append(item)

    if chunk:
        yield chunk

def clear_screen():
    print(chr(27) + "[2j")
    print("\033c")
    print("\x1bc")


"""
CamelCase: https://www.hackerrank.com/challenges/camelcase/problem

"""

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the camelcase function below.
def camelcase(s):
    c=1
    for i in range(len(s)):
        if 'A'<=s[i]<='Z':
            c+=1
    return c

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = camelcase(s)

    fptr.write(str(result) + '\n')

    fptr.close()


# dataset.py
# 加载这些数据啥的. 也可以分开来. 还是写一个演示吧~
import torch
from torchvision import datasets, transforms
from config import MNIST_DATASET_PATH, batch_size

# dataset什么时候可以用, 什么时候不行, 有待研究.
train_dataset = datasets.MNIST(root=MNIST_DATASET_PATH,
                               train=True,
                               transform=transforms.ToTensor(),
                               download=False)

test_dataset = datasets.MNIST(root=MNIST_DATASET_PATH,
                              train=False,
                              transform=transforms.ToTensor(),
                              download=False)

# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False)


# qr_decomposition.py
"""Volume 1: The QR Decomposition.
Nathan Kibanoff
BUDS Training Program
1 September 2019
"""
import numpy as np
from scipy import linalg as la

# Problem 1
def qr_gram_schmidt(A):
    """Compute the reduced QR decomposition of A via Modified Gram-Schmidt.

    Parameters:
        A ((m,n) ndarray): A matrix of rank n.

    Returns:
        Q ((m,n) ndarray): An orthonormal matrix.
        R ((n,n) ndarray): An upper triangular matrix.
    """
    m,n=A.shape
    Q=np.copy(A)
    R=np.zeros((n,n))
    for i in range(n):
        R[i,i]=la.norm(Q[:,i])
        Q[:,i]/=R[i,i]
        for j in range(i+1,n):
            R[i,j]=Q[:,j].T@Q[:,i]
            Q[:,j]-=R[i,j]*Q[:,i]
    return Q,R

# Problem 2
def abs_det(A):
    """Use the QR decomposition to efficiently compute the absolute value of
    the determinant of A.

    Parameters:
        A ((n,n) ndarray): A square matrix.

    Returns:
        (float) the absolute value of the determinant of A.
    """
    return abs(np.prod(np.diag(qr_gram_schmidt(A)[1])))

# Problem 3
def solve(A, b):
    """Use the QR decomposition to efficiently solve the system Ax = b.

    Parameters:
        A ((n,n) ndarray): An invertible matrix.
        b ((n, ) ndarray): A vector of length n.

    Returns:
        x ((n, ) ndarray): The solution to the system Ax = b.
    """
    n=A.shape[0]
    Q,R=qr_gram_schmidt(A)
    y=Q.T@b
    x=np.random.random((n,1))
    for i in range(n-1,-1,-1):
        x_n=y[i,0]
        for j in range(i+1,n):
            x_n-=R[i,j]*x[j,0]
        x[i,0]=x_n/R[i,i]
    return x

# Problem 4
def qr_householder(A):
    """Compute the full QR decomposition of A via Householder reflections.

    Parameters:
        A ((m,n) ndarray): A matrix of rank n.

    Returns:
        Q ((m,m) ndarray): An orthonormal matrix.
        R ((m,n) ndarray): An upper triangular matrix.
    """
    sign=lambda x:1 if x>=0 else -1
    m,n=A.shape
    R=np.copy(A)
    Q=np.identity(m)
    for k in range(n):
        u=np.copy(R[k:,k]) # m-k x 1
        u[0]+=sign(u[0])*la.norm(u)
        u/=la.norm(u)
        R[k:,k:]-=np.outer(2*u,u.T@R[k:,k:])
        Q[k:,:]-=np.outer(2*u,u.T@Q[k:,:])
    return Q.T,R

# Problem 5
def hessenberg(A):
    """Compute the Hessenberg form H of A, along with the orthonormal matrix Q
    such that A = QHQ^T.

    Parameters:
        A ((n,n) ndarray): An invertible matrix.

    Returns:
        H ((n,n) ndarray): The upper Hessenberg form of A.
        Q ((n,n) ndarray): An orthonormal matrix.
    """
    sign=lambda x:1 if x>=0 else -1
    m,n=A.shape
    H=np.copy(A)
    Q=np.identity(m)
    for k in range(n-2):
        u=np.copy(H[k+1:,k])
        u[0]+=sign(u[0])*la.norm(u)
        u/=la.norm(u)
        H[k+1:,k:]-=2*np.outer(u,u.T@H[k+1:,k:])
        H[:,k+1:]-=2*np.outer(H[:,k+1:]@u,u.T)
        Q[k+1:,:]-=2*np.outer(u,u.T@Q[k+1:,:])
    return H,Q.T


'''
Round 1C 2020 - Code Jam 2020
Overrandomized (9pts, 10pts, 17pts)

Problem
Note: Every time this statement says something is randomly chosen, it means "chosen uniformly at random across all valid possibilities, and independently from all other choices".

The company Banana Rocks Inc. just wrote a premium cloud-based random number generation service that is destined to be the new gold standard of randomness.

The original design was that a group of servers would receive a request in the form of a single positive integer M of up to U decimal digits and then respond with an integer from the range 1 through M, inclusive, chosen at random. However, instead of simply having the output written with digits 0 through 9 as usual, the servers were "overrandomized". Each server has a random subset of 10 distinct uppercase English letters to use as digits, and a random mapping from those letters to unique values between 0 and 9.

The formal description of the current situation is as follows: each server has a digit string D composed of exactly 10 different uppercase English letters. The digit string defines the mapping between letters and the base 10 digits: D's j-th character from the left (counting from 0) is the base 10 digit of value j. For example, if D were CODEJAMFUN then C would represent digit 0, O would represent digit 1 and N would represent digit 9. The number 379009 would be encoded as EFNCCN when using that digit string.

When receiving the i-th query with an integer parameter Mi, the server:
chooses an integer Ni at random from the inclusive range 1 through Mi,
writes it as a base 10 string with no leading zeroes using the j-th character of D (counting starting from 0) as the digit of value j, and
returns the resulting string as the response Ri.

We collected some data that we believe we can use to recover the secret digit string D from each server. We sent 104 queries to each server. For each query, we chose a value Mi at random from the range 1 through 10U - 1, inclusive, and received the response Ri, a string of up to U uppercase English letters. We recorded the pairs (Mi, Ri). As we were moving these records to a new data storage device, the values of all the integers Mi within the records of some servers became corrupted and unreadable.

Can you help us find each server's digit string D?

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case contains the records for one server and starts with a line containing a single integer U, representing that 10U - 1 is the inclusive upper bound for the range in which we chose the integers Mi to query that server. Then, exactly 104 lines follow. Each of these lines contains an integer Qi (in base 10 using digits 0 through 9, as usual) and a string Ri, representing the i-th query and response, respectively. If Qi = -1, then the integer Mi used for the i-th query is unknown. Otherwise, Qi = Mi.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the digit string D for the server examined in test case x.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 10.
D is a string of exactly 10 different uppercase English letters, chosen independently and uniformly at random from the set of all such strings.
Mi is chosen independently and uniformly at random from the range 1 through 10U - 1, inclusive, for all i.
Ni is chosen independently and uniformly at random from the range 1 through Mi, inclusive, for all i.
Ri is the base 10 representation of Ni, using the j-th digit from the left of D (counting starting from 0) as the digit of value j, for all i.

Test Set 1 (Visible Verdict)
Qi = Mi, for all i.
U = 2.

Test Set 2 (Visible Verdict)
Qi = Mi, for all i.
U = 16.

Test Set 3 (Visible Verdict)
Qi = -1, for all i.
U = 16.

The sample input is too big to display inline, so we are providing downloadable files instead for the input and output.
'''
from typing import List, Tuple

def main() -> None:
    testcases = int(input())
    for i in range(1, testcases + 1):
        u = int(input())
        samples = []
        for _ in range(10000):
            query, response = input().split()
            samples.append((int(query), response))
        out = crack(u, samples)
        print('Case #%d: %s' % (i, out))

def crack(u: int, samples: List[Tuple[int, str]]) -> str:
    D = [None] * 10
    samples.sort()
    for query, response in samples:
        if is_power_of_10(query) and len(str(query)) == len(response):
            D[1] = response[0]
            if query > 1:
                D[0] = response[1]
        elif query < 10:
            if any(response == D[i] for i in range(query)):
                continue
            D[query] = response
        else:
            for c in response:
                i_none = 0
                for i, d in enumerate(D):
                    if c == d:
                        break
                    if d is None:
                        D[i] = c
        #     lo = 10 ** (len(response) - 1)
        #     hi = min(query, 10**len(response) - 1)
        #     # for i in range(lo, hi + 1):
        #     #     pass
        if all(d is not None for d in D):
            break
    return ''.join(D)

def is_power_of_10(n: int) -> bool:
    while n > 9 and n % 10 == 0:
        n //= 10
    return n == 1

if __name__ == '__main__':
    main()


from fastapi import FastAPI, File
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
import nest_asyncio
from pyngrok import ngrok

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

@app.post("/files/")
async def create_file(file: bytes = File(...)):
    return {"file_size": len(file)}

ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)


import paho.mqtt.client as mqtt
import time
import random
import sys

name = sys.argv[1]

client = mqtt.Client(name)
client.connect("localhost")

def on_publish(client, userdata, result):
    print("data published \n")
    pass

client.on_publish = on_publish

# ret = client.publish("dwm/node/abc1/uplink/location", '{"position":{"x":1.3936733,"y":1.174517,"z":-0.26708269,"quality":81},"superFrameNumber":136}')
x = 5
y = 4
dx = 0.06
dy = 0.05
while True:
    ddx = x + dx
    ddy = y + dy
    if ddx >= 10 or ddx < 0:
        dx = -dx
        continue
    if ddy >= 10 or ddy < 0:
        dy = -dy
        continue
    x = ddx
    y = ddy
    ret = client.publish("dwm/node/" + name + "/uplink/location", '{"position":{"x":' + str(x) + ',"y":' + str(y) + ',"z":-0.26708269,"quality":81},"superFrameNumber":136}')
    print(x, y)
    time.sleep(0.01)

# ret2 = client.publish("abc", "xyz")
client.loop_start()    #start the loop
time.sleep(10)