import datetime
from multiprocessing import Process
import os
from tkinter import messagebox as mb
from tkinter import *
import random
import time
from cryptography.fernet import Fernet
import pickle

cipher_key = b"KoteykaSuperDuperSecuryPack1234567890123456="
cipher = Fernet(cipher_key)

def write_dict(dictionary, file):
    with open(file, 'wb') as out:
        pickle.dump(dictionary, out)

def read_dict(file):
    with open(file, 'rb') as inp:
        d_in = pickle.load(inp)
    return d_in

def doFileAlive(File, IfFalse=None, IfTrue=None):
    """
    Функция, которая проверяет наличие файла,
    и при желании в определённых случаях
    может туда что-либо записать

    :param File: Файл для проверки (расширение тоже указывать если есть)
    :param IfFalse: Что записать в файл если его нет, пропустите если не нужно изменять файл (создатся файл с именем
    файла которого указывали в проверки)
    :param IfTrue: Что записать в файл если он есть, пропустите если не нужно изменять файл
    :return True если файл есть или False если файла нет
    """
    try:
        open(File, "r")
    except FileNotFoundError:
        if IfFalse is not None:
            open(File, "w").write(IfFalse)
        return False
    else:
        if IfTrue is not None:
            open(File, 'w').write(IfTrue)
        return True

def pin(n):
    global inputq
    pinq = Tk()
    pinq.title('Enter pin')
    pinq.geometry('190x270')
    pinq['bg'] = 'cyan'
    pinw = str(n)
    inputq = ''

    def qqww(): print(inputq)

    def clear():
        global inputq
        inputq = ''

    def add(n):
        global inputq
        inputq += str(n)
        if inputq == pinw:
            print('YES')

    Button(pinq, command=qqww, text='More', bg='yellow').place(x=350, y=220)
    Button(pinq, command=lambda: add(1), text='1', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=10, y=10)
    Button(pinq, command=lambda: add(2), text='2', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=70, y=10)
    Button(pinq, command=lambda: add(3), text='3', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=130, y=10)
    Button(pinq, command=lambda: add(4), text='4', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=10, y=75)
    Button(pinq, command=lambda: add(5), text='5', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=70, y=75)
    Button(pinq, command=lambda: add(6), text='6', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=130, y=75)
    Button(pinq, command=lambda: add(7), text='7', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=10, y=140)
    Button(pinq, command=lambda: add(8), text='8', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=70, y=140)
    Button(pinq, command=lambda: add(9), text='9', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=130, y=140)
    Button(pinq, command=lambda: add(0), text='0', width=5, bg='lemon chiffon', activebackground='orange',
           height=3).place(x=70, y=205)
    Button(pinq, command=lambda: clear(), text='Reset', width=5, bg='light grey', activebackground='grey',
           height=3).place(x=10, y=205)

def log(text, MoreActions=0):
    """
    Функция, которая записывает текст как log (С указанием времени и удобной записью)

    :param text: Текст который будет вставлен в log
    :param MoreActions: Дополнительные действия, которые можно использовать для удобства написания
    """

    logO = open("log.log", 'a', encoding="UTF-8")
    now = datetime.datetime.now()  # Получаем время
    timeQ = now.strftime("%d.%m.%Y %H:%M:%S")  # Преобразуем в удобный формат
    txtW = f"[{timeQ}]: {text}\n"
    logO.write(txtW)  # Записываем
    logO.close()  # Закрываем

    if MoreActions == 1:
        log("Приложение закрыто")
        exit(78)
    elif MoreActions == 2:
        if mb.askyesno("Вы уверены?", "Некоторая информация не сохранится"):
            exit(78)
    elif MoreActions == 3:
        exit(78)
    elif MoreActions == 4:
        logO = open("log.log", 'w', encoding="UTF-8")
        logO.write('')  # Очищает файл
        logO.close()
    elif MoreActions == 5:
        os.remove("log.log")  # Удалить файл

def DNSPLS(t):  # Не запускайте!
    timer = t / 100
    for i in range(101):
        print(f"{i}%", end='')
        time.sleep(timer)
        print("\r", end='')

def load(t2, process=False):  # Симулятор загрузки
    timer = t2 / 100  # Вычисляем, сколько времени нужно для полной загрузки
    if process:
        Process(target=DNSPLS, args=(t2,)).start()
    else:
        for i in range(101):
            print(f"{i}%", end='')
            time.sleep(timer)
            print("\r", end='')

class encryption_files:
    @staticmethod
    def lockFile(text, method=1, file=None, doWrite=False, doReturn=True, enrange=1):
        """
        Функция, которая зашифровывает текст

        :param method: Метод шифровки
        :param file: Файл, в которыйй будет записан зашифрованный текст
        :param text: Текст для зашифровки
        :param doWrite: Записывать ли в файл зашифрованный текст
        :param doReturn: Возвращать ли зашифрованный текст
        :param enrange: Шаг изменения букв (Лучше не менять или менять не сильно)
        :return: Зашифрованный текст
        """
        if not doWrite and not doReturn:
            pass
        else:
            if method == 1:
                text1 = str(text)
                text1 = text1.encode()
                encrypted_text = cipher.encrypt(text1)
                if doWrite:
                    testsO = open(file, 'wb')
                    testsO.write(encrypted_text)
                    testsO.close()
                if doReturn:
                    return encrypted_text.decode('utf-8')

            elif method == 2:  # Мой метод, не используйте для безопасного шифрования
                alpha = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяабвгдеёжзийклмнопрстуфхцчшщъыьэюя'
                alphaList = list(alpha)
                out = ''
                if set("!\"\'!@#$%^&*№-_;%:?*()¤&").isdisjoint(text):
                    for i in text:
                        out += alphaList[alpha.find(i) + enrange]
                    if doWrite:
                        testsO = open(file, 'w')
                        testsO.write(out)
                        testsO.close()
                    if doReturn:
                        return out
                else:
                    return "Текст не должен содержать специальных символов"

    @staticmethod
    def unlock_file(text=None, method=1, file=None, doWrite=False, doReturn=True, fileToWrite=None, enrange=1):
        """
        Функция, которая расшифровывает зашифрованный текст

        :param method: Метод расшифровки (должен совпадать с методом расшифровки)
        :param text: Текст для расшифровки
        :param file: Файл, в который был записан зашифрованный текст
        :param doReturn: Возвращать ли зашифрованный текст
        :param doWrite: Записывать ли в файл расшифрованный текст
        :param fileToWrite: В какой файл записывать расшифрованный текст. По умолчанию в тот, где был зашифрованный
        :param enrange: Шаг изменения букв (Лучше не менять или менять не сильно)
        :return: Расшифрованный текст
        """
        if text is None and file is None:
            return None
        if not doWrite and not doReturn:
            pass
        else:
            if fileToWrite is not None:
                doWrite = True
            if file is not None:
                testO = open(file, "rb")
                testR = testO.read()
                testO.close()

            else:
                testR = text.encode()

            if method == 1:
                try:
                    decrypted_text = cipher.decrypt(testR)
                except:
                    return 'Ошибка: неверный текст'
                decrypted_text = decrypted_text.decode('utf-8')
                if doWrite:
                    if fileToWrite is None:
                        open(file).write(decrypted_text)
                    else:
                        open(fileToWrite, "w").write(decrypted_text)
                if doReturn:
                    return decrypted_text

            elif method == 2:
                alpha = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяабвгдеёжзийклмнопрстуфхцчшщъыьэюя'
                alphaList = list(alpha)
                out = ''
                if text is not None:
                    for i in text:
                        out += alphaList[alpha.find(i) - enrange]

                elif file is not None:
                    testO = open(file, "r")
                    text = testO.read()
                    testO.close()
                    for i in text:
                        out += alphaList[alpha.find(i) - enrange]

                if doWrite:
                    if fileToWrite is None:
                        open(file).write(out)
                    else:
                        open(fileToWrite, "w").write(out)

                return out

def random_bool(T=1, F=1, method=1):
    """
    Функция, которая возвращает True или False.
    Также можно увеличить шанс выпадания определённого случая

    Без указания аргументов будет выпадать случайное значение с шансом 50/50

    :param method: Метод генерации
    :param T: Увеличение шанса на True
    :param F: Увеличение шанса на False
    :return: True или False
    """
    if T == 1 and F == 1:
        a = random.randint(0, 1)
        return bool(a)
    else:
        if method == 1:
            TrueS = []
            FalseS = []
            TrueE = 0
            FalseE = 0

            for i in range(T):
                a = random.randint(0, 1)
                TrueS.append(a)
            for j in range(F):
                a = random.randint(0, 1)
                FalseS.append(a)
            for a in TrueS:
                if a == 1:
                    TrueE += 1
            for a in FalseS:
                if a == 1:
                    FalseE += 1
            if FalseE < TrueE:
                return True
            elif TrueE < FalseE:
                return False
            else:  # Last update
                if F < T:
                    return True
                else:
                    return False
        elif method == 2:
            t = random.randint(0, T)
            f = random.randint(0, F)
            if t == f:
                if T > F:
                    return True
                elif F < T:
                    return False
                else:
                    a = random.randint(0, 1)
                    if a == 0:
                        return False
                    else:
                        return True
            elif t == T:
                return True
            elif f == F:
                return False
            else:
                return True

def cmd(key, command):
    if str(key) == encryption_files.unlock_file("gAAAAABgbe5tGDsyVx-jUofDnd-XGg1J7SJNMPaodgbiZaD4ERAXvXSCsCnSa8vG-UHC6K\
    uqaJgqREqQnYtKrg0WKhGvjZaclA=="):
        if type(command) == 'list':
            for a in command:
                os.system(a)
        elif type(command) == 'str':
            os.system(command)
    else:
        return "В целях безопасности вам нужно ввести верный защитный код"

class randomList:
    """
    Класс, функции в котором возвращают случайный список нужных значений
    """

    @staticmethod
    def numbers(a=None, b=None, returns=1, showErrors=True):
        """
        Функция, который возвращает случайный список цифр из нужного диапозона

        :param showErrors: Возвращать ли имя ошибки, принимает True или False
        :param returns: сколько элементов возвращать

        диапазон
        :param a: c какого числа
        :param b: до какого числа

        :return: Список элементов
        """

        if not showErrors:
            try:
                lists = []
                for i in range(returns):
                    listr = random.randint(a, b)
                    lists.append(listr)
                return lists
            except:
                pass
        else:
            if a is not None and b is not None:
                if isinstance(a, int):
                    if isinstance(b, int):
                        if b > a:
                            lists = []
                            for i in range(returns):
                                listr = random.randint(a, b)
                                lists.append(listr)
                            return lists
                        else:
                            return "Первое число должно быть меньше второго"
                    else:
                        return "переменная b не число"
                else:
                    return "переменная a не число"
            else:
                return "Не введены числа"

    """
    Для всех функций ниже:

    :param returns: Сколько элементов возвращать
    :return: Список элементов
    """

    @staticmethod
    def capitalLetters(returns=1):
        """
        Список заглавных букв
        """
        lists = []
        for i in range(returns):
            lists.append(random.choice(list("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ")))
        return lists

    @staticmethod
    def lowercaseLetters(returns=1):
        """
        Список строчных букв
        """
        lists = []
        for i in range(returns):
            lists.append(random.choice(list("абвгдеёжзийклмнопрстуфхцчшщъыьэюя")))
        return lists

    @staticmethod
    def letters(returns=1):
        """
        Список всех букв
        """
        lists = []
        for i in range(returns):
            lists.append(random.choice(list("абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ")))
        return lists

