#All code for the release version is in this cell
#The release version will load the model from memory and start playing
#This version will not record the results or re-train the model
#It will only have the necessary imports
import multiprocessing

if __name__ == "__main__":
    multiprocessing.freeze_support() #Fixing pyinstaller

    import keras.models
    import math
    import pyscreenshot
    import time
    import pyautogui
    import cv2
    import numpy as np
    import copy
    import keras.utils
    import random

    #Let's define some functions to play the game and save the data
    #This finds the position of the game on the current screen
    def autoCalibrate():
        if __name__ == "__main__":
            topLeft  = pyautogui.locateOnScreen('hold.png')
            bottomRight = pyautogui.locateOnScreen('next.png')
            x_pad = topLeft[0] - 12
            y_pad = topLeft[1] - 47
            x_width = bottomRight[0] + 176
            y_length = bottomRight[1] + 550
            return (x_pad, y_pad, x_width, y_length)
        else:
            return None

    #This captures the screen and converts the gamefield into a matrix format
    def getAllBlocks():
        # Define the matrix for all the blocks
        x_size = 10
        y_size = 20
        gameMatrix = [[0 for x in range(x_size)] for y in range(y_size)]
        # Define the color that can't be a block
        black = (0,0,0)
        # Read the frame and translate it into HSV colorspace
        frame = cv2.imread("game.png")
        frame = frame[36:560, 214:482]
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        # Fill the matrix with ones and zeros, so all the blocks would be displayed correctly
        for y in range(y_size):
            for x in range(x_size):
                x1 = 13 + (26*x)
                y1 = (560-36-13) - (26*y)
                r, g, b = frame[y1, x1]
                if (r,g,b) == black:
                    gameMatrix[y][x] = 0
                else:
                    gameMatrix[y][x] = 1
        return gameMatrix

    #This takes an image and figures out which tetromino is displayed
    #Then it returns an index for that tetromino
    def getNextBlock(frame):
        # Read in all the shape files and make them into grayscale
        yellow = cv2.imread("yellow.png")
        purple = cv2.imread("purple.png")
        red = cv2.imread("red.png")
        blue = cv2.imread("blue.png")
        teal = cv2.imread("teal.png")
        green = cv2.imread("green.png")
        orange = cv2.imread("orange.png")
        yellow = cv2.cvtColor(yellow, cv2.COLOR_BGR2GRAY)
        purple = cv2.cvtColor(purple, cv2.COLOR_BGR2GRAY)
        red = cv2.cvtColor(red, cv2.COLOR_BGR2GRAY)
        blue = cv2.cvtColor(blue, cv2.COLOR_BGR2GRAY)
        teal = cv2.cvtColor(teal, cv2.COLOR_BGR2GRAY)
        green = cv2.cvtColor(green, cv2.COLOR_BGR2GRAY)
        orange = cv2.cvtColor(orange, cv2.COLOR_BGR2GRAY)
        # Match the template wih the frame
        res1 = cv2.matchTemplate(frame,yellow,cv2.TM_CCOEFF_NORMED)
        res2 = cv2.matchTemplate(frame,purple,cv2.TM_CCOEFF_NORMED)
        res3 = cv2.matchTemplate(frame,red,cv2.TM_CCOEFF_NORMED)
        res4 = cv2.matchTemplate(frame,blue,cv2.TM_CCOEFF_NORMED)
        res5 = cv2.matchTemplate(frame,teal,cv2.TM_CCOEFF_NORMED)
        res6 = cv2.matchTemplate(frame,green,cv2.TM_CCOEFF_NORMED)
        res7 = cv2.matchTemplate(frame,orange,cv2.TM_CCOEFF_NORMED)
        # Determine whether the block was detected or not
        threshold = 0.8
        match1 = False
        if np.amax(res1) > threshold:
            match1 = True
        match2 = False
        if np.amax(res2) >= threshold:
            match2 = True
        match3 = False
        if np.amax(res3) >= threshold:
            match3 = True
        match4 = False
        if np.amax(res4) >= threshold:
            match4 = True
        match5 = False
        if np.amax(res5) >= threshold:
            match5 = True
        match6 = False
        if np.amax(res6) >= threshold:
            match6 = True
        match7 = False
        if np.amax(res7) >= threshold:
            match7 = True
        # Return the block number in order to determin what block was detected
        if match1:
            return 1
        elif match2:
            return 2
        elif match3:
            return 3
        elif match4:
            return 4
        elif match5:
            return 5
        elif match6:
            return 6
        elif match7:
            return 7
        else:
            #print("getNextBlock didn't match.")
            raise ValueError("getNextBlock didn't match.")
    #         print([res1, res2, res3, res4, res5, res6, res7])
    #         cv2.imshow('image',frame)
    #         cv2.waitKey(0)

    #This starts the game by pressing on the "play" button
    def startGame():
        x, y = pyautogui.locateCenterOnScreen('play.png')
        pyautogui.click(x, y)

    #This function will make a move
    #x - how many squares to the right should the block be dropped? (0 to 9)
    def makeMove(x, x_pad, y_pad):
        pyautogui.click(x_pad+230 + 26*x, y_pad+100)

    #This function will pause and quit the game
    def endGame(x_pad, y_pad):
        pyautogui.click(x_pad+700, y_pad+540)
        time.sleep(0.1)
        pyautogui.click(x_pad+360, y_pad+400)
        time.sleep(0.1)
        pyautogui.click(x_pad+360, y_pad+300)

    #Get the height of the tower of blocks in the game matrix
    def getHeight(gameMatrix):
        matrix = gameMatrix[:14]
        height = 0
        for row in range(14):
            if matrix[row] != [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]:
                height += 1
        return height

    #Let's define some functions to process the data from the model

    #Code to read training data from the text document
    def readData():

        file = open("train_data.txt", "r")
        dataString = file.readlines()
        file.close()
        X = ast.literal_eval(dataString[0])
        y = ast.literal_eval(dataString[1])

        return (X, y)

    #Code for pre-processing the data
    def preProcessData(X, y=[]):
        #X = np.array(X)
        X = copy.deepcopy(X)
        y = copy.deepcopy(y)
        for i in range(len(X)):
            el = X[i]
            #Let's flatten the coordinate matrix so that it is easier to process
            newMatrix = np.array(el[0]).flatten()
            #Let's convert data for the tile to an appropriate one-hot vector
            currentTile = keras.utils.to_categorical([el[1]-1, 6])[0]
            #Now let's add these two lists together
            newMatrix = np.array(np.append(newMatrix, currentTile))
            X[i] = newMatrix

        if y != []:
            y = keras.utils,to_categorical(y)
        return (np.array(X), np.array(y))

    #Code for predicting the next move
    def predict(gameMatrix, currentBlock):
        moveState = [copy.deepcopy(gameMatrix[:10]), copy.copy(currentBlock)]
        modelInput = preProcessData([moveState])[0]
        result = model.predict(modelInput)[0]

        return np.argmax(result)

    model = keras.models.load_model("mudel.h5")

    print()
    print()
    numberOfGames = 10
    cont = False
    while cont == False:
        try:
            numberOfGames = int(input("Please enter the number of games the bot will play: ")) #How many games will be played
            cont = True
        except:
            print("Please enter an integer.")

    useRandom = -1
    cont = False
    while cont == False:
        try:
            useRandom = int(input("Please enter 0 if you want to use the tetris bot or 1 if you want to use random predictions: ")) #How many games will be played
            if useRandom not in [0,  1]:
                print("Please enter either 0 or 1.")
            else:
                cont = True
        except:
            print("Please enter either 0 or 1.")
    completedLines = 0 #Variable that stores the number of successful drops by bot
    #play the game using the model's predictions and makeMove()
    #Keep making moves in a loop.
    #If the total height of the tower was reduced, save the move for training
    print('Please open tetris at https://tetris.com/play-tetris/ so the "PLAY" button is clearly visible along with the rest of the game.')
    print("After the application has finished, you can find the result in tetrisbot_result.txt in this folder.")
    input("To continue, please press enter on this window. ")
    x_pad, y_pad, x_width, y_length = autoCalibrate()
    for i in range(numberOfGames):
        print("Progress: game " + str(i+1) + " out of " + str(numberOfGames))

        startGame()
        time.sleep(1) #This is so the game will have time to load
        giveUp = False #This will be used to end the loop
        lastMove = None #Data about the previous move
        lastMoveState = None #State of the game at prev. move
        lastHeight = -1 #Height of the prev. gamestate

        #Let's get the current block during the countdown
        im=pyscreenshot.grab(bbox =(x_pad,y_pad,x_width,y_length))
        im.save("game.png")

        frame = cv2.imread("game.png")
        frame1 = frame[100:172, 546:668]
        frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
        frame2 = frame[172:244, 546:668]
        frame2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        frame3 = frame[244:309, 546:668]
        frame3 = cv2.cvtColor(frame3, cv2.COLOR_BGR2GRAY)

        currentBlock = getNextBlock(frame1)

        time.sleep(3) #Let's wait for 3 more seconds so the countdown menu ends

        while not giveUp:
            im=pyscreenshot.grab(bbox =(x_pad,y_pad,x_width,y_length))
            im.save("game.png")

            frame = cv2.imread("game.png")
            frame1 = frame[100:172, 546:668]
            frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
            frame2 = frame[172:244, 546:668]
            frame2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
            frame3 = frame[244:309, 546:668]
            frame3 = cv2.cvtColor(frame3, cv2.COLOR_BGR2GRAY)

            gameMatrix = getAllBlocks()
            h = getHeight(gameMatrix)
            nextBlock1 = getNextBlock(frame1)
            nextBlock2 = getNextBlock(frame2)
            nextBlock3 = getNextBlock(frame3)

            #If the tower is over 10 blocks high, the bot ends the game
            if(h > 10):
                giveUp = True

            if useRandom == 0:
                move = predict(gameMatrix, currentBlock)
            else:
                move = random.randint(0, 9)

            if(not giveUp):
                makeMove(move, x_pad, y_pad)

            #If the last move reduced the height of the tower,
            #it was a good move and will be recorded
            if(h < lastHeight):
                completedLines += 1

            #Save the current data for future use
            lastHeight = h
            lastMoveState = [copy.deepcopy(gameMatrix[:10]), copy.copy(currentBlock)]
            lastMove = move
            currentBlock = nextBlock1
        #close the game and start a new one
        endGame(x_pad, y_pad)
    result = "The bot played " + str(numberOfGames) + " games and made " + str(completedLines) + " moves that completed a horizontal line."
    print(result)
    text_file = open("tetrisbot_result.txt", "w")
    text_file.write(result)
    if useRandom == 0:
        text_file.write("The bot used neural network predictions.")
    else:
        text_file.write("The bot used random predictions.")
    text_file.close()


from io import BytesIO

from pyrogram.raw.core.primitives import Int, Long, Int128, Int256, Bool, Bytes, String, Double, Vector
from pyrogram.raw.core import TLObject
from pyrogram import raw
from typing import List, Optional, Any

# # # # # # # # # # # # # # # # # # # # # # # #
#               !!! WARNING !!!               #
#          This is a generated file!          #
# All changes made in this file will be lost! #
# # # # # # # # # # # # # # # # # # # # # # # #

