from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
from PIL.Image import open

class LTexture:
	mTextureID = 0
	mTextureWidth = 0
	mTextureHeight = 0
	def __init__(self):
		#Inicializando ID da textura
		self.mTextureID = 0
		#Inicializando dimensões da textura
		self.mTextureWidth = 0
		self.mTextureHeight = 0

	@staticmethod
	def freeTexture(self):
		#Deletando textura
		if(self.mTextureID != 0):
			glDeleteTextures(1,self.mTextureID)
			self.mTextureID = 0
		mTextureWidth = 0
		mTextureHeight = 0

	def __del__(self):
		#Limpa dados da textura se preciso
		self.freeTexture(self)

	def loadTextureFromPixels32(self,pixels,width,height):
		#Limpa textura se existir
		self.freeTexture(self)

		#Obtém as dimensões da textura
		self.mTextureWidth = width
		self.mTextureHeight = height

		#Gera textura ID
		glGenTextures(1,self.mTextureID)

		#Define a ID da textura
		self.mTextureID = 1

		#Cria textura ID
		glBindTexture(GL_TEXTURE_2D,self.mTextureID)
		glPixelStorei(GL_UNPACK_ALIGNMENT,1)

		#Gera textura
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,pixels)

		#Definindo parâmetros da textura
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

		#Liberando textura
		glBindTexture(GL_TEXTURE_2D,0)

		#Procurando erros
		erro = glGetError()
		if(erro != GL_NO_ERROR):
			print("Erro ao carregar textura de %p pixels! %s\n",pixels,gluErrorString(erro))
			return False
		return True

	def loadTextureFromFile(self,imagem):
		textureLoaded = False

		#Gerando e Definindo uma imagem atual
		im = open(imagem).convert("RGBA")
		if(im != None):
			#Criando a textura a partir dos pixels do arquivo
			imagem = im.tobytes("raw","RGBA",0,-1)
			textureLoaded = self.loadTextureFromPixels32(imagem,im.size[0],im.size[1])
		im.close()
		if(textureLoaded == False):
			print("Não foi possível carregar a imagem!")
		return textureLoaded

	def render(self,x,y):
		#Se a textura existel
		if(self.mTextureID != 0):
			#Remove quaisquer transformações anteriores
			glLoadIdentity()

			#Movendo para o ponto de renderização
			glTranslatef(x,y,0)

			#Definindo textura ID
			glBindTexture(GL_TEXTURE_2D,self.mTextureID)

			#Renderizando quadrados texturizados
			glBegin(GL_QUADS)
			glTexCoord2f(0,0)
			glVertex2f(0,self.mTextureHeight)
			glTexCoord2f(1,0)
			glVertex2f(self.mTextureWidth,self.mTextureHeight)
			glTexCoord2f(1,1)
			glVertex2f(self.mTextureWidth,0)
			glTexCoord2f(0,1)
			glVertex2f(0,0)
			glEnd()

	def getTextureID(self):
		return self.mTextureID

	def textureWidth(self):
		return self.mTextureWidth

	def textureHeight(self):
		return self.mTextureHeight


import os
from pymongo import MongoClient

def connect_db(
        db_name=os.getenv('DATABASE_NAME', 'hyperion-supervisor'),
        host=os.getenv('MONGO_PORT_27017_TCP_ADDR', 'localhost'),
        port=int(os.getenv('MONGO_PORT_27017_TCP_PORT', 27017))):
    client = MongoClient(host, port)
    db = client[db_name]
    return db

db = connect_db()


import numpy as np
from milk.supervised.precluster_learner import precluster_learner
from milk.tests.fast_classifier import fast_classifier

def test_precluster():
    np.random.seed(22)
    learner = precluster_learner([2], base=fast_classifier(), R=12)

    def c0():
        return np.random.rand(8)
    def c1():
        return c0()+2.*np.ones(8)

    features = []
    labels =[]
    for i in xrange(200):
        f = []
        for j in xrange(40):
            use_0 = (i < 100 and j < 30) or (i >= 100 and j >= 30)
            if use_0: f.append(c0())
            else: f.append(c1())
        labels.append((i < 100))
        features.append(f)
    model = learner.train(features,labels)

    assert model.apply([c0() for i in xrange(35)])
    assert not model.apply([c1() for i in xrange(35)])


#!/usr/bin/env python -W ignore

import os, sys, datetime, time

import numpy as np

from sklearn.feature_extraction import DictVectorizer

TAB = "\t"

'''
user_id
card_type
signin_count
payment_amount
resume_count
payment_count
cancel_first
first_payment_amount
first_payment_at
chargeback_at
cancelled_at
zipcode_hawaiian
zipcode_multi_race
zipcode_other_race
zipcode_black
zipcode_white
zipcode_indian
zipcode_asian
zipcode_hispanic
zipcode_non_hispanic
zipcode_poverty
zipcode_avg_rent
zipcode_family_size
zipcode_hh_avg_income
chargeback_file
chargeback_or_not
'''
def load_analysis_raw(filepath, date_split=None, skip_header=True, setting={}, ab_testing=False):
    # if ab_testing is passed as True,
    # skip certain portion of users in the prediction as control group in comparison with experiment group
    is_timing = setting.get("is_timing", False)
    is_zipcode = setting.get("is_zipcode", False)
    is_lifetime = setting.get("is_lifetime", False)
    is_onet = setting.get("is_onet", False)

    date_split = datetime.datetime.strptime(date_split, "%Y%m%d") if date_split else None

    dataset, answerset, personal_dataset = [], np.array([], dtype=np.int64), []
    prediction_dataset, prediction_answerset, prediction_personal_dataset = [], np.array([], dtype=np.int64), []

    def format_number(n):
        return float(n) if n.lower() != "none" else 0

    def get_answerset(dims, true_idx):
        results = np.array(np.zeros(shape=dims), dtype=np.int32)
        results[true_idx] = 1

        return results

    with open(filepath, "rb") as INPUT:
        for line in INPUT:
            if skip_header:
                skip_header = False
                continue
            else:
                user_id, card_type, signin_count, payment_amount, resume_count, payment_count, cancel_first, first_payment_amount, first_payment_at, chargeback_at, lifetime, \
                zipcode_hawaiian, zipcode_multi_race, zipcode_other_race, zipcode_black, zipcode_white, zipcode_indian, zipcode_asian, zipcode_hispanic, zipcode_non_hispanic, zipcode_poverty, zipcode_avg_rent, zipcode_family_size, zipcode_hh_avg_income, \
                onet_transpertation, onet_production, onet_installation, onet_eduation, onet_computer, onet_architecture, onet_arts, onet_construction, onet_business, onet_community, onet_budiling, onet_healthcare, onet_life, onet_food, onet_farming, onet_support, onet_legal, onet_office, onet_sales, onet_protective, onet_personal, onet_military, onet_management, \
                chargeback_file, chargeback_or_not = line.strip().split(TAB)

                if ab_testing and control_or_experiment(user_id):  # skip control group
                    continue

                resume_count = int(resume_count) if resume_count != "[]" else 0
                payment_count = int(payment_count)
                first_payment_amount = int(first_payment_amount)

                origin_card_type = card_type
                card_type = "master" if card_type in ["mastercard"] else card_type
                card_type = "None" if card_type in ["diners_club", "maestro"] else card_type

                cancel_first = 1 if cancel_first == "True" else 0
                first_payment_at = datetime.datetime.strptime(first_payment_at, "%Y-%m-%d")

                if setting:
                    first_payment_at_start = setting.get("first_payment_at_start", None)
                    first_payment_at_end = setting.get("first_payment_at_end", None)

                    if first_payment_at_start != None and first_payment_at < first_payment_at_start:
                        continue
                    if first_payment_at_end != None and first_payment_at > first_payment_at_end:
                        continue

                zipcode_hh_avg_income = format_number(zipcode_hh_avg_income)

                chargeback_or_not = 1 if chargeback_or_not.lower() == "y" else 0

                data = {"first_payment_amount": first_payment_amount,
                        "first_payment_weekday": first_payment_at.weekday(),
                        "resume_count": int(resume_count),
                        "card_type": card_type,
                        "cancel_first": cancel_first,
                        }

                if is_timing:
                    data["signin_count"] = int(signin_count) if signin_count.lower() != "none" else 0
                    data["payment_count"] = payment_count
                    data["payment_amount"] = float(payment_amount)

                if is_onet:
                    data["onet_transpertation"] = float(onet_transpertation)
                    data["onet_production"] = float(onet_production)
                    data["onet_installation"] = float(onet_installation)
                    data["onet_eduation"] = float(onet_eduation)
                    data["onet_computer"] = float(onet_computer)
                    data["onet_architecture"] = float(onet_architecture)
                    data["onet_arts"] = float(onet_arts)
                    data["onet_construction"] = float(onet_construction)
                    data["onet_business"] = float(onet_business)
                    data["onet_community"] = float(onet_community)
                    data["onet_budiling"] = float(onet_budiling)
                    data["onet_healthcare"] = float(onet_healthcare)
                    data["onet_life"] = float(onet_life)
                    data["onet_food"] = float(onet_food)
                    data["onet_farming"] = float(onet_farming)
                    data["onet_support"] = float(onet_support)
                    data["onet_legal"] = float(onet_legal)
                    data["onet_office"] = float(onet_office)
                    data["onet_sales"] = float(onet_sales)
                    data["onet_protective"] = float(onet_protective)
                    data["onet_personal"] = float(onet_personal)
                    data["onet_military"] = float(onet_military)
                    data["onet_management"] = float(onet_management)

                    if setting.get("is_category_onet", False):
                        possible_occupation, max_value = None, -1
                        for key, value in data.items():
                            if key.find("onet_") == 0:
                                if value > max_value:
                                    possible_occupation, max_value = key, value

                        for key in data.keys():
                            if key.find("onet_") == 0:
                                del data[key]

                        data["occupation"] = possible_occupation.split("_")[1]

                        if setting.get("target_occupation", []) and data["occupation"] not in setting["target_occupation"]:
                            continue
                        else:
                            del data["occupation"]

                if is_lifetime:
                    data["lifetime"] = int(lifetime) if lifetime != "None" else -sys.maxint

                if is_zipcode:
                    data["zipcode_hawaiian"] = float(zipcode_hawaiian) if zipcode_hawaiian.lower() != "none" else 0
                    data["zipcode_multi_race"] = float(zipcode_multi_race) if zipcode_multi_race.lower() != "none" else 0
                    data["zipcode_other_race"] = float(zipcode_other_race) if zipcode_other_race.lower() != "none" else 0
                    data["zipcode_black"] = float(zipcode_black) if zipcode_black.lower() != "none" else 0
                    data["zipcode_white"] = float(zipcode_white) if zipcode_white.lower() != "none" else 0
                    data["zipcode_indian"] = float(zipcode_indian) if zipcode_indian.lower() != "none" else 0
                    data["zipcode_asian"] = float(zipcode_asian) if zipcode_asian.lower() != "none" else 0
                    data["zipcode_hispanic"] = float(zipcode_hispanic) if zipcode_hispanic.lower() != "none" else 0
                    data["zipcode_non_hispanic"] = float(zipcode_non_hispanic) if zipcode_non_hispanic.lower() != "none" else 0
                    data["zipcode_poverty"] = float(zipcode_poverty) if zipcode_poverty.lower() != "none" else 0
                    data["zipcode_avg_rent"] = float(zipcode_avg_rent) if zipcode_avg_rent.lower() != "none" else 0
                    data["zipcode_family_size"] = float(zipcode_family_size) if zipcode_family_size.lower() != "none" else 0
                    data["zipcode_hh_avg_income"] = zipcode_hh_avg_income

                if date_split == None or first_payment_at < date_split:
                    dataset.append(data)

                    if answerset.size == 0:
                        answerset = np.array([get_answerset(2, chargeback_or_not)])
                    else:
                        answerset = np.vstack((answerset, get_answerset(2, chargeback_or_not)))

                    personal_dataset.append({user_id: {"file": chargeback_file, "card_type": origin_card_type}})
                else:
                    prediction_dataset.append(data)
                    if prediction_answerset.size == 0:
                        prediction_answerset = np.array([get_answerset(2, chargeback_or_not)])
                    else:
                        prediction_answerset = np.vstack((prediction_answerset, get_answerset(2, chargeback_or_not)))

                    prediction_personal_dataset.append({user_id: {"file": chargeback_file, "card_type": origin_card_type}})

    vec = DictVectorizer()
    transformed_dataset = vec.fit_transform(dataset).toarray() if dataset else vec.fit_transform(prediction_dataset).toarray()
    transformed_title = vec.get_feature_names()

    transformed_prediction_dataset = None
    if date_split:
        transformed_prediction_dataset = None
        for idx, prediction_data in enumerate(prediction_dataset):
            t = []
            for transformed_t in transformed_title:
                if transformed_t.find("=") == -1:
                    t.append(float(prediction_data[transformed_t]))
                else:
                    key, value = transformed_t.split("=", 1)
                    if prediction_data[key] == value:
                        t.append(float(1))
                    else:
                        t.append(float(0))

            if transformed_prediction_dataset == None:
                transformed_prediction_dataset = np.array([t])
            else:
                transformed_prediction_dataset = np.vstack((transformed_prediction_dataset, t))

    return transformed_title, personal_dataset, (transformed_dataset, answerset), (transformed_prediction_dataset, prediction_answerset, prediction_personal_dataset)


