"""Extreme support."""
import time
import re
from netmiko.cisco_base_connection import CiscoSSHConnection

class ExtremeExosBase(CiscoSSHConnection):
    """Extreme Exos support.

    Designed for EXOS >= 15.0
    """

    def session_preparation(self):
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging(command="disable clipaging")
        self.send_command_timing("disable cli prompting")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

    def set_base_prompt(self, *args, **kwargs):
        """
        Extreme attaches an id to the prompt. The id increases with every command.
        It needs to br stripped off to match the prompt. Eg.

            testhost.1 #
            testhost.2 #
            testhost.3 #

        If new config is loaded and not saved yet, a '* ' prefix appears before the
        prompt, eg.

            * testhost.4 #
            * testhost.5 #
        """
        cur_base_prompt = super().set_base_prompt(*args, **kwargs)
        # Strip off any leading * or whitespace chars; strip off trailing period and digits
        match = re.search(r"[\*\s]*(.*)\.\d+", cur_base_prompt)
        if match:
            self.base_prompt = match.group(1)
            return self.base_prompt
        else:
            return self.base_prompt

    def send_command(self, *args, **kwargs):
        """Extreme needs special handler here due to the prompt changes."""

        # Change send_command behavior to use self.base_prompt
        kwargs.setdefault("auto_find_prompt", False)

        # refresh self.base_prompt
        self.set_base_prompt()
        return super().send_command(*args, **kwargs)

    def config_mode(self, config_command=""):
        """No configuration mode on Extreme Exos."""
        return ""

    def check_config_mode(self, check_string="#"):
        """Checks whether in configuration mode. Returns a boolean."""
        return super().check_config_mode(check_string=check_string)

    def exit_config_mode(self, exit_config=""):
        """No configuration mode on Extreme Exos."""
        return ""

    def save_config(
        self, cmd="save configuration primary", confirm=False, confirm_response=""
    ):
        """Saves configuration."""
        return super().save_config(
            cmd=cmd, confirm=confirm, confirm_response=confirm_response
        )

class ExtremeExosSSH(ExtremeExosBase):
    pass

class ExtremeExosTelnet(ExtremeExosBase):
    def __init__(self, *args, **kwargs):
        default_enter = kwargs.get("default_enter")
        kwargs["default_enter"] = "\r\n" if default_enter is None else default_enter
        super().__init__(*args, **kwargs)


import os
import configparser

root_path = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), '..', '..')
config_path = os.path.join(root_path, 'config')
peers_path = os.path.join(config_path, 'peers.ini')

class CfgPeers:
    # for Alice/Bob and Charlie 1

    def read_config_peers(self, user):
        config = configparser.ConfigParser()
        config.read(peers_path)
        ip_address = config.get(user, 'ip_address')
        port_number = config.getint(user, 'port_number')
        return ip_address, port_number

    # for Tracker 2
    def read_config_peers_all(self):
        config_p = configparser.ConfigParser()
        config_p.read(peers_path)
        peers_info = {}
        for peer in config_p.sections():
            p = config_p.getint(peer, 'port_number')
            i = config_p.get(peer, 'ip_address')
            peers_info[peer] = (i, p)
        return peers_info


from django.db import models

class Country(models.Model):
    name = models.CharField(max_length=100)
    h1 = models.CharField(max_length=200, default='')
    words = models.CharField(max_length=255, default=None, blank=True)
    first = models.TextField(default='')
    second = models.TextField(default='')
    description = models.TextField(default='')
    def __unicode__(self):
        return self.name

class Collection(models.Model):
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='uploads/', default='uploads/no_image.jpg')
    text = models.TextField(max_length=1024, default='')
    sq_meter_cost = models.IntegerField(default=0)

    def __unicode__(self):
        return self.name

class Size(models.Model):
    width = models.FloatField(default=0)
    length = models.FloatField(default=0)

    def __unicode__(self):
        return str(self.width) + " x " + str(self.length)

    def square(self):
        return self.width * self.length

class Shape(models.Model):
    name = models.CharField(max_length=100)
    h1 = models.CharField(max_length=255, default=None)
    words = models.CharField(max_length=255, default=None, blank=True)
    first = models.TextField(default='')
    second = models.TextField(default='')
    description = models.TextField(default='')

    def __unicode__(self):
        return self.name