class SecureFile(TLObject):  # type: ignore
    """Telegram API type.

    Constructor of :obj:`~pyrogram.raw.base.SecureFile`.

    Details:
        - Layer: ``151``
        - ID: ``7D09C27E``

    Parameters:
        id (``int`` ``64-bit``):
            N/A

        access_hash (``int`` ``64-bit``):
            N/A

        size (``int`` ``64-bit``):
            N/A

        dc_id (``int`` ``32-bit``):
            N/A

        date (``int`` ``32-bit``):
            N/A

        file_hash (``bytes``):
            N/A

        secret (``bytes``):
            N/A

    """

    __slots__: List[str] = ["id", "access_hash", "size", "dc_id", "date", "file_hash", "secret"]

    ID = 0x7d09c27e
    QUALNAME = "types.SecureFile"

    def __init__(self, *, id: int, access_hash: int, size: int, dc_id: int, date: int, file_hash: bytes, secret: bytes) -> None:
        self.id = id  # long
        self.access_hash = access_hash  # long
        self.size = size  # long
        self.dc_id = dc_id  # int
        self.date = date  # int
        self.file_hash = file_hash  # bytes
        self.secret = secret  # bytes

    @staticmethod
    def read(b: BytesIO, *args: Any) -> "SecureFile":
        # No flags

        id = Long.read(b)

        access_hash = Long.read(b)

        size = Long.read(b)

        dc_id = Int.read(b)

        date = Int.read(b)

        file_hash = Bytes.read(b)

        secret = Bytes.read(b)

        return SecureFile(id=id, access_hash=access_hash, size=size, dc_id=dc_id, date=date, file_hash=file_hash, secret=secret)

    def write(self, *args) -> bytes:
        b = BytesIO()
        b.write(Int(self.ID, False))

        # No flags

        b.write(Long(self.id))

        b.write(Long(self.access_hash))

        b.write(Long(self.size))

        b.write(Int(self.dc_id))

        b.write(Int(self.date))

        b.write(Bytes(self.file_hash))

        b.write(Bytes(self.secret))

        return b.getvalue()


import string
from statistics import mean

from django.contrib.auth.models import User

from django.db import models
from django.db.models.signals import pre_save
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.exceptions import ValidationError

from card.models import Card

# Create your models here.
class Kit(models.Model):
    name = models.CharField(blank=False, null=False, max_length=50, verbose_name="Nombre")
    foreign_language = models.CharField(blank=False, null=False, max_length=20, verbose_name="Idioma Foraneo")
    native_language = models.CharField(blank=False, null=False, max_length=20, verbose_name="Idioma Nativo")
    added_date =  models.DateTimeField(auto_now_add=True, verbose_name="Creado el")
    successful = models.IntegerField(default=0, verbose_name="Ratio de exito", validators=[MinValueValidator(0), MaxValueValidator(100)])
    user = models.ForeignKey(User, editable=True, verbose_name='Usuario', on_delete=models.CASCADE)

    class Meta:
        verbose_name = "Kit"
        verbose_name_plural = "Kits"
        ordering = ['name']

    def __str__(self):
        return self.name + ' - ' + str(self.pk)

    def generate_cards(self, kit_file):
        """
            Generates the letters associated with the language kit.
        """
        cards = []
        foreign_words = []
        for line in kit_file.readlines():
            clean_line = line.strip()
            if clean_line.decode('utf-8') in string.whitespace:
                continue
            words = clean_line.decode('utf-8').split(':')[:2]
            foreign_word = words[0].strip()[:51]
            native_word = words[1].strip()[:51]
            if foreign_word in foreign_words:
                continue
            foreign_words.append(foreign_word)
            cards.append(Card(foreign_word = foreign_word, native_word = native_word, kit=self))
        kit_file.close()
        Card.objects.bulk_create(cards)

    def put_successful(self):
        """
            Calculate and update the value of successful
        """
        self.successful = round(mean(success_value[0] for success_value in self.cards.values_list('success')))
        self.save(update_fields=['successful'])

def unique_name_per_user(sender, instance, *args, **kwargs):
    """
        Verify that the stock name is unique per user.
    """
    # print(instance.lc_lang)
    lc_lang = getattr(instance, 'lc_lang', None)
    kit_by_user = sender.objects.filter(user__pk=instance.user.pk)
    kits_by_name = kit_by_user.filter(name=instance.name)
    if kits_by_name.exists():
        if not instance == kits_by_name.first():
            if lc_lang == "es":
                raise ValidationError("UNPS - Nombre de Kit ya registrado previamente.")
            else:
                raise ValidationError("UNPS - Kit name already registered previously.")

pre_save.connect(unique_name_per_user, sender=Kit)


import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets = ['train', 'test', 'val']
# classes = ['parrester_28','arrester_36','pressure_8','oil_temperature_2','arrester_11','pressure_13','arrester_26','arrester_35','arrester_1','arrester_27']
#classes = ['wcgz', 'xy', 'head', 'bpmh', 'bpps', 'gbps', 'nc', 'gkxfw', 'xmbhyc', 'dmyw', 'gjtbs', 'gjtps', 'hxqyfywyc', 'wkps', 'dsyc', 'helmet', 'jyzps','bjzc']
classes = ['wcgz', 'xy', 'wdaqm', 'bpmh', 'bpps', 'gbps', 'nc', 'gkxfw', 'xmbhyc', 'dmyw', 'gjbs', 'gjtps', 'hxqyfywyc', 'wkps', 'dsyc', 'helmet', 'jyzps','bjzc']
replace_dict = {'jytpl':'jyzps', 'bjps':'bpps', 'hxjyfywyc':'hxqyfywyc', 'bjduyc':'dsyc', 'bjmh':'bpmh', 'bpdsyc':'dsyc', 'gkxw':'gkxfw', 'bpdsyc':'dsyc', 'smbhyc':'xmbhyc', 'jyzpl':'jyzps', 'bjdsyc':'dsyc', 'gjtbs':'gjbs', 'head':'wdaqm'}

device_class = ['', '']
output_class = ['wcgz', 'xy', 'wdaqm']

def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)

def convert_annotation(image_id):
    in_file = open('data/wugaosuo_xml/%s.xml' % (image_id), 'rb')
    out_file = open('data/labels/%s.txt' % (image_id), 'w')
    try:
        tree = ET.parse(in_file)
    except Exception:
        print(image_id)
        return
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls in replace_dict.keys():
            cls = replace_dict[cls]
        if cls not in classes:# or int(difficult) == 1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

wd = getcwd()
print(wd)
for image_set in sets:
    if not os.path.exists('data/labels/'):
        os.makedirs('data/labels/')
    image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split()
    list_file = open('data/%s.txt' % (image_set), 'w')
    for image_id in image_ids:
        list_file.write('data/wugaosuo_pic/%s.jpg\n' % (image_id))
        # try:
        convert_annotation(image_id)
        # except ZeroDivisionError:
        #     print(image_id)
    list_file.close()


from django.conf import settings
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("accounts/", include("allauth.urls")),
    path("pages/", include("pages.urls")),
    path("", include("articles.urls")),
    path("character/", include("dnd_character.urls")),
    path("fediverse/", include("fediverse.urls")),
    path("calculator/", include("calculator.urls")),
    path("tasks/", include("htmx_tasks.urls")),
]

if settings.DEBUG:
    import debug_toolbar
    from django.conf.urls.static import static

    urlpatterns = [
        path("__debug__/", include(debug_toolbar.urls)),
    ] + urlpatterns

    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


"""
    Код должен парсить элементы с сайта, который получает HTML шаблон с сервера, реализованный
с помощью JavaScript.

    Используется библиотека Selenium, предназначенная для автоматизации действий в веб-браузере,
а также для выполнения рутинных задач и тестирования Web-приложений
    (Установка Selenium: pip3 install selenium)

    Для работы с данной библиотекой используется WebDriver, необходимый для эмуляции обычного браузера,
который будет управляться через Selenium.
    (Мой браузер - Chrome, поэтому я устанавливаю ChromeDriver - https://chromedriver.chromium.org/downloads)
    Версия Chrome должна совпадать с версией ChromeDriver.
"""

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s = Service("D:\\Education\\Web\\untitled9\\chromedriver.exe")

def existence_check(num, path_elem):
    try:
        driver.find_element(By.XPATH, path_elem + "tr[%s]/td[2]" % (num + 1))
        return True
    except:
        return False

def collection_results():
    path_elem = "/html[@class='ru detect_flexbox']" \
                "/body[@class='inner-page ranged-list']" \
                "/div[@class='layout']/div[@class='wrapper']" \
                "/div[@id='js-content']/div[@class='wide']" \
                "/div[@class='data-table-container mCustomScrollbar _mCS_1']" \
                "/div[@id='mCSB_1']/div[@id='mCSB_1_container']" \
                "/div[@class='table-container']/table[@class='data with-hover sortable js-fixed js-table-data']" \
                "/tbody/"
    num = 0
    reg_numbers = list()
    sum_points = list()
    accept = list()
    orig = list()

    while existence_check(num, path_elem):
        reg_numbers.append(driver.find_element(By.XPATH, path_elem + "tr[%s]/td[2]" % (num + 1)).text)
        sum_points.append(driver.find_element(By.XPATH, path_elem + "tr[%s]/td[4]" % (num + 1)).text)
        accept.append(driver.find_element(By.XPATH, path_elem + "tr[%s]/td[7]" % (num + 1)).text)
        orig.append(driver.find_element(By.XPATH, path_elem + "tr[%s]/td[8]" % (num + 1)).text)
        num = num + 1

    dic = {}
    for i in range(len(reg_numbers)):
        if accept[i] == "+" and orig[i] == "+":
            dic[reg_numbers[i]] = sum_points[i]

    return dic

def print_result(dic):
    i = 0
    print("---Проходят на бюджет:---")
    for key,value in dic.items():
        i += 1
        if i > count_budget_places:
            break
        else:
            print(str(i) + ". " + key + " " + value)

    print("Количество непрошедших: " + str(len(dic) - count_budget_places))

def main():
    global count_budget_places, driver
    url = input("Ссылка на ранжированный список: ")

    try:
        driver = webdriver.Chrome(service=s)
        driver.get(url=url)
        print(driver.find_element(By.XPATH, "/html[@class='ru detect_flexbox']/body[@class='inner-page ranged-list']"
                                            "/div[@class='layout']/div[@class='wrapper']/div[@id='js-content']/div[@class='col-sm-4 col-md-2']/div[@class='data data-indent'][2]/div[@class='row'][3]/div[@class='col-md-2 col-sm-2 col-xs-4'][2]/direction").text)
        count_budget_places = int(driver.find_element(By.XPATH, "/html[@class='ru detect_flexbox']"
                                                        "/body[@class='inner-page ranged-list']"
                                                        "/div[@class='layout']/div[@class='wrapper']/div[@id='js-content']"
                                                        "/div[@class='col-sm-4 col-md-2']/div[@class='data data-indent'][2]"
                                                        "/div[@class='row'][1]/div[@class='col-md-2 col-sm-2 col-xs-4'][2]").text)
    finally:
        print_result(collection_results())
        driver.close()
        driver.quit()

main()


# Make a paired circles and squares toy dataset for multimodal encoding
# In this example dataset : on of the modalities have a random color grey background dataset that is modality specific information :
# The circles all have the same size and the shared information is the shape

import os