from aiohttp import web
import json
import time
import os

async def get_stocks_list(request):
    f = open('stocks.txt', 'rb')
    content = f.read().decode('utf-8')
    f.close()
    rets=[]
    for line in content.split('\r\n'):
        if line!='':
            rets.append(line.split(',')[0])
    return wrap_response(json.dumps(rets))

def main():
    app = web.Application()
    app.router.add_route('GET', '/stocks', get_stocks_list)
    web.run_app(app, port=8086)

def reverse(a):
    a = a.reverse()
    print(a)

def wrap_response(text, status=200):
    return web.Response(body=text.encode('utf-8'), content_type='application/json', charset='utf-8', status=status)

def wrap_text(text, status=200):
    return web.Response(body=text.encode('utf-8'), content_type='text/plain', charset='utf-8', status=status)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
from twocaptchaapi import TwoCaptchaApi
import urllib
import threading
import os
import timeit
from timeit import Timer
import uuid
api = TwoCaptchaApi('20155321b96e31b09e17119e39218b37')

i = 0

def getI():
 global i
 return i

def incrementI():
  global i
  i = i + 1

def batchProcess():
 j=uuid.uuid1()
 newfile=urllib.URLopener()
 newfile.retrieve("http://services.ecourts.gov.in/ecourtindia_v5/securimage/securimage_show.php",str(j)+".png")
 with open(str(j)+".png", 'rb') as captcha_file:
  captcha = api.solve(captcha_file)
  filename=captcha.await_result()+".png"
  os.rename(str(j)+".png",filename)
  print(filename)

def main():
 for milo  in range(1,40):
    threading.Thread(target=batchProcess).start()

main()


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('agenda', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='event',
            name='image',
            field=models.ImageField(upload_to=b'media/%Y/%m/%d/', blank=True),
        ),
        migrations.AddField(
            model_name='event',
            name='image_title',
            field=models.CharField(unique=True, max_length=20, blank=True),
        ),
        migrations.AlterField(
            model_name='event',
            name='date_event',
            field=models.DateTimeField(verbose_name=b'Event Date'),
        ),
    ]


def bissextile(annee):
	"""Ecrit si l'annee est bissextile et renvoie un booléen"""

	if annee % 400 == 0 or (annee % 4 == 0 and annee % 100 != 0):
		print(annee, "est bissextile.")
		return True
	else:
		print(annee, "n'est pas bissextile.")
		return False

if __name__ == '__main__':
	i = 2000
	while i <2020:
		bissextile(i)
		i-=-1


import argparse
import time
import datetime
import pandas as pd
import math
import torch
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
from torch.optim import Adam, ASGD, SGD
import data
from model import RNNModel
import json

# def run():
parser = argparse.ArgumentParser(description='PyTorch PennTreeBank RNN/LSTM Language Model')
parser.add_argument('--data', type=str, default='./data/penn',
                    help='location of the data corpus')
parser.add_argument('--model', type=str, default='LSTM',
                    help='type of recurrent net (RNN_TANH, RNN_RELU, LSTM, GRU)')
parser.add_argument('--emsize', type=int, default=200,
                    help='size of word embeddings')
parser.add_argument('--nhid', type=int, default=200,
                    help='humber of hidden units per layer')
parser.add_argument('--nlayers', type=int, default=2,
                    help='number of layers')
parser.add_argument('--lr', type=float, default=20,
                    help='initial learning rate')
parser.add_argument('--clip', type=float, default=0.5,
                    help='gradient clipping')
parser.add_argument('--epochs', type=int, default=10,
                    help='upper epoch limit')
parser.add_argument('--batch-size', type=int, default=20, metavar='N',
                    help='batch size')
parser.add_argument('--bptt', type=int, default=20,
                    help='sequence length')
parser.add_argument('--seed', type=int, default=1111,
                    help='random seed')
parser.add_argument('--cuda', type=bool, default=False,
                    help='use CUDA')
parser.add_argument('--log-interval', type=int, default=200, metavar='N',
                    help='report interval')
parser.add_argument('--save', type=str,  default='model_Adam_.pt',
                    help='path to save the final model')
parser.add_argument('--dropout', type=float, default=0.2,
                    help='dropout applied to layers (0 = no dropout)')
parser.add_argument('--tied', type=bool, default=True,
                    help='tie the word embedding and softmax weights')
parser.add_argument('--brown', action='store_true',
                    help='use the brown corpus from nltk')
args = parser.parse_args()

# Set the random seed manually for reproducibility.
torch.manual_seed(args.seed)

###############################################################################
# Load data
###############################################################################

# print("args.data", args.data)
# print("args.model", args.model)
# print("args.emsize", args.emsize)
# print("args.nhid", args.nhid)
# print("args.nlayers", args.nlayers)
# print("args.lr", args.lr)
# print("args.clip", args.clip)
# print("args.epochs", args.epochs)
# print("args.batch_size", args.batch_size)
# print("args.bptt", args.bptt)
# print("args.seed", args.seed)
# print("args.cuda", args.cuda)
# print("args.log_interval", args.log_interval)
# print("args.save", args.save)
# print("args.dropout", args.dropout)
# print("args.tied", args.tied)

corpus = data.Corpus(args.data, args.brown)

def batchify(data, bsz):
    nbatch = data.size(0) // bsz
    data = data.narrow(0, 0, nbatch * bsz)
    data = data.view(bsz, -1).t().contiguous()
    if args.cuda:
        data = data.cuda()
    return data

eval_batch_size = 10
print("Batchifying data...")
train_data = batchify(corpus.train, args.batch_size)
print(train_data.size())
val_data = batchify(corpus.valid, eval_batch_size)
test_data = batchify(corpus.test, eval_batch_size)

###############################################################################
# Build the model
###############################################################################

ntokens = len(corpus.dictionary)
print("Instantiating Model...")
model = RNNModel(args.model, ntokens, args.emsize, args.nhid, args.nlayers, args.dropout, args.tied)
if args.cuda:
    model.cuda()

criterion = nn.CrossEntropyLoss()

###############################################################################
# Training code
###############################################################################

# def clip_gradient(model, clip):
#     """Computes a gradient clipping coefficient based on gradient norm."""
#     totalnorm = 0
#     for p in model.parameters():
#         modulenorm = p.grad.data.norm()
#         totalnorm += modulenorm ** 2
#     totalnorm = math.sqrt(totalnorm)
#     return min(1, args.clip / (totalnorm + 1e-6))

def clip_grad_norm(parameters, max_norm, norm_type=2):
    """Clips gradient norm of an iterable of parameters.

    The norm is computed over all gradients together, as if they were
    concatenated into a single vector. Gradients are modified in-place.

    Arguments:
        parameters (Iterable[Variable]): an iterable of Variables that will have
            gradients normalized
        max_norm (float or int): max norm of the gradients
        norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for infinity norm.
    """
    parameters = list(parameters)
    max_norm = float(max_norm)
    norm_type = float(norm_type)
    if norm_type == float('inf'):
        total_norm = max(p.grad.data.abs().max() for p in parameters)
    else:
        total_norm = 0
        for p in parameters:
            param_norm = p.grad.data.norm(norm_type)
            total_norm += param_norm ** norm_type
        total_norm = total_norm ** (1. / norm_type)
    clip_coef = max_norm / (total_norm + 1e-6)
    if clip_coef >= 1:
        return
    for p in parameters:
        p.grad.data.mul_(clip_coef)

def repackage_hidden(h):
    """Wraps hidden states in new Variables, to detach them from their history."""
    if type(h) == Variable:
        return Variable(h.data)
    else:
        return tuple(repackage_hidden(v) for v in h)

def get_batch(source, i, evaluation=False):
    seq_len = min(args.bptt, len(source) - 1 - i)
    data = Variable(source[i:i+seq_len], volatile=evaluation)
    target = Variable(source[i+1:i+1+seq_len].view(-1))
    return data, target

def evaluate(data_source):
    total_loss = 0
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(eval_batch_size)
    for i in range(0, data_source.size(0) - 1, args.bptt):
        data, targets = get_batch(data_source, i, evaluation=True)
        output, hidden = model(data, hidden)
        output_flat = output.view(-1, ntokens)
        total_loss += len(data) * criterion(output_flat, targets).data
        hidden = repackage_hidden(hidden)
    return total_loss[0] / len(data_source)

train_results = {"type": [], "epoch": [], "batch": [], "lr": [],
                 "time": [], "loss": [], "ppl": []}