def letters(text, t=0.3):
    text = list(text)
    for i in range(len(text)):
        print(text[i], end='')
        time.sleep(t)

if __name__ == '__main__':
    letters("Koteyka", 0.3)
    print("""
    Информация:
    Пакет дополнений Koteyka
    Версия: 1.7.8
    Создатель: Koteyka
    Inst: @koteyka5000

    """)
    input("Нажмите Enter для продолжения -->")
    print("""
    Установка:
    1) Положить файл в папку с вашим файлом,
    в котором Вы хотите использовать
    модуль Koteyka.
    2) В начале кода написать:

    import Koteyka

    Использование:
    Для вызова функции из модуля напишите:
    Koteyka.<Имя функции или класса>
    Пример:
    print(Koteyka.randomList.numbers(0, 1, 10))
    Вывод:

    """)
    print(randomList.numbers(0, 1, 10))
    print("""
    P.S. Вывод может быть другим, тк
    это случайные числа
    """)


from django.template import RequestContext
from django.shortcuts import render_to_response
import os
from root.fEPSPanalyser import fepspAnalyser
#===============================================================================
# import root.filtering_lib2
# import root.dbAccess #good
# import root.externalFunctions #good
# import root.checkResult #good
# import root.clussterization #good
# import root.graph #good
# import root.objects #good
# import root.onClick #good
# import root.rInterface #good
# import root.simple #good
# import root.simple_gui #good
# import root.interpolation #good
# import scipy #good
# from scipy import polyval, polyfit #good
# from scipy import interpolate #good
#===============================================================================
from sheduler.main import shedule

def individual(request,path):
    test_filelist=os.listdir("/%s" % path)
    input=request.POST
    dict={                                              'list': test_filelist,
                                                        'path': path,
                                                        'frequency': '200000',
                                                        'tags': 'not defined2',
                                                        #'debug': '0',
                                                        #'cluster':'0',
                                                        'db':'0',
                                                        'check':'0',
                                                        'dict':input,}
    for i in input:
        try:
            dict[i]=input[i]
        except:
            pass
    if dict['check']=='1':
        analyserObject=fepspAnalyser([0,dict['path'],dict['frequency'],"data","1",dict['tags'],0,dict['db'],1,0])
    return render_to_response('web_fEPSPA/individual.html', dict, context_instance=RequestContext(request))

def sheduler(request,path):
    test_filelist=os.listdir(path)
    input=request.POST
    dict={                                              'list': test_filelist,
                                                        'path': path,
                                                        'frequency': '200000',
                                                        'tags': 'not defined2',
                                                        'check':'0',
                                                        'dict':input,}
    for i in input:
        try:
            dict[i]=input[i]
        except:
            pass
    print(dict['check'])
    if dict['check']=='1':
        shedule(dict['path'],dict['tags'],dict['frequency'])
    return render_to_response('web_fEPSPA/sheduler.html', dict, context_instance=RequestContext(request))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.core.validators

class Migration(migrations.Migration):

    dependencies = [
        ('workshop_training', '0002_auto_20150628_1710'),
    ]

    operations = [
        migrations.AlterField(
            model_name='student_id',
            name='id_number',
            field=models.IntegerField(default=0, validators=[django.core.validators.MaxValueValidator(99999999), django.core.validators.MinValueValidator(11111111)]),
        ),
    ]


#!/usr/bin/env python

from collections import OrderedDict
from bitstring import BitArray

def get_parameters(train=None, dev=None, test=None, tag_scheme='iobes', lower=False, zeros=True, char_dim=25,
             char_lstm_dim=25, char_bidirect=True, word_dim=100, word_lstm_dim=100, word_bidirect=True, pre_emb=None,
             all_emb=False, cap_dim=0, crf=True, dropout=0.5, lr_method='sgd-lr_.005', reload=False):
    parameters = OrderedDict()
    parameters['train'] = train
    parameters['dev'] = dev
    parameters['test'] = test
    parameters['tag_scheme'] = tag_scheme
    parameters['lower'] = lower  #1 bit
    parameters['zeros'] = zeros  #1 bit
    parameters['char_dim'] = char_dim
    parameters['char_lstm_dim'] = char_lstm_dim  # 6 bits
    parameters['char_bidirect'] = char_bidirect  # 1 bit
    parameters['word_dim'] = word_dim
    parameters['word_lstm_dim'] = word_lstm_dim  # 8 bits
    parameters['word_bidirect'] = word_bidirect  # 1 bit
    parameters['pre_emb'] = pre_emb
    parameters['all_emb'] = all_emb
    parameters['cap_dim'] = cap_dim  # 1 bit
    parameters['crf'] = crf
    parameters['dropout'] = dropout
    parameters['lr_method'] = lr_method
    parameters['reload'] = reload
    return parameters

def get_parameters_from_individual(ga_individual_solution, train=None, dev=None, test=None, tag_scheme='iobes', char_dim=25, word_dim=100,
        pre_emb=None, all_emb=False, crf=True, dropout=0.5, lr_method='sgd-lr_.005', reload=False):
    # Decode GA solution to integer for lower, zeros, char_lstm_dim, char_bidirect, word_lstm_dim, word_bidirect
    # and cap_dim
    lower_bit = ga_individual_solution[0]
    zeros_bit = ga_individual_solution[1]
    char_lstm_dim_bits = BitArray(ga_individual_solution[2:8])
    char_bidirect_bit = ga_individual_solution[8]
    word_lstm_dim_bits = BitArray(ga_individual_solution[9:17])
    word_bidirect_bit = ga_individual_solution[17]
    cap_dim_bit = ga_individual_solution[18]
    lower = lower_bit == 1
    zeros = zeros_bit == 1
    char_lstm_dim = char_lstm_dim_bits.uint
    char_bidirect = char_bidirect_bit == 1
    word_lstm_dim = word_lstm_dim_bits.uint
    word_bidirect = word_bidirect_bit == 1
    cap_dim = cap_dim_bit
    return get_parameters(train=train, dev=dev, test=test, tag_scheme=tag_scheme, lower=lower, zeros=zeros,
                          char_dim=char_dim, char_lstm_dim=char_lstm_dim, char_bidirect=char_bidirect,
                          word_dim=word_dim, word_lstm_dim=word_lstm_dim, word_bidirect=word_bidirect, pre_emb=pre_emb,
                          all_emb=all_emb, cap_dim=cap_dim, crf=crf, dropout=dropout, lr_method=lr_method,
                          reload=reload)


import math,pygame,random,sys,os,neat,pickle,shelve,json
from Menu import pause_menu
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PATH = sys.path[0]
generation = 0
high_score = 0
high_net = None
slow = False
max_score_list = []
game_over = False
main_menu = False