import numpy as np
from PIL import Image
import torch
from sklearn.model_selection import train_test_split

dataset_size = 5000
size_image = 32
min_background, max_background = 5, 250
min_pertub, max_pertub = 0,0.3
rayon = r = 0.5
circle_thickness = 0.2
output_path = '../data/empty_full_bk'
if not os.path.exists(output_path):
    os.mkdir(output_path)

x = np.linspace(-1,1,size_image)

def circle(X,Y):
    c = np.random.uniform(-max_pertub, max_pertub, 2)
    shape = ((X-c[0])**2 + (Y-c[1])**2 <= (r + circle_thickness/2)**2)*((X-c[0])**2 + (Y-c[1])**2 >= (r - circle_thickness/2)**2)
    bk_color = np.random.randint(min_background, max_background)/255
    bk = (1-shape)*bk_color
    return shape + bk, bk_color

def square_line(X,Y):
    c = np.random.uniform(-max_pertub, max_pertub, 2)
    shape = (np.abs(X-c[0]) + np.abs(Y-c[1]) <= (r + circle_thickness/2))*(np.abs(X-c[0]) + np.abs(Y-c[1]) >= r - circle_thickness/2)
    bk_color = np.random.randint(min_background, max_background)/255
    bk = (1-shape)*bk_color
    return shape + bk, bk_color

empty, background , full = [], [], []

labels = []

for i in range(dataset_size):
    X, Y = np.meshgrid(x, x)
    # Add a circle pair to the dataset
    c, bkc = circle(X,Y)
    empty.append(c)
    background.append(bkc)
    full.append(X**2 + Y**2 <= r**2)
    # And then a square one
    c, bkc = square_line(X, Y)
    empty.append(c)
    background.append(bkc)
    full.append(np.abs(X) + np.abs(Y) <= r)
    labels.extend([0,1])

# Visualize some examples
output_examples = output_path + '/examples'
if not os.path.exists(output_examples):
    os.mkdir(output_examples)

for i in np.linspace(0,dataset_size-1, 100):
    i = int(i)
    img = Image.fromarray(np.uint8(np.concatenate([empty[i]*255, full[i]*255])))
    img.save(output_examples + f'/example_{i}.png')

# Save in pytorch format
empty = torch.unsqueeze(torch.FloatTensor(empty), 1)
full = torch.unsqueeze(torch.FloatTensor(full), 1)
labels, background = torch.tensor(labels),torch.tensor(background)
# Select some for training and testing
s_train, s_test, c_train, c_test, idx_train, idx_test = train_test_split(empty,full, np.arange(len(labels)), test_size=0.3)

torch.save(s_train, output_path + '/squares_train.pt')
torch.save(s_test, output_path + '/squares_test.pt')
torch.save(c_train, output_path + '/circles_train.pt')
torch.save(c_test, output_path + '/circles_test.pt')
torch.save(labels[idx_train], output_path+ '/labels_train.pt')
torch.save(labels[idx_test], output_path + '/labels_test.pt')
torch.save(background[idx_train], output_path + '/r_squares_train.pt')
torch.save(background[idx_test], output_path + '/r_squares_test.pt')
torch.save(background[idx_train], output_path +'/r_circles_train.pt')
torch.save(background[idx_test], output_path +'/r_circles_test.pt')

print(c_train.shape, c_test.shape)


import random
import logging

from core.models.Cell import Cell
from core.models.Position import Position
from core.models.State import State

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class Grid:
    """This class contains all the informations about the petri organization.
        The main part of this class is the adjacency matrix which allows us to represent the
        neighboring between cells.
        A.K.A Petri
    """

    def __init__(self, adjacency_matrix=[], length=5, width=5, number_generations=5):
        self.length = length
        self.width = width
        self.number_generations = number_generations
        self.adjacency_matrix = adjacency_matrix

    @classmethod
    def build_from_adjacency_matrix(cls, adjacency_matrix, number_generations=5):
        """
          :param adjacency_matrix: this is adjacency matrix to represent the neighboring and the state of each cell.
          :return Grid object
        """
        return cls(adjacency_matrix, len(adjacency_matrix[0]), len(adjacency_matrix), number_generations)

    @classmethod
    def build_random_grid(cls, number_generations):
        """
        :param number_generations: This is a number of the generation to use during the simulation
        :return Grid Object
        """
        width = random.randint(1, 10)
        length = random.randint(1, 10)
        logger.info(" Starting the with random data in the Grid having dimension {0} and {1}".format(width, length))
        adjacency_matrix = []
        for x in range(width):
            adjacency_matrix.append([])
            for y in range(length):
                position = Position(x, y)
                adjacency_matrix[x].append(Cell(State.RANDOM, position))
        return cls(adjacency_matrix, length, width, number_generations)

    def live(self):
        """ This procedure will launche the simulation of the grid
        """
        logger.info("The intial generation ")
        logger.info(self)
        for idx in range(self.number_generations):
            logger.info("The generation :" + str(idx + 1))
            self.next_state_of_grid()
            logger.info(self)

    def next_state_of_grid(self):
        """
            This method will udpdate the state of each cell in the Grid
        """
        matrix_number_alive = self.get_matrix_number_neighbour_living()
        for x in range(self.width):
            for y in range(self.length):
                self.change_state(x, y, matrix_number_alive)

    def change_state(self, x, y, matrix):
        """:param x: the x coordinate of cell to evalute eventual changements
           :param y: the y coordinate of cell to evalute eventual changements
           :param :matrix
           This method will evaluate what cell to kill according to certains condition
        """
        if (self.adjacency_matrix[x][y].is_alive()) and (matrix[x][y] < 2 or matrix[x][y] > 3):
            self.adjacency_matrix[x][y].make_dead()

        if (self.adjacency_matrix[x][y].is_alive()) and matrix[x][y] in [2, 3]:
            self.adjacency_matrix[x][y].make_alive()

        if not self.adjacency_matrix[x][y].is_alive() and matrix[x][y] == 3:
            self.adjacency_matrix[x][y].make_alive()

    def find_neighborhood(self, x_param, y_param):
        """ Return the list of the neighbours of the element
            whose coordinates have been passed in parameter
            :param x_param:
            :param y_param:
        """
        return [(x, y) for x in range(x_param - 1, x_param + 2)
                for y in range(y_param - 1, y_param + 2)
                if (-1 < x_param < self.width and
                    -1 < y_param < self.length and
                    (x_param != x or y_param != y) and
                    (0 <= x < self.width) and
                    (0 <= y < self.length))]

    def get_matrix_number_neighbour_living(self):
        """
           This method return a matrix where item at the position(x,y)
           is a number of neighbours living of the cell at the position(x,y) of the petri.
           :return a matrix having size self.width and self.length
        """
        matrix_alive = []
        for x in range(self.width):
            matrix_alive.append([])
            for y in range(self.length):
                alive_neighbors = 0
                neighbors = self.find_neighborhood(x, y)
                for coordinate in neighbors:
                    if self.adjacency_matrix[coordinate[0]][coordinate[1]].is_alive():
                        alive_neighbors = alive_neighbors + 1
                matrix_alive[x].append(alive_neighbors)

        return matrix_alive

    def __str__(self):
        temp = "\n"
        for x in range(self.width):
            for y in range(self.length):
                temp = temp + str(self.adjacency_matrix[x][y])
            temp = temp + "\n"
        return temp

    def __repr__(self):
        for x in range(self.width):
            for y in range(self.length):
                print(self.adjacency_matrix[x][y], end='')
            print('')


import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

import movie_poster

import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

data_dir = "datas"
img_dir = f"{data_dir}/images"

batch_size = 32
test_size = 0.15

all_dataset = pd.read_csv(f"{data_dir}/all_data.csv")
train_set, test_set = train_test_split(all_dataset, test_size=0.15)

pd.DataFrame(train_set).to_csv(f"{data_dir}/train_data.csv", header=True, index=False)
pd.DataFrame(test_set).to_csv(f"{data_dir}/test_data.csv", header=True, index=False)

test_set_fpath = f"{data_dir}/test_data.csv"

test_transform = transforms.Compose(
    [
        transforms.ToPILImage(),
        transforms.Resize((200, 200)),
        transforms.ToTensor(),
    ]
)

test_dataset = movie_poster.Dataset(test_set_fpath, img_dir, test_transform)
test_dataloader = DataLoader(test_dataset, batch_size=4, shuffle=True)

images, labels = next(iter(test_dataloader))
output = torchvision.utils.make_grid(images)
print("labels", labels)

def imshow(inp):
    inp = inp.transpose((1, 2, 0))
    plt.imshow(inp)

# imshow(output.numpy())
# plt.show()

print("Done")


from __future__ import absolute_import, division, print_function

import os
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

offline = not "DISPLAY" in os.environ

chart_count = 0
def show():
  global chart_count
  if offline:
    plt.savefig('charts/plot-{}.png'.format(chart_count))
    chart_count += 1
  else:
    plt.show()
  # Clear the current axes.
  plt.cla()
  # Clear the current figure.
  plt.clf()
  # Closes all the figure windows.
  plt.close('all')

if offline:
  mpl.use("Agg")

def plot_loss(subplot, epochs, history, test_loss = None):
  plot_metric('loss', 'Loss', subplot, epochs, history, test_loss)

def plot_acc(subplot, epochs, history, test_metric = None):
  plot_metric('acc', 'Accuracy', subplot, epochs, history, test_metric)

def plot_mae(subplot, epochs, history, test_metric = None):
  plot_metric('mean_absolute_error', 'Mean Abs Err', subplot, epochs, history, test_metric)

def plot_metric(metric, metric_name, subplot, epochs, history, test_metric = None):
  val_acc = subplot.plot(epochs, history['val_{}'.format(metric)], '--', label='Val {}'.format(metric_name))
  subplot.plot(epochs, history[metric], color=val_acc[0].get_color(), label='Train {}'.format(metric_name))
  if test_metric:
    subplot.plot(epochs, [test_acc] * len(epochs), label='Test {}'.format(metric_name))
  subplot.set_xlabel('Epochs')
  #subplot.set_yscale('log')
  subplot.set_ylabel(metric_name)
  subplot.set_xlim([0, max(epochs)])

def print_run(
    train_input, train_target, train_predict, test_input, test_target, test_predict, epoch, history):
  plt.figure(figsize=(12, 8))

  splt = plt.subplot(2, 4, 1)
  splt.imshow(train_input.astype("float32"))
  splt = plt.subplot(2, 4, 2)
  splt.imshow(train_target.astype("float32"))
  splt = plt.subplot(2, 4, 3)
  splt.imshow(train_predict.astype("float32"))

  plot_loss(plt.subplot(2, 4, 4), list(range(epoch + 1)), history)

  splt = plt.subplot(2, 4, 5)
  splt.imshow(test_input.astype("float32"))
  splt = plt.subplot(2, 4, 6)
  splt.imshow(test_target.astype("float32"))
  splt = plt.subplot(2, 4, 7)
  splt.imshow(test_predict.astype("float32"))

  if "acc" in history:
    plot_acc(plt.subplot(2, 4, 8), list(range(epoch + 1)), history)
  else:
    plot_mae(plt.subplot(2, 4, 8), list(range(epoch + 1)), history)
  plt.tight_layout()
  show()