class Color(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Picture(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Carpet(models.Model):
    name = models.CharField(max_length=100)
    density = models.IntegerField(default=520000)
    height = models.IntegerField(default=8)
    text = models.TextField()
    image = models.ImageField(upload_to='uploads/', default='uploads/no_image.jpg')
    color = models.ForeignKey(Color)
    collection = models.ForeignKey(Collection)
    country = models.ForeignKey(Country)
    sizes = models.ManyToManyField(Size)
    shape = models.ForeignKey(Shape)
    picture = models.ForeignKey(Picture)
    is_children = models.BooleanField(default=0, null=False)

    def __unicode__(self):
        return self.name

    def have_discounts(self):
        if self.discounts.count() > 0:
            return True
        return False

class Product(models.Model):
    carpet = models.ForeignKey(Carpet)
    size = models.ForeignKey(Size)

    @property
    def price(self):
        try:
            discount = Discount.objects.get(carpet=self.carpet, size=self.size)
            return discount.price
        except:
            cost = self.carpet.collection.sq_meter_cost * self.size.square()
            cost /= 100
            cost = round(cost)*100
            cost = int(cost)
            return cost

class Order(models.Model):
    products = models.ManyToManyField(Product, unique=False)
    date = models.DateTimeField(auto_now_add=True)
    name = models.CharField(max_length=100)
    phone = models.CharField(max_length=20)
    email = models.CharField(max_length=100)
    adress = models.CharField(max_length=200)

class Discount(models.Model):
    carpet = models.ForeignKey(Carpet, related_name='discounts')
    size = models.ForeignKey(Size)
    price = models.IntegerField(null=True, default=None)

    def old_price(self):
        cost = self.carpet.collection.sq_meter_cost * self.size.square()
        cost /= 100
        cost = round(cost)*100
        cost = int(cost)
        return cost

    def percent(self):
        cost = self.old_price()
        percent = float(cost - self.price)*100/cost
        percent = round(percent)
        percent = int(percent)
        return percent

class News(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()
    date = models.DateTimeField()

    def __unicode__(self):
        return self.title


import tkinter as tk
from tkinter import ttk
import threading
import time
from time import strftime
import pyautogui # pip install pyautogui
import random
import numpy as np # pip install numpy
import cv2 # pip install opencv-python-headless
import PIL.ImageGrab as ig # pip install Pillow
import keyboard # pip install keyboard
import win32gui # pip install pywin32
import os
import json
try: import winsound
except: pass

class Pokerbot(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self,None)
        self.parent = None
        self.appRunning = True
        self.logMutex = threading.Lock()
        self.logSize = 0
        self.logStrings = []
        self.escapeEvent = False
        self.paused = False
        self.chrome = None
        self.pokerPos = [0, 0, 0, 0]
        self.settings = {'time_limit':3600, 'sound_alert':True, 'sound_file':'alert.wav'}
        self.lastLog = ""

        logframe = ttk.Frame(self)
        logframe.grid(row=0, column=0, sticky="nw") # position and width
        logframe.grid_propagate(False)
        scrollbar = tk.Scrollbar(logframe) # the scroll bar
        self.logtext = tk.Text(logframe, width=55, height=35, state=tk.DISABLED, yscrollcommand=scrollbar.set) # the log box itself
        self.logtext.pack(side=tk.LEFT, fill=tk.Y, expand=False)
        scrollbar.pack(side=tk.LEFT, fill=tk.Y)
        scrollbar.config(command=self.logtext.yview)

        self.title('(You) Pokerbot rev.8')
        self.resizable(width=False, height=False)
        self.protocol("WM_DELETE_WINDOW", self.close)

        # START
        self.load()
        self.run()

    def load(self):
        try:
            with open('settings.json') as f:
                data = json.load(f)
                if 'time_limit' in data or data['time_limit'] > 0: self.settings['time_limit'] = data['time_limit']
                else: self.log("Invalid 'time_limit' value in 'settings.json', using default (3600 seconds)")
                if 'sound_alert' in data and (data['sound_alert'] == True or data['sound_alert'] == False): self.settings['sound_alert'] = data['sound_alert']
                else: self.log("Invalid 'sound_alert' value in 'settings.json', using default (true)")
                if 'sound_file' in data: self.settings['sound_file'] = data['sound_file']
                return True
        except Exception as e:
            print('load(): ', e)
            try:
                with open('settings.json', 'w') as outfile:
                    json.dump(self.settings, outfile)
            except Exception as f:
                print('load(): ', f)

    def alert(self):
        try: winsound.PlaySound(self.settings['sound_file'], winsound.SND_FILENAME | winsound.SND_ASYNC)
        except: os.system('beep -f %s -l %s' % (200,100)) # if not windows OR failed to load the sound file

    def close(self): # called by the app when closed
        self.appRunning = False
        self.destroy()

    def run(self):
        loopThread = threading.Thread(target=self.bot_loop)
        loopThread.setDaemon(True)
        loopThread.start()
        keyboard.add_hotkey('Escape', self.quit)
        keyboard.add_hotkey('F7', self.pause)
        while self.appRunning:
            self.update_log()
            self.update()
            time.sleep(0.04)
            self.escape()

    def update_log(self):
        if not self.appRunning:
            return
        self.logMutex.acquire()
        if len(self.logStrings) > 0:
            self.logtext.configure(state="normal") # state set to normal to write in
            for i in range(0, len(self.logStrings)):
                self.logtext.insert(tk.END, self.logStrings[i]+"\n")
                if self.logSize >= 200: # one call = one line, so if the number of line reachs the limit...
                    self.logtext.delete(1.0, 2.0)
                else: # else, increase
                    self.logSize += 1
            self.logtext.configure(state="disabled") # back to read only
            self.logtext.yview(tk.END) # to the end of the text
            del self.logStrings[:] # delete the stored lines
        self.logMutex.release()

    def log(self, text, timestamp=True): # add the text to the queue, to be printed in the windows
        if not self.appRunning or text == self.lastLog:
            return
        self.lastLog = text
        self.logMutex.acquire()
        if timestamp: # append to our list of line (see update_log() for more)
            self.logStrings.append("[" + strftime("%H:%M:%S") + "] " + text)
        else:
            self.logStrings.append(text)
        print(text)
        self.logMutex.release()

    def escape(self):
        if self.escapeEvent:
            self.close()

    def quit(self):
        self.escapeEvent = True

    def pause(self):
        self.paused = not self.paused
        if self.paused: self.log('Pause')
        else: self.log('Resume')

    # https://pyautogui.readthedocs.io/en/latest/cheatsheet.html#mouse-functions
    def resetMouse(self): # to move the mouse out of the pokker buttons (they blink if you hover over them)
        pyautogui.moveTo(random.randint(self.chrome[0], self.chrome[0]+20), random.randint(self.chrome[1], self.chrome[1]+20))
        self.delay(30)

    def click(self, x, y, n=1):
        pyautogui.click(x=x, y=y, clicks=n)

    def randomClickArea(self, minX, minY, maxX, maxY, n=1):
        self.click(random.randint(minX, maxX), random.randint(minY, maxY), n)

    def randomClick(self, area, n=1): # area is [X, Y, W, H]
        if len(area) >= 4:
            self.click(random.randint(area[0], area[0]+area[2]), random.randint(area[1], area[1]+area[3]), n)

    # return true if a and b are close (within the diff value)
    def areClose(self, a, b, diff):
        if abs(a - b) <= diff:
            return True
        return False

    # print a string listing the cards in the array
    def printCards(self, array):
        string = "Hand: "
        letters = ["c", "d", "h", "s"]
        cards = ["Joker", "???", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
        for i in range(0, len(array)):
            string += cards[array[i][0]]
            if array[i][0] > 1:
                string += letters[array[i][1]]
            if i < len(array)-1:
                string += ", "
        self.log(string)

    # print the hand strenght
    def printHand(self, value):
        if value == -1:
            self.log("Error, invalid hand")
            return
        hands = [
            "No pairs",
            "One pair",
            "Two pairs",
            "Three of a kind",
            "Straight",
            "Flush",
            "Full House",
            "Four of a kind",
            "Straight Flush",
            "Five of a kind",
            "Royal Straight Flush"
        ]
        self.log(hands[value])

    # calculate the hand strenght
    def checkHand(self, cards):
        if len(cards) != 5:
            return -1
        joker = False
        # same suit?
        suit = -1
        result = True
        for i in range(0, 5):
            if cards[i][0] == 0: # joker
                joker = True
                continue
            if suit == -1:
                suit = cards[i][1]
                continue
            if suit != cards[i][1]:
                result = False
                break
        if result: # is at least a flush
            values = []
            for i in range(0, 5):
                if cards[i][0] == 0:
                    continue
                values.append(cards[i][0])
            values = sorted(values)
            if values[0] >= 10:
                suit = cards[0][1]
                royal = True
                for i in range(1, 5):
                    if suit != cards[i][1]:
                        royal = False
                        break
                # royal straight flush
                if royal:
                    return 10
            if values == range(min(values), max(values)+1):
                # straight flush
                return 8
            # flush
            return 5
        else:
            # check straight
            values = []
            for i in range(0, 5):
                if cards[i][0] == 0:
                    continue
                values.append(cards[i][0])
            values = sorted(values)
            if values == range(min(values), max(values)+1):
                # straight
                return 4
            else:
                same = [0]*15
                best = 0
                pairs = 0
                threes = 0
                for i in values:
                    same[i] += 1
                for i in range(0, len(same)):
                    if same[i] > best:
                        best = same[i]
                    if same[i] == 3:
                        threes += 1
                    if same[i] == 2:
                        pairs += 1
                if best == 4:
                    if joker:
                        # five of a kind
                        return 9
                    # four of a kind
                    return 7
                elif best == 3:
                    if joker:
                        # four of a kind
                        return 7
                    if pairs == 1:
                        # full house
                        return 6
                    # three of a kind
                    return 3
                elif best == 2:
                    if pairs == 2:
                        if joker:
                            # full house
                            return 6
                        else:
                            # two pairs
                            return 2
                    if joker:
                        # three of a kind
                        return 3
                    # pair
                    return 1
        if joker:
            # pair
            return 1
        # no pair
        return 0

    # return the multiplier for each possible hand (royal straigth flush is x250 for example)
    def getWinMultiplier(self, value):
        if value == -1:
            return 1
        hands = [0, 0, 1, 1, 3, 4, 10, 20, 25, 60, 250]
        return hands[value]

    # return a new deck (13x4 cards, true means the card is in the deck)
    def initDeck(self):
        deck = []
        for i in range(0, 13):
            deck .append([True, True, True, True])
        return deck

    # reduce the value in the deck for each card provided
    def updateDeck(self, deck, cards):
        for i in cards:
            if i[0] > 1:
                deck[i[0]-2][i[1]] = False
        return deck

    # read the card on the poker table (DOUBLE UP GAME)
    def readTableDoubleUp(self, array):
        print("Reading the Poker table...")
        screen = self.getScreen(True)
        result = []
        for i in range(0, len(array)):
            for j in range(0, len(array[i])):
                elem = self.searchCardImage(array[i][j], screen, 0.80)
                if elem[0] != -1:
                    dontAppend = False
                    for k in range(0, len(result)):
                        if self.areClose(result[k][2][0], elem[0], 1) and self.areClose(result[k][2][1], elem[1], 1):
                            dontAppend = True
                            if elem[4] > result[k][2][4]:
                                result[k] = [i, j, elem]
                            break
                    if not dontAppend:
                        result.append([i, j, elem])
        return result

    # for the double up game:
    # return probability of a lower card, a draw and a higher card (card must have been removed from the deck beforehand)
    def nextCardProba(self, deck, card):
        count = [0, 0]
        total = 0
        for i in range(0, len(deck)):
            for j in deck[i]:
                if j == True:
                    if card[0]-2 > i: # lower card count
                        count[0] += 1
                    elif card[0]-2 < i: # higher card count
                        count[1] += 1
                    else: continue
                    total += 1
        total = total * 1.0
        for i in range(0, 2):
            count[i] = 100 * count[i] / total
        return count

    # click on the card to hold it
    def holdCard(self, card):
        print("hold the card ", card[0], ", ", card[1])
        looping = True
        while looping:
            self.randomClick([card[2][0]+5, card[2][1]+5, card[2][2]-10, card[2][3]-10])
            if self.waitImageRegion("keep.png", card[2], 10, 0.85)[0] != -1:
                looping = False

    # deal game:
    # determine which card to hold and call holdCard
    def handAction(self, cards, score):
        joker = 0
        # act depending on hand strength
        if score == 0:
            # check same suit
            suit = [0]*4
            for i in cards:
                if i[0] == 0:
                    self.holdCard(i)
                    joker = 1
                else:
                    suit[i[1]] += 1
            # check consecutive
            cons = []
            temp = cards
            for i in range(0, len(temp)-1):
                for j in range(i+1, len(temp)):
                    if temp[i][0] > temp[j][0]:
                        temp[i][0], temp[j][0] = temp[j][0], temp[i][0]
            for i in range(joker, len(temp)-1):
                array = [temp[i]]
                usedJoker = False
                avoid = 0
                for j in range(i+1, len(temp)):
                    if temp[j][0] == temp[j-1][0] + 1:
                        array.append(temp[j])
                        continue
                    elif temp[j][0] == temp[j-1][0] + 2:
                        if joker == 1 and not usedJoker:
                            usedJoker = True
                            array.append(temp[j])
                            continue
                        break
                    elif temp[j][0] == temp[j-1][0]:
                        avoid += 1
                        continue
                    else:
                       break
                if len(array) > 1:
                    cons.append(array)
                    i += len(array)-1+avoid
            # decide
            countSuit = 0
            idSuit = None
            countCons = 0
            idCons = None
            for i in range(0, 4):
                if suit[i] > countSuit:
                    countSuit = suit[i]
                    idSuit = i
            countSuit += joker
            for i in cons:
                if len(i) > countCons:
                    countCons = len(i)
                    idCons = i
            if idSuit is None and idCons is None:
                return
            if countSuit >= countCons:
                for i in cards:
                    if idSuit == i[1]:
                        self.holdCard(i)
            else:
                for i in idCons:
                    self.holdCard(i)
        elif score > 3:
            for i in cards:
                self.holdCard(i)
        else:
            same = [0]*15
            for i in cards:
                if i[0] > 1:
                    same[i[0]] += 1
                else:
                    self.holdCard(i)
            for j in range(0, len(same)):
                if same[j] > 1:
                    for i in cards:
                        if i[0] == j:
                            self.holdCard(i)

    def getScreen(self, pokerMode = False):
        if pokerMode: screen = ig.grab(bbox=[self.pokerPos[0],self.pokerPos[1],self.pokerPos[2],self.pokerPos[3]]) # for the poker table
        else: screen = ig.grab(bbox=[self.chrome[0],self.chrome[1],self.chrome[0]+self.chrome[2],self.chrome[1]+self.chrome[3]]) # get the chrome screenshot
        screen = np.array(screen, dtype='uint8').reshape((screen.size[1],screen.size[0],3)) # convert for opencv
        return cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

    def searchCardImage(self, template, screen, threshold=0.85):
        if template is None or screen is None:
            return [-1]

        w, h = template.shape[::-1] # get the size
        res = cv2.matchTemplate(screen,template,eval('cv2.TM_CCOEFF_NORMED')) # template matching
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        if max_val > threshold:
            return [max_loc[0]+self.pokerPos[0], max_loc[1]+self.pokerPos[1], w, h, max_val]
        else:
            return [-1]

    def searchImage(self, filename, threshold=0.85):
        template = cv2.imread("data/" + filename,0) # read the image file
        if template is None:
            return [[-1]]

        img = self.getScreen() # get the screen

        w, h = template.shape[::-1] # get the size
        res = cv2.matchTemplate(img,template,eval('cv2.TM_CCOEFF_NORMED')) # template matching
        loc = np.where(res >= threshold) # 0.9 is the default threshold

        coor = []
        for pt in zip(*loc[::-1]):
            coor.append([pt[0]+self.chrome[0], pt[1]+self.chrome[1], w, h]) # get all the matches and add the window position

        if len(coor) == 0:
            return [[-1]] # return -1 if empty
        return coor

    def searchImageRegion(self, filename, region, threshold=0.85):
        if not isinstance(region, list) or len(region) < 4:
            return [[-1]]
        res = self.searchImage(filename, threshold)
        if res[0][0] == -1:
            return res
        ok = []
        for i in range(0, len(res)):
            if res[i][0] >= region[0] and res[i][1] >= region[1] and res[i][0]+res[i][2] <= region[0]+region[2] and res[i][1]+res[i][3] <= region[1]+region[3]:
                ok.append(res[i])
        if len(ok) == 0:
            return [[-1]]
        return ok

    def findImage(self, filename, threshold=0.85):
        if self.searchImage(filename, threshold)[0][0] != -1:
            return True
        return False

    def clickImage(self, filename, dClick = False):
        elems = self.searchImage(filename, 0.85)
        if elems[0][0] != -1:
            if dClick: self.randomClick(elems[0], 2)
            else: self.randomClick(elems[0])
            return True
        return False

    def waitImage(self, filename, max, threshold=0.85):
        for i in range(0, max):
            elems = self.searchImage(filename, threshold)[0]
            if elems[0] != -1:
                return elems
            self.delay(100)
        return [-1]

    def waitImageRegion(self, filename, region, max, threshold=0.85):
        for i in range(0, max):
            elems = self.searchImageRegion(filename, region, threshold)[0]
            if elems[0] != -1:
                return elems
            self.delay(100)
        return [-1]

    def waitAndClickImage(self, filename, max):
        elems = self.waitImage(filename, max)
        if elems[0] != -1:
            self.randomClick(elems)
            return True
        return False

    def waitImageList(self, files, max, threshold=0.85):
        for i in range(0, max):
            for j in files:
                if self.searchImage(j, threshold)[0][0] != -1:
                    return True
            self.delay(100)
        return False

    def checkForChrome(self):
        hwnd = win32gui.GetForegroundWindow() # get the focused window
        win = win32gui.GetWindowText(hwnd)
        if win.find(" – Google Chrome") != -1 or win.find(" – Chromium") != -1: # check if google chrome
            if self.chrome == None:
                self.log("Chrome detected")
            size = win32gui.GetWindowRect(hwnd) # update the size
            self.chrome = [size[0], size[1], size[2]-size[0], size[3]-size[1]]
            return True
        else:
            if self.chrome != None:
                self.chrome = None
                self.log("Chrome not found")
            return False

    # check if the poker table is in view, return true if that's the case and update pokerPos value (global screen region of the card area)
    def checkPokerTable(self):
        topleft = self.searchImage("board_01a.png")[0]
        bottomright = self.searchImage("board_02.png")[0]
        if topleft[0] == -1:
            topleft = self.searchImage("board_01b.png")[0]
        if topleft[0] == -1 or bottomright[0] == -1:
            self.pokerPos = [0, 0, 0, 0]
            return False
        self.pokerPos = [topleft[0]+18, topleft[1]+156, topleft[0]+302, topleft[1]+276] # rect(18, 156, 284, 120)
        return True

    # read the cards on the poker table (DEAL GAME)
    def readTable(self, array):
        print("Reading the Poker table...")
        screen = self.getScreen(True)
        result = []
        ignored = []
        for i in range(0, len(array)):
            for j in range(0, len(array[i])):
                elem = self.searchCardImage(array[i][j], screen, 0.80) # search if the card is on screen
                if elem[0] != -1: # if it is
                    dontAppend = False
                    for k in range(0, len(result)): # compare with existing result
                        if self.areClose(result[k][2][0], elem[0], 1) and self.areClose(result[k][2][1], elem[1], 1):
                            dontAppend = True
                            if elem[4] > result[k][2][4]:
                                ignored.append(result[k])
                                result[k] = [i, j, elem]
                            break
                    if not dontAppend: # and decide to keep the match or not
                        result.append([i, j, elem])
                    else:
                        ignored.append([i, j, elem])
        # mismatch protection
        # if incomplete hand and we have discarded matches :
        while len(result) < 5 and len(ignored) > 0:
            best = [0, ignored[0][2][4]]
            for i in range(1, len(ignored)): # we pick the best ignored match
                if ignored[i][2][4] > best[1]:
                    best[0] = i
                    best[1] = ignored[i][2][4]
            result.append(ignored[best[0]]) # add in the result
            del ignored[best[0]]
        return result

    def randomDelay(self, min, max): # ms
        time.sleep(random.randint(min, max)/1000)

    def delay(self, t): # ms
        time.sleep(t/1000)

    def loadImage(self, filename):
        return cv2.imread("data/" + filename,0)

    def resizeImage(self, image, x, y):
        return cv2.resize(image, (x,y), interpolation = cv2.INTER_AREA)

    # resize the card images from array2 into array1
    def resizeCards(self, array1, array2, x, y):
        for i in range(0, len(array2)):
            temp = []
            for j in range(0, len(array2[i])):
                temp.append(self.resizeImage(array2[i][j], x, y))
            array1.append(temp)

    # load the card image files in memory
    def loadCards(self, cards):
        buffer = self.loadImage("00x.png") # joker
        if buffer is None:
            return False
        cards.append([buffer])
        cards.append([])
        letters = ["c", "d", "h", "s"]
        for i in range(2, 15):
            array = []
            for j in range(0, 4):
                if i < 10: filename = '0{}{}.png'.format(i, letters[j])
                else: filename = '{}{}.png'.format(i, letters[j])
                buffer = self.loadImage(filename) # card
                if buffer is None:
                    return False
                array.append(buffer)
            cards.append(array)
        return True

    def bot_loop(self): # main loop
        state = -1 # state machine
        lastState = 0 # previous state
        cards = []
        resizedCards = []
        doubleupCards = []
        gain = 0
        if not self.loadCards(cards):
            self.log("Failed to load cards")
            return
        self.resizeCards(resizedCards, cards, 51, 72)
        self.resizeCards(doubleupCards, cards, 72, 100)
        deck = self.initDeck()
        doubleupChain = 0
        win = -1
        previousHand = []
        choice = False
        error = False
        startTime = time.time()
        self.log("Click on google chrome to start")
        self.log("GBF must be set to the smallest game size")
        self.log("Time limit is set to {} second(s)".format(self.settings['time_limit']))
        self.log("F7 to toggle the Pause, Escape to stop")
        self.log("_______________________________________________________", False)
        while self.appRunning:
            self.delay(100)
            if time.time() - startTime > self.settings['time_limit']:
                self.log("Time limit reached")
                return
            if self.paused:
                continue
            if error:
                self.log("An error occured")
                return
            if self.checkForChrome():
                if self.findImage("capcha_en.png", 0.7):
                    self.log("_______________________________________________________", False)
                    self.log("Capcha, press F7 to resume")
                    self.paused = True
                    if self.settings['sound_alert']:
                        while self.paused:
                            self.alert()
                            self.delay(5000)
                    continue
                elif self.checkPokerTable(): # and if poker table
                    if state != -1: # keep track of our previous state, in case the user pause the bot and go do something else
                        lastState = state
                    if state == -1: # -1 is the undetermined state
                        if self.findImage("yes_en.png") and self.findImage("no_en.png"):
                            test = self.readTable(resizedCards)
                            self.printCards(test) # print the cards
                            score = self.checkHand(test) # get the result
                            self.printHand(score) # and print it
                            if len(self.readTable(resizedCards)) == 5:
                                state = 1
                            elif len(self.readTable(doubleupCards)) > 0:
                                self.clickImage("yes_en.png")
                                state = 3
                            continue
                        elif self.findImage("high_en.png") and self.findImage("low_en.png"):
                            if len(deck) == 0:
                                deck = initDeck()
                            state = 3
                            continue
                        elif self.findImage("deal_en.png") or self.findImage("ok_en.png"):
                            state = 0
                            continue
                        else:
                            state = lastState
                            continue
                    elif state == 0: # starting state (deal game)
                        self.resetMouse()
                        self.log("_______________________________________________________", False)
                        self.log("Deal screen")
                        if self.waitAndClickImage("deal_en.png", 60): # click the deal button
                            self.resetMouse()
                            self.delay(100)
                        else: continue
                        if self.waitImage("ok_en.png", 30)[0] != -1: # if the ok button is visible on screen
                            # initialize the stuff
                            gain -= 1
                            errorCount = 0
                            win = -1
                            currentHand = []
                            self.log("Current gain: {}".format(gain))
                            # read the table (10 try max)
                            while errorCount < 10:
                                currentHand = self.readTable(resizedCards)
                                if len(currentHand) != 5:
                                    errorCount += 1
                                else:
                                    break
                            if errorCount < 10:
                                self.resetMouse()
                                self.printCards(currentHand) # print the cards
                                score = self.checkHand(currentHand) # get the result
                                self.printHand(score) # and print it
                                self.handAction(currentHand, score) # choose which card to keep
                                previousHand = currentHand # store our current card (to keep track later)
                                self.clickImage("ok_en.png") # click ok
                                self.resetMouse()
                                if self.waitImageList(["yes_en.png", "no_en.png", "deal_en.png"], 40): # and wait for the cards
                                    state = 1
                                continue
                            else:
                                self.log("Error: Can't read the table, resetting...")
                                state = -1
                                continue
                    elif state == 1: # deal result, after clicking ok
                        self.log("_______________________________________________________", False)
                        self.log("Deal screen: Result")
                        errorCount = 0
                        currentHand = []
                        while errorCount < 10: # same thing again
                            currentHand = self.readTable(resizedCards)
                            if len(currentHand) != 5:
                                errorCount += 1
                            else:
                                break
                        if errorCount < 10:
                            self.resetMouse()
                            clicked = False
                            # click preemptively
                            if self.clickImage("yes_en.png"):
                                state = 2
                                clicked = True
                            elif self.clickImage("deal_en.png"):
                                state = 0
                                clicked = True
                            # check result
                            self.printCards(currentHand)
                            score = self.checkHand(currentHand)
                            self.printHand(score)
                            win = self.getWinMultiplier(score) # store our multiplier
                            array = [] # compare our current cards with the previous one, add the non duplicate in previousHand
                            for i in currentHand:
                                found = False
                                for j in previousHand:
                                    if i[0] == j[0] and i[1] == j[1]:
                                        found = True
                                        break
                                if not found:
                                    array.append(i)
                            previousHand += array
                            waiting = True
                            while waiting and not clicked:
                                if self.findImage("yes_en.png") and self.findImage("no_en.png"): # we won
                                    state = 2
                                    waiting = False
                                    self.clickImage("yes_en.png") # I need to update this part
                                elif self.findImage("deal_en.png"): # we lost
                                    state = 0
                                    waiting = False
                            continue
                        else:
                            self.log("Error: Can't read the table, resetting...")
                            state = -1
                            continue
                    elif state == 2: # preparing for the double up
                        doubleupChain = 0
                        state = 3
                        deck = self.initDeck()
                    elif state == 3: # double up game
                        self.log("_______________________________________________________", False)
                        self.log("Double Up - Round #{}".format(doubleupChain+1))
                        self.resetMouse()
                        self.waitImageList(["high_en.png", "low_en.png"], 60)
                        if self.findImage("high_en.png") and self.findImage("low_en.png"): # if high and low buttons on screen
                            if win < 1: # if the win value is lesser than 1, we didn't come here the right way
                                win = 1
                            errorCount = 0
                            currentHand = []
                            while errorCount < 10: # read the table
                                currentHand = self.readTableDoubleUp(doubleupCards)
                                if len(currentHand) != 1: # only one card on screen this time
                                    errorCount += 1
                                else:
                                    break
                            if errorCount < 10:
                                deck = self.updateDeck(deck, currentHand) # update the table
                                proba = self.nextCardProba(deck, currentHand[0]) # get the probability
                                self.log("{:.1f}% Low, {:.1f}% High".format(proba[0], proba[1]))
                                button = ""
                                if proba[0] > proba[1]: # if the "low" probability is higher than the "high" probability
                                    button = "low_en.png" # we click the low button
                                    choice = False
                                    self.log("I choose Low")
                                else:
                                    button = "high_en.png" # else the high button
                                    choice = True
                                    self.log("I choose High")
                                self.clickImage(button) # click
                                self.resetMouse()
                                previousHand = currentHand # store the card
                                doubleupChain += 1
                                state = 4
                                self.waitImageList(["yes_en.png", "no_en.png", "deal_en.png"], 40)
                                continue
                            else:
                                self.log("Error: Can't read the table, resetting...")
                                state = -1
                                continue
                    elif state == 4: # double up result
                        if self.findImage("yes_en.png") and self.findImage("no_en.png"): # same thing
                            self.log("_______________________________________________________", False)
                            self.log("Double Up - Round #{} Result".format(doubleupChain))
                            errorCount = 0
                            currentHand = []
                            while errorCount < 10:
                                currentHand = self.readTableDoubleUp(doubleupCards)
                                if len(currentHand) != 2: # we should have 2 cards on the screen (the previous one and the new one) but we check for < 1
                                    errorCount += 1
                                else:
                                    break
                            if len(currentHand) == 1: # if only one card on screen, a desync happened, back to state = 3
                                state = 3
                                continue
                            if errorCount < 10:
                                array = [] # same as for state = 1, we compare with the previous card, add the new one to the list
                                for i in currentHand:
                                    if i[0] != previousHand[0][0] or i[1] != previousHand[0][1]:
                                        array.append(i)
                                        break
                                result = choice # check if the result is high, low or draw
                                draw = True
                                if array[0][0] != previousHand[0][0]:
                                    result = array[0][0] > previousHand[0][0]
                                    draw = False
                                if choice == result: # if we won
                                    self.log("Chain: {}".format(doubleupChain))
                                    if(doubleupChain >= 10): # chain reached 10, back to the start
                                        state = 1
                                        continue
                                    deck = self.updateDeck(deck, array) # update the deck
                                    proba = self.nextCardProba(deck, array[0]) # get the probability for the next round
                                    button = ""
                                    self.resetMouse()
                                    if not draw: # if we didn't get a draw, double the win
                                        win = win * 2
                                        self.log("Current multiplier is x{}".format(win))
                                    if win < 128 or proba[0] > 90 or proba[1] > 90: # decide if we continue or not
                                        button = "yes_en.png"
                                        state = 3
                                    else:
                                        button = "no_en.png"
                                        state = 0
                                        gain += win
                                        win = 0
                                    self.clickImage(button) # and press the button
                                    self.resetMouse()
                                    self.log("{:.1f}% Low, {:.1f}% High".format(proba[0], proba[1]))
                                    self.waitImageList(["high_en.png", "low_en.png", "deal_en.png"], 60)
                                    continue
                        elif self.findImage("deal_en.png"): # deal button on screen, it's over
                            self.log("Game over")
                            state = 0
                else:
                    self.log("No poker table found")
                    state = -1

Pokerbot()


from __future__ import print_function
import argparse
import torch
import torch.nn.functional as F
from torchvision import datasets, transforms
from models import vgg, resnet, densenet
import numpy as np
import os
import sys
from tqdm import tqdm
from utils import *

if __name__ == '__main__':

	parser = argparse.ArgumentParser(description='Cifar10 Evaluation')
	parser.add_argument('--cp-path', type=str, default=None, metavar='Path', help='Path for checkpointing')
	parser.add_argument('--data-path', type=str, default='./data/', metavar='Path', help='Path to data')
	parser.add_argument('--batch-size', type=int, default=100, metavar='N', help='input batch size for testing (default: 100)')
	parser.add_argument('--model', choices=['vgg', 'resnet', 'densenet'], default='resnet')
	parser.add_argument('--dropout-prob', type=float, default=0.25, metavar='p', help='Dropout probability (default: 0.25)')
	parser.add_argument('--no-cuda', action='store_true', default=False, help='Disables GPU use')
	parser.add_argument('--workers', type=int, default=4, metavar='N', help='Data load workers (default: 4)')
	args = parser.parse_args()
	args.cuda = True if not args.no_cuda and torch.cuda.is_available() else False

	transform_test = transforms.Compose([transforms.ToTensor(), transforms.Normalize([x / 255 for x in [125.3, 123.0, 113.9]], [x / 255 for x in [63.0, 62.1, 66.7]])])
	testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
	test_loader = torch.utils.data.DataLoader(testset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers)

	ckpt = torch.load(args.cp_path, map_location = lambda storage, loc: storage)
	try :
		dropout_prob, n_hidden, hidden_size, softmax = ckpt['dropout_prob'], ckpt['n_hidden'], ckpt['hidden_size'], ckpt['sm_type']
	except KeyError as err:
		print("Key Error: {0}".format(err))
		print('\nProbably old cp has no info regarding classifiers arch!\n')
		n_hidden, hidden_size, softmax = get_classifier_config_from_cp(ckpt)
		dropout_prob = args.dropout_prob

	if args.model == 'vgg':
		model = vgg.VGG('VGG16', nh=n_hidden, n_h=hidden_size, dropout_prob=dropout_prob, sm_type=softmax)
	elif args.model == 'resnet':
		model = resnet.ResNet18(nh=n_hidden, n_h=hidden_size, dropout_prob=dropout_prob, sm_type=softmax)
	elif args.model == 'densenet':
		model = densenet.densenet_cifar(nh=n_hidden, n_h=hidden_size, dropout_prob=dropout_prob, sm_type=softmax)

	try:
		model.load_state_dict(ckpt['model_state'], strict=True)
	except RuntimeError as err:
		print("Runtime Error: {0}".format(err))
	except:
		print("Unexpected error:", sys.exc_info()[0])
		raise

	if args.cuda:
		device = get_freer_gpu()
		model = model.cuda(device)

	model.eval()

	correct = 0

	with torch.no_grad():

		iterator = tqdm(test_loader, total=len(test_loader))
		for batch in iterator:

			x, y = batch

			x = x.to(device)
			y = y.to(device)

			embeddings = model.forward(x)

			embeddings_norm = F.normalize(embeddings, p=2, dim=1)

			out = model.out_proj(embeddings_norm, y, eval_=True)

			pred = F.softmax(out, dim=1).max(1)[1].long()
			correct += pred.squeeze().eq(y.squeeze()).detach().sum().item()

	print('\nAccuracy of model {}: {}'.format(args.cp_path.split('/')[-1], 100.*correct/len(testset)))


import torch
import numpy as np
import qpsolvers as qps
import quadprog
import itertools
from qpth.qp import QPFunction, QPSolvers

__author__ = "Xin Wang"
__credits__ = ["Xin Wang"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Xin Wang"
__email__ = "xin3.wang@intel.com"
__status__ = "Development"

def project_half_space(v, v0):
    """
    Simple projection to half space defined by the normal hyperplane of the primary gradient,
    done on device
    v: vector of dimension D, will be updated in place if projection is to be performed
    v0: vector of dimension D, the primary
    """
    v0 = torch.squeeze(v0[-1])
    # if torch.dot(v, v0) < 0.:
    #     print("cos(theta) = {:.4f}".format(torch.dot(v, v0)/torch.norm(v)/torch.norm(v0)))
    v += v0 * F.relu(-torch.dot(v, v0)) / torch.norm(v0)**2 # NOTE: inplace!

def project_convex_cone(v, v0, eps=1e-13, qp_solver='qpth'):
    """
    Projection to convex cone defined by normal hyperplanes of the primary vectors,
    done on cpu if using quadratic programming package, 'quadprog', 'cvxopt', or 'cvxpy'
    done on gpu if using 'qpth'
    v: vector of dimension D, will be updated in place if projection is to be performed
    v0: matrix of dimensions N * D, each of the N rows is a primary vector
    eps: tolerance of Gram matrix entries to trigger an QP optimization
    qp_solver: QP solver, 'qpth', 'quadprog', 'cvxopt', or 'cvxpy'
    """
    # dp = torch.matmul(v0, v)
    # if any(dp<0.):
    #     print("dot(v, v0) before: \33[91m{}\033[0m".format(', '.join(["%.4f" % x for x in torch.matmul(v0, v)])))

    N = v0.shape[0]

    if qp_solver=='qpth':
        Q = torch.matmul(v0, v0.t())
        if torch.any(Q.abs() > eps):
            p = torch.matmul(v, v0.t())
            _v = QPFunction(verbose=-1, solver=QPSolvers.CVXPY)(
                Q, p,
                -torch.eye(N, device=v0.device),
                torch.zeros(N, device=v0.device),
                torch.Tensor(), torch.Tensor()
            )
            v += torch.matmul(_v, v0).squeeze() # NOTE: inplace!
    else: # 'quadprog', 'cvxopt', or 'cvxpy'
        qp_solver = getattr(qps, qp_solver + '_solve_qp')
        Q = torch.matmul(v0, v0.t()).cpu().double().numpy()
        if np.any(np.abs(Q) > eps):
            p = torch.matmul(v, v0.t()).cpu().double().numpy()
            _v = qp_solver(Q, p, -np.eye(N), np.zeros(N))
            v += torch.matmul(v0.t(), torch.Tensor(_v).to(v0.device)) # NOTE: inplace!

    # if any(dp<0.):
    #     print("dot(v, v0) after: \33[92m{}\033[0m".format(', '.join(["%.4f" % x for x in torch.matmul(v0, v)])))


#!/usr/bin/env python
# coding: utf-8
'''
Project Name: EP_WC0100
CTM Name    :
Crawl Cycle : Weekly
Main Website: https://business.591.com.tw
Description : RPA爬網需求：定期蒐尋仲介網站出售案件
Author      : 張至偉
Update Date : 2019.03.22

[20190402] 問題單:20190328131036
1. 調整輸出檔案的欄位順序
2. 新增查詢縣市
3. 新增"坪數"的檢核條件
'''

# In[1]:

import header

import os
import re
import datetime
import logging
import traceback

import requests
import pandas as pd
from bs4 import BeautifulSoup

TODAY = str(datetime.date.today())

WEB_URL = "https://business.591.com.tw"

# In[2]:

def getBlockInfo(itemSoup, patternKey, patternValue):

    # 使用 CSS 選擇器，選取指定區塊
    # 根據區塊處理，並回傳 dict 的查詢格式 {"標題":"內容"}
    rtnMap = {}
    keyList = itemSoup.select(patternKey)
    valueList = itemSoup.select(patternValue)

    for i, key in enumerate(keyList):
        rtnMap[key.text] = valueList[i].text

    return rtnMap

# In[3]:

def selectValueByPattern(soup, patternStr, index, removeChild=False):

    selectedList = soup.select(patternStr)

    # 根據 CSS 選擇器，選擇欲爬取的內容文字；若無文字顯示 "None"
    if len(selectedList) > index:

        # 是否移除子元素
        if removeChild:
            for childElement in selectedList[index].find_all():
                childElement.decompose()

        fieldValue = selectedList[index].text.strip()
    else:
        fieldValue = "None"

    # 以空白取代換行
    fieldValue = ' '.join(fieldValue.splitlines())

    return fieldValue

# In[4]:

def removeWord(inputString, removePatterns):

    rtnString = inputString
    for pString in removePatterns:
        pattern = re.compile("(\s*)" + pString + "(\s*)")
        rtnString = pattern.sub('', rtnString)

    return rtnString

# In[5]:

def getItemInfo(itemUrl):

    itemSoup = request2soup(itemUrl)
    addrInfo = getBlockInfo(itemSoup, "span.info-addr-key", "span.info-addr-value")
    floorInfo = getBlockInfo(itemSoup, "div.info-floor-value", "div.info-floor-key")

    # [20190402] 檢核坪數是否大於 300 坪
    # [20190408] 檢核變數是否為 "None"
    floorArea = removeWord(floorInfo.get("基地面積", "None"), ["坪"])
    if floorArea=="None" or float(floorArea) < 300:
        return None

    rtnMap = {}

    # [20190402] 調整輸出檔案的欄位順序
    rtnMap["縣市"] = selectValueByPattern(itemSoup, "div.breadList > a", 2)
    rtnMap["區域"] = selectValueByPattern(itemSoup, "div.breadList > a", 3)
    rtnMap["地址"] = addrInfo.get("地址", "None")
    rtnMap["土地類別"] = floorInfo.get("土地類別", "None")
    rtnMap["現況"] = addrInfo.get("現況", "None")
    rtnMap["總價(萬元)"] = selectValueByPattern(itemSoup, "span.info-price-num", 0, removeChild=True)
    rtnMap["基地面積(坪)"] = floorArea
    rtnMap["單價(萬/坪)"] = removeWord(selectValueByPattern(itemSoup, "div.info-price-per", 0), ["單價：", "萬/坪房貸試算"])
    rtnMap["標題"] = selectValueByPattern(itemSoup, "h1.detail-title-content", 0)
    rtnMap["待售物件連結"] = itemUrl
    rtnMap["適合用途"] = addrInfo.get("適合用途", "None")
    rtnMap["有效期"] = removeWord(selectValueByPattern(itemSoup, "span.detail-info-span.pull-right", 0), ["有效期："])
    rtnMap["仲介"] = selectValueByPattern(itemSoup, "div.info-host-name > span.info-span-name", 0)
    rtnMap["經紀業"] = selectValueByPattern(itemSoup, "div.info-host-detail > span", 0)
    rtnMap["電話"] = selectValueByPattern(itemSoup, "span.info-host-word", 0)
    rtnMap["屋況特色"] = selectValueByPattern(itemSoup, "div.detail-feature-text", 0)
    rtnMap["編號"] = selectValueByPattern(itemSoup, "span.breadList-last", 0)

    return rtnMap

# In[6]:

def parsingDetail(standbyDataFrame):

    itemUrlList = list(standbyDataFrame["link"])

    # [20190402] 調整輸出檔案的欄位順序
    detailDataFrame = pd.DataFrame(columns=["縣市", "區域", "地址", "土地類別", "現況", "總價(萬元)", "基地面積(坪)", "單價(萬/坪)",
    "標題", "待售物件連結","適合用途", "有效期", "仲介", "經紀業", "電話", "屋況特色", "編號"])

    for itemUrl in itemUrlList:
        try:
            itemInfo = getItemInfo(itemUrl)
            detailDataFrame = detailDataFrame.append(itemInfo, ignore_index=True)
        except:
            # 移除未成功的連結
            standbyDataFrame = standbyDataFrame[standbyDataFrame["link"] != itemUrl]
            setErrorMessage("爬取商品內文失敗，失敗連結：" + itemUrl, setEXIT_CODE=False)

    return standbyDataFrame, detailDataFrame

# In[7]:

def parsingTitle(reqMap):

    try:
        # 取得上次爬網結果
        historyFile = os.path.join(header.LAST_RESULT_PATH, "crawlHistory.csv")
        if os.path.isfile(historyFile):
            historyDataFrame = pd.read_csv(historyFile)
            historyList = list(historyDataFrame["link"])
        else:
            historyDataFrame = None
            historyList = []

        # 爬網日期, 連結, 標題
        standbyDataFrame = pd.DataFrame(columns=["date", "link", "title"])

        queryParams = reqMap["CONDITIONS"]

        # 設定查詢地區
        for regionId in reqMap["REGION_IDs"]:

            # 設定查詢條件
            for queryType in reqMap["QUERY_TYPEs"]:

                queryParams["land"] = queryType
                queryParams["firstRow"] = 0

                # 確認總筆數
                tmpSoup = request2soup(WEB_URL, regionId, queryParams)
                totalNumberPattern = tmpSoup.select("div.pull-left.hasData > i")
                totalNumber = int(totalNumberPattern[0].text) if len(totalNumberPattern) > 0 else 0

                # 查無資料，執行下一個查詢條件
                if totalNumber < 1:
                    continue

                try:
                    # 逐頁處理，每頁30筆
                    for itemNo in range(0, totalNumber, 30):

                        # 設定查詢頁數
                        queryParams["firstRow"] = itemNo

                        # 取得商品列表
                        rtnListSoup = request2soup(WEB_URL, regionId, queryParams)
                        items = rtnListSoup.select("li.infoContent > h3 > a")

                        # 過濾已爬取的物品，並記錄此次待查清單
                        for item in items:
                            url = "https:" + ' '.join(item.get("href").split())
                            if url not in historyList:
                                dataInfo = {"date":TODAY, "link":url, "title":item.text}
                                standbyDataFrame = standbyDataFrame.append(dataInfo, ignore_index=True)

                except:
                    errMsg = "爬取區域：%s 查詢條件：%s 第 %s 頁商品列表發生錯誤"\
                        % (reqMap["REGION_IDs"][region_id], reqMap["QUERY_TYPEs"][query_type], str(int(page_num/30)+1))
                    setErrorMessage(errMsg, setEXIT_CODE=False)

        return standbyDataFrame, historyDataFrame

    except:
        setErrorMessage("爬取商品列表失敗")
        return pd.DataFrame(columns=["date", "link", "title"]), None

# In[8]:

def request2soup(url, regionId=None, conditions=None):

    HEADERS = {
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "en-US,en;q=0.9",
        "connection": "keep-alive",
        "dnt": "1"
    }

    # 設定查詢地區
    regionCookie = None if regionId == None else {"urlJumpIp": regionId}

    res = requests.get(url, headers=HEADERS, cookies=regionCookie, params=conditions)
    res.encoding = "utf-8"

    return BeautifulSoup(res.text, "html.parser")

# In[9]:

def setErrorMessage(errorMsg, setEXIT_CODE=True):

    if setEXIT_CODE:
        header.EXIT_CODE = -1

    print(errorMsg)
    logging.error(errorMsg)
    traceback.print_exc()

# In[10]:

def main():
    header.processBegin(url=WEB_URL)
    header.clearFolder()

    try:
        reqMap = {
            # [20190402] 新增查詢的縣市
            "REGION_IDs": {
                "1" : "台北市", "2" : "基隆市", "3" : "新北市", "4" : "新竹市", "5" : "新竹縣", "6" : "桃園市",
                "7" : "苗栗縣", "8" : "台中市", "10": "彰化縣", "11": "南投縣", "12": "嘉義市", "13": "嘉義縣",
                "14": "雲林縣", "15": "台南市", "17": "高雄市", "19": "屏東縣", "21": "宜蘭縣", "22": "台東縣",
                "23": "花蓮縣", "24": "澎湖縣", "25": "金門縣", "26": "連江縣"
            },
            "QUERY_TYPEs": {'1':"住宅用地", '2':"商業用地", '3':"工業用地"},
            "CONDITIONS": {
                "is_new_list": '1',
                "type": '2',
                "searchtype": '1',
                "firstRow": '0',
                "kind": "11", # 廠房土地出售
                "area": "300,"# 最小 300 坪
            }
        }

        standbyDataFrame, historyDataFrame = parsingTitle(reqMap)

        if len(standbyDataFrame) < 1:
            # 無資料更新
            logMsg = "無資料更新，爬網日期：" + TODAY
            print(logMsg)
            logging.critical(logMsg)
        else:
            finishDataFrame, detailDataFrame = parsingDetail(standbyDataFrame)

            header.outputCsv(detailDataFrame, header.PROJECT)
            header.RESULT_COUNT = len(detailDataFrame)

            # 更新 crawlHistory 檔案
            updateHistoryDataFrame = pd.concat([historyDataFrame, finishDataFrame], ignore_index=True)
            header.outputCsv(updateHistoryDataFrame, "crawlHistory", header.LAST_RESULT_PATH)

        header.zipFile()
        header.createInfoFile()
        header.createOKFile()

    except:
        setErrorMessage("執行爬網作業失敗")
        header.createInfoFile()
        header.zipFile(zipFolder=header.LOG_PATH, zipResultWithLog=False)

    header.processEnd()

# In[11]:

if __name__ == "__main__":
    print(header.TIMELABEL)
    logging.fatal("FINAL_PATH:" + header.FINAL_PATH)
    main()


import ctypes
import json
import time
import requests
from api_key_file import api_key_f

def get_weather(location_key):
    api_key = api_key_f
    req2 = requests.get(
        f'http://dataservice.accuweather.com/currentconditions/v1/{location_key}?apikey={api_key}').json()
    return req2[0]["WeatherText"]

def main(city):
    api_key = api_key_f
    req = requests.get(
        f'http://dataservice.accuweather.com/locations/v1/cities/search?apikey={api_key}&q={city}').json()
    location_key = req[0]["Key"]
    req2 = requests.get(
        f'http://dataservice.accuweather.com/currentconditions/v1/{location_key}?apikey={api_key}').json()
    if req2:
        print(f'Weather right now: {req2[0]["WeatherText"]}')
        main2(location_key)
    else:
        print('Network Error...')

def main2(location_key):
    print('Here you can freely create your wallpaper based on weather outside c: \n')
    try:
        with open('data.txt', 'r') as file:
            data = file.read()
        print('If you want, you can add more wallpapers, just type "add"')
        print('Or print "start" to launch the program')

    except FileNotFoundError:
        with open('data.txt', 'w') as file:
            pass
        print('To start, you have to create your wallpapers \n')
        print(f'How to do that: \n'
              f'1. Type "add" \n'
              f'2. Type what weather this wallpaper will appear with \n'
              f'3. Paste absolute path to this wallpaper \n'
              f'4. Profit :3')

    w = input()
    if w == 'add':
        d = dict()
        w = input('Type what weather this wallpaper will appear with \n')
        print('Paste absolute path to this wallpaper \n')
        w2 = input()
        d[w] = w2
        with open('data.txt', 'w') as file:
            json.dump(d, file)

    elif w == 'start':
        print('Type "stop" anytime you want to stop the program')
        with open('data.txt', 'r') as file:
            data = json.load(file)
        while True:
            try:
                path = data[get_weather(location_key)]
                print(path)
                ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 0)
            except KeyError:
                pass
            time.sleep(800)


import pandas as pd
import numpy as np
from scipy import stats
import tensorflow as tf
from sklearn.model_selection import train_test_split

def windows(data, size):
    start = 0
    while start < data.count():
        yield int(start), int(start + size)
        start += (size / 2)

def segment_signal(data,window_size = 12):
    segments = np.empty((0,window_size,6))
    labels = np.empty((0))
    for (start, end) in windows(data['Date'], window_size):
        x = data["mav5"][start:end]
        y = data["mav10"][start:end]
        z = data["mav20"][start:end]
        a = data["mav30"][start:end]
        b = data["mav50"][start:end]
        c = data["mav100"][start:end]
        if(len(data['Date'][start:end]) == window_size):
            segments = np.vstack([segments,np.dstack([x,y,z,a,b,c])])
            labels = np.append(labels,stats.mode(data["Action"][start:end])[0][0])
    return segments, labels

def get_batches(X, y, batch_size = 100):
    """ Return a generator for batches """
    n_batches = len(X) // batch_size
    X, y = X[:n_batches*batch_size], y[:n_batches*batch_size]

    # Loop over batches and yield
    for b in range(0, len(X), batch_size):
        yield X[b:b+batch_size], y[b:b+batch_size]

def create_tensorflow_train_data(csvfilename):
    df = pd.read_csv('sampledata/'+ csvfilename)
    segments, labels = segment_signal(df)
    list_ch_train = df.Action.unique()
    labels = np.asarray(pd.get_dummies(labels), dtype = np.int8)
    X_tr, X_vld, lab_tr, lab_vld = train_test_split(segments, labels, stratify = labels, random_state = 123)

    return X_tr, X_vld, lab_tr, lab_vld, list_ch_train

def create_tensorflow_test_data(csvfilename):
    df = pd.read_csv('sampledata/'+ csvfilename)
    segments, labels = segment_signal(df)
    list_ch_test = df.Action.unique()
    labels = np.asarray(pd.get_dummies(labels), dtype = np.int8)
    X_test = segments
    y_test = labels

    return X_test, y_test, list_ch_test

Trading Rule Identification by CNN/tensorflow_setup.py


'''
Dropout is easily implemented only during training by randomly selecting nodes
to be dropped-out with a given probability (e.g. 20%) each weight update cycle.
Only used during the training of a model and is not used when evaluating the
skill of the model.

Trained network is capable of better generalization and is less likely to
overfit the training data.
'''
import numpy
import pandas
from keras.constraints import maxnorm
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.optimizers import SGD
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, StandardScaler

# Create and compile model
def create_model():
    model = Sequential()
    model.add(Dense(60, input_dim=60, init='normal', activation='relu',
                    W_constraint=maxnorm(3)))
    model.add(Dropout(0.2))
    model.add(Dense(30, init='normal', activation='relu',
                    W_constraint=maxnorm(3)))
    model.add(Dropout(0.2))
    model.add(Dense(1, init='normal', activation='sigmoid'))
    sgd = SGD(lr=0.1, momentum=0.9, decay=0.0, nesterov=False)
    model.compile(loss='binary_crossentropy', optimizer=sgd,
                  metrics=['accuracy'])
    return model

# Fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# Load dataset
dataframe = pandas.read_csv("csv/sonar.csv", header=None)
dataset = dataframe.values
X = dataset[:, 0:60].astype(float)
Y = dataset[:, 60]

# Encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)

# Evaluate model with standardized dataset
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasClassifier(build_fn=create_model,
                  nb_epoch=300, batch_size=16, verbose=0)))

# Executes one or more models within a pass of the cross validation procedure
pipeline = Pipeline(estimators)

# 10-fold cross validation test
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)

# Evaluate a score by cross-validation
results = cross_val_score(pipeline, X, encoded_Y, cv=kfold)
print("Best: %.2f%% (%.2f%%)" %
      (results.mean()*100, results.std()*100))


# -*- coding: utf-8 -*-
"""
Created on Fri Feb 23 16:04:52 2018

@author: Orlov
"""

import argparse

cl_parser=argparse.ArgumentParser(description='Test mini argparse programm for 1st GitHub publishing. \
                                  Powers 1st number in the 2nd value')
cl_parser.add_argument('-n', '--number', help='number to be powered', type=float)
cl_parser.add_argument('-p', '--power', help='power value', type=float)

args=cl_parser.parse_args()
num=args.number
power=args.power

print(num**power)


# Import the required libraries
import pandas as pd
import numpy as np
import os
import glob
import json
import csv
import pyarrow as pa
from pyarrow import Table
import pyarrow.parquet as pq
import pyarrow.csv as pv
import pandasql as ps

#Folder path of source file
path=r'dataset'
os.listdir(path)
# List all files in a directory using os.listdir
json_pattern = os.path.join(path, '*.json')
file_list = glob.glob(json_pattern)

for fs in file_list:
    print(fs)
    df= pd.read_json(fs)
    #print(df)

#define the schema

def get_list_of_json_files():
    path=r'dataset'
    os.listdir(path)
    # List all files in a directory using os.listdir
    json_pattern = os.path.join(path, '*.json')
    list_of_files = glob.glob(json_pattern)
    return list_of_files

def create_list_from_json(jsonfile):
    with open(jsonfile) as f:
        data = json.load(f)
        #print(data)
        df2= pd.json_normalize(data, "attributes")
        #df1=pd.DataFrame(data)
        df2.to_csv('output.csv',mode='a', encoding='utf-8',index=False)

def write_csv(path):
    list_of_files = get_list_of_json_files()
    #print(list_of_files)

    for file in list_of_files:
        #path1=path+'\\'+file
        create_list_from_json(file)  # create the row to be added to csv for each file (json-file)

# Save the csv file in parquet format and partion on the basis of Status.
def convert_csvToParquet(csvfile):
        df=pd.read_csv(csvfile)
        table = pa.Table.from_pandas(df)
        pq.write_to_dataset(table,root_path='output1.parquet',partition_cols=['STATUS'])
        pandas_df=pd.read_parquet('output1.parquet')
        print(pandas_df)

# Get the total sales value of the cancelled orders
def query_total_sales_cancelled(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    sqlquery = "select sum(SALES) from pandas_df where STATUS='Cancelled'"
    print(ps.sqldf(sqlquery))

#  total sales value of the orders currently on hold for the year 2005
def query_totalsales_2005(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    sqlquery = "select sum(SALES) from pandas_df where STATUS='On Hold' and ORDERDATE like '%2005%'"
    print(ps.sqldf(sqlquery))

# Count of distinct products per product line
def query_count_distinct_productlines(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    sqlquery = "select PRODUCTLINE,count(*) from pandas_df group by PRODUCTLINE"
    print(ps.sqldf(sqlquery))

# Total sales variance for sales calculated at both sales price and MSRP
def query_variance(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    sqlquery_sales = "select sum(SALES)from pandas_df"
    sqlquery_expected = "select sum(MSRP*QUANTITYORDERED)from pandas_df"
    value_sales=ps.sqldf(sqlquery_sales)
    value_expected=ps.sqldf(sqlquery_expected)
    #total_variance=sqlquery_expected-sqlquery_sales
    print("Variance"+value_sales)

# Percentage change in sales YoY for classic cars
def query_PerChangeClassicCars(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    sqlquery_2004 = "select sum(SALES) from pandas_df where STATUS='Shipped' and ORDERDATE like '%2004%'"
    sqlquery_2005 = "select sum(SALES) from pandas_df where STATUS='Shipped' and ORDERDATE like '%2005%'"
    value_sales_2004=ps.sqldf(sqlquery_2004)
    value_expected_2005=ps.sqldf(sqlquery_2005)
    print(value_expected_2005)
    per_Change=((value_expected_2005-value_expected_2004)/value_expected_2004)*100
    total_variance=sqlquery_expected-sqlquery_sales
    print("PerChangeClassicCars"+total_variance)

# Percentage change in sales YoY for classic cars
def query_FilterProductLine(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    print(pandas_df[(pandas_df['PRODUCTLINE'] == 'Vintage Cars') | (pandas_df['PRODUCTLINE'] == 'Classic Cars') | (pandas_df['PRODUCTLINE'] == 'Motorcycles') | (pandas_df['PRODUCTLINE'] == 'Trucks and Buses') ])
    # print(pandas_df)

# Qunatity Based discounts
def query_QuantityBasedDiscount(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    pandas_df['QUANTITYORDERED'] = pd.to_numeric(pandas_df.QUANTITYORDERED, errors='coerce').fillna(0).astype(np.int64)
    pandas_df['SALES'] = pd.to_numeric(pandas_df.SALES, errors='coerce').fillna(0).astype(np.int64)
    pandas_df['DiscountedSale']=pandas_df['SALES']
    for index,item in pandas_df.iterrows():
        if item["QUANTITYORDERED"]<=30:
            pandas_df.iloc[index,7]= pandas_df.iloc[index,2]
        if item["QUANTITYORDERED"]>30 & item["QUANTITYORDERED"] <= 60:
            pandas_df.iloc[index]['DiscountedSale']= pandas_df.iloc[index]['SALES']*0.975
        if item["QUANTITYORDERED"] >60 & item["QUANTITYORDERED"] <=80:
            pandas_df.iloc[index]['DiscountedSale']= pandas_df.iloc[index]['SALES']*0.96
        if item["QUANTITYORDERED"] >80 & item["QUANTITYORDERED"] <=100:
            pandas_df.iloc[index]['DiscountedSale']= pandas_df.iloc[index]['SALES']*0.94
        if item["QUANTITYORDERED"] >100:
            pandas_df.iloc[index]['DiscountedSale']= pandas_df.iloc[index]['SALES']*0.90
    print(pandas_df)

def query_recalculateMSRP(parquetfile):
    pandas_df=pd.read_parquet('output1.parquet')
    pandas_df['QUANTITYORDERED'] = pd.to_numeric(pandas_df.QUANTITYORDERED, errors='coerce').fillna(0).astype(np.int64)
    pandas_df['SALES'] = pd.to_numeric(pandas_df.SALES, errors='coerce').fillna(0).astype(np.int64)
    pandas_df['RecalculatedSales']=pandas_df['SALES']/pandas_df['QUANTITYORDERED']
    print(pandas_df)

if __name__=="__main__":
    path=r'dataset'
    # Total sales value of the cancelled orders
    query_total_sales_cancelled('output1.parquet')

    # Total sales value of the orders currently on hold for the year 2005
    query_totalsales_2005('output1.parquet')

    # Cunt of distinct products per product line
    query_count_distinct_productlines('output1.parquet')

    # total sales variance for sales calculated at both sales price and MSRP
    query_variance('output1.parquet')

    #  percentage change in sales YoY for classic cars, for years 2004 and 2005 where the status is shipped
    query_PerChangeClassicCars('output1.parquet')

    # Filter the data from productline
    query_FilterProductLine('output1.parquet')

    # Quantity based sales column
    query_QuantityBasedDiscount('output1.parquet')

    # Calculated column by recalculating sales using MSRP.
    query_recalculateMSRP('output1.parquet')


import pandas as pd

from PIL import Image
from pathlib import Path

from fastprogress import master_bar, progress_bar

from src.constants import HEIGHT, WIDTH

if __name__ == "__main__":
    data_dir = Path("input/bengaliai-cv19")

    mb = master_bar(["train", "test"])
    for data_type in mb:
        output_dir = data_dir / f"{data_type}_images"
        output_dir.mkdir(parents=True, exist_ok=True)

        for i in progress_bar(range(4), parent=mb, leave=False):
            df = pd.read_parquet(
                data_dir / f"{data_type}_image_data_{i}.parquet")
            images = df.iloc[:, 1:].values.reshape(-1, HEIGHT, WIDTH)

            for im in range(len(images)):
                image = Image.fromarray(images[im])
                image_id = df.loc[im, "image_id"]
                image.save(output_dir / f"{image_id}.png")


from typing import List, Dict
import torch
from torch import tensor
from torch._C import device
from torch_utils import pi

def calculate_tangent_rots(
    subdivision_level: List[int],
    device: torch.device = torch.device("cpu")
    ) -> List[Dict[str, float]]:
    """ Calculate rotation parameters for transformation

        params:
            subdivision_level (int): set number of faces, which equals to:
                    num_of_faces = 20*(4^b), where b=subdivision_level
        returns:
            rots (List[[dict]]): list of dicts with rots parameters.

    """
    rots = [[] for _ in subdivision_level]
    radius = 10
    for i, sub_lvl in enumerate(subdivision_level):
        faces = init_20_faces(radius, device=device)
        for _ in range(0, sub_lvl):
            faces = subdivide_faces(faces, radius, device=device)
        angles = calculate_angles(faces, device=device)
        for ang in angles:
            rots[i].append(
                {
                    'roll': 0.,
                    'pitch': ang[1],
                    'yaw': ang[0]
                }
            )
    return rots

def calculate_tangent_angles(
    subdivision_level: List[int],
    device: torch.device = torch.device("cpu")
    ) -> List[torch.Tensor]:
    """ Calculate angles to icosahedron faces

        params:
            subdivision_level (int): set number of faces, which equals to:
                    num_of_faces = 20*(4^b), where b=subdivision_level
        returns:
            angles (List[[np.ndarray]]): list of angles.

    """
    angles = [[] for _ in subdivision_level]
    radius = 10
    for i, sub_lvl in enumerate(subdivision_level):
        faces = init_20_faces(radius, device=device)
        for _ in range(0, sub_lvl):
            faces = subdivide_faces(faces, radius, device=device)
        angles[i] = calculate_angles(faces, device=device)
    return angles

def calculate_angles(
    faces: torch.Tensor,
    device: torch.device=torch.device("cpu")
    ) -> torch.Tensor:
    """ Calculate angles from (0,0,0) point to centroid of input faces

        params:
            faces: array of triplets with coordinates of icosahedron faces. Each face
            represented as array of shape [n,3,3], where n - number of faces.

        returns:
            angles: array of longtitude and latitude angles to input faces. Returns
            array of shape [n,2]

    """
    centroids = calculate_centroids(faces, device)
    angles = torch.zeros(centroids.shape[0], 2, device=device)
    for i, c in enumerate(centroids):
        lat = calculate_lat(c, tensor([c[0], c[1], 0], dtype=torch.float32, device=device))
        lon = calculate_lon(c[:2], tensor([0,1], dtype=torch.float32, device=device))
        angles[i] = tensor([lon, lat])
    angles[:, 1] *= torch.sign(centroids[:, 2])
    return angles * -1

def subdivide_faces(
    faces: torch.Tensor,
    radius: int=10,
    device: torch.device = torch.device("cpu")
    ):
    """ Subdivide faces of icosahedron on next level.

        params:
            faces: array of triplets with coordinates of icosahedron faces. Each face
            represented as array of shape [n,3,3], where n - number of faces.

        returns:
            new_faces: array of subdivided faces. Returns array of shape [n*4,3,3]

    """
    new_faces = torch.zeros(faces.shape[0]*4,3,3, device=device)
    for i, face in enumerate(faces):
        v1,v2,v3 = face

        v1_new = __computeHalfVertex(v1, v2)
        v2_new = __computeHalfVertex(v2, v3)
        v3_new = __computeHalfVertex(v1, v3)

        # add 4 new triangles to vertex array
        new_faces[i*4] = torch.vstack([v1,    v1_new, v3_new])
        new_faces[i*4+1] = torch.vstack([v1_new, v2,    v2_new])
        new_faces[i*4+2] = torch.vstack([v1_new, v2_new, v3_new])
        new_faces[i*4+3] = torch.vstack([v3_new, v2_new, v3])
    return new_faces

def init_icosahedron_vertices(
    radius: int = 10,
    device: torch.device = torch.device("cpu")
    ) -> torch.Tensor:
    """ Calculate icosahedron vertices at zero subdivision_level.
        Used for icosahedron initialization

        params:
            radius: radius of icosahedron sphere. Can be removed with constant in all methods.

        returns:
            vertices: points of initialized icosahedron, array of shape [20,3]

    """
    PI, r = pi.to(device), radius
    H_ANGLE = torch.clone(PI) / 180 * 72
    V_ANGLE = torch.arctan(tensor(1.0 / 2, device=device))
    vertices = torch.zeros(12,3, device=device)
    hAngle1, hAngle2 = -PI / 2 - H_ANGLE / 2, -PI / 2

    vertices[0] = tensor([0,0,r], device=device)
    vertices[-1] = tensor([0,0,-r], device=device)

    xy,z = torch.ones(10,2, device=device), torch.ones(10,1, device=device)
    xy *= r*torch.cos(V_ANGLE)
    z *= r*torch.sin(V_ANGLE)
    h1 = (torch.arange(0,5, device=device) * H_ANGLE) + hAngle1
    h2 = (torch.arange(0,5, device=device) * H_ANGLE) + hAngle2
    xy[:5,0] = xy[:5,0] * torch.cos(h1)
    xy[:5,1] = xy[:5,1] * torch.sin(h1)
    xy[5:,0] = xy[5:,0] * torch.cos(h2)
    xy[5:,1] = xy[5:,1] * torch.sin(h2)
    z[5:] = -z[5:]
    xyz = torch.hstack([xy,z])

    vertices[1:-1] = xyz
    return vertices

def init_20_faces(
    radius: int = 10,
    device: torch.device = torch.device("cpu")
    ):
    """ Calculate icosahedron faces at zero subdivision level.
        Used for icosahedron initialization

        params:
            radius: radius of icosahedron sphere. Can be removed with constant in all methods.

        returns:
            faces: coordinates of icosahedron faces, array of shape [20,3,3]

    """
    vertices = init_icosahedron_vertices(radius, device)
    faces = torch.zeros(20,3,3, device=device)
    upper_pent = vertices[1:6]
    down_pent = vertices[-6:]
    for i in range(1, 6):
        faces[i-1] = torch.vstack([vertices[0], upper_pent[i%5], upper_pent[i-1]])
        faces[-i] = torch.vstack([vertices[-1], down_pent[i%5], down_pent[i-1]])
    # head, middle and tail pointers for triangles
    ptr_h, ptr_m, ptr_t = -1, 0, 0
    for i in range(1, 11):
        if i%2 != 0:
            ptr_h += 1
            ptr_t += 1
            faces[i+4] = torch.vstack([upper_pent[ptr_h], down_pent[ptr_m%5], upper_pent[ptr_t%5]]).reshape((1,3,3))
        if i%2 == 0:
            ptr_m += 1
            faces[i+4] = torch.vstack([down_pent[ptr_h], upper_pent[ptr_m%5], down_pent[ptr_t%5]]).reshape((1,3,3))
    return faces

def calculate_centroids(
    faces: torch.Tensor,
    device: torch.device = torch.device("cpu")
    ):
    """ Calculate centroids of faces

        params:
            faces: array of faces

        returns:
            centroids: array of centroid cordinates for input faces. Output array
            with shape of [n,3], where n - number of input faces
    """
    centroids = torch.zeros((faces.shape[0],3), device=device)
    for i, face in enumerate(faces):
        centroids[i] = face.sum(axis=0)/3
    return centroids

def __computeHalfVertex(v1, v2, radius:int = 10):
    """ Compute half of icosahedron vertices

        params:
            v1,v2 (np.ndarray): two vertices points
            radius: radius of icosahedron sphere. Can be removed with constant in all methods.

        returns:
            new_v (np.ndarray): coordinates of half verice scaled on scale factor
    """
    new_v = v1+v2
    scale = radius / torch.sqrt(torch.sum(new_v**2))
    return new_v * scale

def calculate_lat(
    vec1: torch.Tensor,
    vec2: torch.Tensor,
    device: torch.device = torch.device("cpu")
    ):
    """ Calculate latitude between two vectors
    """
    unit_vector_1 = (vec1 / torch.linalg.norm(vec1)).to(device)
    unit_vector_2 = (vec2 / torch.linalg.norm(vec2)).to(device)
    dot_product = torch.dot(unit_vector_1, unit_vector_2)
    return torch.arccos(dot_product)

def calculate_lon(
    vec1: torch.Tensor,
    vec2: torch.Tensor
    ):
    """ Calculate longtitude between two vectors
    """
    dot = torch.dot(vec1, vec2)
    det = torch.linalg.det(torch.vstack([vec1, vec2]))
    return torch.atan2(det, dot)


import numpy as np
import os

from bokeh.io import vform

from smartpy.pps import PPSRegression
from bokeh.plotting import output_file, show, figure
from bokeh.models import ColumnDataSource, CustomJS, Slider

output_file(os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'polynomial_approximation.html'),
            title='Aproximação de Polinômios através do PPS')

# creating polynomial function
def poly1(x):
    return x ** 5 - 8 * x ** 3 + 10 * x + 6
poly1 = np.vectorize(poly1, otypes=[np.float])

# generating and processing the polynomial functions
X = np.arange(-5., 5., 0.1)

x1 = poly1(X)

# display the original function
p = figure(title='Aproximação de Polinômios através do PPSs')
p.line(X, x1, legend="x^2 + 2", line_color="blue", line_width=2)

# compute and show the predictions
pps1_approximation = []
for i in range(1, 11):
    name = 'PPS1 with %d order' % i
    pps1 = PPSRegression(name=name, order=i)
    pps1.train(X, x1)
    pps1_approximation.append(pps1.predict(X))
source1_plot = ColumnDataSource(data=dict(x=X, y=pps1_approximation[0], pps=pps1_approximation))
p.line('x', 'y', source=source1_plot, legend="PPS: x^2 + 2", line_color='blue', line_dash='4 4')
callback = CustomJS(args=dict(source=source1_plot), code="""
        var data = source.get('data');
        var i = cb_obj.get('value')
        console.log(data);
        console.log(i);
        x = data['x']
        y = data['y']
        _y = data['pps'][i]
        for(var k = 0; k < _y.length; k++){
            y[k] = _y[k]
        }
        source.trigger('change');
    """)
slider = Slider(start=1, end=10, value=0, step=1, title="PPS 1", callback=callback)

layout = vform(slider, p)
show(layout)


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 28 21:41:33 2017

@author: Congying.Xu
"""

import requests
response = requests.get("http://211.249.63.55")
text = response.text
print(text)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct  7 11:44:30 2013

@author: jinpeng.li@cea.fr
"""

import unittest
import numpy as np
from sklearn import datasets
from epac import Methods
from epac.workflow.splitters import WarmStartMethods
from epac.tests.utils import comp_2wf_reduce_res
from epac.tests.utils import compare_two_node

class TOY_CLF:
    def __init__(self, v_lambda):
        self.v_lambda = v_lambda
        self.v_beta = None

    def _get_error(self, v_beta, v_lambda, X, y):
        pred_y = np.dot(X, v_beta)
        ss_error = np.sum((y - pred_y) ** 2, axis=0)
        return ss_error

    def transform(self, X, y):
        len_beta = X.shape[1]

        min_err = 0
        if not (self.v_beta is None):
            min_err = self._get_error(self.v_beta, self.v_lambda, X, y)

        # Search the beta which minimizes the error function
        # ==================================================
        for i in range(10):
            v_beta = np.random.random(len_beta)
            err = self._get_error(v_beta, self.v_lambda, X, y)
            if (self.v_beta is None) or err < min_err:
                self.v_beta = v_beta
                min_err = err

        pred_y = np.dot(X, self.v_beta)
        return {"y/pred": pred_y, "y/true": y, "best_beta": self.v_beta}

class TestWorkFlow(unittest.TestCase):
    def test_prev_state_methods(self):
        ## 1) Build dataset
        ## ================================================
        X, y = datasets.make_classification(n_samples=5,
                                            n_features=20,
                                            n_informative=2)
        Xy = {"X": X, "y": y}
        methods = Methods(*[TOY_CLF(v_lambda=v_lambda)
                            for v_lambda in [2, 1]])
        methods.run(**Xy)

        ps_methods = WarmStartMethods(*[TOY_CLF(v_lambda=v_lambda)
                                        for v_lambda in [2, 1]])
        ps_methods.run(**Xy)
        self.assertTrue(compare_two_node(methods, ps_methods))
        self.assertTrue(comp_2wf_reduce_res(methods, ps_methods))

if __name__ == '__main__':
    unittest.main()


import pyqrcode
from enum import Enum

class QrFormats(Enum):
    PNG = 'png'
    SVG = 'svg'
    EPS = 'eps'

    @classmethod
    def has_value(cls, value):
        return any(value == item.value for item in cls)

    @classmethod
    def list(cls):
        return [item.value for item in cls]

def generate(content, file, file_format=QrFormats.PNG, scale=6):
    if isinstance(file_format, str):
        try:
            file_format = QrFormats(file_format.lower())
        except ValueError:
            print("File format not in ", QrFormats.list())

    qr = pyqrcode.create(content=content)
    file = f"{file}.{file_format.value}"

    if file_format == QrFormats.PNG:
        qr.png(file=file, scale=scale)
    elif file_format == QrFormats.SVG:
        qr.svg(file=file, scale=scale)
    elif file_format == QrFormats.EPS:
        qr.eps(file=file, scale=scale)
    else:
        qr.terminal()


import json
import logging
import os

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError

HOOK_URL = os.environ['HookUrl']

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info("Event: " + str(event))
    message = event['message']
    try:
        stage = " on stage - " + message['detail']['stage']
        logger.info(stage)
    except:
        stage=""
    if message['detail']['pipeline'] != "SC-019725351547-pp-wnmj5clcy4qgq-BackendPipeline-3E189YXAX76N":
        return {
            "statusCode":200,
            "body": "No msg sent to MS Teams"
        }

    message = {
    "@type": "MessageCard",
    "@context": "http://schema.org/extensions",
    "themeColor": "0076D7",
    "summary": message['source'],
    "title": message['detail-type'],
    "sections": [
        {
            "activityTitle": message['detail']['pipeline'] + " has " + message['detail']['state'] +stage,
            "activitySubtitle": "On "+ message['time'],
            "activityImage": "https://img.stackshare.io/service/3297/aws-codepipeline.png",
        }
    ]
}

    req = Request(HOOK_URL, json.dumps(message).encode('utf-8'))
    try:
        response = urlopen(req)
        response.read()
        logger.info(message)
        logger.info("Message posted")
        return {
            "statusCode":200,
            "body": "Success"
        }
    except HTTPError as e:
        logger.error("Request failed: %d %s", e.code, e.reason)
    except URLError as e:
        logger.error("Server connection failed: %s", e.reason)


import os
import psutil
import shutil
import sys

class colors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def printcol(s, c):
    print(c + s + colors.ENDC)

def file_duplication(f):
    if os.path.isfile(f):
        shutil.copy(f, f + '.dupl')
        if os.path.exists: # checking if duplication was successful
            print("duplicated: " + f)
            return True
        else:
            printcol("ERROR while duplicating file: " + f, colors.FAIL)
            return False
    else:
        printcol("ignoring directory: " + f, colors.WARNING)
        return False

def delete_duplicates():
    file_list = os.listdir(".")
    cnt = 0
    for f in file_list:
        if os.path.isfile(f) and f.endswith('.dupl'):
            print("to be removed: " + f)
            os.remove(f)
            if not os.path.exists(f):
                cnt += 1
    return cnt

def system_info():
    printcol("SYSTEM INFO", colors.BLUE)
    printcol("platform: " + sys.platform, colors.BLUE)
    printcol("os name: " +  os.name, colors.BLUE)
    printcol("current dir: " + os.getcwd(), colors.BLUE)
    printcol("current user: " + os.getlogin(), colors.BLUE)
    printcol("file system coding: " + sys.getfilesystemencoding(), colors.BLUE)
    printcol("CPU count: " + str(psutil.cpu_count(logical=False)), colors.BLUE)

printcol("\nHello\n", colors.HEADER)

is_continue = "y"
while True:
    if is_continue == "y":

        printcol("I can do following: ", colors.GREEN)
        printcol(" [1] - Print file list", colors.GREEN)
        printcol(" [2] - Print system info", colors.GREEN)
        printcol(" [3] - Print process list", colors.GREEN)
        printcol(" [4] - Duplicate files in cur dir", colors.GREEN)
        printcol(" [5] - Duplicate specified file", colors.GREEN)
        printcol(" [6] - Remove duplicates", colors.GREEN)

        do = int(raw_input(colors.GREEN + "select, please: " + colors.ENDC))

        if do == 1: # Print file list
            print(os.listdir("."))

        elif do == 2: # Print system info
           system_info()

        elif do == 3: # Print process list
            print(psutil.pids())

        elif do == 4: #Duplicate file list
            print("duplicating....")
            file_list = os.listdir(".")
            for f in file_list:
                file_duplication(f)

        elif do == 5: # Duplicate specified file
            print("duplicating....")
            f = raw_input(colors.GREEN + "enter file name: " + colors.ENDC)
            file_duplication(f)

        elif do == 6: # Remove duplicates
            print("removing duplicates....")
            cnt = delete_duplicates()
            printcol("total: " + str(cnt), colors.WARNING)

        else:
            print("unknown operation")
    elif is_continue == "n":
        printcol("\nNoooooooooo\n", colors.HEADER)
        break
    else:
        printcol("\nHwat??\n", colors.FAIL)

    is_continue = raw_input(colors.GREEN + "continue (y/n)? " + colors.ENDC)


import pygame, sys
from pygame import *
from map.map import Map
from army.army import Army

WIDTH = 480
HEIGHT = 480
WHITE = (255, 255, 255)     # RGB
BLACK = (0, 0, 0)           # RGB
RED = (255, 0, 0)           # RGB
BLUE = (0, 0, 255)          # RGB

def launch(field: Map, blue: Army, red: Army) -> None:
    pygame.init()
    screen = display.set_mode((field.width, field.height), 0, 32)
    display.set_caption("CRAZY COMBAT SIM")
    screen.fill(WHITE)
    timer = pygame.time.Clock()

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        timer.tick(60)  # 60 times per second you can do the math for 17 ms
        screen.fill(WHITE)
        drawArmy(screen, blue, BLUE)
        drawArmy(screen, red, RED)
        display.update()
        blue.step(red)
        red.step(blue)

def drawArmy(screen, army: Army, color: (int, int, int)) -> None:
    for sl in army.army:
        pos_on_screen = (int(sl.x), int(sl.y))
        draw.circle(screen, color, pos_on_screen, 1)


#
# Authors: Robert Abram <robert.abram@entpack.com>
#
# Copyright (C) 2015 EntPack
# see file 'LICENSE' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
from distutils.core import setup
from setuptools import find_packages

__VERSION__ = "0.6"

def find_package_data_files(dirs):
    paths = []
    for directory in dirs:
        for (path, directories, filenames) in os.walk(directory):
            for filename in filenames:
                paths.append(os.path.join('..', path, filename))
    return paths

def setup_package():

    # Recursively gather all non-python module directories to be included in packaging.
    core_files = find_package_data_files([
    ])

    setup(name='SilentDune-Server',
        version=__VERSION__,
        description='Silent Dune Server',
        author='Robert Abram',
        author_email='robert.abram@entpack.com',
        url='https://github.com/EntPack/SilentDune',
        packages=find_packages(exclude=['tests']),
        package_data={
            'silentdune_server': core_files,
        },
        keywords=['firewall', 'security'],  # arbitrary keywords
        classifiers=[
            'Development Status :: 4 - Beta',
            'Programming Language :: Python',
            'Environment :: Console',
            'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
            'Operating System :: POSIX :: Linux',
        ],

        install_requires=[
            'django',
            'django-oauth-toolkit',
            'djangorestframework',
            'django-compressor',
            'django-celery',
            'django-celery-with-redis',
            'django-countries',
            'django-axes',
            'django-filter',
            'cryptography',
            'mysqlclient',
            'requests',
            'raven',
            'sqlparse',
        ],

        tests_require=[
            'pytest',
            'pytest-runner',
            'pytest-pythonpath',
        ],
     )

if __name__ == "__main__":
    setup_package()