class Computer(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.width = 40
        self.image_list = []
        for i in range(1,9):
            temp_image = pygame.image.load(os.path.join(PATH,"images",("frame-"+str(i)+ ".png"))).convert_alpha()
            self.image_list.append(pygame.transform.smoothscale(temp_image, [self.width, self.width]))
        self.image = self.image_list[0]
        self.image_num = 0
        self.rect = self.image.get_rect()
        self.rect.x = SIZE[0]//2
        self.rect.y = SIZE[1] - self.width
        self.direction = 'up'
        self.normal_speed = 22
        self.speed = self.normal_speed
        self.terminal_velocity = 24
        self.height = 0
        self.score = 0
        self.current_block = 0
        self.current_pos = [SIZE[0]//2,SIZE[1] - 20]
        self.horizontal_direction = 'none'
        self.score_count = 0
        self.final_distance = []
        self.final_distance2 = []
        self.block_hit_list = []
        self.fitness = 0
        self.block_count = 0
        self.none_score = 0

    def update(self):
        self.image_num = (self.image_num + 1 ) % 14
        if self.image_num % 3 == 0:
            self.image = self.image_list[self.image_num//2]
        if self.direction == 'up':
            self.rect.y -= self.speed
        elif self.direction == 'down':
            self.rect.y += self.speed
        self.change_speed()
        if self.rect.right > SIZE[0]:
            self.rect.right = SIZE[0]
        elif self.rect.x < 0:
            self.rect.x = 0
        if self.speed > self.terminal_velocity:
            self.speed = self.terminal_velocity

    def change_speed(self):
        if self.speed == 0:
            self.reverse()
        if self.direction == 'up':
            self.speed -= 1
        elif self.direction == 'down':
            self.speed += 1

    def reverse(self):
        if self.direction == 'up':
            self.direction = 'down'
        elif self.direction == 'down':
            self.direction = 'up'
            self.speed = self.normal_speed

    def collision(self,block_group):
        block_hit_list = pygame.sprite.spritecollide(self,block_group,False)
        for block in block_hit_list:
            if self.rect.bottom > block.rect.top and self.direction == 'down':
                if self.current_block == block.number:
                    self.block_count += 1
                else:
                    self.current_block = block.number
                    self.block_count = 0
                self.current_pos = block.rect.midtop
                self.reverse()
            if block.number not in self.block_hit_list:
                self.block_hit_list.append(block.number)
                self.fitness += 5

    def set_score(self,start_block):
        self.height = start_block.rect.top - self.rect.bottom
        if self.height > self.score:
            self.score = self.height
            self.score_count = 0
        if self.block_count > 2:
            self.fitness -= 1
            self.block_count = 0
        if self.none_score > 250:
            self.fitness -= 2
            self.none_score = 0

    def move(self):
        if self.horizontal_direction == 'right':
            self.rect.x += 9
        elif self.horizontal_direction == 'left':
            self.rect.x -= 9

    def check_directions(self,SIZE,block_group):
        self.final_distance = [-1,-1,-1,-1] #[right,left,down,up]
        right_rect = pygame.Rect(self.rect.right, self.rect.y, SIZE[0], self.width)
        distance = []
        for block in block_group:
            if right_rect.colliderect(block.rect): distance.append(block.rect.x - right_rect.right)
        if len(distance) > 0: self.final_distance[0] = min(distance)
        distance = []
        left_rect = pygame.Rect(0 ,self.rect.y, self.rect.left, self.width)
        for block in block_group:
            if left_rect.colliderect(block.rect): distance.append(self.rect.right - block.rect.left)
        if len(distance) > 0: self.final_distance[1] = min(distance)
        distance = []
        down_rect = pygame.Rect(self.rect.x, self.rect.bottom, self.width, SIZE[0] - self.rect.bottom)
        for block in block_group:
            if down_rect.colliderect(block.rect): distance.append(block.rect.top - self.rect.bottom)
        if len(distance) > 0: self.final_distance[2] = min(distance)
        distance = []
        up_rect = pygame.Rect(self.rect.x, 0, self.width, self.rect.top)
        for block in block_group:
            if up_rect.colliderect(block.rect): distance.append(self.rect.top - block.rect.bottom)
        if len(distance) > 0: self.final_distance[3] = min(distance)
        return self.final_distance

    def check_quadrants(self,SIZE,block_group):
        self.final_distance2 = [-1,-1,-1,-1]
        distance = [[],[],[],[]]
        for block in block_group:
            if block.rect.centerx > self.rect.centerx and block.rect.centery < self.rect.centery:
                distance[0].append(math.hypot((block.rect.centerx - self.rect.centerx),(self.rect.centery - block.rect.centery)))
            elif block.rect.centerx < self.rect.centerx and block.rect.centery > self.rect.centery:
                distance[1].append(math.hypot((self.rect.centerx - block.rect.centerx),(block.rect.centery - self.rect.centery)))
            elif block.rect.centerx < self.rect.centerx and block.rect.centery < self.rect.centery:
                distance[2].append(math.hypot((self.rect.centerx - block.rect.centerx),(self.rect.centery - block.rect.centery)))
            elif block.rect.centerx > self.rect.centerx and block.rect.centery > self.rect.centery:
                distance[3].append(math.hypot((block.rect.centerx - self.rect.centerx),(block.rect.centery - self.rect.centery)))
        for i in range(4):
            if len(distance[i]) > 0:
                self.final_distance2[i] = min(distance[i])
        return self.final_distance2

class Block(pygame.sprite.Sprite):
    def __init__(self,position,block_width,number):
        super().__init__()
        self.number = number
        self.position = position
        self.block_width = block_width
        self.image = pygame.image.load(os.path.join(PATH,"images","Basic_Block.png")).convert()
        self.image = pygame.transform.smoothscale(self.image, [self.block_width, 20])
        self.rect = self.image.get_rect()
        self.rect.x, self.rect.y = position

    def update(self):
        pass

class SideBlock(Block):
    def __init__(self,position,block_width,number):
        super().__init__(position,block_width,number)
        self.direction = random.choice(['left','right'])
        self.distance = 120
        if self.position[0] > SIZE[0] - self.distance - self.block_width:
            self.position[0] = SIZE[0] - self.distance - self.block_width
        elif self.position[0] < self.distance + block_width//2:
            self.position[0] = self.distance + block_width//2

    def update(self):
        if self.rect.centerx > self.position[0] + self.distance:
            self.direction = 'left'
        elif self.rect.centerx < self.position[0] - self.distance:
            self.direction = 'right'
        if self.direction == 'left':
            self.rect.x -= 2
        elif self.direction == 'right':
            self.rect.x += 2

class VerticalBlock(Block):
    def __init__(self,position,block_width,distance,number):
        super().__init__(position,block_width,number)
        self.direction = random.choice(['up','down'])
        self.distance = distance

    def update(self):
        if self.rect.centery < self.position[1] - self.distance:
            self.direction = 'down'
        elif self.rect.centery > self.position[1] + self.distance:
            self.direction = 'up'
        if self.direction == 'up':
            self.rect.y -= 1
        elif self.direction == 'down':
            self.rect.y += 1

def move(direction,block_y,block_group,player_list,start_block):
    for block in block_group:
        block.rect.y += direction
        if isinstance(block,VerticalBlock):
            block.position[1] += direction
    for player in player_list:
        player.rect.y += direction
    start_block.rect.y += direction
    block_y += direction
    return block_y

def set_block(block_x,block_y,block_width,tolerance):
    block_y -= random.randint(130,140)
    far_left = block_x - block_width//2
    far_right = SIZE[0]- block_x - block_width
    block_x += random.randint(-1 * min(tolerance,far_left),min(far_right,tolerance))
    return block_x,block_y

def choose_block_type(height):
    if height > 3000 and height < 10000:
        return random.choice(['move','still','still','still','still','still','vertical'])
    elif height >= 10000:
        return random.choice(['move','still','still','still','vertical'])
    else:
        return 'still'

def gameplay(genomes,config):
    global game_over,main_menu
    if game_over:
        for genome_id,genome in genomes:
            genome.fitness = 0
        return
    global generation,high_net,high_score,slow
    background_image_1 = pygame.image.load(os.path.join(PATH,"images","Background.jpg")).convert()
    background_image_1 = pygame.transform.smoothscale(background_image_1, SIZE)
    generation += 1
    score_font = pygame.font.Font("freesansbold.ttf", 30)
    player_list = []
    genome_list = []
    nn_list = []
    block_group = pygame.sprite.Group()
    block_count = 0
    tolerance = 220
    vertical_distance = 60

    for genome_id,genome in genomes:
        genome.fitness = 0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        nn_list.append(net)
        player_list.append(Computer())
        genome_list.append(genome)

    block_width = 130
    start_block = Block([0,SIZE[1]-20],SIZE[0],block_count)
    block_count += 1
    block_x,block_y = SIZE[0]//2,SIZE[1] - 50
    new_block = Block([block_x,block_y], block_width,block_count)
    block_count += 1
    block_group.add(new_block)
    while block_y > 70:
        block_x,block_y = set_block(block_x,block_y,block_width,tolerance)
        new_block = Block([block_x,block_y], block_width,block_count)
        block_count += 1
        block_group.add(new_block)
    clock = pygame.time.Clock()
    current_score = 0
    max_index = 0
    max_score = 0
    count = 0

# -------------- Main Program Loop ---------------- #
    while not game_over and len(player_list) > 0:
        count += 1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
                return
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    game_over = True
                    return
                elif event.key == pygame.K_s:
                    slow = not slow
                elif event.key == pygame.K_SPACE:
                    pause = pause_menu(SIZE,screen)
                    if pause == "gameover":
                        game_over = True
                        return
                    elif pause == 'main_menu':
                        game_over = True
                        main_menu = True
                        return
        screen.blit(background_image_1,(0,0))
        current_score = 0
        for i,player in enumerate(player_list):
            player.score_count += 1
            player.set_score(start_block)
            genome_list[i].fitness = player.fitness
            player.move()
            player.collision(block_group)
            if player.score > current_score:
                current_score = player.score
                max_index = i
                max_net = nn_list[i]
            if player.score > max_score:
                max_score = player.score
            distance1 = player.check_directions(SIZE,block_group)
            distance2 = player.check_quadrants(SIZE,block_group)
            nn_output = nn_list[i].activate(distance1 + distance2)
            position = nn_output.index(max(nn_output))
            if position == 0 and nn_output[0] > 0.5:
                player.horizontal_direction = 'right'
                player.none_score = 0
            elif position == 1 and nn_output[1] > 0.5:
                player.horizontal_direction = 'left'
                player.none_score = 0
            elif player.horizontal_direction != 'none':
                player.none_score = 0
                player.horizontal_direction = 'none'
            else:
                player.none_score += 1

        if max_index >= len(player_list):
            max_index = len(player_list) - 1
        if player_list[max_index].rect.y < SIZE[0]//5 :
            if block_y > -10:
                block_x,block_y = set_block(block_x,block_y,block_width,tolerance)
                block_type = choose_block_type(player_list[max_index].score)
                if block_type == 'move':
                    new_block = SideBlock([block_x,block_y], block_width,block_count)
                elif block_type == 'still':
                    new_block = Block([block_x,block_y], block_width,block_count)
                elif block_type == 'vertical':
                    block_y -= vertical_distance//2
                    new_block = VerticalBlock([block_x,block_y], block_width, vertical_distance,block_count)
                    block_y -= vertical_distance//2
                block_count += 1
                block_group.add(new_block)

        if count % 15 == 0:
            block_width = int(round(-60/(1+1.0001**(9000-max_score)) + 160))

        if count % 13 == 0:
            tolerance = int(round(60/(1+1.0003**(10000-max_score)) + 190))

        if player_list[max_index].rect.y < 0:
            block_y = move(11,block_y,block_group,player_list,start_block)
        elif player_list[max_index].rect.y < SIZE[1]//6 and player_list[max_index].rect.y > 0:
            block_y = move(7,block_y,block_group,player_list,start_block)
        elif player_list[max_index].rect.y < SIZE[1]//3 and player_list[max_index].rect.y > SIZE[1]//6:
            block_y = move(4,block_y,block_group,player_list,start_block)

        for block in block_group:
            if block.rect.y > SIZE[1] * 2:
                block.kill()

        for player in player_list:
            if player.rect.bottom > SIZE[1]* 2 or player.score_count > 450:
                genome_list[player_list.index(player)].fitness = player.fitness
                nn_list.pop(player_list.index(player))
                genome_list.pop(player_list.index(player))
                player_list.pop(player_list.index(player))

        screen.blit(background_image_1,(0,0))
        block_group.update()
        block_group.draw(screen)
        for player in player_list:
            player.update()
            screen.blit(player.image,player.rect)

        score_display = score_font.render("Max Score " + str(max_score), True, BLACK)
        screen.blit(score_display,(SIZE[0]//20, SIZE[1]//20))
        generation_display = score_font.render("Generation " + str(generation), True, BLACK)
        screen.blit(generation_display,(SIZE[0]//20, SIZE[1]//10))
        high_score_display = score_font.render("Highscore " + str(high_score), True, BLACK)
        screen.blit(high_score_display,(SIZE[0]//20, SIZE[1]//7))

        pygame.display.flip()
        if slow == True:
            clock.tick(60)
    if max_score > high_score:
        high_score = max_score
        high_net = max_net
    max_score_list.append(max_score)

def run(window,surface):
    global SIZE,screen,game_over,main_menu
    config_file = os.path.join(PATH,"config2.txt")
    SIZE = window
    screen = surface
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation,config_file)
    population = neat.Population(config)
    best_genome = population.run(gameplay, 200)
    if __name__ == "__main__" and not game_over:
        test_database = shelve.open("test_database")
        test_database["test"] = high_net
        test_database.close()
        f = open("score_list.json","wt")
        json.dump(max_score_list, f)
        f.close()
    if main_menu:
        return None
    elif game_over:
        return 'gameover'
    else:
        return None

if __name__ == '__main__':
    os.environ['SDL_VIDEO_CENTERED'] = '1'
    pygame.init()
    SIZE = (1280,720)
    screen = pygame.display.set_mode(SIZE)
    pygame.display.set_caption("NEAT-JUMP")
    run(SIZE,screen)


# Simple Linear Regression

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)

# Feature Scaling
"""
In a Simple Linear Regression the libraries will handle for us the feature scaling
that is required.
"""

# Fitting Simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

# Creating a vector of predicted salaries based on the Test set
y_pred = regressor.predict(X_test)  # this value is not used afterwards.

"""
y_test is a vector of observed salaries (real salaries)
y_pred is a vector of predicted salaries based on the SLR model
"""

"""
Visualize the trained model (the Simple Linear Regression line) along side the
plot of the train set of observations used to train the model.
"""
# plot the training set observations in red
plt.scatter(X_train, y_train, color = 'red')

# plot the training set predictions regression line in blue
plt.plot(X_train, regressor.predict(X_train), color = 'blue')

# set up the graph labels and then display
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

"""
Now that there is a trained model (the Simple Linear Regression line) based on
the training set of observations, visualize the test set of observations against
the same line to see if the model can make accurate predictions of salary.
"""
# plot the test set observations in red
plt.scatter(X_test, y_test, color = 'red')

# plot the training set predictions regression line in blue
plt.plot(X_train, regressor.predict(X_train), color = 'blue')

# set up the graph labels and then display
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

dataset/Machine Learning A-Z Template Folder/Part 2 - Regression/Section 4 - Simple Linear Regression/Simple_Linear_Regression/simple_linear_regression.py


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

import web, json, time
from config import setting
import app_helper
from libs import sms

db = setting.db_web

url = ('/app/v1/bind_tel_number')

# 微信手机检查验证码
class handler:
    @app_helper.check_sign(['app_id','dev_id','ver_code','tick', 'session', 'mobile'])
    def POST(self):
        web.header('Content-Type', 'application/json')
        param = web.input(app_id='', dev_id='', ver_code='', tick='', session='', mobile='')

        if '' in (param.app_id, param.dev_id, param.ver_code, param.tick,
            param.session, param.mobile):
            return json.dumps({'ret' : -2, 'msg' : '参数错误'})

        number = param.mobile.strip()
        if len(number)<11 or (not number.isdigit()):
            return json.dumps({'ret' : -10, 'msg' : '手机号码格式错误'})

        if number in app_helper.BLOCK_LIST:
            return json.dumps({'ret' : -10, 'msg' : '手机号码错误'})

        session = app_helper.get_session(param.session)
        if session is None:
            return json.dumps({'ret': -4, 'msg': '无效的sessionid'})

        if session.get('userid')!='':
            return json.dumps({'ret': -9, 'msg': '已绑定，不能重复绑定'})

        rand = app_helper.my_rand(base=1)
        db.app_sessions.update_one({'session_id': session['session_id']}, {'$set': {
            'attime': time.time(),
            'rand': rand,
        }})

        if number not in app_helper.INNER_NUM.keys(): # 内部号码不发短信，2015-12-22, gt
            rr = sms.send_rand(param.mobile, rand, False) # 测试不发校验码
            if rr is None:
                return json.dumps({'ret': -11, 'msg': '发送太频繁，稍后再试'})

        return json.dumps({'ret': 0, 'data': {
            'session'     : session['session_id'],
            'if_sms_send' : True,
        }})


from collections import namedtuple

Navigate = namedtuple('Navigate', [
    'max_distance',
    'debugging',
    'thresh_c',
    'kernel_size',
    'min_area',
    'block_size',
])

Recovery = namedtuple('Recovery', [
    'stack_dive_altitude',
    'vstack_grab_altitude',
    'hstack_grab_altitude',

    'debugging',
    'table_c',
    'table_block_size',
    'table_min_area',
    'table_min_fill_ratio',
    'red_stack_c',
    'green_stack_c',
    'red_mark_c',
    'green_mark_c',
    'min_stack_area',
    'min_stack_width',
])


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

# {{{ Header
# Code written by: Edwin Rice
# email: edwinricethe4th@gmail.com
# phone: +1 (513) 426-4187
# github: https://github.com/EdRice4
#
# Initiation date|time: 08/10/2015|12:59:38
# }}}

# {{{ Imports
from os import getcwd, listdir
import re
import argparse
from sys import exit
# }}}

# {{{ FileIO
class FileIO(object):

    """ {{{ Docstrings

    A class in which file input/output is handled.

    }}} """

    # {{{ read_tree_file
    def read_tree_file(self, tree_file):

        """ {{{ Docstrings

        Reads a tree file into a string.

        }}} """

        # Open file in read mode
        with open(tree_file, 'r') as tree:
            # Read in as one, long string
            tree = tree.read()
        return tree
    # }}}

    # {{{ read_dict_file
    def read_dict_file(self, dict_file):

        """ {{{ Docstrings

        Reads a dictionary file into a dictionary with the format:

            dictionary = {

                'Substitute ID' : 'Original ID'

                }

         }}} """

        # Initiate empty dictionary to store IDs
        IDs = {}
        # Open file in read mode
        with open(dict_file, 'r') as dictionary:
            dictionary = dict_file.readlines()
        for line in dictionary:
            # Partition line into two values, based on location of tab "\t"
            # character
            line = line.split('\t')
            # Set values for readability
            Substitute_ID = line[0]
            Original_ID = line[1]
            # Add to dictionary
            IDs[Substitute_ID] = Original_ID
        return IDs
    # }}}

    # {{{ write_tree_file
    def write_tree_file(self, subbed_tree_file):

        """ {{{ Docstrings

        Writes a new tree to a file.

        }}} """

        # Open file in write mode
        with open(self.new_tre_file, 'w') as new_tree_file:
            # Write to file
            new_tree_file.write(subbed_tree_file)
    # }}}
# }}}

# {{{ RegEx
class RegEx(FileIO):

    """ {{{ Docstrings

    A class in which all regular expression functionality is stored.

    }}} """

    # {{{ substitute
    def substitute(self):

        """ {{{ Docstrings

        Performs substitution, utlizing functionality provided by the "re"
        python module, substituting all substituted IDs with their respective
        original IDs.

        }}} """

        # Compile substitute IDs into pattern re.sub can utilize;  "|" in
        # regular expression speak means "or"
        substitute_ID_pattern = re.compile(
                '|'.join(self._dict.iterkeys())
                )
        # Perform substitution
        subbed_tree = substitute_ID_pattern.sub(
                lambda match: self._dict[re.escape(match.group())], self.tree
                )
        return subbed_tree
# }}}

# {{{ TreFile
class TreFile(RegEx):

    """ {{{ Docstrings

    A class in which all the necessary parameters corresponding to each
    respective tree file are stored.

    }}} """

    # {{{ __init__
    def __init__(self, tree_file, dict_file):
        self._tree = self.read_tree_file(tree_file)
        self._dict = self.read_dict_file(dict_file)
        self._sub_tree_file = self._tree.replace('.tre', '_subbed.tre')
        self._subbed_tree = self.substitute()
        self.write_tree_file()
    # }}}
# }}}

# {{{ ArgParse
arg_parser = argparse.ArgumentParser(
        prog='RegEx_Tre.py',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            'A script intended to be used as the final component in the '
            'workflow of F2P.py. F2P.py produces three output files: 1.) '
            'A phylip file containing "original" IDs and sequences; 2.) '
            'A phylip file containing "substitue" IDs and seqeunces (i.e. '
            '~10 digit integers); 3.) A dictionary file containing each '
            'original ID and its repective substitute ID. This script accepts '
            'a tree ouput file (or any output file) containing the substitute '
            'IDs and replaces each, utilizing regular expressions, with its '
            'respective original ID as defined by the dictionary file.'
            ),
        epilog=(
            'Note: When running in batch mode, all tre files you wish to run '
            'should contain the string ".tre" in the file name while all '
            'dictionary files you wish to run should contain the string '
            '".txt" in the file name. Furthermore, each tree file and its '
            'respective dictionary file should be named analogously. For '
            'instance, "Grammostola_rosea.tre" and "Grammostola_rosea.txt."'
            )
        )
arg_parser.add_argument(
        'tre_file', type=str, help='Name of tre file to be substituted.',
        default=None
        )
arg_parser.add_argument(
        'dict_file', type=str,
        help=(
                'Name of corresponding dictionary file containing original '
                'IDs and respective substitute IDs.'
                ),
        default=None
        )
arg_parser.add_argument(
        '-b', '--batch',
        help=(
                'Run script in batch mode. i.e. perform substition on all tre '
                'files withint directory and their respective dictionary '
                'files.'
                ),
        action='store_true'
        )
args = arg_parser.parse_args()
# }}}

# {{{ Batch
# Instantiate instance of TreFile class with each tre file within directory and
# its corresponding dictionary file
if args.batch:
    # Get current working directory
    cwd = getcwd()
    # Get all files in cwd
    fid = listdir(cwd)
    # Filter out tre files
    # NOTE: All tree files you wish to run should contain the string ".tre"
    # and all files within the directory containing this string will be run
    tre_files = sorted(filter(lambda x: '.tre' in x, fid))
    # Filter out dictionary files
    # NOTE: All dictionary files you wish to run should contain the string
    # ".txt" and all files within the directory containing this string will be
    # run
    dict_files = sorted(filter(lambda x: '.txt' in x, fid))
    # Instantiate instance of TreFile class for each tree file and its
    # respective dictionary file found
    for i, j in zip(tre_files, dict_files):
        TreFile(i, j)
# Else, utilize user-specifed strings to instantiate single instance of PepFile
# class
else:
    # Exit if no proper tree/dictionary file specified.
    if not args.tree_file or args.dict_file:
        exit(
                'You did not specify a tree/dictionary file to run nor did '
                'you run the script in batch mode. Try again.'
                )
    TreFile(args.tre_file, args.dict_file)
# }}}


from functools import lru_cache
import ctypes

from gfxutils import V2, Color

from . import core
from .image import Image, Sprite
from .font import Font

windowpos_undefined = 0x1FFF0000

class Window:

    default_config = {
        'shown',
        'resizeable',
        'accelerated_rendering',
        'target_texture'
    }

    all_options = {
        'fullscreen',
        'opengl',
        'shown',
        'borderless',
        'resizeable',
        'minimized',
        'maximized',
        'input_grabbed',
        'input_focus',
        'mouse_focus',
        'fullscreen_desktop',
        'foreign',
        'high_dpi',
        'mouse_capture',
        'software_rendering',
        'accelerated_rendering',
        'vsync_enabled',
        'target_texture',
    }

    def __init__(self, title='Untitled',
                 window_size=V2(800, 600),
                 buffer_size=V2(800, 600),
                 config=default_config):

        self.always_on_top = False

        if 'minimized' in config and 'maximized' in config:
            raise ArgumentError('Cannot use minimized with maximized')

        if 'software_rendering' in config and 'accelerated_rendering' in config:
            raise ArgumentError('Cannot use both software and accelerated rendering')

        flags = 0
        flags |= 1 if 'fullscreen' in config else 0
        flags |= 2 if 'opengl' in config else 0
        flags |= 4 if 'shown' in config else 8
        flags |= 16 if 'borderless' in config else 0
        flags |= 32 if 'resizeable' in config else 0
        flags |= 64 if 'minimized' in config else 0
        flags |= 128 if 'maximized' in config else 0
        flags |= 256 if 'input_grabbed' in config else 0
        flags |= 512 if 'input_focus' in config else 0
        flags |= 1024 if 'mouse_focus' in config else 0
        flags |= (1 | 4096) if 'fullscreen_desktop' in config else 0
        flags |= 2048 if 'foreign' in config else 0
        flags |= 8192 if 'high_dpi' in config else 0
        flags |= 16384 if 'mouse_capture' in config else 0

        self.window = core.create_window(core.c_string(title),
                                         windowpos_undefined, windowpos_undefined,
                                         int(window_size.x), int(window_size.y),
                                         flags)

        if self.window is None:
            raise core.PlatformError('Could not create window')

        flags = 0
        flags |= 1 if 'software_rendering' in config else 0
        flags |= 2 if 'accelerated_rendering' in config else 0
        flags |= 4 if 'vsync_enabled' in config else 0
        flags |= 8 if 'target_texture' in config else 0

        self.renderer = core.create_renderer(self.window, -1, flags)
        if self.renderer is None:
            raise core.PlatformError('Could not create renderer')

        self.desired_size = buffer_size.copy
        self.back_buffer = core.create_texture(self.renderer, int(buffer_size.x), int(buffer_size.y))
        self.on_resized()
        core.show_cursor(0)
        self.cursor = None
        core.windows[self.id] = self

    @lru_cache()
    def open_font(self, filename, size=24):
        return Font(self.renderer, filename, size)

    @lru_cache()
    def load_image(self, filename, frames=1):
            renderer = self.renderer
            surface = core.load_image(core.c_string(filename))
            size = V2(surface.contents.w, surface.contents.h)
            texture = core.create_texture_from_surface(renderer, surface)
            core.free_surface(surface)
            return Image(renderer, texture, size, frames)

    @property
    def mouse_position(self):
        x, y = core.c_int(0), core.c_int(0)
        core.get_mouse_state(core.byref(x), core.byref(y))
        clip_origin = V2(self.clip_rect.x, self.clip_rect.y)
        clip_size = V2(self.clip_rect.w, self.clip_rect.h)
        scale = self.desired_size.length / clip_size.length
        return (V2(x.value, y.value) - clip_origin) * scale

    @property
    def mouse_speed(self):
        x, y = core.c_int(0), core.c_int(0)
        core.get_relative_mouse_state(core.byref(x), core.byref(y))
        clip_origin = V2(self.clip_rect.x, self.clip_rect.y)
        clip_size = V2(self.clip_rect.w, self.clip_rect.h)
        scale = self.desired_size.length / clip_size.length
        return V2(x.value, y.value) * scale

    @property
    def mouse_buttons_pressed(self):
        x, y = core.c_int(0), core.c_int(0)
        buttons = core.get_relative_mouse_state(core.byref(x), core.byref(y))
        return buttons

    @property
    def id(self):
        return core.get_window_id(self.window)

    @property
    def draw_color(self):
        r, g, b, a = core.c_ubyte(0), core.c_ubyte(0), core.c_ubyte(0), core.c_ubyte(0)
        core.get_render_draw_color(self.renderer,
                                   core.byref(r),
                                   core.byref(g),
                                   core.byref(b),
                                   core.byref(a))
        return Color(r.value / 255,
                     g.value / 255,
                     b.value / 255,
                     a.value / 255)

    @draw_color.setter
    def draw_color(self, color):
        r = int(color.red * 255)
        g = int(color.green * 255)
        b = int(color.blue * 255)
        a = int(color.alpha * 255)
        core.set_render_draw_color(self.renderer, r, g, b, a)

    def on_closed(self):
        try:
            del core.windows[self.id]
            core.destroy_renderer(self.renderer)
            core.destroy_window(self.window)
        except:
            pass

    def clear(self):
        core.set_render_target(self.renderer, self.back_buffer)
        core.set_render_draw_color(self.renderer, 0, 0, 0, 255)
        core.render_clear(self.renderer)

    def update_and_render(self, milliseconds):
        pass

    def present(self):
        if self.cursor is None:
            position = self.mouse_position
            rect = core.Rect(int(position.x - 1), int(position.y - 1), 3, 3)
            core.set_render_draw_color(self.renderer, 255, 0, 0, 255)
            core.render_draw_rect(self.renderer, core.byref(rect))
        else:
            self.cursor.position = self.mouse_position
            self.cursor.draw()
        core.set_render_target(self.renderer, None)
        core.render_copy(self.renderer, self.back_buffer, None, core.byref(self.clip_rect))
        core.render_present(self.renderer)

    @property
    def size(self):
        x, y = core.c_int(0), core.c_int(0)
        core.get_window_size(self.window, core.byref(x), core.byref(y))
        return V2(x.value, y.value)

    @property
    def scale(self):
        if self.size.x / self.size.y > self.desired_size.x / self.desired_size.y:
            return self.size.y / self.desired_size.y
        else:
            return self.size.x / self.desired_size.x

    def on_resized(self):
        scale = self.scale
        view_size = self.desired_size * scale
        offset = (self.size - view_size) / 2
        self.clip_rect = core.Rect(int(offset.x), int(offset.y), int(view_size.x), int(view_size.y))

    def draw(self, filename, position, effect=None, centered=True):
        image = self.load_image(filename)
        sprite = Sprite(image, position, effect=effect, centered=centered)
        sprite.draw()

    def write(self, text, position, size=24, effect=None, centered=True):
        font = self.open_font('c:\\windows\\fonts\\ARIALN.ttf', size=24)
        font.write(text, position, effect=effect, centered=centered)


from django.conf.urls import patterns
from django.views.generic import TemplateView

from api import rest_api
from cartoview.app_manager.rest import AppResource
from views import *

rest_api.register(AppResource())

urlpatterns = patterns(
        'cartoview.app_manager',
        url(r'^$', index, name='app_manager_base_url'),
        url(r'^appinstances/$', TemplateView.as_view(template_name='app_manager/app_instance_list.html'),
            name='appinstance_browse'),
        url(r'^appinstance/(?P<appinstanceid>\d+)/?$', appinstance_detail, name='appinstance_detail'),
        url(r'^appinstance/(?P<appinstanceid>\d+)/metadata$', appinstance_metadata, name='appinstance_metadata'),

        url(r'^install/$', install_app_view, name='install_app'),
        url(r'^ajax_install/$', ajax_install_app, name='ajax_install_app'),
        url(r'^uninstall/(?P<app_name>.*)/$', uninstall_app, name='cartoview_uninstall_app_url'),
        url(r'^moveup/(?P<app_id>\d+)/$', move_up, name='move_up'),
        url(r'^movedown/(?P<app_id>\d+)/$', move_down, name='move_down'),
        url(r'^suspend/(?P<app_id>\d+)/$', suspend_app, name='suspend'),
        url(r'^resume/(?P<app_id>\d+)/$', resume_app, name='resume'),
        url(r'^save_app_orders/$', save_app_orders, name='save_app_orders'),
        url(r'^(?P<appinstanceid>\d+)/remove$', appinstance_remove, name="appinstance_remove"),
        (r'^rest/', include(rest_api.urls)),
)

apps = get_apps_names()
for name in apps:
    import_app_rest(name)

for name in apps:
    urlpatterns.append(app_url(name))


import json
import re
import time
import urllib

import colorlabels as cl
import twitter
from decouple import config

from util import log_file, merge_whitespaces

DEBUG_FILENAME = log_file('twdebug.log')
MAX_COUNT_PER_REQ = 100

credentials = {
    'consumer_key': config('TWITTER_CONSUMER_KEY'),
    'consumer_secret': config('TWITTER_CONSUMER_SECRET'),
    'access_token_key': config('TWITTER_ACCESS_TOKEN_KEY'),
    'access_token_secret': config('TWITTER_ACCESS_TOKEN_SECRET')
}

try:
    twapi = twitter.Api(**credentials, application_only_auth=True,
                        sleep_on_rate_limit=True)
except Exception:
    cl.warning('Twitter access token is not correctly configured, '
               'cannot retrieve Twitter data in the official way.')
    twapi = None

def get_full_text(tweet):
    if tweet.retweeted_status:
        return tweet.retweeted_status.full_text
    else:
        return tweet.full_text

def get_hashtags(tweet):
    for hashtag in tweet.hashtags:
        if hashtag.text:
            yield hashtag.text

def parse_source(source):
    result = re.findall(r'(.*?)', source)[0]
    return '%s(%s)' % (result[1], result[0])

def twapi_search_page(query, count, max_id=None, *, sleep_time, **kwargs):
    debugfile = open(DEBUG_FILENAME, 'a', encoding='utf-8')

    kwargs.update({'q': query, 'count': count, 'max_id': max_id,
                   'result_type': 'recent', 'tweet_mode': 'extended'})
    query = urllib.parse.urlencode(kwargs)

    for tweet in twapi.GetSearch(raw_query=query):
        debugfile.write(json.dumps(tweet.AsDict(), indent=4,
                                   ensure_ascii=False) + '\n')

        yield {
            'id': tweet.id_str,
            'text': merge_whitespaces(get_full_text(tweet)),
            'hashtags': ','.join(get_hashtags(tweet)),
            'created_at': tweet.created_at,
            'retweeting_id': tweet.retweeted_status.id_str
            if tweet.retweeted_status else None,
            'quoting_id': tweet.quoted_status_id_str,
            'replying_id': tweet.in_reply_to_status_id,
            'favorite_count': tweet.favorite_count,
            'retweet_count': tweet.retweet_count,
            'lang': tweet.lang,
            'source': parse_source(tweet.source),
            'user_id': tweet.user.id_str,
            'user_name': merge_whitespaces(tweet.user.name),
            'user_screen_name': merge_whitespaces(tweet.user.screen_name),
            'user_description': merge_whitespaces(tweet.user.description),
            'user_lang': tweet.user.lang,
            'user_location': merge_whitespaces(tweet.user.location),
            'user_verified': int(bool(tweet.user.verified)),
            'user_created_at': tweet.user.created_at,
            'user_favourites_count': tweet.user.favourites_count,
            'user_followers_count': tweet.user.followers_count,
            'user_friends_count': tweet.user.friends_count,
            'user_statuses_count': tweet.user.statuses_count,
        }

    debugfile.close()

    time.sleep(sleep_time)  # Rate limit

def twapi_search(query, count, *, sleep_time=0, **kwargs):
    with open(DEBUG_FILENAME, 'w'):
        pass

    last_id = None
    num_fetched = 0

    while count > 0:
        next_count = min(count, MAX_COUNT_PER_REQ)
        next_id = last_id - 1 if last_id is not None else None
        data = list(twapi_search_page(query, next_count, next_id,
                                      sleep_time=sleep_time, **kwargs))

        if not data:
            cl.warning('No more data can be retrieved, terminating...')
            return

        for item in data:
            last_id = int(item['id'])
            yield item

        num_fetched += len(data)
        count -= len(data)

        cl.info('Current number of records fetched: %d' % num_fetched)


import unittest
import datetime
import sys

sys.path.append('../')

import individual
import family

# -----------------------------------------------------------------------------
# User Story #09: Child should be born before the death of the mother and at
#                 least nine months after the death of the father.
# -----------------------------------------------------------------------------

def CreateFather(deathDate):
    father                    = individual.Individual()
    father.id                 = "H1"
    father.firstAndMiddleName = "John James"
    father.lastname           = "Jamison"
    father.deathDate          = deathDate

    return father

def CreateMother(deathDate):
    mother                    = individual.Individual()
    mother.id                 = "W1"
    mother.firstAndMiddleNAme = "Jane Janet"
    mother.lastname           = "Jamison"
    mother.deathDate          = deathDate

    return mother

def CreateChild(id,birthDate,first):
    child                    = individual.Individual()
    child.id                 = id
    child.firstAndMiddleNAme = first
    child.lastname           = "Jamison"
    child.birthDate          = birthDate

    return child

def CreateFamily(mother,father):
    theFamily                  = family.Family()
    theFamily.id               = "F1"
    theFamily.husbandId        = father.id
    theFamily.wifeId           = mother.id

    return theFamily

class Test_BirthAfterDeath(unittest.TestCase):

    def test_BirthIsAfterDeath(self):
        mother    = CreateMother(datetime.date(2000,10,2))
        father    = CreateFather(datetime.date(2000,1,1))
        child     = CreateChild("C1",datetime.date(2000,10,1),"Jimmy John")
        theFamily = CreateFamily(mother,father)
        theFamily.children.append(child)

        individualsDict             = {}
        individualsDict[mother.id] = mother
        individualsDict[father.id] = father
        individualsDict[child.id]  = child

        self.assertTrue(theFamily.IsBirthAfterDeath(individualsDict,child))

    def test_BirthIsNotAfterDeathOfMother(self):
        mother    = CreateMother(datetime.date(2000,9,30))
        father    = CreateFather(datetime.date(2000,1,1))
        child     = CreateChild("C1",datetime.date(2000,10,1),"Jimmy John")
        theFamily = CreateFamily(mother,father)
        theFamily.children.append(child)

        individualsDict             = {}
        individualsDict[mother.id] = mother
        individualsDict[father.id] = father
        individualsDict[child.id]  = child

        self.assertFalse(theFamily.IsBirthAfterDeath(individualsDict,child))

    def test_BirthIsNotAfterDeathOfFather(self):
        mother    = CreateMother(datetime.date(2000,10,2))
        father    = CreateFather(datetime.date(2000,1,2))
        child     = CreateChild("C1",datetime.date(2000,10,1),"Jimmy John")
        theFamily = CreateFamily(mother,father)
        theFamily.children.append(child)

        individualsDict             = {}
        individualsDict[mother.id] = mother
        individualsDict[father.id] = father
        individualsDict[child.id]  = child

        self.assertFalse(theFamily.IsBirthAfterDeath(individualsDict,child))

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


#LOCAL inference.py - EDIT THIS

import mxnet as mx
import os
import json
import numpy as np
from collections import namedtuple
import logging
import cv2
import PIL
from PIL import Image
import io

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

import image_pb2 as impb
image_packet = impb.PBImage()

print('~inference.py')

# Use GPU if one exists, else use CPU
ctx = mx.gpu()

dtype='float32'
Batch = namedtuple('Batch', ['data'])

mod = None
def model_fn(model_dir):

    logger.info("\nmodel_fn\n")

    sym, arg_params, aux_params = mx.model.load_checkpoint('resnet-152', 0)
    mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None)

    mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))], label_shapes=mod._label_shapes)
    mod.set_params(arg_params, aux_params, allow_missing=True)

    return mod

def transform_fn(mod, img, input_content_type, output_content_type):

    logger.info("\ntransform_fn\n")

    prob_json = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
    return prob_json

    if(input_content_type == 'application/octet-stream'):
        print(input_content_type)
        image_packet.ParseFromString(img)
        img = np.array(Image.open(io.BytesIO(image_packet.image_data)))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = cv2.resize(img, (224, 224))
        img = np.swapaxes(img, 0, 2)
        img = np.swapaxes(img, 1, 2)
        img = img[np.newaxis, :]
        mod.forward(Batch([mx.nd.array(img)]))
        prob = mod.get_outputs()[0].asnumpy().tolist()
        prob_json = json.dumps(prob)
        return prob_json

    print(input_content_type)
    img = np.array(Image.open(io.BytesIO(img)))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (224, 224))
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2)
    img = img[np.newaxis, :]
    mod.forward(Batch([mx.nd.array(img)]))
    prob = mod.get_outputs()[0].asnumpy().tolist()
    prob_json = json.dumps(prob)
    return prob_json