def print_end(history, test_loss, test_acc):
  plt.figure(figsize=(12, 8))
  plot_loss(plt.subplot(1, 2, 1), history.epoch, history.history, test_loss)
  plot_acc(plt.subplot(1, 2, 2), history.epoch, history.history, test_acc)
  #plt.legend()
  show()


from flask import Flask, jsonify, request

app = Flask(__name__)
accounts = [
    {"id": 1, "name": "John", "balance": 123.0},
    {"id": 2, "name": "Steve", "balance": 234.0},
]

@app.route("/")
def hello_word():
    return "Hello World"

@app.route("/accounts", methods=["GET"])
def get_accounts():
    return jsonify(accounts)

@app.route("/accounts/<account_id>", methods=["GET"])
def get_account(account_id):
    for acct in accounts:
        if acct.get("id", None) == int(account_id):
            return jsonify(acct)
    return {}

@app.route("/account", methods=["POST"])
def add_account():
    name = request.json["name"]
    balance = request.json["balance"]
    account_id = len(accounts) + 1
    data = {"id": account_id, "name": name, "balance": round(float(balance), 1)}
    accounts.append(data)

if __name__ == "__main__":
    app.run(port=8080)


import re
m = re.match('[a-z]+', 'Python')
print(m)
print(re.search('apple','I like apple!'))

p = re.compile('[a-z]+') # 알파벳 소문자
m = p.match('python')
print(m)
print(p.search('I like apple 123'))

print('---------------------정규 표현식--------------------')
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
html = urlopen('http://www.pythonscraping.com/pages/page3.html')
soup = BeautifulSoup(html, 'html.parser')
# 정규식: ('img.*\.jpg'): img 다음에 임의의 한 문자가 0회 이상: img.jpg, img1.jpg, imga.jpg 등
img_tag = re.compile('/img/gifts/img.*.jpg')
# find_all()에서 img의 src 속성값에 정규식 사용
images = soup.find_all('img', {'src': img_tag})

for image in images:
    print(image, end=" -> ['src'] 속성: ")
    print(image['src'])


from logging import warning, info

from overrides import overrides
import random
from typing import NamedTuple, List

from agents.utils.types import Transition

class AbstractReplayMemory:
    """
    Abstract class that describes basic API for Replay Memory
    """

    def __init__(self, capacity: int = 100_000):
        self.capacity = capacity

    def push(self, transition: NamedTuple):
        """
        Add transition to the replay memory.
        :param transition: transition performed by an agent, regularly: state, action, reward, next_state.
        """
        raise NotImplementedError("Push method should be implemented!")

    def sample(self, batch_size: int) -> List[NamedTuple]:
        """
        Sample batch of transitions from the replay memory.
        :param batch_size: size of batch
        :return: sampled batch of transitions
        """
        raise NotImplementedError("Sample method should be implemented!")

class ExperienceReplay(AbstractReplayMemory):
    """
    Simple Experience Replay memory.
    """

    def __init__(self, capacity: int = 100_000):
        super(ExperienceReplay, self).__init__(capacity=capacity)
        self.buffer = []
        self.position = 0

    @overrides
    def push(self, transition: NamedTuple):
        """
        Add transition to the replay memory.
        :param transition: transition performed by an agent, regularly: state, action, reward, next_state.
        """
        if len(self.buffer) < self.capacity:
            self.buffer.append(None)
        self.buffer[self.position] = transition
        self.position = (self.position + 1) % self.capacity

    @overrides
    def sample(self, batch_size: int) -> List[NamedTuple]:
        if len(self.buffer) < batch_size:
            raise ValueError(
                f"Can't sample batch of size {batch_size} from the buffer!\n"
                f"There are only {len(self.buffer)} elements in the buffer."
            )
        return random.sample(self.buffer, batch_size)

    def __len__(self):
        return len(self.buffer)

# TODO: create sum tree data structure
# TODO: continue creation of Prioritized Experience replay
class PrioritizedReplayMemory(AbstractReplayMemory):
    """
    Experience Replay memory that stores transitions with priorities and samples
    elements according to these priorities.
    """

    def __init__(
        self,
        capacity: int = 100_000,
        alpha: float = 0.6,
        beta_init: float = 0.4,
        beta_anneal_steps: int = 10000,
    ):
        super(PrioritizedReplayMemory, self).__init__(capacity=capacity)
        self.alpha_smoothing = alpha
        self.buffer = []
        self.beta_init = beta_init
        self.beta_anneal_steps = beta_anneal_steps
        self.beta_steps = 0

    @overrides
    def push(self, transition: NamedTuple):
        pass

    @overrides
    def sample(self, batch_size: int) -> List[NamedTuple]:
        pass

    def _beta_by_step(self, step: int):
        beta_ang = (1 - self.beta_init) / self.beta_anneal_steps
        beta = beta_ang * step + self.beta_init
        return min(beta, 1.0)

    def __len__(self):
        return len(self.buffer)

class BinaryPrioritizeReplayMemory(AbstractReplayMemory):
    def __init__(self, capacity: int = 100_000, priority_fraction: float = 0.0):
        super(BinaryPrioritizeReplayMemory, self).__init__(capacity=capacity)
        self.prior_buffer = []
        self.prior_position = 0
        self.prior_capacity = int(self.capacity * priority_fraction)
        self.secondary_buffer = []
        self.secondary_position = 0
        self.secondary_capacity = self.capacity - self.prior_capacity
        self.priority_fraction = priority_fraction

    def push(self, transition: NamedTuple, is_prior: bool = False):
        if self.priority_fraction == 0.0:
            is_prior = False
        if is_prior:
            self.prior_position = self._push(
                transition, self.prior_buffer, self.prior_position
            )
        else:
            self.secondary_position = self._push(
                transition, self.secondary_buffer, self.secondary_position
            )

    def _push(self, transition: Transition, buffer, position):
        if len(buffer) < self.prior_capacity:
            buffer.append(None)
        buffer[position] = transition
        position = (position + 1) % self.prior_capacity
        return position

    def sample(self, batch_size: int) -> List[NamedTuple]:
        if self.priority_fraction == 0.0:
            samples = random.sample(self.secondary_buffer, batch_size)
        else:
            prior_size = min(
                int(batch_size * self.priority_fraction), len(self.prior_buffer)
            )
            secondary_size = min(batch_size - prior_size, len(self.secondary_buffer))
            prior_samples = random.sample(self.prior_buffer, prior_size)
            secondary_samples = random.sample(self.secondary_buffer, secondary_size)
            samples = prior_samples + secondary_samples
            # random.shuffle(samples)
            if len(samples) != batch_size:
                warning(
                    f"Not enough elements in buffer: {len(prior_samples), len(secondary_samples)}"
                )

        # if len(self.secondary_buffer) < batch_size:
        #     raise ValueError(
        #         f"Can't sample batch of size {batch_size} from the buffer!\n"
        #         f"There are only {len(self.secondary_buffer)} elements in the buffer."
        #     )
        return samples

    def __len__(self) -> int:
        return len(self.prior_buffer) + len(self.secondary_buffer)

    def get_len_prior(self) -> int:
        return len(self.prior_buffer)

    def get_len_secondary(self) -> int:
        return len(self.secondary_buffer)

class SeqTernaryPrioritizeReplayMemory(AbstractReplayMemory):
    def __init__(
        self,
        capacity: int = 100_000,
        good_samples_fraction: float = 0.0,
        bad_samples_fraction: float = 0.0,
    ):
        super().__init__(capacity=capacity)
        self.good_samples_fraction = good_samples_fraction
        self.bad_samples_fraction = bad_samples_fraction
        self.good_seqs_buffer = []
        self.good_buffer_position = 0
        self.bad_seqs_buffer = []
        self.bad_buffer_position = 0
        self.neutral_seqs_buffer = []
        self.neutral_buffer_position = 0
        self.good_samples_capacity = int(self.capacity * self.good_samples_fraction)
        self.bad_samples_capacity = int(self.capacity * self.bad_samples_fraction)
        self.secondary_capacity = (
            self.capacity - self.good_samples_capacity - self.bad_samples_capacity
        )

    def push(self, transitions: List[Transition]):
        is_good = False
        is_bad = False
        for transition in transitions:
            if transition.reward > 0.5:
                is_good = True
            elif transition.reward < -0.5:
                is_bad = True
        if is_good:
            self.good_buffer_position = self._push(
                transitions=transitions,
                position=self.good_buffer_position,
                buffer=self.good_seqs_buffer,
                capacity=self.good_samples_capacity,
            )
        if is_bad:
            self.bad_buffer_position = self._push(
                transitions=transitions,
                position=self.bad_buffer_position,
                buffer=self.bad_seqs_buffer,
                capacity=self.bad_samples_capacity,
            )
        if not is_good and not is_bad:
            self.neutral_buffer_position = self._push(
                transitions=transitions,
                position=self.neutral_buffer_position,
                buffer=self.neutral_seqs_buffer,
                capacity=self.secondary_capacity,
            )

    def _push(self, transitions, buffer, position, capacity):
        if len(buffer) < capacity:
            buffer.append(None)
        buffer[position] = transitions
        return (position + 1) % capacity

    def sample(self, batch_size: int):
        if self.good_samples_fraction == 0.0:
            batch_size = min(batch_size, len(self.neutral_seqs_buffer))
            return random.sample(self.neutral_seqs_buffer, batch_size)
        else:
            good_size = min(
                int(self.good_samples_fraction * batch_size), len(self.good_seqs_buffer)
            )
            bad_size = min(
                int(self.bad_samples_fraction * batch_size), len(self.bad_seqs_buffer)
            )
            secondary_size = min(
                batch_size - bad_size - good_size, len(self.neutral_seqs_buffer)
            )
            pos_prior_samples = random.sample(self.good_seqs_buffer, good_size)
            neg_prior_samples = random.sample(self.bad_seqs_buffer, bad_size)
            secondary_samples = random.sample(self.neutral_seqs_buffer, secondary_size)
            samples = pos_prior_samples + neg_prior_samples + secondary_samples
            info(
                f"Good samples: {len(pos_prior_samples)}; bad samples: {len(neg_prior_samples)} rest samples: {len(secondary_samples)}"
            )
            return samples