def train():
    total_loss = 0
    start_time = time.time()
    cur_loss = np.Inf
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(args.batch_size)
    optimizer = SGD(model.parameters(), lr=lr)
    for batch, i in enumerate(range(0, len(train_data) - 1, args.bptt)):
        # zero grad
        optimizer.zero_grad()
        # model.zero_grad()
        # get data
        data, targets = get_batch(train_data, i)
        hidden = repackage_hidden(hidden)
        # get outputs from models
        output, hidden = model(data, hidden)
        loss = criterion(output.view(-1, ntokens), targets)
        loss.backward()

        if cur_loss == np.Inf:
            cur_loss = loss.data[0]
        else:
            cur_loss = 0.9 * cur_loss + 0.1 * loss.data[0]
        clip_grad_norm(model.parameters(), args.clip)

        # clipped_lr = lr * clip_gradient(model, args.clip)
        # for p in model.parameters():
        #     p.data.add_(-clipped_lr, p.grad.data)

        # take step
        optimizer.step()

        total_loss += loss.data

        if batch % args.log_interval == 0 and batch > 0:
            cur_loss = total_loss[0] / args.log_interval
            elapsed = time.time() - start_time
            print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} |'
                  'loss {:5.2f} | ppl {:8.2f}'.format(
                epoch, batch, len(train_data) // args.bptt, lr,
                elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
            train_results["epoch"].append(epoch)
            train_results["batch"].append(batch)
            train_results["lr"].append(lr)
            train_results["time"].append(elapsed * 1000 / args.log_interval)
            train_results["loss"].append(cur_loss)
            train_results["ppl"].append(math.exp(cur_loss))
            train_results["type"].append("train")
            total_loss = 0
            start_time = time.time()
    opt_name = str(optimizer.__class__).split(".")[2]
    return opt_name

# Loop over epochs.
lr = args.lr
prev_val_loss = None
for epoch in range(1, args.epochs+1):
    epoch_start_time = time.time()
    opt_name = train()
    val_loss = evaluate(val_data)
    print('-' * 89)
    print('| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | '
            'valid ppl {:8.2f}'.format(epoch, (time.time() - epoch_start_time),
                                       val_loss, math.exp(val_loss)))
    print('-' * 89)
    train_results["epoch"].append(epoch)
    train_results["batch"].append("Nan")
    train_results["lr"].append("Nan")
    train_results["time"].append(time.time() - epoch_start_time)
    train_results["loss"].append(val_loss)
    train_results["ppl"].append(math.exp(val_loss))
    train_results["type"].append("val")
    # Anneal the learning rate.
    if prev_val_loss and val_loss > prev_val_loss:
        lr /= 3.0
    prev_val_loss = val_loss

train_ = pd.DataFrame(train_results)

# Run on test data and save the model.
test_loss = evaluate(test_data)
print('=' * 89)
print('| End of training | test loss {:5.2f} | test ppl {:8.2f}'.format(
    test_loss, math.exp(test_loss)))
print('=' * 89)
with open("word_emb_Adam.json", 'wb') as outfile:
    json.dump(model.encoder.weight.data.cpu().numpy().tolist(), outfile)
train_results["epoch"].append("Nan")
train_results["batch"].append("Nan")
train_results["lr"].append("Nan")
train_results["time"].append("Nan")
train_results["loss"].append(test_loss)
train_results["ppl"].append(math.exp(test_loss))
train_results["type"].append("test")
df = pd.DataFrame(train_results)
today = "_".join(str(datetime.date.today()).split("-"))
file_name = opt_name + "_drp" + str(args.dropout) + "_lyr" + str(args.nlayers) + "_" + args.model + "_" + str(args.emsize) + "_" + str(args.nhid) + "_" + str(args.nlayers) + "_" + today + "penn_results.csv"
df.to_csv(file_name)
if args.save != '':
    with open(args.save, 'wb') as f:
        torch.save(model, f)


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

import sys
import urllib

from lxml import etree
from handhelper import ehsyparse
from httphelper import ehsyHtml

reload(sys)
sys.setdefaultencoding("utf8")

class ehsy(object):

    """
    负责执行流程的控制
    """

    def ehsyGoods(self,keywords):

        keywords = str(keywords)
        keys = urllib.quote(keywords)
        url = "%s%s" % ("http://www.ehsy.com/search?k=", keys)

        xy = ehsyHtml.ehsyHtml()
        html = xy.ehstHtml(url)

        html = etree.HTML(html)

        xyparse = ehsyparse.ehsyparse()
        xyparse.getGoods_html(html)

e =ehsy()
e.ehsyGoods("扳手")


#!/usr/bin/env python

"""
    An OSX tool to automate downloads from showRSS
"""

from datetime import datetime
from dateutil import parser
from os import path, stat, utime
from pytz import utc
from subprocess import call
from urllib import urlencode
from urllib2 import build_opener
from xml.dom import minidom
from time import mktime

CONFIGURATION = {
    # Show RSS user id
    'user_id': USER_ID,

    # 'null'  Per show settings,
    # 0       Only standard torrents,
    # 1       Only 720p HD
    # 2       Both types of torrents
    'hd': 'null',

    # 'null'  Per show settings,
    # 0       Skip proper/repack
    # 1       Include proper/repack
    'proper': 'null',

    # Uncomment this line to use torrent files instead of the magnet protocol
    # 'magnets': 'false',

    # For reference
    # 'namespaces': 'false',
    # 'raw': 'false',
}

STAMP_FILE = path.join(path.expanduser("~"), '.show-rss')
RSS_FEED_URL = 'http://showrss.info/rss.php?' + urlencode(CONFIGURATION)

def update_timestamp(date=None):
    with open(STAMP_FILE, 'a'):
        if date is None:
            utime(STAMP_FILE, None)
        else:
            date = mktime(date.timetuple())
            utime(STAMP_FILE, (date , date) )

def get_timestamp():
    if not path.exists(STAMP_FILE):
        return None
    modtime = stat(STAMP_FILE).st_mtime
    odate = datetime.fromtimestamp(modtime)
    ldate = utc.localize(odate)
    return ldate, odate

def download_feed(url):
    opener = build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    return opener.open(url, timeout=10).read()

def parse_dates_links(feed):
    xml = minidom.parseString(feed)

    links = [z.firstChild.nodeValue
             for z in xml.getElementsByTagName('link')[1:]]

    dates = [parser.parse(z.firstChild.nodeValue)
             for z in xml.getElementsByTagName('pubDate')]

    assert(len(links) == len(dates))
    return zip(dates, links)

if __name__ == '__main__':
    timestamp = get_timestamp()
    latest = None
    odate = None
    if timestamp:
        ldate, odate = timestamp
        #print "L: ", ldate, " O: ", odate
    feed = download_feed(RSS_FEED_URL)

    for date, link in parse_dates_links(feed):
        if timestamp is None or date > ldate:
           call(['open', '-g', link])
           if latest is None or date > latest:
                latest = date
    if (latest is None) and (odate is not None):
        latest = odate
    update_timestamp(latest)


from django.db import models
from django.utils.translation import ugettext_lazy as _
from chaolife.models import Order
from model_utils.fields import MonitorField
from datetime import timedelta,datetime
from chaolifeProject.settings import AUTH_USER_MODEL
from .refunds import OrderRefund
from moneyed import Money
from account.models import BillHistory
class OrderBill(models.Model):

    SETTLEMENT_IMMEDIATELY = 1 # 立即结算
    SETTLEMENT_DELAY = 2# 延迟结算
    SETTLEMENT_CHOICE = (
        (SETTLEMENT_IMMEDIATELY,'立即结算'),
        (SETTLEMENT_DELAY,'延迟结算'),
    )

    order = models.OneToOneField(Order)
    seller = models.ForeignKey(AUTH_USER_MODEL,null=True)
    create_at = models.DateTimeField(auto_now_add=True,verbose_name=_('订单创建时间'))
    commission = models.FloatField(verbose_name=_('佣金(积分)'),default=0)
    order_amount = models.FloatField(verbose_name=_('订单总额'))
    refund_amount = models.FloatField(verbose_name=_('退单金额'),help_text='退单金额，可能是用户取消订单，退还的金额')
    capital_settlement = models.IntegerField( choices=SETTLEMENT_CHOICE,verbose_name=_('商家结算方式'),help_text='资金结算方式，有包括立即结算，和延时结算')
    delay_settlement_time = models.DateTimeField(verbose_name=_('需要结算的日期'),null=True)
    seller_gains = models.FloatField(verbose_name=_('卖家收益积分'),help_text='在产生积分赔付的情况下，该值可能为负')
    settled  = models.BooleanField(default=False,verbose_name=_('已结算?'),help_text='是否已结算')
    settled_at = MonitorField(monitor='settled',when=[True,])

    class Meta:
        verbose_name = "订单结算"
        verbose_name_plural = "订单结算"

    def __str__(self):
        return '订单'+self.order_id

    @classmethod
    def create_for_roomOrder_cancel(cls,roomOrder,refund_amount,):
        order_amount = roomOrder.amount
        assert refund_amount < order_amount
        can_gains_points = order_amount - refund_amount
        sell_gains = can_gains_points * 0.75
        commission = can_gains_points * 0.25
        orderbill = cls(
            order=roomOrder,
            seller=roomOrder.seller,
            commission=commission,
            order_amount=order_amount,
            refund_amount=refund_amount,
            capital_settlement=cls.SETTLEMENT_IMMEDIATELY,
            delay_settlement_time = datetime.now(),
            seller_gains=sell_gains,
            settled=True
        )
        if orderbill.capital_settlement == cls.SETTLEMENT_IMMEDIATELY:
            customer = roomOrder.customer
            seller = roomOrder.seller
            seller.add_partner_points(can_gains_points)
            customer.add_customer_points(refund_amount)
        return orderbill

    @classmethod
    def create_for_perfect_room_sell(cls,hotelPackageOrder):
        orderbill = cls(
            order=hotelPackageOrder,
            seller=hotelPackageOrder.seller,
            commission=hotelPackageOrder.amount * 0.25,
            order_amount=hotelPackageOrder.amount,
            refund_amount=0,
            capital_settlement=cls.SETTLEMENT_DELAY,
            seller_gains=hotelPackageOrder.amount * 0.75,
            delay_settlement_time=hotelPackageOrder.checkout_time + timedelta(days=2),
            settled=False
        )
        return orderbill

    @classmethod
    def create_for_refund_order(cls,orderRefund):
        order = orderRefund.order
        seller_gains = -orderRefund.losses_money * 10
        orderBill = cls(
            order=order,
            seller=order.seller,
            order_amount=order.amount,
            refund_amount=orderRefund.refund_points,
            capital_settlement=cls.SETTLEMENT_IMMEDIATELY,
            commission=0,
            seller_gains =  seller_gains,
            settled = False
        )
        orderBill.save()
        BillHistory.createForRefundOrderBill(orderBill)


import argparse
from itertools import count

import gym
import gym.spaces
import scipy.optimize
import numpy as np
import math

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from models import *
from replay_memory import Memory
from running_state import ZFilter
from torch.autograd import Variable
from trpo import trpo_step
from utils import *
from loss import *

import matplotlib.pyplot as plt

import reacher
import ant
import swimmer
import driving
import panda_custom

import pickle

import time
import glob

import pdb

import IPython

import sys

torch.utils.backcompat.broadcast_warning.enabled = True
torch.utils.backcompat.keepdim_warning.enabled = True

torch.set_default_tensor_type('torch.DoubleTensor')
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

parser = argparse.ArgumentParser(description='PyTorch actor-critic example')
parser.add_argument('--delta-s', type=float, default=None, help='parameter delta s')
parser.add_argument('--sigma', type=float, default=50., help='parameter sigma')
parser.add_argument('--gamma', type=float, default=0.995, metavar='G',
                    help='discount factor (default: 0.995)')
parser.add_argument('--env', type=str, default="Reacher-v1", metavar='G',
                    help='name of the environment to run')
parser.add_argument('--tau', type=float, default=0.97, metavar='G',
                    help='gae (default: 0.97)')
parser.add_argument('--l2-reg', type=float, default=1e-3, metavar='G',
                    help='l2 regularization regression (default: 1e-3)')
parser.add_argument('--max-kl', type=float, default=1e-2, metavar='G',
                    help='max kl value (default: 1e-2)')
parser.add_argument('--damping', type=float, default=1e-1, metavar='G',
                    help='damping (default: 1e-1)')
parser.add_argument('--seed', type=int, default=1111, metavar='N',
                    help='random seed (default: 1111')
parser.add_argument('--test_seed', type=int, default=2333, metavar='N',
                    help='test env random seed (default: 2333')
parser.add_argument('--batch-size', type=int, default=5000, metavar='N',
                    help='size of a single batch')
parser.add_argument('--log-interval', type=int, default=1, metavar='N',
                    help='interval between training status logs (default: 10)')
parser.add_argument('--eval-interval', type=int, default=1, metavar='N',
                    help='interval between training status logs (default: 10)')
parser.add_argument('--fname', type=str, default='expert', metavar='F',
                    help='the file name to save trajectory')
parser.add_argument('--num-epochs', type=int, default=500, metavar='N',
                    help='number of epochs to train an expert')
parser.add_argument('--hidden-dim', type=int, default=100, metavar='H',
                    help='the size of hidden layers')
parser.add_argument('--lr', type=float, default=1e-3, metavar='L',
                    help='learning rate')
parser.add_argument('--optimality', action='store_true',
                    help='consider optimality or not')
parser.add_argument('--feasibility', action='store_true',
                    help='consider feasibility or not')
parser.add_argument('--only', action='store_true',
                    help='only use labeled samples')
parser.add_argument('--noconf', action='store_true',
                    help='use only labeled data but without conf')
parser.add_argument('--vf-iters', type=int, default=30, metavar='V',
                    help='number of iterations of value function optimization iterations per each policy optimization step')
parser.add_argument('--vf-lr', type=float, default=3e-4, metavar='V',
                    help='learning rate of value network')
parser.add_argument('--noise', type=float, default=0.0, metavar='N')
parser.add_argument('--eval-epochs', type=int, default=3, metavar='E',
                    help='epochs to evaluate model')
parser.add_argument('--prior', type=float, default=0.2,
                    help='ratio of confidence data')
parser.add_argument('--ofolder', type=str, default='log')
parser.add_argument('--ifolder', type=str, default='demonstrations')
parser.add_argument('--demo_file_list', type=str, nargs='+')
parser.add_argument('--percent_list', type=float, nargs='+')
parser.add_argument('--test_episodes', type=int, help='Number of episodes')
parser.add_argument('--result_file', type=str, help='Result file name')
parser.add_argument('--snapshot_file', type=str, help='Snapshot file name')
args = parser.parse_args()

env = gym.make(args.env)

num_inputs = env.observation_space.shape[0]
num_actions = env.action_space.shape[0]

obs_len_init = num_inputs
env.reset()

env.seed(args.seed)
torch.manual_seed(args.seed)
np.random.seed(args.seed)

policy_net = CloningPolicy(num_inputs, num_actions, args.hidden_dim).float()
value_net = Value(num_inputs, args.hidden_dim).float().to(device)

disc_criterion = nn.BCEWithLogitsLoss()
policy_optimizer = optim.Adam(policy_net.parameters(), args.lr)

def load_demos(file_list, percent_list):
    state_action_pairs = []
    confs = []
    sequences = []
    initial_reward = []
    for k in range(len(file_list)):
        fname = file_list[k]
        episodes = pickle.load(open(fname, 'rb'))
        len_epi_for_train = len(episodes) if len(episodes) < 1000 else 1000
        for j in range(int(len_epi_for_train*percent_list[k])):
            episode = episodes[j]
            sequences.append([])
            reward_sum = 0.
            for i in range(len(episode['action'])):
                sequences[-1].append(np.concatenate([episode['state'][i].squeeze(), episode['action'][i].reshape(-1)]))
                #state_action_pairs.append(np.concatenate([episode['state'][i].squeeze(), episode['action'][i].reshape(-1)]))
                if episode['action'][i].reshape(-1).shape[0] == 3:
                    state_action_pairs.append(np.concatenate([episode['state'][i].squeeze(), episode['action'][i].reshape(-1)[[0,2]]]))
                else:
                    state_action_pairs.append(np.concatenate([episode['state'][i].squeeze(), episode['action'][i].reshape(-1)]))
                #confs.append([np.exp(episode['reward'][i])])
                if type(episode['reward'][i]) == np.ndarray:
                    reward_sum += episode['reward'][i].squeeze()
                else:
                    reward_sum += episode['reward'][i]
            sequences[-1].append(np.concatenate([episode['state'][len(episode['action'])].squeeze(), episode['action'][len(episode['action'])-1].reshape(-1)]))
            for i in range(len(episode['action'])):
                confs.append([reward_sum])
            initial_reward.append(np.concatenate([episode['state'][0][0], np.array([reward_sum])]))
            sequences[-1] = np.array(sequences[-1])
    confs = np.array(confs)
    print(np.mean(confs))
    return np.array(state_action_pairs), confs, sequences, np.array(initial_reward)

def load_and_train_inverse_dynamic(args, env):
    feasible_seq = []
    feasible_traj = []
    file_list = glob.glob('demos/'+args.env+'_random_explore*.pkl')
    order = np.random.permutation(len(file_list))
    episodes = pickle.load(open(file_list[order[0]], 'rb'))
    for j in range(len(episodes)):
        episode = episodes[j]
        feasible_seq.append([])
        for i in range(len(episode['action'])):
            feasible_seq[-1].append(np.concatenate([episode['state'][i].squeeze(), episode['action'][i].reshape(-1)]))
    return feasible_seq

def predict_action_with_inverse(args, env, sequences, obs_len_init, num_inputs, state_action_len):
    norms = []
    state_action_pairs = []
    i = 0
    for sequence in sequences:
      try:
        env.reset()
        norms.append([])
        state = env.reset_with_obs(sequence[0][0:obs_len_init])
        for step in range(len(sequence)-1):
            action = env.inverse_dynamic(state[0:num_inputs], sequence[step+1][0:num_inputs])
            action = np.array([action])
            next_state, _, _, _ = env.step(action)
            if i >= state_action_len:
                state_action_pairs.append(np.concatenate([state[0:num_inputs], action], axis=0))
            norms[-1].append(np.linalg.norm(next_state - sequence[step+1][0:num_inputs]))
            state = next_state
        i += 1
      except Exception as reason:
        print(reason)
        pdb.set_trace()
    norms = np.array([sum(norms1) for norms1 in norms])
    return np.array(state_action_pairs), norms

expert_traj, expert_conf, sequences, initial_reward = load_demos(args.demo_file_list, args.percent_list)
num_label = expert_conf.shape[0]

if args.feasibility:
    feasible_seq = load_and_train_inverse_dynamic(args, env)
    all_sequences = feasible_seq + sequences
    state_action_pairs, norms = predict_action_with_inverse(args, env, all_sequences, obs_len_init, num_inputs, len(feasible_seq))
    len_list = []
    for seq in all_sequences:
        len_list.append(len(seq))
    len_list = np.array(len_list)
    norms = norms/len_list
    max_ = np.max(norms[0:len(feasible_seq)])
    min_ = np.min(norms[0:len(feasible_seq)])
    max_1 = np.max(norms[len(feasible_seq):])

    if max_*2 > max_1:
        upper_bound = max_1
    else:
        upper_bound = max_*2
    weight = (norms[len(feasible_seq):] - (min_))/(upper_bound-min_)
    weight[weight>1] = 1.0
    weight[weight<0] = 0.0
    weight_step = []
    for www in range(weight.shape[0]):
        weight_step += [weight[www]] * (len(sequences[www])-1)
    weight_step = np.array(weight_step)
    feas_weight = 1.-weight_step.reshape(-1,1)

if args.optimality:
    rectify_data = []
    rectify_label = []
    for i in range(1000):
        max_rew = -np.inf
        id_ = 0
        for j in range(1000):
            if np.linalg.norm(initial_reward[j, 0:2] - initial_reward[i, 0:2]) < 0.1:
                if max_rew < initial_reward[j, -1]:
                    max_rew = initial_reward[j, -1]
                    id_ = j
        rectify_data.append(initial_reward[i, 0:num_inputs])
        rectify_label.append(max_rew)
    rectify_data = np.array(rectify_data)
    rectify_label = np.array(rectify_label)
    reward_list = rectify_label
    rewards_all = []
    for www in range(reward_list.shape[0]):
        rewards_all += [reward_list[www]] * (len(sequences[www])-1)
    rectify_rewards = expert_conf.squeeze() - rewards_all
    rectify_rewards = np.exp(-np.square(rectify_rewards)/(2*(args.sigma**2)))
    rectify_rewards[rectify_rewards<0] = 0.
    rectify_rewards[rectify_rewards>1] = 1.
    conf_weight = rectify_rewards.reshape(-1, 1)

if (args.optimality and args.feasibility):
    weight = feas_weight * conf_weight
elif args.feasibility:
    weight = feas_weight
elif args.optimality:
    weight = conf_weight
else:
    weight = np.ones(expert_conf.shape)
weight /= np.sum(weight)
if np.sum(weight) < 1:
    weight[0] = 1-np.sum(weight[1:])
else:
    weight /= np.sum(weight)
if np.sum(weight) != 1:
    weight[1] = 1-weight[0]-np.sum(weight[2:])

mean_reward_list = []
min_reward_list = []
max_reward_list = []

all_idx = np.arange(0, expert_traj.shape[0])

snapshot_dir = os.path.dirname(args.snapshot_file)
os.makedirs(snapshot_dir, exist_ok=True)
result_dir = os.path.dirname(args.result_file)
os.makedirs(result_dir, exist_ok=True)
max_mean_reward = -1000000000

for i_episode in range(args.num_epochs):
    env.seed(int(time.time()))
    loss_count = 0
    loss_sum = 0
    for iter_num in range(expert_traj.shape[0]//64):
        policy_optimizer.zero_grad()
        idx = np.random.choice(all_idx, 64, p=weight.reshape(-1))
        expert_state_next_state = expert_traj[idx, :]
        expert_state_next_state = torch.Tensor(expert_state_next_state).float().to(device)
        e_action = policy_net(expert_state_next_state[:, 0:num_inputs])
        loss = torch.nn.SmoothL1Loss(reduction='none')(e_action.squeeze(), expert_state_next_state[:, num_inputs:].squeeze())
        loss = torch.mean(loss)
        loss.backward()
        policy_optimizer.step()
        loss_sum += loss.item()
        loss_count += 1
    print('Cloning Loss, ', loss_sum/loss_count)
    if i_episode % args.log_interval == 0:
        env.seed(args.test_seed)
        reward_list = []
        with torch.no_grad():
          for i in range(args.test_episodes):
            state = env.reset()
            reward_sum = 0
            while True:
                state = torch.from_numpy(state).float().unsqueeze(0)
                action = policy_net(state)
                action = action.data[0].numpy()
                action = np.clip(action, env.action_space.low, env.action_space.high)
                next_state, true_reward, done, infos = env.step(action)
                reward_sum += true_reward
                if done:
                    break
                state = next_state
            reward_list.append(reward_sum)
        print('Episode {}, Average reward: {:.3f}, Max reward: {:.3f}, Min reward: {:.3f}'.format(i_episode, np.mean(reward_list), max(reward_list), min(reward_list)))
        mean_reward_list.append(np.mean(reward_list))
        std_reward_list.append(reward_list)
        max_reward_list.append(max(reward_list))
        min_reward_list.append(min(reward_list))
        if max_mean_reward < np.mean(reward_list):
            max_mean_reward = np.mean(reward_list)
            torch.save({'policy_net':policy_net.cpu().state_dict(), 'value_net':value_net.cpu().state_dict()}, args.snapshot_file.replace('.tar', 'best.tar'))

        torch.save({'policy_net':policy_net.cpu().state_dict(), 'value_net':value_net.cpu().state_dict()}, args.snapshot_file)

        episode_id_list = np.array(list(range(((args.num_epochs-1) // args.log_interval) + 1))) * args.log_interval + 1
        pickle.dump((' '.join(sys.argv[1:]), episode_id_list, mean_reward_list, min_reward_list, max_reward_list, std_reward_list), open(args.result_file, 'wb'))

episode_id_list = np.array(list(range(((args.num_epochs-1) // args.log_interval) + 1))) * args.log_interval + 1
pickle.dump((' '.join(sys.argv[1:]), episode_id_list, mean_reward_list, min_reward_list, max_reward_list, std_reward_list), open(args.result_file, 'wb'))


import requests #send http request
from bs4 import BeautifulSoup #Support html parser
baseurl = 'https://www.bachhoaxanh.com/'

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'#using linux and chrome
}

cateLinks=[]
file1=open("categoriesLinks/categoriesLinks.txt",'r')
lines = file1.readlines()
for line in lines:
	link=line.replace("\n","/")
	cateLinks.append(link)
file1.close()

productLinks=[] #link to all product still need to fix because a page may have many layer, this only take one layer
for item in cateLinks:
	r=requests.get(item)
	soup=BeautifulSoup(r.content,'lxml')
	productList=soup.find_all('li',class_='hideExpired product hasNotUnit')#List of products
	for item in productList:
		for link in item.find_all('a',href=True):
			productLinks.append(baseurl+link['href'])

#Save product Link to file txt
file=open("productLinks/productLinks.txt",'a')
file.truncate(0)
for item in productLinks:
	file.write(repr(item).replace("'",""))
	file.write("\n")
file.close()


import os
import re
import pickle
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

todos = []

# Load the saved list if it exists
if os.path.exists('todos.pkl'):
    with open('todos.pkl', 'rb') as file:
        todos = pickle.load(file)

@app.route('/')
def index():
    error = request.args.get('error', '')
    return render_template('index.html', todos=todos, error=error)

@app.route('/submit', methods=['POST'])
def submit():
    task = request.form['task']
    email = request.form['email']
    priority = request.form['priority']

    # Validate email
    email_regex = r'^[\w\.-]+@[\w\.-]+\.\w+$'
    if not re.match(email_regex, email):
        return redirect(url_for('index', error='Invalid email'))

    # Validate priority
    if priority not in ['Low', 'Medium', 'High']:
        return redirect(url_for('index', error='Invalid priority'))

    new_todo = {
        'task': task,
        'email': email,
        'priority': priority
    }
    todos.append(new_todo)

    return redirect(url_for('index'))

@app.route('/delete/<int:todo_index>', methods=['POST'])
def delete(todo_index):
    if 0 <= todo_index < len(todos):
        del todos[todo_index]
    return redirect(url_for('index'))

@app.route('/clear', methods=['POST'])
def clear():
    todos.clear()
    return redirect(url_for('index'))

@app.route('/save', methods=['POST'])
def save():
    with open('todos.pkl', 'wb') as file:
        pickle.dump(todos, file)
    return redirect(url_for('index'))

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


import torch
import torch.nn.functional as tnnfunc
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import matplotlib.pyplot as plt
import hw1_318864436_204585301_eval as eval
import numpy as np

"""
short summary:
Inside the train file is the MNIST training process.
We got an input of 784 (28 pixels * 28 pixels), the training worked best for us with 1 hidden layer in size 64,
batch size of 100, learning rate = 1 and with 50 epochs.
We first set up a net with weights and biases (according to the size of the hidden layer).
We started the forward process where we used sigmoid on the hidden layer and softmax on the result.
Next, we performed the backward process, using the gradients we calculated in the theoretical part and the predicted
values from the forward phase.
In the backward process we tried to learn with learning rate = 0.001, then with 0.01 and in the end we got
the best result with learning rate = 1.

To know the percentage of accuracy on the net, we performed a loop that goes through each epoch
(at first we tried sizes 10, 30 and in the end we got the best result with a size of 50).
For each epoch we calculated whether we were right in our prediction.
In the evaluate file we performed the same process, on the test set.

At the end we printed the average accuracy on the test set and two graphs describing the accuracy
of the sets in relation to the time (the epochs)
"""

# Hyper Parameters
input_size = 784 #28*28=784
hidden_size = 64
num_classes = 10
num_epochs = 50
batch_size = 100
learning_rate = 1

# train and test sets
transform = transforms.Compose([transforms.ToTensor()])

train_dataset = dsets.MNIST(root='./data',
                            train=True,
                            transform=transform,
                            download=True)

test_dataset = dsets.MNIST(root='./data',train=False,transform=transform)

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

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

# utils functions
def sigmoid(s):
    return 1 / (1 + torch.exp(-s))

def sigmoidPrime(s):
    # derivative of sigmoid
    # s: sigmoid output
    return s * (1 - s)

def softmax(X):
    exps = torch.exp(X)
    return (exps / torch.sum(exps))

class Neural_Network:
    def __init__(self, input_size, output_size, hidden_size):
        # parameters
        self.inputSize = input_size
        self.outputSize = output_size
        self.hiddenSize = hidden_size

        # weights
        self.W1 = torch.randn(self.inputSize, self.hiddenSize)
        self.b1 = torch.zeros(self.hiddenSize)

        self.W2 = torch.randn(self.hiddenSize, self.outputSize)
        self.b2 = torch.zeros(self.outputSize)

    def forward(self, X):
        self.z1 = torch.matmul(X, self.W1) + self.b1
        self.h = sigmoid(self.z1)
        self.z2 = torch.matmul(self.h, self.W2) + self.b2
        return softmax(self.z2)

    def backward(self, X, y, y_hat, lr=learning_rate):
        batch_size = y.size(0)
        y_hat1 = tnnfunc.one_hot(y_hat)
        y1 = tnnfunc.one_hot(y)

        if y1.shape[1] < 10:
            diff = 10 - y1.shape[1]
            add = torch.zeros(y1.shape[0], diff)
            y1 = torch.cat((y1, add), 1)

        if y_hat1.shape[1] < 10:
            diff = 10 - y_hat1.shape[1]
            add = torch.zeros(y_hat1.shape[0], diff)
            y_hat1 = torch.cat((y_hat1, add), 1)

        dl_dz2 = (1 / batch_size) * (y_hat1 - y1)
        dl_dh = torch.matmul(dl_dz2, torch.t(self.W2))
        dl_dz1 = dl_dh * sigmoidPrime(self.h)

        self.W1 -= lr * torch.matmul(torch.t(X), dl_dz1)
        self.b1 -= lr * torch.matmul(torch.t(dl_dz1), torch.ones(batch_size))
        self.W2 -= lr * torch.matmul(torch.t(self.h), dl_dz2)
        self.b2 -= lr * torch.matmul(torch.t(dl_dz2), torch.ones(batch_size))

    def train(self, X, y):
        # forward + backward pass for training
        o = self.forward(X)
        _, predicted = torch.max(o.data, 1)
        self.backward(X, y, predicted)
        return predicted

def to_var(x):
    return Variable(x)

# building the net
net = Neural_Network(input_size, num_classes, hidden_size)

correct_train = 0
total_train = 0
correct_test = 0
total_test = 0
epochs_plt = []
train_plt = []
test_plt = []
avg_test=[]
# training
for epoch in range(num_epochs):
    epochs_plt.append(epoch)

    for i, (images, labels) in enumerate(train_loader):
        images = to_var(images.view(-1, 28 * 28))
        labels = to_var(labels)
        predicted = net.train(images, labels)
        total_train += labels.size(0)
        correct_train += (predicted == labels).sum()
    #train_plt.append(correct_train / total_train)

    for i, (images, labels) in enumerate(test_loader):
        images = to_var(images.view(-1, 28 * 28))
        labels = to_var(labels)
        out = net.forward(images)
        _, predicted = torch.max(out.data, 1)
        total_test += labels.size(0)
        correct_test += (predicted == labels).sum()
    avg_test.append(correct_test / total_test)
    train_plt.append(correct_train / total_train)
    test_plt.append(correct_test / total_test)

torch.save(net, 'hw1_model.pkl')
"""avg = eval.evaluate_hw1()
print('avg=',avg)"""

#ploting the train and test graphs
plt.plot(epochs_plt,train_plt,color='blue', label='train set') #plt.plot(x,y)
plt.plot(epochs_plt,test_plt, color='red', label='test set')
plt.title('Accuracy As A Function Of Time (Epochs)')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend()
plt.show()


# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utils for model evaluation."""

import numpy as np

def compute_approximate_sequence_accuracy(sequence1, sequence2):
  """Find a sub-optimal matching of two sequences of integers.

  This sub-optimal matching is obtained by a greedy algorithm. It's not the
  best matching. To compute the real best matching, the Hungarian algorithm
  is needed, which requires additional dependencies.

  For example, if sequence1=[0,0,1,2,2], sequence2=[3,3,4,4,1], then accuracy
  will be 4/5=0.8. Notice that the two sequences are not exchangable, in other
  words, you may get different results if you switch the order of the two
  sequences.

  Args:
    sequence1: a list of integer - The first sequence to match.
    sequence2: a list of integer - The second sequence to match.

  Returns:
    sub-optimal matching accuracy
  """
  assert len(sequence1) == len(sequence2), (
      "The two sequences should should of the same length")
  assert sequence1, "The sequences cannot be empty"

  unique_id1, counts1 = np.unique(sequence1, return_counts=True)
  unique_id2, _ = np.unique(sequence2, return_counts=True)
  # transform into numpy arrays
  dict1 = dict(zip(unique_id1, np.arange(len(unique_id1))))
  dict2 = dict(zip(unique_id2, np.arange(len(unique_id2))))
  sequence1 = np.array([dict1[k] for k in sequence1])
  sequence2 = np.array([dict2[k] for k in sequence2])
  unique_id1 = np.arange(len(unique_id1))
  unique_id2 = np.arange(len(unique_id2))

  idx1 = np.argsort(counts1)[::-1]
  n_forward_match = 0
  copy_unique_id2 = np.copy(unique_id2)
  for seq1_idx in idx1:
    if not copy_unique_id2.shape[0]:
      break
    n_match = []
    for seq2_idx in copy_unique_id2:
      n_match.append(
          len(
              np.intersect1d(
                  np.where(sequence1 == unique_id1[seq1_idx])[0],
                  np.where(sequence2 == seq2_idx)[0])))
    best_seq2_idx = np.argmax(n_match)
    n_forward_match += np.max(n_match)
    copy_unique_id2 = np.delete(copy_unique_id2, best_seq2_idx)

  return n_forward_match / len(sequence1)

def evaluate_result(true_labels, predict_labels):
  accuracy = np.max((
      compute_approximate_sequence_accuracy(true_labels, predict_labels),
      compute_approximate_sequence_accuracy(predict_labels, true_labels)))
  return accuracy, len(true_labels)


"""
Unit tests for :module:`interpolation`
"""

import unittest
import numpy.testing as npt

from ..backmap_cones_package.interpolation  import *

import shared_test_setup as sts

class InterpolationTestCase(unittest.TestCase):
    def setUp(self):
        self.box_halfwidths = [random.uniform(100., 200.),
                               random.uniform(100., 200.),
                               random.uniform(100., 200.)]

        self.sites_in_chains = 10
        self.random_chain   = sts.build_random_chain(self.sites_in_chains)
        self.straight_chain = sts.build_straight_chain(self.sites_in_chains)
        self.zero_chain     = sts.build_straight_chain(self.sites_in_chains, separation=0.)

        self.chain_on_box_before_pbc = sts.copy_chain_onto_bounding_box(
            self.random_chain, self.box_halfwidths)

        self.list_of_chains = [self.chain_on_box_before_pbc, self.random_chain, self.straight_chain]

class TestScales(InterpolationTestCase):
    def test_agrees_with_scipy_interpolate(self):
        x_i = random.uniform(-10, 10)
        x_f = x_i + random.uniform(.1, 10)
        y_i = random.uniform(-10, 10)
        y_f = y_i + random.uniform(.1, 10)

        samples = random.randint(3, 20)
        x_values = np.linspace(x_i, x_f, samples)

        slope = (y_f - y_i) / (x_f - x_i)
        y_intercept = y_f - slope * x_f

        y_vals_lin_scale = np.zeros(samples)
        for n in range(samples):
            y_vals_lin_scale[n] = linear_scale(x_i, y_i, x_f, y_f, x_values[n])

        y_vals_numpy = np.polynomial.polynomial.polyval(
            x_values, np.array([y_intercept, slope]))

        npt.assert_allclose(y_vals_lin_scale, y_vals_numpy)

    def test_against_one_over_x_works(self):
        known_value_pairs = [(1, 1), (0.1, 10), (.25, 4), (-1, -1), (-0.1, -10), (-.25, -4)]
        for x, y in known_value_pairs:
            self.assertAlmostEqual(power_scale({-1: 1}, x), y)

    def test_agrees_with_linear_scale(self):
        x_i = random.uniform(-10, 10)
        x_f = x_i + random.uniform(.1, 10)
        y_i = random.uniform(-10, 10)
        y_f = y_i + random.uniform(.1, 10)

        samples = random.randint(3, 20)
        x_values = np.linspace(x_i, x_f, samples)

        slope = (y_f - y_i) / (x_f - x_i)
        y_intercept = y_f - slope * x_f

        y_vals_lin_scale = np.zeros(samples)
        y_vals_pow_scale = np.zeros(samples)
        for n in range(samples):
            y_vals_lin_scale[n] = linear_scale(x_i, y_i, x_f, y_f, x_values[n])
            y_vals_pow_scale[n] = power_scale({0: y_intercept, 1: slope}, x_values[n])

        npt.assert_allclose(y_vals_lin_scale, y_vals_pow_scale)

class TestInterpolations(InterpolationTestCase):
    def test_cone_at_origin_of_unit_radius_yields_unit_vector_perp_to_inputs(self):
        rand_float = random.uniform(0.1, 10.)

        plus_x  = rand_float * np.array([ 1, 0, 0])
        minus_x = -plus_x
        interpolation = interpolate_on_cone(plus_x, minus_x, 1)
        # The first test depends on the two initial points being antipodal WRT origin
        self.assertAlmostEqual(vec.magnitude(interpolation), 1)
        self.assertAlmostEqual(interpolation.dot(plus_x - minus_x), 0)

    def test_zero_cone_radius_does_not_crash_cone_interpolation(self):
        """If a zero cone radius is given to the cone interpolater, it should return the input.

        This was written after a bug was discovered where the above situation caused the code
        to raise a zero division exception.
        """
        rand_float = random.uniform(0.1, 10.)

        plus_x  = rand_float * np.array([ 1, 0, 0])
        minus_x = -plus_x
        interpolation = interpolate_on_cone(plus_x, minus_x, 0)

        # Since the interpolated point is 0 away from the midpoint, it is just the midpoint
        npt.assert_allclose(interpolation, np.zeros(3))

    def test_focusing_on_bond_angles_obtains_desired_angles(self):
        bond_length = random.uniform(0.1, 10.)
        x = bond_length * np.array([ 1, 0, 0])
        z = bond_length * np.array([ 0, 0, 1])
        test_chain = chn.Chain([x, z])
        threshold = .1
        target_angle = 0.5 * math.pi

        interpolated_chain = interpolate_chain(
            test_chain, target_angle, 1., bond_length, bond_angle_threshold=threshold,
            test_mode=True)
        for site in range(len(interpolated_chain) - 2):
            self.assertTrue(
                abs(interpolated_chain.get_bond_angle(site) - target_angle) < threshold)

    def test_focusing_on_bond_lengths_obtains_desired_lengths(self):
        test_chain = self.random_chain
        bond_length = max(test_chain.get_bond_length(x) for x in range(len(test_chain) - 1))
        target_angle = 0.5 * math.pi

        interpolated_chain = interpolate_chain(test_chain, target_angle, 0., bond_length)
        for site in range(len(interpolated_chain) - 1):
            self.assertAlmostEqual(interpolated_chain.get_bond_length(site), bond_length)

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


import pickle
import os
import numpy as np

def write_pickle(dict_, path):
    f = open(path, 'wb')
    pickle.dump(dict_, f, protocol=2)
    f.close()

def load_pickle(path):
    f = open(path, 'rb')
    dicts = pickle.load(f)
    f.close()
    return dicts

def _read_one_batch(path):
    fo = open(path, 'rb')
    dicts = pickle.load(fo, encoding='latin1')
    data = dicts['data']
    labels = np.array(dicts['labels'])
    return data, labels

def read_all_batches(root, num_batch, img_dim, shuffle=True):
    print(" CIFAR-10 loading starts ".center(50, '-'))

    data = np.array([]).reshape([0, img_dim[0]*img_dim[1]*img_dim[2]])
    label = np.array([])

    batch_list = ['data_batch_{}'.format(i+1) for i in range(num_batch)]
    for batch in batch_list:
        path = os.path.join(root, batch)
        print('Reading data from {}'.format(path))
        batch_data, batch_label = _read_one_batch(path)
        data = np.concatenate((data, batch_data))
        label = np.concatenate((label, batch_label))

    num_data = label.shape[0]
    data = data.reshape((num_data, img_dim[0]*img_dim[1], img_dim[2]), order='F')
    data = data.reshape((num_data, img_dim[0], img_dim[1], img_dim[2]))

    if shuffle is True:
        print('Shuffling data...')
        order = np.random.permutation(num_data)
        data = data[order, ...]
        label = label[order]

    data = data.astype(np.float32)
    print("Totally {} data has been loaded from {} batches".format(num_data, num_batch))
    print(" CIFAR-10 loading finifshed ".center(50, '-'))
    return data, label

def read_val_data(root, img_dim, shuffle=True):
    print(" CIFAR-10 testset loading starts ".center(50, '-'))

    data = np.array([]).reshape([-1, img_dim[0]*img_dim[1]*img_dim[2]])
    label = np.array([])

    path = os.path.join(root, 'test_batch')
    print('Reading data from {}'.format(path))
    batch_data, batch_label = _read_one_batch(path)
    data = np.concatenate((data, batch_data))
    label = np.concatenate((label, batch_label))

    num_data = label.shape[0]
    data = data.reshape((num_data, img_dim[0]*img_dim[1], img_dim[2]), order='F')
    data = data.reshape((num_data, img_dim[0], img_dim[1], img_dim[2]))

    if shuffle is True:
        print('Shuffling data...')
        order = np.random.permutation(num_data)
        data = data[order, ...]
        label = label[order]

    data = data.astype(np.float32)
    print("Totally {} validation data has been loaded".format(num_data))
    print(" CIFAR-10 tesetset loading finifshed ".center(50, '-'))
    return data, label


# -*- coding: utf-8 -*-
# @Time:2018.11.15 15:57
# @Author:Zhang
# @Desc  :
from greenlet import greenlet
import time

def test1():
    while True:
        print("test1")
        gr2.switch()
        time.sleep(0.5)
        print('test1--end')

def test2():
    while True:
        print("test2")
        gr1.switch()
        time.sleep(0.5)
        print('test2--end')

gr1 = greenlet(test1)
gr2 = greenlet(test2)

# 切换到gr1中运行
gr1.switch()


# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import datetime
from report import report_sxw
import re

class report_treasury(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(report_treasury, self).__init__(cr, uid, name, context)
        self.localcontext.update( {
            'time': time,
			'comma_me': self.comma_me,
        })

    def comma_me(self,amount):
        if  type(amount) is float :
            amount = str('%.2f'%amount)
        else:
            amount = str(amount)
        if (amount == '0'):
            return ' '
        orig = amount
        new = re.sub("^(-?\d+)(\d{3})", "\g<1>,\g<2>", amount)
        if orig == new:
            return new
        else:
            return self.comma_me(new)

report_sxw.report_sxw('report.treasury', 'account.treasury', 'addons/bias_payment_wo_cc/report/treasury.rml', parser=report_treasury, header=True)
report_sxw.report_sxw('report.global.treasury', 'account.global.treasury', 'addons/bias_payment_wo_cc/report/treasury.rml', parser=report_treasury, header=True)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


import os.path as osp
import json
import os
from tqdm import tqdm
import numpy as np
import copy

def maybe_mkdir(x):
    if not osp.exists(x):
        os.makedirs(x)

add_data_dir = os.environ.get('SM_CHANNEL_TRAIN', '../../input/data/camper')
# 여기 부분 가지고 계신 폴더구성에 맞추어 수정해주시구요

with open(osp.join(add_data_dir, 'ufo/{}.json'.format('annotation')), 'r') as f:
# 여기 부분도 가지고 계신 파일명에 맞추어서 수정해주시길 요청드립니다.
    anno = json.load(f)

anno = anno['images']

anno_temp = copy.deepcopy(anno)

count = 0
count_normal = 0
count_none_anno = 0

for img_name, img_info in tqdm(anno.items()) :
    if img_info['words'] == {} :
        del(anno_temp[img_name])
        count_none_anno += 1
        continue
    for obj, obj_info in img_info['words'].items() :
        # illegibility는 전부 단어 이므로 false
        anno_temp[img_name]['words'][obj]['illegibility'] = False
        if len(img_info['words'][obj]['points']) == 4 :
            count_normal += 1
            continue

        elif len(img_info['words'][obj]['points']) < 4 :
            del(anno_temp[img_name]['words'][obj])
        # 폴리곤 수정시에는 여기 부분을 수정해주시면 됩니다!!
        # 다음 예제는 polygon이 넘칠 경우 해당 폴리곤을 illegibility를 삭제처리
            if anno_temp[img_name]['words'] == {} :
                del(anno_temp[img_name])
                count_none_anno += 1
                continue
        else :
            # 현동님의 기여로 만들어진 부분
            over_polygon_temp = copy.deepcopy(anno_temp[img_name]['words'][obj])
            over_poly_region = copy.deepcopy(over_polygon_temp)
            over_poly_region['points'] = []
            for index in range(len(img_info['words'][obj]['points'])//2 -1):
                over_poly_region['points'].append(over_polygon_temp['points'][index])
                over_poly_region['points'].append(over_polygon_temp['points'][index+1])
                over_poly_region['points'].append(over_polygon_temp['points'][-index-1])
                over_poly_region['points'].append(over_polygon_temp['points'][-index])
                anno_temp[img_name]['words'][obj+f'{index+911}'] = copy.deepcopy(over_poly_region) #911 현동님 생일 >_<
                over_poly_region['points'] = []
            del anno_temp[img_name]['words'][obj]
            # 폴리곤을 사각단위로 잘라서 각 부분을 word의 영역으로 사용하는 코드입니다.
            if anno_temp[img_name]['words'] == {} :
                del(anno_temp[img_name])
                count_none_anno += 1
                continue
            count += 1

print(f'normal polygon count : {count_normal}')
print(f'deleted {count} over polygon')
print(count_none_anno)

# np_point = np.array(img_info['words'][obj]['points'])
# starting_point = np_point[0]

anno = {'images': anno_temp}

ufo_dir = osp.join('../input/data/camper', 'ufo')
maybe_mkdir(ufo_dir)
with open(osp.join(ufo_dir, 'trains.json'), 'w') as f:
    json.dump(anno, f, indent=4)


import services.controlers.loggControler
import services.querys.actionForm201Query
from services.exceptions import *
import os
import hashlib
import re
import cgi
import urllib

class ActionForms201Controler:
	def get(self):
		actionForm201Query = services.querys.actionForm201Query.ActionForm201Query()
		GoalsList=[]
		try:
			data = actionForm201Query.getAll()
			for ActionForms201Object in data:
				GoalsList.append(ActionForms201Object.toDictFront())
		except Exception as e:
			loggControler = services.controlers.loggControler.LoggControler()
			loggControler.addLogg('Critical: ActionForms201Controler - get()', ERROR_NO_DEFINIDO, e.message)
		return GoalsList

	def getById(self, identifier):
		ActionForm201={}
		try:
			actionForm201Query = services.querys.actionForm201Query.ActionForm201Query()
			attributesList=[]
			ActionForms201Object = actionForm201Query.getById(identifier)
			ActionForm201 = ActionForms201Object.toDict()
		except Exception as e:
			loggControler = services.controlers.loggControler.LoggControler()
			loggControler.addLogg('Critical: ActionForms201Controler - getById()', ERROR_NO_DEFINIDO, e.message)
		return ActionForm201

	def getByEventId(self, eventId):
		actionForm201Query = services.querys.actionForm201Query.ActionForm201Query()
		GoalsList=[]
		try:
			data = actionForm201Query.getByEventId(eventId)
			for ActionForms201Object in data:
				GoalsList.append(ActionForms201Object.toDictFront())
		except Exception as e:
			loggControler = services.controlers.loggControler.LoggControler()
			loggControler.addLogg('Critical: ActionForms201Controler - getByEventId()', ERROR_NO_DEFINIDO, e.message)
		return GoalsList

	def add(self, eventId, actionDescription, minute, hour):
		message="OBJETIVO_DE_PAI_NO_REGISTRADO"
		state = 300
		try:
			actionForm201Query = services.querys.actionForm201Query.ActionForm201Query()
			# VALIDACIONES
			doRegister = True
			# REGISTRO
			if doRegister == True:
				state, ActionForms201Id = actionForm201Query.add(eventId, actionDescription, minute, hour)
				if state == OK:
					message="OBJETIVO_DE_PAI_REGISTRADO"
		except Exception as e:
			loggControler = services.controlers.loggControler.LoggControler()
			loggControler.addLogg('Critical: ActionForms201Controler - add()', ERROR_NO_DEFINIDO, e.message)
		return ActionForms201Id, message, state

	def edit(self, eventId, actionDescription, minute, hour, identifier):
		message="OBJETIVO_DE_PAI_NO_MODIFICADO"
		state = 300
		try:
			actionForm201Query = services.querys.actionForm201Query.ActionForm201Query()
			obj = actionForm201Query.getById(identifier)
			if eventId is not None:
				obj.eventId=eventId
			if actionDescription is not None:
				obj.actionDescription=actionDescription
			if minute is not None:
				obj.minute=minute
			if hour is not None:
				obj.hour=hour
			actionForm201Query.edit(obj)
			message="OBJETIVO_DE_PAI_MODIFICADO"
			state = OK
		except Exception as e:
			loggControler = services.controlers.loggControler.LoggControler()
			loggControler.addLogg('Critical: ActionForms201Controler - edit()', ERROR_NO_DEFINIDO, e.message)
		return message, state


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 10 10:42:39 2017

@author: Emanuele

Binary Tree to deal with the routes, their expansions, their cost calculations

Rise up this mornin', Smiled with the risin' sun, 'Tree' little birds. Pitch by my doorstep [Bob Marley]
"""

import numpy as np;

#==============================================================================
# class Node that defines the nodes in the binary tree
# it hase the following attributes
# left, right: the left and right parents of the node
# parent: the parent of the node
# cost: the cost associated to the route that leads to that node(e.g. if the node is the path described by a route that covers t1,t3,t4, it will contain the cost associated to that route)
# height: the height of the node in the tree
# isLeaf: it is used to see if the node is a leaf
#==============================================================================
class Node(object):
    def __init__(self, left, right, parent, cost, height, isleaf):
        self.left = left;
        self.right = right;
        self.parent = parent;
        self.cost = cost;
        self.height = height;
        self.isleaf = isleaf;
    def getLeft(self):
        return self.left;
    def getRight(self):
        return self.right;
    def getParent(self):
        return self.parent;
    def getCost(self):
        return self.cost;
    def getHeight(self):
        return self.height;
    def isLeaf(self):
        return self.isLeaf;
    def setLeft(self, left):
        self.left = left;
    def setRight(self, right):
        self.right = right;
    def setParent(self, parent):
        self.parent = parent;
    def setCost(self, cost):
        self.cost = cost;
    def setHeight(self, height):
        self.height = height;
    def setIsLeaf(self, isleaf):
        self.isleaf = isleaf;

#==============================================================================
# class that defines the binary tree
#  it has as elements a root which is the root of the tree (it corresponds to a "virtual" vertex on G where the cost
#  of moving from that node to every node on G is 0). This trick is used to take into consideration the
#  case where the initial vertex is a target itself: so it could be a covering route of some kind.
#==============================================================================
class BTree(object):
    def __init__(self):
        self.root = None;
        self.SP_cost = np.array([]);
    def getShortestPaths(self, SP):
        self.SP_cost = np.array(SP);
    def getRootNode(self):
        return self.root;

#==============================================================================
#     search in the tree if the route v we have found has a cost which
#     is higher than one which is present in the tree, returns 1, 0 otherwise
#     in the first case the route we passed to the function is better than the previous one stored
#     in the tree, so we will update() the tree acoordingly to the new route, otherwise nothing happens
#     takes as input:
#     cost of the route v
#     the route expressed as binary vector wrt the order imposed to the targets (0 if the i-th target is not covered by v, 1 otherwise)
#     return True if the cost is less than the cost of a route already present in the tree(or if the route v is not present), False otherwise
#==============================================================================
    def search(self, cost, v):
        node = self.getRootNode();
        for i in v:
            if i==1:
                node = node.right;
                if node is None:
                    return (True, True);
            else:
                node = node.left;
                if node is None:
                    return (True, True);
        if cost < (node.getCost()):#if the route v is better than one which is present, return True (we should update the tree)
            node.setCost(cost);
            return (True, False);
        else:
            return (False, False);

#==============================================================================
#     update function: it updates the btree used to store the routes
#     we go down through the tree and update a route
#     if the last target in the route is covered, build a new tree from that point on where the root is the new
#      target, and its cost is the sum of the previous route's cost plus the shortest path from
#      prevoius vertex to this new one
#     if the target is not covered, just create a new tree on the right where the new cost is
#      the provious cost of the route (a general case of stand still for D wrt a target)
#     oldroute is the non-ordered route, used to calculate its cost (which is dependent on the targets' order obviously)
#     update takes as input:
#      the route to be updated
#      the set of targets
#      the root of the tree(should be removed)
#      the route expressed as binary vector wrt the order imposed to the targets (0 if the i-th target is not covered by v, 1 otherwise)
#      the route in the original order (not ordered by indexvertex),on which we calculate the sp cost
#==============================================================================
    def update(self, route, targets, node, v, oldroute):
        if self.root is None: #if the tree is empty, create the first node (i.e. the root)
            route = np.sort(route); #oder route by indexvertex order
            targets = np.sort(targets); #order targets by indexvertex order
            self.root = Node(None, None, None, 0, 0, False);
            node = self.root;
        if len(route)==1 and route[0] not in targets:#if the route is just the starting vertex, return after creating the tree
            return;
        if v[0]==1:
            if node.right is None: #there's no such a route that covers the target under exam
                node.right = Node(None, None, node, calculateCostOfRoute(oldroute, self.SP_cost), node.getHeight()+1, True);
                node.isleaf = False;#if we insert a new target on a route, the previous one is not still a route (or at least is strictly dominated)
                return; #we assume that we call this function at each iteration (i.e. every time we update a route in the tree, we can at most extend it with one element)
            else:#it exists a route that covers that target, so let's go down the tree
                return self.update(route[1:], targets[1:], node.right,v[1:],oldroute);
        else:
            if node.left is None: #in this case a target is not covered by a route, but another one which comes after that one (wrt the order imposed at the beginning on T) is so
                node.left = Node(None, None, node, node.getCost(), node.getHeight()+1, False);#we use the getCost function to propagate the cost of a route(till that time) through the tree
                node.isleaf = False;
                return self.update(route, targets[1:], node.left, v[1:],oldroute);
            else:# in this case a route that does not cover a target already exists (but for sure that route will cover at most another target which comes after in T)
                return self.update(route, targets[1:], node.left,v[1:],oldroute);

#==============================================================================
# transform a route into a binary vector where each entry v[i] is 1 if the corresponding
# target in T[i] is covered by r, 0 otherwise.
# takes as input
#  the route r (which is a vector!)
#  the targets T in topological order (wrt Vertex.vertex_number)
# returns the binary vector v as defined above
#==============================================================================
def binaryVectorFromRoute(r, T):
    i=0;
    r = np.sort(r);
    T = np.sort(T);
    v = np.array([]);
    for t in range(len(T)):
        if T[t] in r:
            v = np.append(v,1);
            i+=1;
        else:
            v = np.append(v,0);
    return v.astype(int);

#==============================================================================
# create a 'purged' version of the binaryVectorFromRoute:
#  i.e. delete all the terminal zeros in the end of the vector
#  it is used to search in the tree for routes that are already in
#  e.g. if the input v is [0,1,1,0] it returns [0,1,1] etc.
# takes as input
#  the binary vector v to "purge"
#  returns the "purged" vector v
#==============================================================================
def purgeBinaryVector(v):
    if 1 not in v:
        return np.array([]);
    else:
        temp = v[::-1].tolist().index(1);
        return v[:len(v)-temp]

#==============================================================================
# #given a route as an ordered set of targets, calculate the cost of the shortest path that covers all the target inside in it
#==============================================================================
def calculateCostOfRoute(route, SP_cost):
    cost = 0;
    for r in range(0,len(route)-1):
        cost += SP_cost[route[r]][route[r+1]];
    return cost;

"""
Little testing to see if the algorithms work as expected
"""
verbose = False; # this variable controls whether the output is printed
if verbose:
    #create the tree
    bt = BTree();
    #get the shortest path matrix
    bt.getShortestPaths([[0,2,2,1,1],[2,0,1,1,2],[2,1,0,1,2],[1,1,1,0,1],[1,2,2,1,0]]);
    #this is how you can create a new path
    #first inizialize the binary vector route-targets
    #then call the update tree function
    v = np.array(binaryVectorFromRoute([0],[1,2,3,4])); #route that does not cover anything, just create the tree
    bt.update([0],[1,2,3,4],bt.root,v,[0]);#update the tree
    v = np.array(binaryVectorFromRoute([0,2],[1,2,3,4])); #route that covers target 2, starting from vertex 0
    bt.update([0,2],[1,2,3,4],bt.root,v,[0,2]);#update the tree, its cost should be 2 (according to SP matrix defined previously)

    v1 = np.array(binaryVectorFromRoute([0,2],[1,2,3,4]));#create the route that covers 0,2. It is present in the tree
    v2 = np.array(binaryVectorFromRoute([0,1],[1,2,3,4]));#create the route that covers 0,1. It is not present in the tree

    print(bt.search(2, purgeBinaryVector(v1)));#we see if there a route that covers 0,2 with a cost lower than 2(e.g. 1), we expect that there's not such a route(i.e. in the tree there's a better route)
    print(bt.search(1, purgeBinaryVector(v1)));#we see if there a route that covers 0,2 with a cost lower than 0(e.g. -1), we expect that there's such a route (i.e. in the tree there's no better route)
                                                        #we expect that from now on route [0,2] will cost 0
    print(bt.search(0, purgeBinaryVector(v2)));#we see if there's a route that covers 0,1: any cost should return false (even negative) since that route is not present (so we give it a low cost)


from subprocess import Popen, PIPE
import shlex
from inspect import stack
from collections import Iterable
from functools import reduce
import re
from .container import Container

class Formatter:
    def __mod__(self, fstring):
        frame = stack()[1].frame
        scope = frame.f_globals.copy()
        scope.update(frame.f_locals)
        return fstring.format(**scope)

    def __rmod__(self, fstring):
        frame = stack()[1].frame
        scope = frame.f_globals.copy()
        scope.update(frame.f_locals)
        return fstring.format(**scope)

class Stream(Container):
    def __init__(self):
        self.sh = Pipe(pipelink=self)

    def __or__(self, expression):
        if isinstance(expression, Pipe):
            if isinstance(self.state, str):
                expression.state = self.state
            elif isinstance(self.state, Iterable):
                expression.state = u'\n'.join(self.state)
            else:
                expression.state = str(self.state)
            return expression

        elif isinstance(expression, Stream):
            expression.state = self.state
            return expression

        elif isinstance(expression, (Substituter, Matcher)):
            if isinstance(self.state, str):
                self.state &= expression
            else:
                self.state = (self.state | expression).state
            return self

        elif expression in (map, filter, reduce):
            return Mapper(expression, self)

        elif expression == print:
            sub = expression
            if (not isinstance(self.state, str)
                and isinstance(self.state, Iterable)):
                print('\n'.join(str(i).rstrip() for i in self.state))
            else:
                print(self.state)
            return self

        else:
            if callable(expression):
                expression = expression(self.state)
            self.state = expression
            return self

    def __lt__(self, a_file):
        self.state = open(a_file)
        return self

    def __gt__(self, a_file):
        with open(a_file, 'w') as the_file:
            try:
                the_file.write(self.state)
            except TypeError:
                the_file.write(repr(self.state))
        return self

    def __rshift__(self, a_file):
        with open(a_file, 'a') as the_file:
            the_file.write(self.state)
        return self

class Pipe(Container):
    def __init__(self, pipelink):
        self.state = ''
        self.link = pipelink

    def __or__(self, command):
        if isinstance(command, Stream):
            command.state = self._lazysplit('\n')
            return command

        elif isinstance(command, Pipe):
            command.state = self.state
            return command

        elif isinstance(command, (Matcher, Substituter)):
            self.state = '\n'.join(self._lazysplit('\n') | command)
            return self

        elif not isinstance(command, str):
            return self.link|command

        else:
            self.command = command
            self.state = Popen(shlex.split(command),
                             stdin=PIPE,
                             universal_newlines=True,
                             stdout=PIPE).communicate(self.state)[0]
            self.link.state = self._lazysplit('\n')
            return self

    def _lazysplit(self, char):
        string = self.state
        index = string.find(char)
        while index != -1:
            yield string[:index]
            string = string[index+1:]
            index = string.find(char)
        if string != '':
            yield string

    def __ror__(self, other):
        if isinstance(other, str):
            self.state = other
        elif isinstance(other, Iterable):
            self.state = '\n'.join(other)
        else:
            self.state = str(other)
        return self

    def __lt__(self, a_file):
        with open(a_file) as the_file:
            self.state = Popen(shlex.split(self.command),
                            stdin=the_file,
                            universal_newlines=True,
                            stdout=PIPE).communicate()[0]
        self.link.state = self._lazysplit('\n')
        return self

    def __gt__(self, a_file):
        with open(a_file, 'w') as the_file:
            the_file.write(self.state)
        return self

    def __rshift__(self, a_file):
        with open(a_file, 'a') as the_file:
            the_file.write(self.state)
        return self

    def __str__(self):
        return self.state.rstrip()

    def __repr__(self):
        return self.__str__()

    def __iter__(self):
        return self._lazysplit('\n').__iter__()

    def __getitem__(self, index):
        return self.state.splitlines()[index]

    def __missing__(self, other):
        return self.state.splitlines().__missing__(other)

    def __reversed__(self):
        return self.state.splitlines().__reversed__()

class Mapper:
    def __init__(self, function, parent):
        self.parent = parent

    def __or__(self, other):
        self.parent.state = function(other, self.parent.state)
        return self.parent

class SubStarter:
    def __truediv__(self, pattern):
        return Substituter(pattern)

class Substituter:
    def __init__(self, pattern):
        self.pattern = pattern
        self.replacement = None
        self.count = 1

    def __truediv__(self, argument):
        if self.replacement:
            if 'g' in argument:
                self.count = 0
            if 'g' != argument:
                argument = argument.replace('g', '')
                self.pattern = ('(?%s)' % argument + self.pattern)
        else:
            self.replacement = argument
        return self

    def __rand__(self, string):
        if isinstance(self.pattern, str):
            self.pattern = re.compile(self.pattern)
        return self.pattern.sub(self.replacement, string, self.count)

    def __ror__(self, iterable):
        return Stream()|(i & self for i in iterable)

    def __gt__(self, filename):
        return Stream()|(i & self for i in open(filename))

class MatchStarter:
    def __truediv__(self, pattern):
        return Matcher(pattern)

class Matcher:
    def __init__(self, pattern):
        self.pattern = pattern

    def __truediv__(self, options):
        self.pattern = ('(?%s)' % options) + self.pattern
        return self

    def __rand__(self, string):
        if isinstance(self.pattern, str):
            self.pattern = re.compile(self.pattern)
        return self.pattern.search(string)

    def __ror__(self, iterable):
        return Stream()|(i for i in iterable if i & self)

    def __gt__(self, filename):
        return Stream()|(i for i in open(filename) if i & self)


import csv
import json
import os.path as osp

rows = []

# CSV got by https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol=GME&interval=5min&slice=year1month2&apikey=ALPHA_VANTAGE_API_KEY
# Where ALPHA_VANTAGE_API_KEY is your API key.
with open('raw_data/GME_y1m1_5min.csv') as f:
	rows.extend(csv.DictReader(f))

per_day_data = {}

for row in rows:
	day, _, tm = row['time'].partition(' ')
	per_day_data.setdefault(day, []).append(row)

for day, dt in per_day_data.iteritems():
	dt.sort(key=lambda x:x['time'])
	list_of_rows = []
	for x in dt:
		list_of_rows.append(['', float(x['low']), float(x['open']), float(x['close']), float(x['high'])])
	with open(osp.join('real_stonks', day), 'w') as f:
		json.dump(list_of_rows, f)


import os
import cv2
import torch
import torchvision
import torch.utils.data
import numpy as np
from tqdm import tqdm
from Net import Net

def recognize_digit(image: np.ndarray) -> int:
    transform = torchvision.transforms.Compose([
        torchvision.transforms.ToPILImage(),
        torchvision.transforms.Resize((224, 224)),
        torchvision.transforms.ToTensor(),
        torchvision.transforms.Lambda(lambda tensor: tensor.repeat(1, 3, 1, 1))
    ])

    data = transform(image).cuda()
    y_out = net(data)
    y_out = y_out.cpu()
    _, y_pred = torch.max(y_out, dim=1)

    return int(y_pred)

def move_digit_images() -> None:
    num_train = [0] * 10
    num_test = [0] * 10

    for digit_image in tqdm(os.listdir("../../data/digits/u")):
        src_path = os.path.join("../../data/digits/u", digit_image)
        image = cv2.imread(src_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        digit = recognize_digit(image)
        if np.random.random() < 0.8:
            if num_train[digit] >= 1000:
                break
            dst_path = os.path.join(f"../../data/digits/train/{digit}", f"{num_train[digit]}.jpg")
            num_train[digit] += 1
        else:
            if num_test[digit] >= 1000:
                break
            dst_path = os.path.join(f"../../data/digits/test/{digit}", f"{num_test[digit]}.jpg")
            num_test[digit] += 1
        os.rename(src_path, dst_path)

if __name__ == '__main__':
    net = Net().eval().cuda()
    net.load_state_dict(torch.load("mnist_5e.pth"))
    move_digit_images()


import constants
from sdfbuilder.physics import Friction
from sdfbuilder.structure import Collision
from sdfbuilder.util import number_format as nf
from sdfbuilder import Element

def apply_surface_parameters(model, intended_step_size=0.005):
    """
    Applies surface parameters to all collisions in the given model.
    :param model:
    :type model: Model
    :param intended_step_size:
    :return:
    """
    surface_kd = constants.SURFACE_ERP / (constants.SURFACE_CFM * intended_step_size)
    surface_kp = 1.0 / constants.SURFACE_CFM - (constants.SURFACE_ERP / constants.SURFACE_CFM)

    # Add friction surfaces to all body parts
    surf = Element(tag_name="surface")
    friction = Friction(
        friction=constants.SURFACE_FRICTION1,
        friction2=constants.SURFACE_FRICTION2,
        slip1=constants.SURFACE_SLIP1,
        slip2=constants.SURFACE_SLIP2
    )
    contact = "<contact>" \
              "<ode>" \
              "<kd>%s</kd>" \
              "<kp>%s</kp>" \
              "</ode>" \
              "</contact>" % (
                  nf(surface_kd), nf(surface_kp)
              )

    surf.add_element(contact)
    surf.add_element(friction)

    collisions = model.get_elements_of_type(Collision, recursive=True)
    for collision in collisions:
        collision.add_element(surf)