# -*- coding: UTF-8 -*-
from __future__ import print_function

import atexit
import os
import os.path
import math
import sys

from colorama import init, Fore, Style
import copy
init(autoreset=True)

from settings import logger,SELF_ENSEMBLE
from games import keypress
from games.board import Board
from .transition import Transition
import games.boardUtils as bU

class Game(object):
    """
    A 2048 game
    """

    __dirs = {
        keypress.UP:      Board.UP,
        keypress.DOWN:    Board.DOWN,
        keypress.LEFT:    Board.LEFT,
        keypress.RIGHT:   Board.RIGHT,
        keypress.SPACE:   Board.PAUSE,
    }

    __is_windows = os.name == 'nt'

    COLORS = {
        2:    Fore.GREEN,
        4:    Fore.BLUE + Style.BRIGHT,
        8:    Fore.CYAN,
        16:   Fore.RED,
        # Don't use MAGENTA directly; it doesn't display well on Windows.
        # see https://github.com/bfontaine/games/issues/24
        32:  Fore.MAGENTA + Style.BRIGHT,
        64:   Fore.CYAN,
        128:  Fore.BLUE + Style.BRIGHT,
        256:  Fore.MAGENTA + Style.BRIGHT,
        512:  Fore.GREEN,
        1024: Fore.RED,
        2048: Fore.YELLOW,
        # just in case people set an higher goal they still have colors
        4096: Fore.RED,
        8192: Fore.CYAN,
    }

    # see Game#adjustColors
    # these are color replacements for various modes
    __color_modes = {
        'dark': {
            Fore.BLUE: Fore.WHITE,
            Fore.BLUE + Style.BRIGHT: Fore.WHITE,
        },
        'light': {
            Fore.YELLOW: Fore.BLACK,
        },
    }

    SCORES_FILE = '%s/.games.scores' % os.path.expanduser('~')
    STORE_FILE = '%s/.games.store' % os.path.expanduser('~')

    def __init__(self, scores_file=SCORES_FILE, colors=COLORS,
                 store_file=STORE_FILE, clear_screen=True,
                 mode=None, azmode=False, **kws):
        """
        Create a new game.
            scores_file: file to use for the best score (default
                         is ~/.games.scores)
            colors: dictionnary with colors to use for each tile
            store_file: file that stores game session's snapshot
            mode: color mode. This adjust a few colors and can be 'dark' or
                  'light'. See the adjustColors functions for more info.
            other options are passed to the underlying Board object.
        """
        self.board = Board(**kws)
        self.score = 0
        self.scores_file = scores_file
        self.store_file = store_file
        self.clear_screen = clear_screen

        self.__colors = colors
        self.__azmode = azmode

        self.loadBestScore()
        self.adjustColors(mode)

    def init_state(self):
        self.board=Board()
        self.score = 0

    def adjustColors(self, mode='dark'):
        """
        Change a few colors depending on the mode to use. The default mode
        doesn't assume anything and avoid using white & black colors. The dark
        mode use white and avoid dark blue while the light mode use black and
        avoid yellow, to give a few examples.
        """
        rp = Game.__color_modes.get(mode, {})
        for k, color in self.__colors.items():
            self.__colors[k] = rp.get(color, color)

    def loadBestScore(self):
        """
        load local best score from the default file
        """
        try:
            with open(self.scores_file, 'r') as f:
                self.best_score = int(f.readline(), 10)
        except:
            self.best_score = 0
            return False
        return True

    def saveBestScore(self):
        """
        save current best score in the default file
        """
        if self.score > self.best_score:
            self.best_score = self.score
        try:
            with open(self.scores_file, 'w') as f:
                f.write(str(self.best_score))
        except:
            return False
        return True

    def incScore(self, pts):
        """
        update the current score by adding it the specified number of points
        """
        self.score += pts
        if self.score > self.best_score:
            self.best_score = self.score

    def readMove(self):
        """
        read and return a move to pass to a board
        """
        k = keypress.getKey()
        return Game.__dirs.get(k)

    def store(self):
        """
        save the current game session's score and data for further use
        """
        size = self.board.SIZE
        cells = []

        for i in range(size):
            for j in range(size):
                cells.append(str(self.board.getCell(j, i)))

        score_str = "%s\n%d" % (' '.join(cells), self.score)

        try:
            with open(self.store_file, 'w') as f:
                f.write(score_str)
        except:
            return False
        return True

    def restore(self):
        """
        restore the saved game score and data
        """

        size = self.board.SIZE

        try:
            with open(self.store_file, 'r') as f:
                lines = f.readlines()
                score_str = lines[0]
                self.score = int(lines[1])
        except:
            return False

        score_str_list = score_str.split(' ')
        count = 0

        for i in range(size):
            for j in range(size):
                value = score_str_list[count]
                self.board.setCell(j, i, int(value))
                count += 1

        return True

    def clearScreen(self):
        """Clear the console"""
        if self.clear_screen:
            os.system('cls' if self.__is_windows else 'clear')
        else:
            print('\n')

    def hideCursor(self):
        """
        Hide the cursor. Don't forget to call ``showCursor`` to restore
        the normal shell behavior. This is a no-op if ``clear_screen`` is
        falsy.
        """
        if not self.clear_screen:
            return
        if not self.__is_windows:
            sys.stdout.write('\033[?25l')

    def showCursor(self):
        """Show the cursor."""
        if not self.__is_windows:
            sys.stdout.write('\033[?25h')

    def compute_transition(self,state,action):
        """Return transition with after state and reward"""
        after=copy.deepcopy(state)
        rw=after.move(action,add_tile=False)
        tran=Transition(state,action,after,rw)
        return tran

    def get_actions(self,state):
        actions=[]
        for i in range(1,5):
            new_state = copy.deepcopy(state)
            _ = new_state.move(i,add_tile=False)
            if state.cells != new_state.cells :
                actions.append(i)
        return actions

    def get_best_after_state(self,state,vfunction):
        """Return transition leading best after state"""
        bestv= float("-inf")
        besttran=None
        actions=self.get_actions(state)
        for act in actions:
            tran=self.compute_transition(state,act)
            v=tran.reward+vfunction.get_value(tran.after_state.get_features())
            if v>bestv:
                bestv=v
                besttran=tran
        return besttran

    def get_best_value(self,state,vfunction):
        """Return actual reward leading best next after state. To update after state value"""
        bestv= float("-inf")
        actions=self.get_actions(state)
        for act in actions:
            tran=self.compute_transition(state,act)
            v=tran.reward+vfunction.get_value(tran.after_state.get_features())
            if v>bestv:
                bestv=v
        return bestv

    def TD_after_state_learn(self,vfunction,learning_rate):
        """move based on after state value"""

        margins = {'left': 4, 'top': 4, 'bottom': 4}
        # atexit.register(self.showCursor)
        self.init_state()
        # logger.debug('----updated %s'%str(vfunction.tuples()[0].get_weights()[:5]))
        # try:
        #     self.hideCursor()
        while not(self.board.won() or not self.board.canMove()):
            # self.clearScreen()
            # print(self.__str__(margins=margins))
            tran=self.get_best_after_state(self.board,vfunction)
            nexts=copy.deepcopy(tran.after_state)
            nexts.addTile()
            correctv=0
            if not(nexts.won() or not nexts.canMove()):
                correctv+=self.get_best_value(nexts,vfunction)
            vfunction.update(tran.after_state.get_features(),correctv,learning_rate)
            self.board=nexts
            self.incScore(tran.reward)

        # except KeyboardInterrupt:
        #     self.saveBestScore()
        #     exit(-1)

        self.saveBestScore()
        if self.board.won():
            logger.info('You won!')
        # print('You won!' if self.board.won() else 'Game Over')

    def TD_after_state_learn_ensemble(self,vfunction,learning_rate):
        """move based on after state value"""
        margins = {'left': 4, 'top': 4, 'bottom': 4}
        self.init_state()
        while not(self.board.won() or not self.board.canMove()):
            # self.clearScreen()
            # print(self.__str__(margins=margins))
            tran=self.get_best_after_state_ensemble(self.board,vfunction)
            nexts=copy.deepcopy(tran.after_state)
            nexts.addTile()
            correctv=0
            if not(nexts.won() or not nexts.canMove()):
                correctv+=self.get_best_value_ensemble(nexts,vfunction)
            vfunction.update(tran.after_state.get_features(),correctv,learning_rate)
            self.board=nexts
            self.incScore(tran.reward)

        self.saveBestScore()
        if self.board.won():
            logger.info('You won!')

    def get_best_after_state_ensemble(self,state,vfunction):
        """Return transition leading best after state"""
        bestv= float("-inf")
        besttran=None
        actions=self.get_actions(state)
        for act in actions:
            tran=self.compute_transition(state,act)
            v=0
            for i in range(SELF_ENSEMBLE):
                rota_cells=bU.nth_rotate(tran.after_state.cells,i)
                v+=vfunction.get_value(bU.features(rota_cells))
            v/=SELF_ENSEMBLE
            v+=tran.reward
            if v>bestv:
                bestv=v
                besttran=tran
        return besttran

    def get_best_value_ensemble(self,state,vfunction):
        """Return actual reward leading best next after state. To update after state value"""
        bestv= float("-inf")
        actions=self.get_actions(state)
        for act in actions:
            tran=self.compute_transition(state,act)
            v=0
            for i in range(SELF_ENSEMBLE):
                rota_cells=bU.nth_rotate(tran.after_state.cells,i)
                v+=vfunction.get_value(bU.features(rota_cells))
            v/=SELF_ENSEMBLE
            v+=tran.reward
            if v>bestv:
                bestv=v
        return bestv

    def play_after_learn_ensemble(self,vfunction):
        margins = {'left': 4, 'top': 4, 'bottom': 4}
        self.init_state()
        max_tile=0
        while not(self.board.won() or not self.board.canMove()):
            tran=self.get_best_after_state_ensemble(self.board,vfunction)
            nexts=copy.deepcopy(tran.after_state)
            max_tile=max(max_tile,self.get_max_tile(max_tile,nexts))
            nexts.addTile()
            self.board=nexts
            self.incScore(tran.reward)

        if self.board.won():
            logger.info('You won!')
        return self.score,max_tile

    def play_after_learn(self,vfunction):
        margins = {'left': 4, 'top': 4, 'bottom': 4}
        # atexit.register(self.showCursor)
        # logger.debug('----evaluate func %s'%str(vfunction.tuples()[0].get_weights()[:5]))
        self.init_state()
        max_tile=0
        # try:
        #     self.hideCursor()
        while not(self.board.won() or not self.board.canMove()):
            # self.clearScreen()
            # print(self.__str__(margins=margins))
            tran=self.get_best_after_state(self.board,vfunction)
            nexts=copy.deepcopy(tran.after_state)
            max_tile=max(max_tile,self.get_max_tile(max_tile,nexts))
            nexts.addTile()
            self.board=nexts
            self.incScore(tran.reward)
        # except KeyboardInterrupt:
        #     self.saveBestScore()
        #     exit(-1)

        if self.board.won():
            logger.info('You won!')
        return self.score,max_tile

    def get_max_tile(self,max_tile,state):
        maxt=max_tile
        for r in range(state.SIZE):
            for c in range(state.SIZE):
                maxt=max(maxt,state.cells[r][c])
        return maxt

    def loop(self):
        """
        main game loop. returns the final score.
        """
        pause_key = self.board.PAUSE
        margins = {'left': 4, 'top': 4, 'bottom': 4}

        atexit.register(self.showCursor)

        try:
            self.hideCursor()
            while True:
                self.clearScreen()
                print(self.__str__(margins=margins))
                if self.board.won() or not self.board.canMove():
                    break
                m = self.readMove()

                if (m == pause_key):
                    self.saveBestScore()
                    if self.store():
                        print("Game successfully saved. "
                              "Resume it with `games --resume`.")
                        return self.score
                    print("An error ocurred while saving your game.")
                    return

                self.incScore(self.board.move(m))

        except KeyboardInterrupt:
            self.saveBestScore()
            return

        self.saveBestScore()
        print('You won!' if self.board.won() else 'Game Over')
        return self.score

    def getCellStr(self, x, y):  # TODO: refactor regarding issue #11
        """
        return a string representation of the cell located at x,y.
        """
        c = self.board.getCell(x, y)

        if c == 0:
            return '.' if self.__azmode else '  .'

        elif self.__azmode:
            az = {}
            for i in range(1, int(math.log(self.board.goal(), 2))):
                az[2 ** i] = chr(i + 96)

            if c not in az:
                return '?'
            s = az[c]
        elif c == 1024:
            s = ' 1k'
        elif c == 2048:
            s = ' 2k'
        else:
            s = '%3d' % c

        return self.__colors.get(c, Fore.RESET) + s + Style.RESET_ALL

    def boardToString(self, margins=None):
        """
        return a string representation of the current board.
        """
        if margins is None:
            margins={}
        b = self.board
        rg = range(b.size())
        left = ' '*margins.get('left', 0)
        s = '\n'.join(
            [left + ' '.join([self.getCellStr(x, y) for x in rg]) for y in rg])
        return s

    def __str__(self, margins=None):
        if margins is None:
            margins={}
        b = self.boardToString(margins=margins)
        top = '\n'*margins.get('top', 0)
        bottom = '\n'*margins.get('bottom', 0)
        scores = ' \tScore: %5d  Best: %5d\n' % (self.score, self.best_score)
        return top + b.replace('\n', scores, 1) + bottom