class TernaryPrioritizeReplayMemory(AbstractReplayMemory):
    def __init__(self, capacity: int = 100_000, priority_fraction: float = 0.0):
        super(TernaryPrioritizeReplayMemory, self).__init__(capacity=capacity)
        self.priority_fraction = priority_fraction
        self.pos_prior_buffer = []
        self.pos_prior_position = 0
        self.neg_prior_buffer = []
        self.neg_prior_position = 0
        self.prior_capacity = int(self.capacity * self.priority_fraction) // 2
        self.secondary_buffer = []
        self.secondary_position = 0
        self.secondary_capacity = self.capacity - 2 * self.prior_capacity

    def push(self, transition: NamedTuple):
        if self.priority_fraction == 0.0:
            is_prior = False
        else:
            is_prior = transition.reward != 0.0
        if is_prior:
            self._push_prior(transition)
        else:
            self._push_secondary(transition)

    def sample(self, batch_size: int):
        if self.priority_fraction == 0.0:
            batch_size = min(batch_size, len(self.secondary_buffer))
            return random.sample(self.secondary_buffer, batch_size)
        else:
            prior_size = min(
                int(batch_size * self.priority_fraction) // 2,
                min(len(self.pos_prior_buffer), len(self.neg_prior_buffer)),
            )
            secondary_size = min(batch_size - prior_size, len(self.secondary_buffer))
            pos_prior_samples = random.sample(self.pos_prior_buffer, prior_size)
            neg_prior_samples = random.sample(self.neg_prior_buffer, prior_size)
            secondary_samples = random.sample(self.secondary_buffer, secondary_size)
            samples = pos_prior_samples + neg_prior_samples + secondary_samples
            return samples

    def _push_prior(self, transition: NamedTuple):
        if transition.reward > 0.0:
            if len(self.pos_prior_buffer) < self.prior_capacity:
                self.pos_prior_buffer.append(None)
            self.pos_prior_buffer[self.pos_prior_position] = transition
            self.pos_prior_position = (
                self.pos_prior_position + 1
            ) % self.prior_capacity
        else:
            if len(self.neg_prior_buffer) < self.prior_capacity:
                self.neg_prior_buffer.append(None)
            self.neg_prior_buffer[self.neg_prior_position] = transition
            self.neg_prior_position = (
                self.neg_prior_position + 1
            ) % self.prior_capacity

    def _push_secondary(self, transition: NamedTuple):
        if len(self.secondary_buffer) < self.secondary_capacity:
            self.secondary_buffer.append(None)
        self.secondary_buffer[self.secondary_position] = transition
        self.secondary_position = (
            self.secondary_position + 1
        ) % self.secondary_capacity

    def __len__(self) -> int:
        return (
            len(self.pos_prior_buffer)
            + len(self.neg_prior_buffer)
            + len(self.secondary_buffer)
        )

    def get_size(self) -> int:
        return len(self)

    def get_len_prior(self) -> int:
        return len(self.pos_prior_buffer) + len(self.neg_prior_buffer)

    def get_len_secondary(self) -> int:
        return len(self.secondary_buffer)


from flask import Flask, render_template, request
from pytube import YouTube
from youtubesearchpython import VideosSearch
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context

app = Flask(__name__)

@app.route("/", methods=['POST', 'GET'])
def anime_home():
    if request.method == "POST":

        searchstring = ""

        anime_name = request.form.get("name")
        searchstring += str(anime_name.lower())

        op_or_ed = request.form.get("op_or_ed")
        number = request.form.get("number")
        if op_or_ed == "opening":
            searchstring += f" op{str(number)}"
        else:
            searchstring += f" ed{str(number)}"

        # path = request.form.get("path")
        path = "/Users/jerrickban/Desktop/Test"

        print(searchstring)
        link = get_yt_url_from_keywords(searchstring)
        download_link(link, path)
        print(anime_name)
        print(op_or_ed)
        print(number)
        print(path)

    return render_template("index.html")

def get_new_op_num():
    file = open("op_number.txt", "r")
    num = int(file.read())
    new_num = num + 1
    file.write(str(new_num))
    return new_num

def get_yt_url_from_keywords(keywords):
    video = VideosSearch(keywords, limit=1)
    link = video.result()["result"][0]["link"]
    print(link)
    return link

def download_link(link, path):
    yt = YouTube(link,
                    use_oauth=False,
                    allow_oauth_cache=True)

    yd = yt.streams.get_highest_resolution()

    yd.download(path)


from django.contrib.contenttypes.models import ContentType

from companyinfo.models import CompanyInfo
from companyinfo.serializers import CompanyInfoSerializer
from expenses.models import Expense
from django.contrib.admin.models import LogEntry, CHANGE, ADDITION, DELETION

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import permissions
from .serializers import ExpenseSerializer
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view, permission_classes

from ratelimit.decorators import ratelimit
from django.utils.decorators import method_decorator

# Create your views here.