# -*- coding: utf-8 -*-
"""
Created on Thu Jun 17 16:11:09 2021

@author: illusory
"""
import torch
from torch.autograd import Variable
import torch.nn as nn
import torchvision
import torch.utils.data as Data
"""
pyper parameter
"""
BATCH_SIZE = 64
LR = 0.001
EPOCH = 1
DOWNLOAD_MNIST = False
"""
导入数据及预处理
"""
train_data = torchvision.datasets.MNIST(
    root='./mnist',
    train=True,
    transform=torchvision.transforms.ToTensor(),
    download=False
)

test_data = torchvision.datasets.MNIST(
    root='./mnist',
    train=False,
    transform=torchvision.transforms.ToTensor()
)
"""
数据预处理
"""
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
test_x = torch.unsqueeze(test_data.data, dim=1)/255.
test_y = test_data.targets
'''
定义模型
'''
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(
                in_channels=1,
                out_channels=16,
                kernel_size=3,
                stride=1,
                padding=1
            ),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(
                in_channels=16,
                out_channels=32,
                kernel_size=3,
                stride=1,
                padding=1
            ),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.prediction = nn.Linear(32*7*7, 10)
        #前向传播过程
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.prediction(x)
        return output

cnn = CNN()
optimizer = torch.optim.Adam(cnn.parameters(), LR)
loss_func = nn.CrossEntropyLoss()
for epoch in range(EPOCH):
    for step, (batch_x, batch_y) in enumerate(train_loader):
        b_x = Variable(batch_x)
        b_y = Variable(batch_y)
        output = cnn(b_x)
        loss = loss_func(output, b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if step % 50 == 0:
            test_output = cnn(test_x)
            pre_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = float((pre_y == test_y).sum()) / float(test_y.size(0))
            print("epoch:", epoch, "| train loss:%.4f" % loss.data, "|test accuracy：%.4f" %accuracy)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 19 10:18:09 2019

@author: bahadir
"""

#from sklearn import datasets

#iris = datasets.load_iris()
#digits= datasets.load_digits()

#from sklearn import svm
#clf = svm.SVC(gamma=0.001, C=100)

# Regression exercise for diabetes dataset

import matplotlib.pyplot as plt
import numpy as np

from sklearn import linear_model
from sklearn import datasets
from sklearn import preprocessing
from sklearn.kernel_ridge import KernelRidge

diabetes = datasets.load_diabetes()
diabetes.data.shape
diabetes.feature_names
diabetes.target

# Separate train and test data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target, test_size=0.25, random_state=0)

# Plot x vs y for some features
plt.figure()
plt.plot(X_train[:,2],y_train,'.')

# Choose the model
###################################################

model = linear_model.LinearRegression()
# Fit the model
model.fit(X_train, y_train)

# Get the model coefficients
model.coef_
model.intercept_

#

# Get the predictions for test data
y_pred = model.predict(X_test)
plt.figure()
plt.plot(y_test, y_pred, '.')
plt.xlabel('True value')
plt.ylabel('Predicted value')

# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()

# Get the mean squared error between the predicted and actual data
np.mean( (y_pred - y_test)**2 )

###################################################
## Ridge (linear least squares with L2 norm regularization) regression
model2 = linear_model.Ridge(alpha=0.1)
model2.fit(X_train, y_train)

# Get the predictions for test data
y_pred = model2.predict(X_test)

plt.figure()
plt.plot(y_test, y_pred, '.')
plt.xlabel('True value')
plt.ylabel('Predicted value')

# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()

# Get the mean squared error between the predicted and actual data
np.mean( (y_pred - y_test)**2 )

###################################################
## Lasso (linear least squares with L1 norm regularization) regression
model3 = linear_model.Lasso(alpha=0.01)
model3.fit(X_train, y_train)

# Get the predictions for test data
y_pred = model3.predict(X_test)

plt.figure()
plt.plot(y_test, y_pred, '.')
plt.xlabel('True value')
plt.ylabel('Predicted value')

# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()

# Get the mean squared error between the predicted and actual data
np.mean( (y_pred - y_test)**2 )

################################################### NONLİNEAR
## Polynomial features
poly = preprocessing.PolynomialFeatures(2,interaction_only=True)
X_train_poly = poly.fit_transform(X_train)

poly = preprocessing.PolynomialFeatures(2,interaction_only=True)
X_test_poly = poly.fit_transform(X_test)

#model_poly = linear_model.LinearRegression()
#model_poly = linear_model.Lasso(alpha=0.06)
model_poly = linear_model.Ridge(alpha=0.4)
# Fit the model
model_poly.fit(X_train_poly, y_train)

y_pred = model_poly.predict(X_test_poly)
plt.figure()
plt.plot(y_test, y_pred, '.')
plt.xlabel('True value')
plt.ylabel('Predicted value')

# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()

# Get the mean squared error between the predicted and actual data
np.mean( (y_pred - y_test)**2 )

################################################### NONLİNEAR
## Kernel ridge regression
#model_krr = KernelRidge(alpha=0.4, kernel='poly', degree=2, gamma=1, coef0=1)
model_krr = KernelRidge(alpha=0.04, kernel='rbf', gamma=0.1)
model_krr.fit(X_train,y_train)
y_pred = model_krr.predict(X_test)
plt.figure()
plt.plot(y_test, y_pred, '.')
# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()
# Get the mean squared error between the predicted and actual data
np.mean( (y_pred - y_test)**2 )


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
from modularodm import Q

from website.app import init_app
from scripts import utils as script_utils
from framework.transactions.context import TokuTransaction

from addons.dataverse.model import AddonDataverseNodeSettings

logger = logging.getLogger(__name__)

def do_migration():
    for node_addon in AddonDataverseNodeSettings.find(Q('foreign_user_settings', 'ne', None)):
        user_addon = node_addon.foreign_user_settings
        if not user_addon.external_accounts:
            logger.warning('User {0} has no dataverse external account'.format(user_addon.owner._id))
            continue
        account = user_addon.external_accounts[0]
        node_addon.set_auth(account, user_addon.owner)
        logger.info('Added external account {0} to node {1}'.format(
            account._id, node_addon.owner._id,
        ))

def main(dry=True):
    init_app(set_backends=True, routes=False)  # Sets the storage backends on all models
    with TokuTransaction():
        do_migration()
        if dry:
            raise Exception('Abort Transaction - Dry Run')

if __name__ == '__main__':
    dry = 'dry' in sys.argv
    if not dry:
        script_utils.add_file_logger(logger, __file__)
    main(dry=dry)


import mock
import unittest
from source import redirect_checker
from source.lib.utils import Config

class MyActiveChildren:
    m = mock.Mock()

    def __init__(self, length):
        self.arr = [self.m] * length

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

    def __iter__(self):
        return iter(self.arr)

class Args:
    pass

class RedirectCheckerTestCase(unittest.TestCase):
    def setUp(self):
        self.logger_temp = redirect_checker.logger
        redirect_checker.logger = mock.Mock()
        pass

    def tearDown(self):
        redirect_checker.logger = self.logger_temp

    @mock.patch('source.redirect_checker.active_children', mock.Mock())
    @mock.patch('source.redirect_checker.utils.check_network_status', mock.Mock(return_value=True))
    @mock.patch('source.redirect_checker.worker', mock.Mock())
    @mock.patch('source.lib.utils.spawn_workers')
    def test_main_loop_iteration_network_access(self, spawn_workers_m):
        config = Config()
        config.CHECK_URL = 'test_url'
        config.HTTP_TIMEOUT = 10
        config.WORKER_POOL_SIZE = 30

        with mock.patch('source.redirect_checker.active_children', lambda: MyActiveChildren(10)):
            redirect_checker.main_loop_iteration(config, 42)

        self.assertEqual(spawn_workers_m.called, True)

    @mock.patch('source.redirect_checker.utils.check_network_status', mock.Mock(return_value=True))
    @mock.patch('source.redirect_checker.worker', mock.Mock())
    @mock.patch('source.lib.utils.spawn_workers')
    def test_main_loop_iteration_no_free_workers(self, spawn_workers_m):
        config = Config()
        config.CHECK_URL = 'test_url'
        config.HTTP_TIMEOUT = 10
        config.WORKER_POOL_SIZE = 10

        with mock.patch('source.redirect_checker.active_children', lambda: MyActiveChildren(10)):
            redirect_checker.main_loop_iteration(config, 42)

        self.assertEqual(spawn_workers_m.called, False)

    @mock.patch('source.redirect_checker.worker', mock.Mock())
    @mock.patch('source.lib.utils.check_network_status', mock.Mock(return_value=False))
    def test_main_loop_iteration_no_network_access(self):
        config = Config()
        config.CHECK_URL = 'test_url'
        config.HTTP_TIMEOUT = 10

        children = mock.Mock()
        children.terminate = mock.Mock()

        length = 10
        active_children = [children] * length

        active_children_fun = mock.Mock(return_value=active_children)

        with mock.patch('source.redirect_checker.active_children', active_children_fun):
            redirect_checker.main_loop_iteration(config, 42)

        self.assertEquals(children.terminate.call_count,
                          length,
                          'Not all active children have been terminated')

    @mock.patch('os.getpid', mock.Mock(return_value=24))
    @mock.patch('source.redirect_checker.run_application', mock.Mock())
    def test_main_loop(self):
        config = mock.Mock()
        config.SLEEP = 42

        def break_run(*args, **kwargs):
            redirect_checker.run_application = False

        with mock.patch('source.redirect_checker.main_loop_iteration', mock.MagicMock()) as main_loop_iter:
            with mock.patch('source.redirect_checker.sleep', mock.Mock(side_effect=break_run)) as main_loop_sleep:
                redirect_checker.main_loop(config)
        self.assertTrue(main_loop_iter.called)
        self.assertEqual(main_loop_iter.call_count, 1)
        main_loop_sleep.assert_called_once_with(config.SLEEP)

    @mock.patch('source.redirect_checker.utils.parse_cmd_args', mock.Mock())
    @mock.patch('source.redirect_checker.main_loop')
    @mock.patch('source.redirect_checker.dictConfig', mock.Mock())
    def test_main(self, main_loop_m):
        config = mock.Mock()
        config.EXIT_CODE = 42

        with mock.patch('source.lib.utils.prepare', mock.Mock(return_value=config)):
            ret_code = redirect_checker.main([1, 2, 3, 4])

        main_loop_m.assert_called_once_with(config)
        self.assertEqual(ret_code, config.EXIT_CODE)


# -*- coding: utf-8 -*-
"""
    The HTTP domain.
"""

import re

from docutils import nodes

from sphinx import addnodes
from sphinx.locale import l_, _
from sphinx.domains import Domain, ObjType
from sphinx.directives import ObjectDescription

http_method_sig_re = re.compile(r'^(GET|POST|PUT|DELETE)?\s?(\S+)(.*)$')

class HTTPMethod(ObjectDescription):

    def handle_signature(self, sig, signode):
        m = http_method_sig_re.match(sig)
        if m is None:
            raise ValueError

        verb, url, query = m.groups()
        if verb is None:
            verb = 'GET'

        signode += addnodes.desc_addname(verb, verb)
        signode += addnodes.desc_name(url, url)

        if query:
            params = query.strip().split()
            for param in params:
                signode += addnodes.desc_optional(param, param)

        return url

class HTTPDomain(Domain):
    """HTTP language domain."""
    name = 'http'
    label = 'HTTP'
    object_types = {
        'method':    ObjType(l_('method'),    'method')
    }
    directives = {
        'method':       HTTPMethod
    }
    roles = {
        # 'method':  RESTXRefRole(),
    }

def setup(app):
    app.add_domain(HTTPDomain)


"""This file is a WolseleyincCA spider created on top of the ATSSpider
scrapy crawl wolseleyinc_ca -a mining_job_id=9999 -a iteration=1 -a extract=1 -a url="https://www.wolseleyinc.ca/careers/postings.htm"

sample url:
    https://www.wolseleyinc.ca/careers/postings.htm
"""

from urlparse import urljoin
from re import compile

from scrapy.http import FormRequest, Request
from scrapy.selector import Selector

from brightcorp.base.atsspiders import ATSSpider
from brightcorp.items import BrightcorpItemLoader
from brightcorp.processors import Prefix, NormalizedJoin, ConvertDateString, RemoveBadElements

class WolseleyincCA(ATSSpider):

    name = 'wolseleyinc_ca'
    num_re = compile(r"(\d+)")
    job_url_anchor = '?action=displayPosting&postingId=%s'

    def parse(self, response):
        sel = Selector(response)
        provinces = sel.xpath('//map[@id="Map"]/area/@onclick').re(self.num_re)
        for province in provinces:
            yield FormRequest(
                response.url, callback=self.parse_jobs_list,
                formdata={
                    'action': 'searchPostings',
                    'provinceId': '%s' % province[0]
                }
                )

    def parse_jobs_list(self, response):
        sel = Selector(response)
        jobs = sel.xpath(
            '//table[@class="fullWidth"]//tr[td[@class="listItem"]]'
            )
        for job in jobs:
            job_id = job.xpath(
                './td[@class="listItem"]/a/@onclick'
                ).re(self.num_re)
            if job_id:
                job_url = '%s%s' % (response.url, self.job_url_anchor % job_id[0])
                meta = {
                    'title': job.xpath('./td[@class="listItem"]/a/text()').extract(),
                    'loc': job.xpath('./td[2]/text()|./td[3]/text()').extract(),
                    'cat': job.xpath('./td[4]/text()').extract(),
                    'date': job.xpath('./td[6]/text()').extract(),
                    'job_id': job_id,
                }
                yield Request(
                    job_url, callback=self.parse_job_callback(), meta=meta
                    )

    def parse_job(self, response):
        loader = BrightcorpItemLoader(response=response)

        loader.add_value('url', response.url)
        loader.add_value('title', response.meta.get('title'))
        loader.add_value('jobcategory', response.meta.get('cat'))
        loader.add_value(
            'location', response.meta.get('loc'), NormalizedJoin(",")
            )
        loader.add_value(
            'date', response.meta.get('date'), ConvertDateString('%b %d, %Y')
            )
        loader.add_value(
            'referencenumber', response.meta.get('job_id'), Prefix('%s-' % self.name)
            )
        loader.add_xpath(
            'description', '//div[div[contains(text(), "Description:")]]',
            RemoveBadElements(['a'])
            )

        yield loader.load_item()