class ExpenseView(APIView):
    permission_classes = [permissions.IsAuthenticated]

    @method_decorator(ratelimit(key='ip', rate='10/s', block=True, method='GET'))
    def get(self, *args, **kwargs):
        try:
            expenses = ExpenseSerializer(Expense.objects.all(), many=True)
            company_info = CompanyInfoSerializer(
                CompanyInfo.objects.all(), many=True)
            context = {
                'title': 'Expenses',
                'expenses': expenses.data,
                'company_info': company_info.data
            }
            return Response(context, status=status.HTTP_200_OK)
        except:
            return Response({"message": "Something went wrong."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    @method_decorator(ratelimit(key='ip', rate='10/s', block=True, method='POST'))
    def post(self, request, *args, **kwargs):
        try:
            name = request.data['name']
            amount = request.data['amount']

            expense = Expense()
            expense.name = name
            expense.amount = amount
            expense.save()

            ct = ContentType.objects.get_for_model(Expense)
            LogEntry.objects.log_action(user_id=self.request.user.id, content_type_id=ct.pk,
                                        object_id=expense.pk, object_repr=expense.name, action_flag=ADDITION, change_message="")

            return Response({"message": "Expense added successfully."}, status=status.HTTP_200_OK)
        except:
            return Response({"message": "Something went wrong."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

@api_view(['POST'])
@permission_classes([permissions.IsAuthenticated])
@csrf_exempt
@ratelimit(key='ip', rate='10/s', block=True)
def delete_expense(request, id):
    if request.method == 'POST':
        try:
            expense = Expense.objects.get(pk=id)
            expense.delete()

            ct = ContentType.objects.get_for_model(Expense)
            LogEntry.objects.log_action(user_id=request.user.id, content_type_id=ct.pk, object_id=expense.pk,
                                        object_repr=expense.name, action_flag=DELETION, change_message="")

            return Response({"message": "Expense deleted successfully."}, status=status.HTTP_200_OK)
        except:
            return Response({"message": "Something went wrong."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

@api_view(['GET', 'PATCH'])
@permission_classes([permissions.IsAuthenticated])
@csrf_exempt
@ratelimit(key='ip', rate='10/s', block=True)
def edit_expense(request, id):
    if request.method == 'GET':
        try:
            expense = ExpenseSerializer(Expense.objects.get(pk=id))
            company_info = CompanyInfoSerializer(
                CompanyInfo.objects.all(), many=True)
            context = {
                'title': 'Edit Expense',
                'expense': expense.data,
                'company_info': company_info.data
            }

            return Response(context, status=status.HTTP_200_OK)
        except:
            return Response({"message": "Something went wrong."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    if request.method == 'PATCH':
        try:
            name = request.data['name']
            amount = request.data['amount']

            expense = Expense.objects.get(pk=id)
            expense.name = name
            expense.amount = amount
            expense.save()

            ct = ContentType.objects.get_for_model(Expense)
            LogEntry.objects.log_action(user_id=request.user.id, content_type_id=ct.pk,
                                        object_id=expense.pk, object_repr=expense.name, action_flag=CHANGE, change_message="")

            return Response({"message": "Expense edited successfully."}, status=status.HTTP_200_OK)
        except:
            return Response({"message": "Something went wrong."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import nltk
import matplotlib.pyplot as plt

lemmatizer = WordNetLemmatizer()

stop_words = set(stopwords.words('english'))

with open('text.txt', 'r') as f:
	text = f.read()

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.3)

filtered_text = [word for word in word_tokenize(text) if word not in stop_words]
lemmatized_text = list(map(lemmatizer.lemmatize, filtered_text))
lemmatized_text = [w for w in lemmatized_text if w.isalpha()]
fdist = nltk.FreqDist(lemmatized_text)

fdist.plot(20,cumulative=False)
plt.show()


# -*-coding: utf-8 -*-
#@Author: 'ljd'
#@Time：2016/9/10 11:13

import logging
logger = logging.getLogger('simple_logging.log')
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.debug('debug message')


import darknet
import cv2
import time

class Detector:

    def __init__(self, config_file, data_file, weights, batch_size, threshold=0.10):
        self.network, self.class_names, self.class_colors = darknet.load_network(
            config_file,
            data_file,
            weights,
            batch_size=batch_size
        )
        self.threshold = threshold
        self.width = darknet.network_width(self.network)
        self.height = darknet.network_height(self.network)

    def detect_frame(self, frame):
        start_time = time.time()

        image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        image_resized = cv2.resize(image_rgb, (self.width, self.height))
        darknet_image = darknet.make_image(self.width, self.height, 3)
        darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
        detections = darknet.detect_image(self.network, self.class_names, darknet_image,
                                          thresh=self.threshold)
        darknet.free_image(darknet_image)

        result = self._process_detection(detections)
        fps = (1/(time.time()-start_time))

        return result, fps

    def detect_frame_set(self, frame_set):
        stat, frame = frame_set.next()

        while stat:
            result, fps = self.detect_frame(frame)
            print("%2.4f completed. Current FPS: %.4f" %
                  (frame_set.file_idx*100/frame_set.file_cnt, fps))
            # code to proccess result
            stat, frame = frame_set.next()

    def _process_detection(self, detections):
        class_names, height, width = self.class_names, self.height, self.width
        label_map = {k[1]: int(k[0]) for k in enumerate(class_names)}
        result = []

        for label, confidence, bbox in detections:
            x, y, w, h = bbox
            x /= width
            w /= width
            y /= height
            h /= height

            result.append({
                "label": label_map[label],
                "confidence": float(confidence),
                "bbox": (x, y, w, h)
            })
            # print("%d %.4f %.4f %.4f %.4f" % (label_map[label], x, y, w, h))

        return result


import requests
from bs4 import BeautifulSoup

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
url ='https://www.ptt.cc/ask/over18?from=%2Fbbs%2FGossiping%2Findex.html'       #是否滿18歲頁面

ss = requests.session()

# Session1
#res = requests.get(url,headers=headers)
res = ss.get(url,headers=headers)            #目的在讓request在同一個session內
soup = BeautifulSoup(res.text,'html.parser')
button = soup.select('button[class="btn-big"]')[0]
print(button)
print(button['name'])               #取button(postdata)的key(name)
print(button['value'])              #取button(postdata)的value
print(ss.cookies)

hidden = soup.select('input')           #尋找hidden
print(hidden)

data = {}
data[button['name']] = button['value']
for k in hidden:                    #將值帶入
    data[k['name']] = k['value']
print(data)

# Session2
target_url = 'https://www.ptt.cc/ask/over18'
#res_target = requests.post(target_url,data=data,headers=headers)
res_target = ss.post(target_url,data=data,headers=headers)      #目的在讓request在同一個session內

# Session3
final_url = 'https://www.ptt.cc/bbs/Gossiping/index.html'
#final_res = requests.get(final_url,headers=headers)
final_res = ss.get(final_url,headers=headers)           #目的在讓request在同一個session內
print(final_res.text)

print(ss.cookies)           #可看到cookie內的東西


#
# MICA (Multiple and Inhomogeneous Component Analysis)
# A Non-parametric Approach to Constrain the Transfer Function in Reverberation Mapping
#
# Yan-Rong Li, liyanrong@mail.ihep.ac.cn
#
# Reference: Li, Y.-R. et al. 2016, arXiv:1608.03741
#
#
# analysis.py
#

import numpy as np
from scipy.optimize import curve_fit
from lmfit import minimize, Parameters, fit_report
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

pdf = PdfPages('hist.pdf')

nbin = 50
ntau = 100
tau1 = 0.0
tau2 = 50.0
nc = 4
nb = 30000

data = np.loadtxt("../data/mcmc_04.txt", skiprows=nb)   # change into the file name you want to diagnose
nt=data.shape[0]
npar = data.shape[1]

print(nt, npar)

par=np.zeros((npar-1, 3))

def gaussian(x, amp, cen, wid):
    return (amp) * np.exp(-(x-cen)**2 /(2*wid**2))

def residual(params, x, data, eps_data):
    amp = params['amp'].value
    cen = params['cen'].value
    wid = params['wid'].value

    model = amp * np.exp(-(x-cen)**2 /(2*wid**2))

    return (data-model)/eps_data

for i in range(1,npar):
  print(i)
  sort=np.sort(data[:, i])
  mean = np.mean(data[:,i])
  std = np.std(data[:,i])
  low = np.percentile(data[:,i], 15.85, interpolation='linear')
  up = np.percentile(data[:,i], 100.0-15.85, interpolation='linear')
  hrange=[data[:,i].min(), data[:,i].max()]

  hist_data = data[:,i]
  hist_range = (hist_data.min(), hist_data.max())
  print(i, hist_range)

  hist, bin_edges = np.histogram(hist_data, density=False, bins=nbin, range=hist_range)

  bin_centres = (bin_edges[:-1] + bin_edges[1:])/2
  eps_data = np.zeros(len(hist)) + 1.0

  params = Parameters()
  idx = np.argmax(hist);
  print(i, idx, hist[idx], )
  params.add('amp', value=np.max(hist), min=0.0)
  params.add('cen', value=bin_centres[idx], min=bin_centres[0], max=bin_centres[-1])
  params.add('wid', value=std, min=0.0)
  idx = np.where( hist > np.max(hist)*0.0)

  out = minimize(residual, params, args=(bin_centres[idx[0]], hist[idx[0]], eps_data[idx[0]]), method='leastsq')

# Get the fitted curve
  bin_fit = np.linspace(bin_centres[0], bin_centres[-1], nbin)
  hist_fit = gaussian(bin_fit, out.params['amp'].value, out.params['cen'].value,out.params['wid'].value)

  fig=plt.figure()

  #plt.plot(bin_centres, hist, label='Test data')
  plt.hist(hist_data, normed=False, bins=nbin, range=hist_range)
  plt.plot(bin_fit, hist_fit, label='Fitted data')

  print(np.min(data[:, i]), np.max(data[:, i]))
  plt.xlim(hist_range)
# Finally, lets get the fitting parameters, i.e. the mean and standard deviation:
  mean = out.params['cen'].value
  par[i-1, 0] = mean
  par[i-1, 1] = mean - low
  par[i-1, 2] = up - mean
  pdf.savefig(fig)
  plt.close()

pdf.close()

print(par)

np.savetxt("../data/par.txt", par, fmt="%f\t%f\t%f")

fig = plt.figure()
ax = fig.add_subplot(111)

tau = np.linspace(tau1, tau2, ntau)
tf = np.zeros(ntau)
tau_grid = np.linspace(tau1, tau2, nc)

width = par[2, 0]
for i in range(nc):
  fk = np.exp(par[3+i, 0])/np.sqrt(2.0*np.pi)/width * np.exp( -0.5*(tau - tau_grid[i])**2/width/width)
  tf += fk
  ax.plot(tau, fk, "r:")

ax.plot(tau, tf, linewidth=2)

plt.show()


'''
Created on Aug 7, 2018

@author: marcelo
'''

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl

# ignore SSL certificate erros
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

# url = input('Enter -')
html = urllib.request.urlopen('http://www.google.com.br', context=ctx).read()
#print('Html:', html)

soup = BeautifulSoup(html, 'html.parser')
#print('Soup:', soup, '\n\n')

tags = soup('a')
print('Tags type', type(tags))
print(tags, '\n\n')
for tag in tags:
    print(tag.get('href', None))


import sys

from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QWidget, QApplication

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 200, 200)
        self.setWindowTitle('Рисование')

    # Метод срабатывает, когда виджету надо
    # перерисовать свое содержимое,
    # например, при создании формы
    def paintEvent(self, event):
        # Создаем объект QPainter для рисования
        qp = QPainter()
        # Начинаем процесс рисования
        qp.begin(self)
        self.draw_flag(qp)
        # Завершаем рисование
        qp.end()

    def draw_flag(self, qp):
        # Задаем кисть
        qp.setBrush(QColor(255, 0, 0))
        # Рисуем прямоугольник заданной кистью
        qp.drawRect(30, 30, 120, 30)
        qp.setBrush(QColor(0, 255, 0))
        qp.drawRect(30, 60, 120, 30)
        qp.setBrush(QColor(0, 0, 255))
        qp.drawRect(30, 90, 120, 30)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec())


from __future__ import print_function

"""03/14/2018"""
"""XD"""
"""lenet + mnist + keras"""

import datetime
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import matplotlib.pyplot as plt
from keras.utils import plot_model
from keras.optimizers import Adam
from keras import initializers

now = datetime.datetime.now

batch_size = 128
# num_classes = 5
epochs = 10

# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
filters = 32
# size of pooling area for max pooling
pool_size = 2
# convolution kernel size
kernel_size = 5

if K.image_data_format() == 'channels_first':
    input_shape = (1, img_rows, img_cols)
else:
    input_shape = (img_rows, img_cols, 1)

def train_model(model, train, test, num_classes):
    x_train = train[0].reshape((train[0].shape[0],) + input_shape)
    x_test = test[0].reshape((test[0].shape[0],) + input_shape)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(train[1], num_classes)
    y_test = keras.utils.to_categorical(test[1], num_classes)

    model.compile(loss='categorical_crossentropy',
                  optimizer=adam,
                  metrics=['accuracy'])

    t = now()
    history = model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_test, y_test),
              )
    print('Training time: %s' % (now() - t))
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])

    # # summarize history for loss
    # plt.plot(history.history['loss'])
    # plt.plot(history.history['val_loss'])
    # plt.title('model loss')
    # plt.ylabel('loss')
    # plt.xlabel('epoch')
    # plt.legend(['train', 'test'], loc='upper left')
    # plt.show()

    # summarize history for accuracy
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    # plt.ylim(0.5,1.0)
    plt.title('accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# create two datasets one with digits below 5 and one with 5 and above

num_classes = 5
num_classes2 = 5
x_train_lt5 = x_train[y_train <=4] # [0 1 2 3 4]
y_train_lt5 = y_train[y_train <=4]
x_test_lt5 = x_test[y_test <=4]
y_test_lt5 = y_test[y_test <=4]

x_train_gte5 = x_train[y_train >=5 ]
y_train_gte5 = y_train[y_train >=5 ] - 5
x_test_gte5 = x_test[y_test >= 5]
y_test_gte5 = y_test[y_test >=5 ] - 5

# define two groups of layers: feature (convolutions) and classification (dense)
feature_layers = [
    Conv2D(32, kernel_size,
           padding='same',
           input_shape=input_shape,
           kernel_initializer='random_normal'),
    Activation('relu'),
    MaxPooling2D(2, 2, 'same'),
    Conv2D(64, kernel_size,
           padding='same',
           kernel_initializer='random_normal'),
    Activation('relu'),
    MaxPooling2D(2, 2, 'same'),
    # Dropout(0.25),
    Flatten(),
    Dense(1024, kernel_initializer='random_normal'),
    Activation('relu'),
]

classification_layers1 = [
    # Dropout(0.5),
    # Dense(1024, kernel_initializer='random_normal'),
    # Activation('relu'),
    Dense(num_classes, kernel_initializer='random_normal'),
    Activation('softmax')
]

classification_layers2 = [

    # Dropout(0.5),
    # Dense(1024, kernel_initializer='random_normal'),
    # Activation('relu'),
    Dense(num_classes2, kernel_initializer='random_normal'),
    Activation('softmax')
]

# create complete model
model1 = Sequential(feature_layers + classification_layers1)
adam = Adam(lr=5e-4)

# train model for 5-digit classification [0..4]
train_model(model1,
            (x_train_lt5, y_train_lt5),
            (x_test_lt5, y_test_lt5), num_classes)
plot_model(model1, to_file='model1.png')

# import scipy.io as scio
# scio.savemat('divide_dataset_mode0.mat', {'first_image':first_image, 'first_label_long':first_label_long, 'first_label_short':first_label_short,
#             'second_image':second_image, 'first_label_global':first_label_global, 'second_label_long':second_label_long, 'second_global_label':second_global_label, 'second_label_short':second_label_short})

adam = Adam(lr=1e-4)
model2 = Sequential(feature_layers + classification_layers2)
# freeze feature layers and rebuild model
for l in feature_layers:
    l.trainable = False

# transfer: train dense layers for new classification task [5..9]
train_model(model2,
            (x_train_gte5, y_train_gte5),
            (x_test_gte5, y_test_gte5), num_classes2)

plot_model(model2, to_file='model2.png')

plt.legend(['train first list', 'test first list','train second list', 'test second list'], loc='best')
plt.show()


import json
import logging
import traceback
from django.http import HttpResponse
from restclients_core.exceptions import DataFailureException
from myuw.dao.gws import is_grad_student
from myuw.dao.grad import get_grad_degree_for_current_user,\
    get_grad_committee_for_current_user, get_grad_leave_for_current_user,\
    get_grad_petition_for_current_user, degree_to_json, committee_to_json,\
    leave_to_json, petition_to_json
from myuw.logger.timer import Timer
from myuw.logger.logresp import log_msg, log_success_response
from myuw.views.rest_dispatch import RESTDispatch
from myuw.views.error import data_not_found, handle_exception

logger = logging.getLogger(__name__)

class MyGrad(RESTDispatch):
    """
    Performs actions on resource at /api/v1/grad/.
    """

    def GET(self, request):
        """
        GET returns 200 with the student account balances
        of the current user
        """
        timer = Timer()
        try:
            if not is_grad_student():
                log_msg(logger, timer, "Not a grad student, abort!")
                return data_not_found()

            json_ret = {"degrees": None,
                        "committees": None,
                        "leaves": None,
                        "petitions": None}

            try:
                committee_reqs = get_grad_committee_for_current_user()
                json_ret["committees"] = committee_to_json(committee_reqs)
            except DataFailureException as ex:
                logger.error(ex)
                if ex.status != 404:
                    json_ret["comm_err"] = ex.status

            try:
                degree_reqs = get_grad_degree_for_current_user()
                json_ret["degrees"] = degree_to_json(degree_reqs, request)
            except DataFailureException as ex:
                logger.error(ex)
                if ex.status != 404:
                    json_ret["degree_err"] = ex.status

            try:
                leave_reqs = get_grad_leave_for_current_user()
                json_ret["leaves"] = leave_to_json(leave_reqs, request)
            except DataFailureException as ex:
                logger.error(ex)
                if ex.status != 404:
                    json_ret["leave_err"] = ex.status

            try:
                petition_reqs = get_grad_petition_for_current_user()
                json_ret["petitions"] = petition_to_json(
                    petition_reqs, request)
            except DataFailureException as ex:
                logger.error(ex)
                if ex.status != 404:
                    json_ret["petit_err"] = ex.status

            log_success_response(logger, timer)
            return HttpResponse(json.dumps(json_ret))
        except Exception:
            return handle_exception(logger, timer, traceback)


from collections import deque
dx = [1, 0, 0, -1]
dy = [0, -1, 1, 0]

def solution(board):
    n = len(board)
    dq = deque([[0, 0, -1, 0]])  # x, y, direction, cost
    visited = [[0] * n for _ in range(n)]

    while dq:
        x, y, d, c = dq.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < n and not board[ny][nx]:
                cost = c + (100 if d == i or d == -1 else 600)
                if not visited[ny][nx] or visited[ny][nx] >= cost:
                    dq.append([nx, ny, i, cost])
                    visited[ny][nx] = cost
    return visited[-1][-1]

board=[[0, 0, 1, 0], [0, 0, 0, 0], [0, 1, 0, 1], [1, 0, 0, 0]]
print(solution(board))

카카오/2020 인턴십/경주로 건설.py


import socket
import time
from numpy import linspace, reshape
from multiprocessing import Pool
import pickle
import sys
from psutil import cpu_count, cpu_freq

maxiter = 30

def get_power():
    ratio = 1

    if len(sys.argv) > 1:
        ratio = int(sys.argv[1]) / 100

    cpu_frequency = cpu_freq()

    return int(cpu_count() * ratio * cpu_frequency.max / 1000)

def mandelbrot(z):
    c = z
    global maxiter
    for n in range(maxiter):
        if abs(z) > 2:
            return n
        z = z * z + c
    return maxiter

def mandel(xmin, xmax, ymin, ymax, nx, ny, part):
    start = time.time()
    X = linspace(xmin, xmax, nx)  # lists of x and y
    Y = linspace(ymin, ymax, ny)  # pixel co-ordinates
    Yloc = Y[part[0]:part[1] + 1]

    # main loops
    if len(sys.argv) > 1:
        if int(sys.argv[1]) < 100:
            p = Pool(int(cpu_count() * sys.argv[1] / 100))
    else:
        p = Pool()

    Z = [complex(x, y) for y in Yloc for x in X]
    N = p.map(mandelbrot, Z)

    N = reshape(N, (len(Yloc), nx))  # change to rectangular array
    print("Mandelbrot generation " + str(part[0]) + "to" + str(part[1]))
    print(time.time() - start)
    return N

def sendData(mandelList):
    start = time.time()
    file = pickle.dumps(mandelList)
    file += b''
    print('data size : {}'.format(sys.getsizeof(file)))
    s.send(file)
    print("Data sent ")
    print(time.time() - start)

def test_send():
    dat = [1 for n in range(1000)]
    file = pickle.dumps(dat)
    print('array lenght : {}'.format(len(dat)))
    print('data size : {}'.format(sys.getsizeof(file)))
    s.send(file)
    print('sent')
    try:
        time.sleep(100)
    except:
        pass
    sys.exit()
    s.close()

if __name__ == '__main__':
    # Programme : mandelbrot.py
    # Langage : Python 3.6 - Pygame 1.9
    # Auteur : Mathieu
    # Description : Calcule et affiche la fractale de Mandelbrot en noir et blanc

    # mand = mandel(-2.0, 0.5,-1.25, 1.25,1000, 1000,20,[600,1000])
    # sendData(mand)

    HOST = '91.178.172.194'  # The server's hostname or IP address
    PORT = 65432  # The port used by the server

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    message = get_power()

    res = s.recv(1024)
    print(res.decode('utf-8'))

    # test_send()
    # test_send()

    s.send(str.encode(str(get_power())))

    print("waiting for dimensions")
    res = s.recv(1024)
    lst = pickle.loads(res)

    part = lst[0]
    nx, ny = lst[1]
    xmin, xmax = lst[2]
    ymin, ymax = lst[3]
    maxiter = lst[4]
    print("Dimension received, we handle computation !")

    mandelList = mandel(xmin, xmax, ymin, ymax, nx, ny, part)
    print("sending data")
    sendData(mandelList)


from hashlib import sha256
import hmac
import json
import os
import threading
import urllib.parse

from dropbox import Dropbox, DropboxOAuth2Flow
from dropbox.files import DeletedMetadata, FolderMetadata, WriteMode
from flask import abort, Flask, redirect, render_template, Response, request, session, url_for
from markdown import markdown
import redis

redis_url = os.environ['REDISTOGO_URL']
redis_client = redis.from_url(redis_url, decode_responses=True)

# App key and secret from the App console (dropbox.com/developers/apps)
APP_KEY = os.environ['APP_KEY']
APP_SECRET = os.environ['APP_SECRET']

app = Flask(__name__)
app.debug = True

# A random secret used by Flask to encrypt session data cookies
app.secret_key = os.environ['FLASK_SECRET_KEY']

def get_url(route):
    '''Generate a proper URL, forcing HTTPS if not running locally'''
    host = urllib.parse.urlparse(request.url).hostname
    url = url_for(
        route,
        _external=True,
        _scheme='http' if host in ('127.0.0.1', 'localhost') else 'https'
    )

    return url

def get_flow():
    return DropboxOAuth2Flow(
        APP_KEY,
        APP_SECRET,
        get_url('oauth_callback'),
        session,
        'dropbox-csrf-token')

@app.route('/welcome')
def welcome():
    return render_template('welcome.html', redirect_url=get_url('oauth_callback'),
        webhook_url=get_url('webhook'), home_url=get_url('index'), app_key=APP_KEY)

@app.route('/oauth_callback')
def oauth_callback():
    '''Callback function for when the user returns from OAuth.'''

    auth_result = get_flow().finish(request.args)
    account = auth_result.account_id
    access_token = auth_result.access_token

    # Extract and store the access token for this user
    redis_client.hset('tokens', account, access_token)

    process_user(account)

    return redirect(url_for('done'))

def process_user(account):
    '''Call /files/list_folder for the given user ID and process any changes.'''

    # OAuth token for the user
    token = redis_client.hget('tokens', account)

    # cursor for the user (None the first time)
    cursor = redis_client.hget('cursors', account)

    dbx = Dropbox(token)
    has_more = True

    while has_more:
        if cursor is None:
            result = dbx.files_list_folder(path='')
        else:
            result = dbx.files_list_folder_continue(cursor)

        for entry in result.entries:
            # Ignore deleted files, folders, and non-markdown files
            if (isinstance(entry, DeletedMetadata) or
                isinstance(entry, FolderMetadata) or
                not entry.path_lower.endswith('.md')):
                continue

            # Convert to Markdown and store as <basename>.html
            _, resp = dbx.files_download(entry.path_lower)
            html = markdown(resp.content.decode("utf-8"))
            dbx.files_upload(bytes(html, encoding='utf-8'), entry.path_lower[:-3] + '.html', mode=WriteMode('overwrite'))

        # Update cursor
        cursor = result.cursor
        redis_client.hset('cursors', account, cursor)

        # Repeat only if there's more to do
        has_more = result.has_more

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

@app.route('/login')
def login():
    return redirect(get_flow().start())

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

@app.route('/webhook', methods=['GET'])
def challenge():
    '''Respond to the webhook challenge (GET request) by echoing back the challenge parameter.'''

    resp = Response(request.args.get('challenge'))
    resp.headers['Content-Type'] = 'text/plain'
    resp.headers['X-Content-Type-Options'] = 'nosniff'

    return resp

@app.route('/webhook', methods=['POST'])
def webhook():
    '''Receive a list of changed user IDs from Dropbox and process each.'''

    # Make sure this is a valid request from Dropbox
    signature = request.headers.get('X-Dropbox-Signature')
    key = bytes(APP_SECRET, encoding="ascii")
    computed_signature = hmac.new(key, request.data, sha256).hexdigest()
    if not hmac.compare_digest(signature, computed_signature):
        abort(403)

    for account in json.loads(request.data)['list_folder']['accounts']:
        # We need to respond quickly to the webhook request, so we do the
        # actual work in a separate thread. For more robustness, it's a
        # good idea to add the work to a reliable queue and process the queue
        # in a worker process.
        threading.Thread(target=process_user, args=(account,)).start()
    return ''

if __name__=='__main__':
    app.run(debug=True)


import gym
import numpy as np

from ddpg_planning.Agent import Agent

env = gym.make("LunarLanderContinuous-v2", render_mode="human")

agent = Agent(
    alpha=0.000025,
    beta=0.00025,
    input_dims=[8],
    tau=0.001,
    batch_size=64,
    fc1_dims=400,
    fc2_dims=300,
    n_actions=2,
)
agent.load_models()
agent.eval()

np.random.seed(0)

score_history = []
for i in range(10):
    done = False
    score = 0
    obs = env.reset()[0]
    while not done:
        act = agent.choose_action(obs)
        new_state, reward, done, info, _ = env.step(act)
        score += reward
        obs = new_state
        env.render()

    score_history.append(score)
    print(
        "episode",
        i,
        "score %.2f" % score,
        "100 game average %.2f" % np.mean(score_history[-100:]),
    )


import json
import os
import PIL

import pytorch_lightning as pl
from torch.utils.data import DataLoader
from torchvision import transforms as T

from data.dataset import SRDataset, SRImageProcessor

INTERPOLATION = {
    "linear": PIL.Image.LINEAR,
    "bilinear": PIL.Image.BILINEAR,
    "bicubic": PIL.Image.BICUBIC,
    "lanczos": PIL.Image.LANCZOS,
}

class SRDataModule(pl.LightningDataModule):
    def __init__(
        self,
        mapping_file:str,
        return_clip_embedding:bool,
        return_t5_embedding:bool,
        image_size:int,
        val_image_size:int,
        batch_size:int,
        val_batch_size:int,
        num_workers:int,
        interpolation:str,
        downscale_factor:int,
        min_crop_factor,
        max_crop_factor,
        random_crop:bool,
        degradation:str,
        gaussian_blur:bool,
        blur_prob:float=0.5,
        random_flip_prob:float=0.5,
    ):
        super().__init__()
        # build train mapping file and val mapping file
        self.train_mapping_file, self.val_mapping_file = self._build_mapping_file(mapping_file, val_batch_size)
        # hparam
        self.batch_size = batch_size
        self.val_batch_size = val_batch_size
        self.num_workers = num_workers
        self.return_clip_embedding = return_clip_embedding
        self.return_t5_embedding = return_t5_embedding

        # transforms functions
        train_HR_transforms = T.Compose([
            T.Resize((image_size, image_size), interpolation=INTERPOLATION[interpolation]),
            T.CenterCrop((image_size, image_size)),
            T.ToTensor(),
        ])
        train_LR_transforms = T.Compose([
            T.Resize(
                (image_size//downscale_factor, image_size//downscale_factor),
                interpolation=INTERPOLATION[interpolation]),
            T.CenterCrop((image_size//downscale_factor, image_size//downscale_factor)),
            T.ToTensor(),
        ])
        val_HR_transforms = T.Compose([
            T.Resize((val_image_size, val_image_size), interpolation=INTERPOLATION[interpolation]),
            T.CenterCrop((val_image_size, val_image_size)),
            T.ToTensor(),
        ])
        val_LR_transforms = T.Compose([
            T.Resize(
                (val_image_size//downscale_factor, val_image_size//downscale_factor),
                interpolation=INTERPOLATION[interpolation]),
            T.CenterCrop((val_image_size//downscale_factor, val_image_size//downscale_factor)),
            T.ToTensor(),
        ])

        self.sr_train_image_processor = SRImageProcessor(
            image_size=image_size,
            downscale_factor=downscale_factor,
            min_crop_factor=min_crop_factor,
            max_crop_factor=max_crop_factor,
            random_crop=random_crop,
            degradation=degradation,
            gaussian_blur=gaussian_blur,
            blur_prob=blur_prob,
            random_flip_prob=random_flip_prob,
            HR_transforms=train_HR_transforms,
            LR_transforms=train_LR_transforms,
        )

        self.sr_val_image_processor = SRImageProcessor(
            image_size=val_image_size,
            downscale_factor=downscale_factor,
            min_crop_factor=min_crop_factor,
            max_crop_factor=0.8,
            random_crop=random_crop,
            degradation=degradation,
            gaussian_blur=gaussian_blur,
            blur_prob=blur_prob,
            random_flip_prob=random_flip_prob,
            HR_transforms=val_HR_transforms,
            LR_transforms=val_LR_transforms,
        )

    def _build_mapping_file(self, mapping_file, val_batch_size):
        train_mapping_file = mapping_file.replace('.json', '_train.json')
        val_mapping_file = mapping_file.replace('.json', '_val.json')
        if os.path.exists(train_mapping_file) and os.path.exists(val_mapping_file):
            return train_mapping_file, val_mapping_file
        with open(mapping_file, 'r') as f:
            mapping = json.load(f)
        keys = sorted(list(mapping.keys()))
        val_mapping = {k: mapping[k] for k in keys[:val_batch_size]}
        train_mapping = {k: mapping[k] for k in keys[val_batch_size:]}
        # dump to json file
        with open(train_mapping_file, 'w') as f:
            json.dump(train_mapping, f)
        with open(val_mapping_file, 'w') as f:
            json.dump(val_mapping, f)
        return train_mapping_file, val_mapping_file

    def setup(self, stage=None):
        self.train_set = SRDataset(
            mapping_file=self.train_mapping_file,
            sr_image_processor=self.sr_train_image_processor,
            return_clip_embedding=self.return_clip_embedding,
            return_t5_embedding=self.return_t5_embedding,
        )
        self.val_set = SRDataset(
            mapping_file=self.val_mapping_file,
            sr_image_processor=self.sr_train_image_processor,
            return_clip_embedding=self.return_clip_embedding,
            return_t5_embedding=self.return_t5_embedding,
        )

    def train_dataloader(self):
        return DataLoader(
            self.train_set,
            self.batch_size,
            shuffle=True,
            num_workers=self.num_workers,
            drop_last=True,
        )

    def val_dataloader(self):
        return DataLoader(
            self.val_set,
            self.val_batch_size,
            shuffle=False,
            num_workers=self.num_workers,
        )


import re
from datetime import date, timedelta

import pytest

from domain.model import Batch, OrderLine, OutOfStock, allocate

today = date.today()
tomorrow = today + timedelta(days=1)
later = today + timedelta(days=10)

def test_prefers_current_stock_batches_to_shipments():
    in_stock_batch = Batch("in-stock-batch", "KEY-CHAIN", 100, eta=None)
    shipment_batch = Batch("shipment-batch", "KEY-CHAIN", 200, eta=tomorrow)
    line = OrderLine("order-ref", "KEY-CHAIN", 10)

    allocate(line, [in_stock_batch, shipment_batch])

    assert in_stock_batch.available_quantity == 90
    assert shipment_batch.available_quantity == 200

def test_prefers_earlier_batches():
    earliest_batch = Batch("speedy-batch", "EXTRAVAGANT-TOASTER", 100, eta=today)
    later_batch = Batch("speedy-batch", "EXTRAVAGANT-TOASTER", 100, eta=tomorrow)
    latest_batch = Batch("speedy-batch", "EXTRAVAGANT-TOASTER", 100, eta=later)
    line = OrderLine("order-ref", "EXTRAVAGANT-TOASTER", 10)

    allocate(line, [earliest_batch, later_batch, latest_batch])

    assert earliest_batch.available_quantity == 90
    assert later_batch.available_quantity == 100
    assert latest_batch.available_quantity == 100

def test_returns_allocated_batch_reference():
    in_stock_batch = Batch("in-stock-batch", "KEY-CHAIN", 100, eta=None)
    shipment_batch = Batch("shipment-batch", "KEY-CHAIN", 200, eta=tomorrow)
    line = OrderLine("order-ref", "KEY-CHAIN", 10)
    allocation = allocate(line, [in_stock_batch, shipment_batch])

    assert allocation == in_stock_batch.reference

def test_raises_out_of_stock_exception_if_cannot_allocate():
    batch = Batch("batch-001", "TINY-MELON", 3, eta=today)
    allocate(OrderLine("order-001", "TINY-MELON", 3), [batch])

    with pytest.raises(OutOfStock, match=re.compile(r".*TINY_MELON.*")):
        allocate(OrderLine("order-002", "TINY_MELON", 2), [batch])


import os
import json
import time
from collections import OrderedDict
import pandas as pd

with open("songs.json", encoding="utf-8", newline="") as plik:
    dane = json.load(plik)

parent_dir = "C:/Users/grzeg/PycharmProjects/pythonProject1/"
directory = "piosenki"

path = os.path.join(parent_dir, directory)
os.mkdir(path)

artistList = []
print(dane)
for i in range(len(dane)):
    artistList.append(dane[i]['artist'])
print(len(artistList))

res = list(OrderedDict.fromkeys(artistList))
print(len(res))

root_path = "C:/Users/grzeg/PycharmProjects/pythonProject1/piosenki/"
for x in res:
    pathArtist = os.path.join(root_path, x)
    print(pathArtist)
    os.mkdir(pathArtist)

for y in range(len(dane)):
    artist = ((dane[y]['artist'])) + "/"
    title = ((dane[y]['title']))
    lyrics = ((dane[y]['lyrics']))

    _dir = os.path.join(root_path, artist)

    completeName = os.path.join(_dir, title + ".txt")
    print(completeName)
    file1 = open(completeName, "w")
    file1.write(lyrics)
    file1.close()


"""Corporate models."""
import uuid
from typing import (
    TYPE_CHECKING,
    ClassVar,
    Self,
    cast,
)

from django.contrib.auth.models import (
    AbstractBaseUser,
)
from django.db import (
    models,
    transaction,
)
from django.utils.translation import gettext_lazy as _

if TYPE_CHECKING:
    from workspace.models import (  # noqa: F401
        Workspace,
    )

class CustomerQuerySet(models.QuerySet["Customer"]):
    """Customer QuerySet."""

    def get_by_uuid(self, uuid: uuid.UUID) -> "Customer":
        """Get Customer by UUID."""
        return self.get(uuid=uuid)

    def get_by_workspace_uuid(self, workspace_uuid: uuid.UUID) -> "Customer":
        """Get workpsace by UUID."""
        return self.get(workspace__uuid=workspace_uuid)

    def filter_by_user(self, user: AbstractBaseUser) -> Self:
        """Filter by user."""
        return self.filter(workspace__users=user)

    def get_for_user_and_uuid(
        self, user: AbstractBaseUser, uuid: uuid.UUID
    ) -> "Customer":
        """Get customer by user and uuid."""
        return self.filter_by_user(user).get(uuid=uuid)

    def get_by_stripe_customer_id(self, stripe_customer_id: str) -> "Customer":
        """Get customer by stripe customer id."""
        return self.get(stripe_customer_id=stripe_customer_id)

class CustomerSubscriptionStatus(models.TextChoices):
    """Customer subscription status choices."""

    ACTIVE = "ACTIVE", _("Active")
    UNPAID = "UNPAID", _("Unpaid")
    CANCELLED = "CANCELLED", _("Cancelled")

class Customer(models.Model):
    """Customer model. One to one linked to workspace."""

    workspace = models.OneToOneField["Workspace"](
        "workspace.Workspace",
        on_delete=models.CASCADE,
    )
    seats = models.PositiveIntegerField(default=1)
    uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
    subscription_status = models.CharField(
        max_length=9,
        choices=CustomerSubscriptionStatus.choices,
        default=CustomerSubscriptionStatus.UNPAID,
    )
    stripe_customer_id = models.CharField(
        max_length=150,
        null=True,
        blank=True,
        # XXX
        # unique=True,
        # db_index=True,
    )

    objects: ClassVar[CustomerQuerySet] = cast(  # type: ignore[assignment]
        CustomerQuerySet, CustomerQuerySet.as_manager()
    )

    def activate_subscription(self) -> None:
        """
        Activate customer subscription.

        Saves model instance.
        """
        self.subscription_status = CustomerSubscriptionStatus.ACTIVE
        self.save()

    def cancel_subscription(self) -> None:
        """
        Cancel customer subscription.

        Saves model instance.
        """
        self.subscription_status = CustomerSubscriptionStatus.CANCELLED
        self.save()

    def assign_stripe_customer_id(self, stripe_customer_id: str) -> None:
        """
        Assign stripe customer id.

        Saves model instance.
        """
        self.stripe_customer_id = stripe_customer_id
        self.save()

    def set_number_of_seats(self, seats: int) -> None:
        """
        Set the number of seats.

        Saves model instance.
        """
        if self.seats == seats:
            return None
        self.seats = seats
        self.save()

    @property
    def active(self) -> bool:
        """Return if active customer."""
        return self.subscription_status == CustomerSubscriptionStatus.ACTIVE

    @property
    @transaction.atomic
    def seats_remaining(self) -> int:
        """Return the number of seats remaining."""
        num_users = len(self.workspace.users.all())
        invites_qs = self.workspace.workspaceuserinvite_set.all()
        num_invites = len(invites_qs)
        return self.seats - num_users - num_invites


from gui import add_device_dialog_ui
from dynamic_device_attrs_widget import DynamicDeviceAttrsWidget

from bes_entities import ontology_strings

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QListWidgetItem
from PyQt5.QtGui import QFont
from PyQt5.QtCore import pyqtSignal

class AddDeviceDialog(QtWidgets.QDialog):

    new_device = pyqtSignal(str, str, str, str)

    def __init__(self, ref_entity_id):
        super().__init__()
        self.ui = add_device_dialog_ui.Ui_AddDeviceDialog()
        self.ui.setupUi(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        # setup generic ui elements
        self.ui.line_edit_device_id.setText("<enter_device_id>")
        self.ui.line_edit_entity_name.setText(f"{ref_entity_id}:<device_id>")
        self.setup_entity_types_combo_box()
        self.dynamic_device_attrs_widget = DynamicDeviceAttrsWidget()
        self.ui.scroll_area_dynamic_device_attrs.setWidget(self.dynamic_device_attrs_widget)

        # connect buttons to functions
        self.ui.button_ok.clicked.connect(self.add_device_to_bes)
        self.ui.button_cancel.clicked.connect(self.cancel)

    def setup_entity_types_combo_box(self):
        for device in ontology_strings.device_definitions:
            self.ui.combo_box_entity_type.addItem(device)

    def add_device_to_bes(self):
        device_id = self.ui.line_edit_device_id.text()
        entity_name = self.ui.line_edit_entity_name.text()
        entity_type = self.ui.combo_box_entity_type.currentText()
        self.new_device.emit(device_id, entity_name, entity_type, "temperature_attr")
        self.close()

    def cancel(self):
        self.close()