#coding: utf-8
import math
import numpy as np
import torch
from torch import nn
from torch.nn import Parameter
from torch.nn.modules.rnn import GRU
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence

from .cuda_helper import zeros, Tensor, LongTensor, cuda, ones
from .gumbel import gumbel_max, gumbel_max_with_mask
from .storage import Storage

from transformers import GPT2LMHeadModel

class DecoderBase(nn.Module):
	def __init__(self):
		super().__init__()

	def _freerun(self, inp, max_sent_length, nextStep, updateEOS=None, mode='max', top_k=10, temperature=1):
		# inp contains: batch_size, dm, embLayer, max_sent_length, [init_h]
		# nextStep(embedding, flag):  pass embedding to RNN and get gru_h, flag indicates i th sentence is end when flag[i]==1

		# output: w_o emb length

		batch_size = inp.batch_size

		gen = Storage()
		gen.w_pro = []
		gen.w_o = []
		flag = zeros(batch_size).int()
		EOSmet = []

		next_emb = LongTensor([inp.go_id]).repeat(batch_size, 1)

		for i in range(max_sent_length):
			now = next_emb

			w = nextStep(now, flag, i) / temperature

			gen.w_pro.append(w.softmax(dim=-1))
			if mode == "max":
				w = torch.argmax(w, dim=1)
			elif mode == "gumbel" or mode == "sample":
				w_onehot = gumbel_max(w)[0]
				w = torch.argmax(w_onehot, dim=1)
			elif mode == "samplek":
				_, index = w.topk(top_k, dim=-1, largest=True, sorted=True) # batch_size, top_k
				mask = torch.zeros_like(w).scatter_(-1, index, 1.0)
				w_onehot = gumbel_max_with_mask(w, mask)
				w = torch.argmax(w_onehot, dim=1)

			gen.w_o.append(w)
			next_emb = w.unsqueeze(-1)

			EOSmet.append(flag)
			flag = flag | (w == inp.eos_id).int()
			if updateEOS:
				flag = updateEOS(flag)

			if (i+1) % 10 == 0:
				if torch.sum(flag).detach().cpu().numpy() == batch_size:
					break

		EOSmet = 1 - torch.stack(EOSmet)
		gen.length = torch.sum(EOSmet.long(), 0).detach().cpu().numpy()
		seqlen = max(gen.length)
		EOSmet = EOSmet[:seqlen]
		gen.w_o = (torch.stack(gen.w_o[:seqlen]) * EOSmet.long()).transpose(0, 1)

		return gen

	def _beamsearch(self, inp, max_length, nextStep, top_k=10, length_penalty=0.7, temperature=1):

		batch_size = inp.batch_size

		w_o = []
		flag = zeros(batch_size, top_k).byte()
		EOSmet = []
		score = zeros(batch_size, top_k)
		score[:, 1:] = -1e8
		now_length = zeros(batch_size, top_k)
		back_index = []
		regroup = LongTensor([i for i in range(top_k)]).repeat(batch_size, 1)

		next_emb = LongTensor([inp.go_id]).repeat(batch_size, top_k)

		for i in range(max_length):
			now = next_emb

			w, valid_flag = nextStep(now, flag, i, regroup=regroup) # batch_size, top_k, hidden_size
			w = w / temperature
			valid_flag = (1 - flag.float()) * valid_flag.float()

			w = w.log_softmax(dim=-1)

			new_score = (score.unsqueeze(-1) + w * valid_flag.unsqueeze(-1)) / ((now_length.float() + valid_flag + 1e-9).unsqueeze(-1) ** length_penalty)
			new_score[:, :, 1:] = new_score[:, :, 1:] + (1 - valid_flag.float()).unsqueeze(-1) * new_score.min() * 10
			_, index = new_score.reshape(batch_size, -1).topk(top_k, dim=-1, largest=True, sorted=True) # batch_size, top_k

			sum_score = (score.unsqueeze(-1) + w * valid_flag.unsqueeze(-1)).reshape(batch_size, -1)
			# assert (regroup >= new_score.shape[1]).sum().tolist() == 0
			score = torch.gather(sum_score, dim=1, index=index)

			vocab_size = w.shape[-1]
			regroup = index / vocab_size # batch_size, top_k
			back_index.append(regroup)
			w_id = torch.fmod(index, vocab_size) # batch_size, top_k

			# regroup = cuda(torch.LongTensor(np.arange(10))).unsqueeze(0).repeat(32, 1)
			# _, w = w.max(dim=-1)

			# assert (regroup >= flag.shape[1]).sum().tolist() == 0
			flag = torch.gather(flag, dim=1, index=regroup)
			# assert (regroup >= now_length.shape[1]).sum().tolist() == 0
			now_length = torch.gather(now_length, dim=1, index=regroup) + valid_flag

			w_o.append(w_id)
			next_emb = w_id

			EOSmet.append(flag)

			flag = flag | ((w_id == inp.eos_id) * valid_flag).byte()

			if torch.sum(flag).detach().cpu().numpy() == batch_size * top_k:
				break

		#back tracking
		gen = Storage()
		back_EOSmet = []
		gen.w_o = []
		now_index = LongTensor([i for i in range(top_k)]).repeat(batch_size, 1)

		for i, index in reversed(list(enumerate(back_index))):
			gen.w_o.append(torch.gather(w_o[i], dim=1, index=now_index))
			back_EOSmet.append(torch.gather(EOSmet[i], dim=1, index=now_index))
			now_index = torch.gather(index, dim=1, index=now_index)

		back_EOSmet = 1-torch.stack(list(reversed(back_EOSmet)))
		gen.w_o = torch.stack(list(reversed(gen.w_o))) * back_EOSmet.long()
		gen.length = torch.sum(back_EOSmet, 0).detach().cpu().numpy()

		return gen

class GPT2Decoder(DecoderBase):
	def __init__(self, pretrained_model):
		super().__init__()
		self.model = GPT2LMHeadModel.from_pretrained(pretrained_model)

	def forward(self, incoming, attn_mask):
		logits, _ = self.model(incoming, attention_mask=attn_mask)
		return logits

	def init_forward(self):
		past = None

		def nextStep(incoming, stopmask, position):
			nonlocal past
			logits, present = self.model(input_ids=incoming, attention_mask=1-stopmask, past=past)
			past = present
			return logits[:, 0, :]

		return nextStep

	def init_forward_3d(self, batch_size, top_k):
		past = None
		i = 0

		def nextStep(incoming, stopmask, position, regroup=None):
			nonlocal past, i, top_k
			i = position

			new_incoming = incoming.unsqueeze(-1)

			flatten_batch = batch_size * top_k
			new_incoming = new_incoming.reshape(flatten_batch, -1)
			stopmask = stopmask.reshape(flatten_batch)

			if past:
				d1, d2, d3, d4, d5 = past[0].shape
				assert d2 == flatten_batch
				past = tuple(m.reshape(d1, batch_size, top_k, d3, d4, d5).\
						gather(2, index=regroup.unsqueeze(0).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).repeat(d1, 1, 1, d3, d4, d5)).\
						reshape(d1, flatten_batch, d3, d4, d5) for m in past)

			logits, present = self.model(input_ids=new_incoming, attention_mask=1-stopmask.unsqueeze(-1), \
				past=past)

			past = present

			valid_flag = ones(batch_size, top_k)
			return logits[:, 0, :].reshape(batch_size, top_k, -1), valid_flag

		return nextStep

	def freerun(self, inp, mode='max', top_k=10, temperature=1):
		nextStep = self.init_forward()
		return self._freerun(inp, inp.max_sent_length, nextStep, None, mode, top_k, temperature)

	# def beamsearch(self, inp, top_k, wLinearLayerCallback, input_callback=None, length_penalty=0.7):
	# 	nextStep = self.init_forward_3d(inp.batch_size, top_k, inp.get("init_h", None))
	# 	return self._beamsearch(inp, top_k, nextStep, wLinearLayerCallback, input_callback, length_penalty)

class GPT2Seq2seq(DecoderBase):
	def __init__(self, pretrained_model):
		super().__init__()
		self.model = GPT2LMHeadModel.from_pretrained(pretrained_model)

	def _concat(self, post, post_length, resp, resp_length, go_id):
		batch_size = len(post)
		concat_length = [post_length[i] + resp_length[i] - 1 for i in range(batch_size)]
		indexes = np.zeros((batch_size, max(concat_length)), dtype=int)
		incoming = ones(batch_size, max(concat_length), device=post).long() * go_id
		attn_mask = zeros(batch_size, max(concat_length), device=post)
		type_mask = zeros(batch_size, max(concat_length), device=post).long()

		for i in range(batch_size):
			indexes[i, :post_length[i]] = np.arange(post_length[i])
			indexes[i, post_length[i]:concat_length[i]] = np.arange(resp_length[i] - 1) + post.shape[1] + 1
			attn_mask[i, :concat_length[i]] = 1
			type_mask[i, post_length[i]:concat_length[i]] = 1
		incoming = torch.cat([post, resp], 1).gather(1, LongTensor(indexes))

		return incoming, attn_mask, type_mask

	def _split(self, incoming, post_length, resp_length):
		batch_size = len(incoming)
		indexes = np.zeros((batch_size, max(resp_length)), dtype=int)
		for i in range(batch_size):
			indexes[i, :resp_length[i]-1] = np.arange(resp_length[i] - 1) + post_length[i] - 1
			indexes[i, resp_length[i]-1:] = resp_length[i] - 1 + post_length[i] - 1

		if len(incoming.shape) == 2:
			res = torch.gather(incoming, 1, LongTensor(indexes))
		else:
			res = torch.gather(incoming, 1, LongTensor(indexes).unsqueeze(-1).expand(-1, -1, incoming.shape[-1]))
		return res

	def forward(self, post, post_length, resp, resp_length, go_id):
		incoming, attn_mask, type_mask = self._concat(post, post_length, resp, resp_length, go_id)
		logits, _ = self.model(incoming, attention_mask=attn_mask, token_type_ids=type_mask)
		return self._split(logits, post_length, resp_length)

	def init_forward(self, batch_size, post, post_length):
		past = None
		i = 0
		type_mask = ones(batch_size, post.shape[1], device=post).long()
		for i in range(batch_size):
			type_mask[i, :post_length[i]] = 0

		def nextStep(incoming, stopmask, position):
			nonlocal past, i
			i = position

			if i < post.shape[1]:
				new_incoming = post[:, i:i+1] * (1 - type_mask[:, i:i+1]) + incoming * type_mask[:, i:i+1]
				new_type_mask = type_mask[:, i:i+1]
			else:
				new_incoming = incoming
				new_type_mask = torch.ones_like(type_mask[:, :1])

			logits, present = self.model(input_ids=new_incoming, attention_mask=1-stopmask.unsqueeze(-1), \
				token_type_ids=new_type_mask, past=past)
			past = present
			return logits[:, 0, :]

		def updateStopMask(stopmask):
			nonlocal i
			if i < post.shape[1]:
				return type_mask[:, i].int() * stopmask
			else:
				return stopmask

		return nextStep, updateStopMask

	def init_forward_3d(self, batch_size, post, post_length, top_k):
		past = None
		i = 0
		type_mask = ones(batch_size, post.shape[1], device=post).long()
		for i in range(batch_size):
			type_mask[i, :post_length[i]] = 0
		type_mask = type_mask.unsqueeze(1).expand(batch_size, top_k, post.shape[1])

		def nextStep(incoming, stopmask, position, regroup=None):
			nonlocal past, i, post
			i = position

			if i < post.shape[1]:
				post_3d = post[:, i:i+1].unsqueeze(1).expand(batch_size, top_k, 1)
				new_incoming = post_3d * (1 - type_mask[:, :, i:i+1]) + incoming.unsqueeze(-1) * type_mask[:, :, i:i+1]
				new_type_mask = type_mask[:, :, i:i+1]
			else:
				new_incoming = incoming.unsqueeze(-1)
				new_type_mask = torch.ones_like(type_mask[:, :, :1])

			flatten_batch = batch_size * top_k
			new_incoming = new_incoming.reshape(flatten_batch, -1)
			new_type_mask = new_type_mask.reshape(flatten_batch, -1)
			stopmask = stopmask.reshape(flatten_batch)

			if past:
				d1, d2, d3, d4, d5 = past[0].shape
				assert d2 == flatten_batch
				past = tuple(m.reshape(d1, batch_size, top_k, d3, d4, d5).\
						gather(2, index=regroup.unsqueeze(0).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).repeat(d1, 1, 1, d3, d4, d5)).\
						reshape(d1, flatten_batch, d3, d4, d5) for m in past)

			logits, present = self.model(input_ids=new_incoming, attention_mask=1-stopmask.unsqueeze(-1), \
				token_type_ids=new_type_mask, past=past)

			past = present

			if i + 1 < post.shape[1]:
				valid_flag = type_mask[:, :, i + 1].int()
			else:
				valid_flag = torch.ones_like(type_mask[:, :, 0])
			return logits[:, 0, :].reshape(batch_size, top_k, -1), valid_flag

		return nextStep

	def freerun(self, inp, mode='max', top_k=10, temperature=1):
		nextStep, updateStopMask = self.init_forward(inp.batch_size, inp.post, inp.post_length)
		gen = self._freerun(inp, inp.max_sent_length + inp.post.shape[1], nextStep, updateStopMask, mode, top_k, temperature)
		res = Storage()
		res.length = np.minimum(gen.length - inp.post_length, np.array([inp.max_sent_length - 1]))
		res.w_o = self._split(gen.w_o, inp.post_length, res.length + 1)
		return res

	def beamsearch(self, inp, top_k, length_penalty=0.7, temperature=1):
		nextStep = self.init_forward_3d(inp.batch_size, inp.post, inp.post_length, top_k)
		gen = self._beamsearch(inp, inp.max_sent_length + inp.post.shape[1], nextStep, top_k, length_penalty, temperature)
		res = Storage()
		res.length = np.minimum(gen.length[:, 0] - inp.post_length, np.array([inp.max_sent_length - 1]))
		res.w_o = self._split(gen.w_o[:, :, 0].transpose(0, 1), inp.post_length, res.length + 1)
		return res


import datetime as dt
from stock_period import *
from technical_features import *
from learn_scorer import *
import classify as cl
from sklearn.ensemble import RandomForestClassifier

def convert_to_sparse(ex, num_class):
    res = [0] * num_class
    res[ex] = 1
    return res

def all_to_sparse(exs, num_class):
    return map(convert_to_sparse, exs, [num_class] * len(exs))

class LearnerWrapper():
    def __init__(self, learner):
        self.learner = learner

    def convert_to_index(self, y):
        result = []
        for x in y:
            if sum(x) == 0:
                result.append(1)
            else:
                result.append(list(x).index(1))
        return result

    def fit(self, X, y):
        self.learner.fit(X, self.convert_to_index(y))

    def predict(self, X):
        res = self.learner.predict(X)
        return all_to_sparse(res, 3)

class MachineLearner():
    default_random_forest_options = {
        "n_estimators": 50,
        "criterion": "gini",
        "max_features": "auto",
        "max_depth": None,
        "min_samples_split": 1
        }
    def __init__(self,
                 symbol,
                 classifier = cl.BuySellClassifier(threshold=0.005, days=6),
                 feature_gen = TechnicalFeatures,
                 stock_period = False,
                 random_forest_options = False):
        self.random_forest_options = random_forest_options
        self.symbol = symbol
        if not stock_period:
            self.sp = StockPeriod(self.symbol, dt.datetime(2000,1,1), self._today())
        else:
            self.sp = stock_period
        self.feats = feature_gen(self.sp)
        self.learner = self.create_learner()
        self.classifier = classifier
        self.classified = self.classifier.classify(self.feats)
        self.absolute_classified = self.classifier.absolute_classify(self.feats)

    def _today(self):
        now = dt.datetime.now()
        return dt.datetime(now.year, now.month, now.day)

    def create_learner(self):
        #return LearnerWrapper(KNeighborsClassifier(n_neighbors=10, weights='distance', algorithm='auto', leaf_size=60))
        #if self.random_forest_options:
        #    options = dict(self.default_random_forest_options.items() +
        #                   self.random_forest_options.items())
        #    print options
        #    return RandomForestClassifier(n_estimators = options["n_estimators"],
        #                                  criterion = options["criterion"],
        #                                  max_features=options["max_features"],
        #                                  max_depth=options["max_depth"],
        #                                  min_samples_split=options["min_samples_split"],
        #                                  random_state=0)
        #else:
        return RandomForestClassifier(n_estimators=50, max_depth=None, min_samples_split=1, random_state=0)

    def slice_x_and_y(self, start_date, end_date):
        X = self.feats.slice_features(start_date, end_date)
        y = self.classified[start_date:end_date]
        if y.ix[-1] == None:
            raise Exception('not enough feature data to fill y array')
        # date ranges should be equal
        if not all(X.index == y.index):
            raise Exception('bad date index alignment')
        return X, y

    def learn_period(self, start_date, end_date):
        X, y = self.slice_x_and_y(start_date, end_date)
        self.learner.fit(X.values, list(y.values))

    def predict(self, X_values):
        predictions = self.learner.predict(X_values)
        return self.clean_predictions(predictions)

    def clean_predictions(self, predictions):
        predictions[np.where(predictions.sum(axis=1) == 0), 1] = 1
        return predictions

    def predict_period(self, start_date, end_date):
        X = self.feats.slice_features(start_date, end_date)
        #X = X.fillna(method = 'ffill')
        predictions = self.predict(X.values)
        result = pd.DataFrame(index = X.index, data= predictions)
        return result

    def validate_period(self, start_date, end_date):
        X, y = self.slice_x_and_y(start_date, end_date)
        #X = X.fillna(method = 'ffill')
        absolute_y = self.absolute_classified[start_date:end_date]
        predictions = self.predict(X.values)
        pred_frame = pd.DataFrame(index = X.index, data= predictions)
        return LearnScorer(y,
                           absolute_y,
                           pred_frame,
                           self.feats.relative_data['close'][self.symbol],
                           self.classifier.days)

    def validate_period_for_predictions(self, predictions, start_date, end_date):
        y = self.classified[start_date:end_date]
        if y.ix[-1] == None:
            raise Exception('not enough feature data to fill y array')
        #X = X.fillna(method = 'ffill')
        absolute_y = self.absolute_classified[start_date:end_date]
        return LearnScorer(y,
                           absolute_y,
                           predictions[start_date:end_date],
                           self.feats.relative_data['close'][self.symbol],
                           self.classifier.days)

    def predict_move_for_day(self, date):
        previous_date_key = self.sp.previous_nyse_day(date)
        feature = self.feats.feature_for_date(previous_date_key)
        return self.predict(feature.values)[0]


from flask import Flask, Markup, render_template,redirect, url_for, request,make_response
import pandas as pd
import json
from flask import jsonify
import datetime
import pymysql
from sqlalchemy import create_engine

app = Flask(__name__)

session = create_engine('mysql+pymysql://b1988b353f38a9:eb355ccd@us-cdbr-east-02.cleardb.com:3306/heroku_57e05bf7b956609')

@app.route('/fetchalliteams', methods = ['GET', 'POST'])
def fetchalliteams():
    result = pd.read_sql("select * from amazon_shoe_store",con=session)
    #print(result)
    return render_template('index.html',tables=[result.to_html(classes='result', header="true")])

@app.route('/selecteditem', methods = ['GET', 'POST'])
def selecteditem():
    selectedprodid=request.form['productid']
    result = pd.read_sql("select * from amazon_shoe_store where sell_price = {0}".format(float(selectedprodid)),con=session)
    return render_template('index.html',tables=[result.to_html(classes='result', header="true")])

@app.route('/range', methods = ['GET', 'POST'])
def range():
    selectedprodidmin=request.form['productidmin']
    selectedprodidmax = request.form['productidmax']
    result = pd.read_sql("select * from amazon_shoe_store where sell_price >= {0} and sell_price <= {1}".format(float(selectedprodidmin),float(selectedprodidmax)),con=session)
    return render_template('index.html',tables=[result.to_html(classes='result', header="true")])

def define_discount(x,maximum_discount):
    if x> 0 and x <= 20:
        return 'Up to 20% Discount'
    elif x> 20 and x<=50:
        return 'Minimum 20% to Up to 50% Discount'
    else:
        return 'More than 50% Discount Up to {0}%'.format(maximum_discount)

def reviewer_range(x):
    if x>= 1 and x <= 2:
        return 'Rating from 1.0 to 2.0'
    elif x>= 2 and x <= 3:
        return 'Rating from 2.0 to 3.0'
    elif x>= 3 and x <= 4:
        return 'Rating from 3.0 to 4.0'
    elif x>= 4 and x <= 5:
        return 'Rating from 4.0 to 5.0'

@app.route('/charts')
def charts():
    cleaned_dataset = pd.read_sql("select mrp_price,sell_price from amazon_shoe_store",con=session)
    discount = cleaned_dataset[(cleaned_dataset.mrp_price - cleaned_dataset.sell_price)>0][['mrp_price','sell_price']]
    discount['discount_precentage'] = round(((cleaned_dataset.mrp_price - cleaned_dataset.sell_price)/cleaned_dataset.mrp_price)*100,2)
    mimimum_discount = discount.discount_precentage.min()
    maximum_discount = discount.discount_precentage.max()
    discount['category'] = discount['discount_precentage'].apply(lambda x:define_discount(x,maximum_discount))
    d = discount.category.value_counts()
    discount_chart = pd.DataFrame({'discount_category':d.keys(),'number_of_products':d.values})

    xaxis=discount_chart['discount_category'].values.tolist()
    yaxis=discount_chart['number_of_products'].values.tolist()
    labels = xaxis
    values = yaxis

    colors = [
        "#32CD32", "#FDB45C", "#B22222"]
    bar_labels = labels
    bar_values = values
    max_v = discount_chart.number_of_products.max() + 100
    varaiable1 = str(discount_chart.number_of_products.sum())
    varaiable2 = str(mimimum_discount)+'%'
    varaiable3 = str(maximum_discount)+'%'

    return render_template('charts.html',var1=varaiable1,var2=varaiable2,var3=varaiable3, title='Number of products in each discount category', max=max_v, set=zip(values, labels, colors))

@app.route('/charts1')
def charts1():
    cleaned_dataset = pd.read_sql("select rating from amazon_shoe_store",con=session)
    ratings = cleaned_dataset[['rating']]
    rat = ratings.rating.value_counts()
    rating_chart = pd.DataFrame({'rating':rat.keys(),'number_of_products':rat.values})
    rating_chart = rating_chart[['rating','number_of_products']].sort_values('rating').reset_index(drop='index')

    xaxis=rating_chart['rating'].values.tolist()
    yaxis=rating_chart['number_of_products'].values.tolist()

    labels = xaxis
    values = yaxis
    bar_labels = labels
    bar_values = values
    max_v2 = rating_chart.number_of_products.max()+100

    return render_template('charts1.html', title='Rating wise Number of Products', max=max_v2, labels=bar_labels, values=bar_values)

@app.route('/charts2')
def charts2():
    cleaned_dataset = pd.read_sql("select product,sell_price,rating,reviews from amazon_shoe_store",con=session)
    reviewers = cleaned_dataset[['product','sell_price','rating','reviews']]
    reviewers['rating_range'] = reviewers['rating'].apply(lambda x:reviewer_range(x))
    reviewers = reviewers.groupby('rating_range').agg({'reviews':['min','max'],'sell_price':['min','max']}).reset_index()
    print(reviewers)
    reviewers.columns = ['rating','minimum_reviewers','maximum_reviewers','min_price_product','max_price_product']
    xaxis=reviewers['rating'].values.tolist()
    yaxis=reviewers['minimum_reviewers'].values.tolist()
    #print(yaxis)
    labels = xaxis
    values = yaxis

    bar_labels = labels
    bar_values = values
    max1 = reviewers['minimum_reviewers'].max() +1

    bar_labels1 = xaxis
    bar_values1 = reviewers['maximum_reviewers'].values.tolist()
    max2 = reviewers['maximum_reviewers'].max() +100

    bar_labels2 = xaxis
    bar_values2 = reviewers['min_price_product'].values.tolist()
    max3 = reviewers['min_price_product'].max() +10

    bar_labels3 = xaxis
    bar_values3 = reviewers['max_price_product'].values.tolist()
    max4 = reviewers['max_price_product'].max() + 50
    print(max1,max2,max3,max4)
    return render_template('charts2.html', title='', max1=max1,max2=max2,max3=max3,max4=max4, labels=bar_labels, values=bar_values,labels1=bar_labels1, values1=bar_values1,labels2=bar_labels2, values2=bar_values2,labels3=bar_labels3, values3=bar_values3)

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

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


import unittest
import json
import UserNRank

class Unittest(unittest.TestCase):
    def test(self):
        input = {"name": "First", "point": 10}
        self.assertEqual(UserNRank.ranking(), json.dumps([{"name": "First", "point": 11}, {"name": "hi9", "point": 9}, {"name": "hi8", "point": 8}, {"name": "hi7", "point": 7}, {"name": "hi6", "point": 6}]))
        self.assertEqual(UserNRank.createUser(json.dumps(input)), json.dumps("choose a new name"))

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


from __future__ import absolute_import

import functools
import logging
import six

from ably.util.unicodemixin import UnicodeMixin

log = logging.getLogger(__name__)

class AblyException(BaseException, UnicodeMixin):
    def __init__(self, reason, status_code, code):
        super(AblyException, self).__init__()
        self.reason = reason
        self.code = code
        self.status_code = status_code

    def __unicode__(self):
        return six.u('%s %s %s') % (self.code, self.status_code, self.reason)

    @staticmethod
    def raise_for_response(response):
        if response.status_code >= 200 and response.status_code < 300:
            # Valid response
            return

        try:
            json_response = response.json()
            if json_response:
                try:
                    raise AblyException(json_response['reason'],
                                        json_response['statusCode'],
                                        json_response['code'])
                except KeyError:
                    msg = "Unexpected exception decoding server response: %s"
                    msg = msg % response.text
                    raise AblyException(msg, 500, 50000)
        except:
            log.debug("Response: %d %s", response.status_code, response.text)
            raise AblyException(
                response.text,
                response.status_code,
                response.status_code * 100)

        raise AblyException("", response.status_code, response.status_code*100)

    @staticmethod
    def from_exception(e):
        if isinstance(e, AblyException):
            return e
        return AblyException("Unexpected exception: %s" % e, 500, 50000)

def catch_all(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            log.exception(e)
            raise AblyException.from_exception(e)

    return wrapper


"""Telegram bot interface."""

import time
import random
import requests

import telegram
from telegram.ext import (
    Updater,
    MessageHandler,
    CommandHandler,
    Filters,
)

from assistant.utils import colored

from assistant.custom.telegram_config import (
    TOKEN,
    ALLOWED_USERS_DICT,
    KEYBOARD,
    KEYS_ACTIONS,
)
# custom functions/objects
from assistant.custom.telegram_config import *   # noqa: F403, F401

KEYS = set(key for row in KEYBOARD for key in row)

def handle_buttons(bot, update):
    text = update.message.text
    if text in KEYS:
        exec(KEYS_ACTIONS[text])
        return True

def get_full_name(update):
    name = (update.message.chat.first_name, update.message.chat.last_name)
    return " ".join(w for w in name if w)

def allowed_users_only(func):
    def wrapper(self, bot, update):
        if update.message.chat_id in ALLOWED_USERS_DICT.values():
            func(self, bot, update)
        else:
            bot.send_message(text="Access denied.",
                             chat_id=update.message.chat_id)
            # let admin know
            msg = (
                "Unauthorized user "
                f"{get_full_name(update)}({update.message.chat_id}) "
                f"sent message: {update.message.text}"
            )
            bot.send_message(text=msg, chat_id=ALLOWED_USERS_DICT["admin"])
    return wrapper

class TelegramBot(Updater):
    """Telegram bot interface."""

    def __init__(
        self,
        assistant,
        token=TOKEN,
        allowed_users=ALLOWED_USERS_DICT,
    ):
        """Init."""
        Updater.__init__(self, token=token)

        self.assistant = assistant
        self.admin_id = allowed_users["admin"]
        self.users = allowed_users
        self.nlp = assistant.nlp

    @property
    def last_id(self):
        try:
            return self.last_update.message.chat_id
        except NameError:
            return self.users["admin"]

    @allowed_users_only
    def send_keyboard(self, bot, update):
        """Send custom keyboard to user"""
        reply_markup = telegram.ReplyKeyboardMarkup(KEYBOARD)
        bot.send_message(
            chat_id=update.message.chat_id,
            text="Custom keyboard",
            reply_markup=reply_markup,
        )

    @allowed_users_only
    def handle_message(self, bot, update):
        """A text message handler method."""
        self._describe_message(update)
        self.last_update = update

        text = update.message.text
        print(text)

        if not handle_buttons(bot, update):
            self._handle_text(text)

    @allowed_users_only
    def handle_voice(self, bot, update):
        self.last_update = update
        voice_bytes = bytes(
            update.message.voice.get_file(
            ).download_as_bytearray()
        )
        text = self.assistant.voice.recognize_from_bytes(voice_bytes)
        self._handle_text(text)

    def _handle_text(self, text):
        """Calls handle function even if there not enough
        info in the text, handle function will then
        either assume the lacking information or do nothing.
        """

        for struct in self.nlp.structs(text):
            if struct not in self.nlp.completed:
                self.assistant.skills.handle(
                    text_struct=struct, interface=self
                )
                self.nlp.completed.append(struct)
        self.nlp.previous = self.nlp.completed
        self.nlp.completed = []

    def _describe_message(self, update):
        name = colored(
            get_full_name(update),
            "OKBLUE",
            frame=False,
        )
        sender_id = update.message.chat_id
        report = f"{name} ({sender_id}): {update.message.text}"
        print(report)

    def input(self, text, regex=None, chat_id=None):
        if not chat_id:
            chat_id = self.last_id
        print(" *** UPDATES ***")
        print(self.bot.get_updates())

        last_message_id = self.bot.get_updates()[-1].message.message_id
        self.bot.send_message(chat_id=chat_id, text=text)

        # wait for the answer
        while True:
            update = self.bot.get_updates()[-1]
            if all(
                update.message.message_id != last_message_id,
                update.message.chat_id == chat_id,
            ):
                return update.message.text
            time.sleep(0.1)

    def output(self, text, chat_id=None, prob=1):
        if prob == 1 or random.random() < prob:
            if not chat_id:
                chat_id = self.last_id
            self.bot.send_message(chat_id=chat_id, text=text)

    def run(self):
        """Run telegram bot."""
        print(colored("Telegram bot: active", "OKGREEN", frame=False))
        text_message_handler = MessageHandler(
            Filters.text, self.handle_message
        )
        voice_message_handler = MessageHandler(
            Filters.voice, self.handle_voice
        )
        keyboard_handler = CommandHandler("keyboard", self.send_keyboard)

        self.dispatcher.add_handler(text_message_handler)
        self.dispatcher.add_handler(voice_message_handler)
        self.dispatcher.add_handler(keyboard_handler)

        self.start_polling(clean=True)


# -*- coding: utf8 -*-
from flask import request
from flask.globals import _request_ctx_stack
from werkzeug.datastructures import ImmutableMultiDict, CombinedMultiDict, MultiDict

class ServerRequest(object):
    """
    To understand flask.request: https://www.cnblogs.com/MnCu8261/p/10013702.html
        replace current Request object with ServerRequest
        remain the original Request methods and attributes except args/form/json/values
    """

    def __init__(self, data):
        self._raw_request = request._get_current_object()

        self.args = ImmutableMultiDict()
        self.form = ImmutableMultiDict()
        self.json = {}
        if request.method.lower() == "get":
            self.args = ImmutableMultiDict(data)
        elif request.content_type == "application/json":
            self.json = data
        else:
            self.form = ImmutableMultiDict(data)

        setattr(_request_ctx_stack._local.stack[-1], "request", self)

    def __getattr__(self, item):
        return getattr(self._raw_request, item)

    @property
    def values(self):
        args = []
        for d in self.args, self.form:
            if not isinstance(d, MultiDict):
                d = MultiDict(d)
            args.append(d)
        return CombinedMultiDict(args)


import sqlite3
from utils import display
from PyQt5.QtWidgets import QDialog
from PyQt5.QtCore import pyqtSlot
from PyQt5 import uic

# Classe permettant d'afficher la fenêtre de visualisation des données
class AppTablesData(QDialog):

    # Constructeur
    def __init__(self, data:sqlite3.Connection):
        super(QDialog, self).__init__()
        self.ui = uic.loadUi("gui/tablesData.ui", self)
        self.data = data

        # On met à jour l'affichage avec les données actuellement présentes dans la base
        self.refreshAllTables()

    ####################################################################################################################
    # Méthodes permettant de rafraichir les différentes tables
    ####################################################################################################################

    # Fonction de mise à jour de l'affichage d'une seule table
    def refreshTable(self, label, table, query):
        display.refreshLabel(label, "")
        try:
            cursor = self.data.cursor()
            result = cursor.execute(query)
        except Exception as e:
            table.setRowCount(0)
            display.refreshLabel(label, "Impossible d'afficher les données de la table : " + repr(e))
        else:
            display.refreshGenericData(table, result)

    # Fonction permettant de mettre à jour toutes les tables
    @pyqtSlot()
    def refreshAllTables(self):
        self.refreshTable(self.ui.label_equipiers, self.ui.tableEquipiers, "SELECT numEq, numSp  FROM LesEquipiers")
        self.refreshTable(self.ui.label_epreuves, self.ui.tableEpreuves, "SELECT nomDi, numEp, nomEp, formeEp, categorieEp, nbSportifsEp, dateEp FROM LesEpreuves")
        self.refreshTable(self.ui.label_inscriptions, self.ui.tableInscriptions, "SELECT numIn, numEp FROM LesInscriptions")
        self.refreshTable(self.ui.label_resultats, self.ui.tableResultats, "SELECT numEp, gold, silver, bronze FROM LesResultats")
        self.refreshTable(self.ui.label_sportifs, self.ui.tableSportifs, "SELECT numSp, nomSp, prenomSp, pays, categorieSp,age FROM LesSportifs")
        self.refreshTable(self.ui.label_disciplines, self.ui.tableDisciplines,"SELECT nomDi FROM LesDisciplines")
        self.refreshTable(self.ui.label_disciplines, self.ui.tableEquipes, "SELECT numEq,nbEquipiersEq FROM LesEquipes")


from django.shortcuts import render, redirect
from district.models import District, Taluka, Village
from .forms import BankForm, BranchForm
from django.contrib.auth.decorators import login_required

# Create your views here.
from .models import Bank, Branch

@login_required(login_url='login')
def add_bank(request):
    forms = BankForm()
    if request.method == 'POST':
        forms = BankForm(request.POST)
        if forms.is_valid():
            forms.save()
            return redirect('bank')
    bank = Bank.objects.all()
    context = {'forms': forms, 'bank': bank}
    return render(request, 'bank/bank.html', context)

@login_required(login_url='login')
def add_branch(request):
    forms = BranchForm()
    if request.method == 'POST':
        forms = BranchForm(request.POST)
        if forms.is_valid():
            forms.save()
            return redirect('branch')
    branch = Branch.objects.all()
    context = {'forms': forms, 'branch': branch}
    return render(request, 'bank/branch.html', context)

@login_required(login_url='login')
def branch_list(request):
    branch = Branch.objects.all()
    context = {'branch': branch}
    return render(request, 'bank/branch_list.html', context)

@login_required(login_url='login')
def load_taluka(request):
    district_id = request.GET.get('district')
    taluka = Taluka.objects.filter(district_id=district_id).order_by('name')

    taluka_id = request.GET.get('taluka')
    village = Village.objects.filter(taluka_id=taluka_id).order_by('name')
    context = {
        'taluka': taluka,
        'village': village
    }
    return render(request, 'others/taluka_dropdown_list_options.html', context)


import numpy as np
import scipy.constants as sp

N = int(1e6)
a = 1e-6

# Product and then sum
aProdSum = 0
for i in range(N):
    aProdSum += a * sp.mu_0
print ("Prod Sum diff: %f\n", aProdSum - sp.mu_0)

# Sum and then product
aSumProd = 0
for i in range(N):
    aSumProd += a
aSumProd *= sp.mu_0
print ("Sum Prod diff: %f\n", aSumProd - sp.mu_0)

# Rounding
for i in range (20):
    print ( i, i/10, i*.1 )

print("END")


#!/usr/bin/python
#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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
#
#   http://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.
#
#-------------------------------------------------------------

# Assumption: pip install keras
#
# This test validates SystemML's deep learning APIs (Keras2DML, Caffe2DML and nn layer) by comparing the results with that of keras.
#
# To run:
#   - Python 2: `PYSPARK_PYTHON=python2 spark-submit --master local[*] --driver-memory 10g --driver-class-path SystemML.jar,systemml-*-extra.jar test_nn_numpy.py`
#   - Python 3: `PYSPARK_PYTHON=python3 spark-submit --master local[*] --driver-memory 10g --driver-class-path SystemML.jar,systemml-*-extra.jar test_nn_numpy.py`

# Make the `systemml` package importable
import os
import sys
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")
sys.path.insert(0, path)

import unittest

import numpy as np
from keras.models import Sequential
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, Flatten, LSTM, UpSampling2D, SimpleRNN
from keras import backend as K
from keras.models import Model
from systemml.mllearn import Keras2DML
from pyspark.sql import SparkSession

batch_size = 32
input_shape = (3,64,64)
K.set_image_data_format("channels_first")
# K.set_image_dim_ordering("th")
keras_tensor = np.random.rand(batch_size,input_shape[0], input_shape[1], input_shape[2])
sysml_matrix = keras_tensor.reshape((batch_size, -1))
tmp_dir = 'tmp_dir'

spark = SparkSession.builder.getOrCreate()

def are_predictions_all_close(keras_model, rtol=1e-05, atol=1e-08):
    sysml_model = Keras2DML(spark, keras_model, input_shape=input_shape, weights=tmp_dir)
    keras_preds = keras_model.predict(keras_tensor).flatten()
    sysml_preds = sysml_model.predict_proba(sysml_matrix).flatten()
    #print(str(keras_preds))
    #print(str(sysml_preds))
    return np.allclose(keras_preds, sysml_preds, rtol=rtol, atol=atol)

class TestNNLibrary(unittest.TestCase):
    def test_1layer_upsample_predictions1(self):
        keras_model = Sequential()
        keras_model.add(UpSampling2D(size=(2, 2), input_shape=input_shape))
        keras_model.add(Flatten())
        keras_model.add(Dense(10, activation='softmax'))
        self.failUnless(are_predictions_all_close(keras_model, atol=1e-06))

    def test_1layer_upsample_predictions2(self):
        keras_model = Sequential()
        keras_model.add(UpSampling2D(size=(2, 3), input_shape=input_shape))
        keras_model.add(Flatten())
        keras_model.add(Dense(10, activation='softmax'))
        self.failUnless(are_predictions_all_close(keras_model, atol=1e-06))

    def test_1layer_cnn_predictions(self):
        keras_model = Sequential()
        keras_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape, padding='valid'))
        keras_model.add(Flatten())
        keras_model.add(Dense(10, activation='softmax'))
        self.failUnless(are_predictions_all_close(keras_model))

    def test_multilayer_cnn_predictions(self):
        keras_model = Sequential()
        keras_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape, padding='valid'))
        keras_model.add(MaxPooling2D(pool_size=(2, 2)))
        keras_model.add(Conv2D(64, (3, 3), activation='relu'))
        keras_model.add(MaxPooling2D(pool_size=(2, 2)))
        keras_model.add(Flatten())
        keras_model.add(Dense(256, activation='softmax'))
        keras_model.add(Dropout(0.25))
        keras_model.add(Dense(10, activation='softmax'))
        self.failUnless(are_predictions_all_close(keras_model))

    def test_simplernn_predictions1(self):
        data_dim = 16
        timesteps = 8
        num_classes = 10
        batch_size = 64
        model = Sequential()
        model.add(SimpleRNN(32, return_sequences=False, input_shape=(timesteps, data_dim)))
        model.add(Dense(10, activation='softmax'))
        x_train = np.random.random((batch_size, timesteps, data_dim))
        y_train = np.random.random((batch_size, num_classes))
        from systemml.mllearn import Keras2DML
        sysml_model = Keras2DML(spark, model, input_shape=(timesteps,data_dim,1), weights='weights_dir').set(debug=True)
        keras_preds = model.predict(x_train).flatten()
        sysml_preds = sysml_model.predict_proba(x_train.reshape((batch_size, -1))).flatten()
        self.failUnless(np.allclose(sysml_preds, keras_preds))

    def test_simplernn_predictions2(self):
        data_dim = 16
        timesteps = 8
        num_classes = 10
        batch_size = 100
        model = Sequential()
        model.add(SimpleRNN(32, return_sequences=False, input_shape=(timesteps, data_dim)))
        model.add(Dense(10, activation='softmax'))
        x_train = np.random.random((batch_size, timesteps, data_dim))
        y_train = np.random.random((batch_size, num_classes))
        from systemml.mllearn import Keras2DML
        sysml_model = Keras2DML(spark, model, input_shape=(timesteps,data_dim,1), weights='weights_dir').set(debug=True)
        keras_preds = model.predict(x_train).flatten()
        sysml_preds = sysml_model.predict_proba(x_train.reshape((batch_size, -1))).flatten()
        self.failUnless(np.allclose(sysml_preds, keras_preds))

    def test_lstm_predictions1(self):
        data_dim = 32
        timesteps = 8
        num_classes = 10
        batch_size = 64
        w1 = np.random.random((data_dim, 4*data_dim))
        w2 = np.random.random((data_dim, 4*data_dim))
        b = np.zeros(128)
        model = Sequential()
        model.add(LSTM(32, return_sequences=False, recurrent_activation='sigmoid', input_shape=(timesteps, data_dim),  weights=[w1, w2, b]))
        model.add(Dense(10, activation='softmax'))
        x_train = np.random.random((batch_size, timesteps, data_dim))
        y_train = np.random.random((batch_size, num_classes))
        from systemml.mllearn import Keras2DML
        sysml_model = Keras2DML(spark, model, input_shape=(timesteps,data_dim,1), weights='weights_dir').set(debug=True)
        keras_preds = model.predict(x_train)
        sysml_preds = sysml_model.predict_proba(x_train.reshape((batch_size, -1)))
        np.allclose(sysml_preds, keras_preds)

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


import numpy as np
import cv2
from cv_utils import add_text

## Convert COCO keypoints to PoseTrack keypoints
def conver_coco_poseTrack(kps):
    """
        input:  N, 17, 3
            kp_names = [0:'nose', 1:'l_eye', 2:'r_eye', 3:'l_ear', 4:'r_ear', 5:'l_shoulder',
                        6:'r_shoulder', 7:'l_elbow', 8:'r_elbow', 9:'l_wrist', 10:'r_wrist',
                        11:'l_hip', 12:'r_hip', 13:'l_knee', 14:'r_knee', 15:'l_ankle', 16:'r_ankle']
        output: N, 15, 3
            {0: 'right_ankle', 1: 'right_knee', 2: 'right_hip', 3: 'left_hip', 4:'left_knee', 5:'left_ankle',  6:'right_wrist', 7:'right_elbow', 8:'right_shoulder',9:'left_shoulder', 10:'left_elbow', 11:'left_wrist',12:'head_bottom', 13:'nose', 14'head_top']

    """
    MAP ={16:0, 14:1, 12:2, 11:3, 13:4, 15:5, 10:6, 8:7, 6:8, 5:9, 7:10, 9:11, 0:13}

    pose_track_kps = np.zeros((kps.shape[0], 15, 3))

    for k in range(kps.shape[0]):
        pivel = (kps[k][5] + kps[k][6])/2
        head_bottom = (pivel+kps[k][0])/2 # 12
        head_top = 2*kps[k][0] - head_bottom # 14
        head_top[0] = (head_bottom[0] + kps[k][0][0])/2 #改变x

        for coco_i, pt_j in MAP.items():
            pose_track_kps[k][pt_j] = kps[k][coco_i]
        pose_track_kps[k][12] = head_bottom
        pose_track_kps[k][14] = head_top

    return pose_track_kps

def draw_bbox(img, bbox, text=None, change_color=None):
    x1, y1, w, h = bbox
    pt1 = (int(x1), int(y1))
    pt2 = (int(x1+w), int(y1+h))
    if change_color:
        img = cv2.rectangle(img, pt1, pt2, (2,0,2), 3, 2)
    else:
        img = cv2.rectangle(img, pt1, pt2, (255,0,255), 3, 2)

    if text:
        img = add_text(img, str(text), pt1, 1)
    return img


import datetime
import os

from smartninja_mongo.odm import Model
from models.database import mongo_db
import smartninja_redis

collection = mongo_db.bitts
redis = smartninja_redis.from_url(os.environ.get("REDIS_URL"))

class Bitt(Model):
    def __init__(self, text, username, **kwargs):
        self.username = username
        self.text = text
        self.created = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')

        if type(self.text) is not str:
            raise TypeError("Bitt text must be a string!")

        super().__init__(**kwargs)

    def insert(self):
        bitt_id = collection.insert_one(self.__dict__).inserted_id
        self._id = bitt_id  # save id from MongoDB into the Bitt object
        self.id = str(bitt_id)  # convert Mongo's ObjectId to string

        # save ID into Redis (last Bitt ID)
        redis.set(name="last-bitt-id", value=str(bitt_id))

        return bitt_id

    @classmethod
    def get_all_bitts(cls):
        bitts = collection.find()

        # some pre-made bitts (you can delete them)
        bitts_dicts = [
            {
                "id": "1e4eabc8654211e99e4eacbc329afba7",
                "username": "Bittman",
                "created": "2019-04-19T07:32:09",
                "text": "I'm fine. Thanks for not asking."
            },
            {
                "id": "9125e4ea654211e99e4eacbc329afba7",
                "username": "bossbabe",
                "created": "2019-04-21T22:05:12",
                "text": "Sometimes you have to unfollow people in real life."
            },
            {
                "id": "6e38adc2654311e99e4eacbc329afba7",
                "username": "karmalicious",
                "created": "2019-04-22T18:40:45",
                "text": "I hope karma slaps you in the face before I do."
            },
        ]

        for bitt in bitts:
            bitt_dict = dict(bitt)

            try:
                bitt_dict["id"] = str(bitt["_id"])  # convert Mongo's ObjectId to string
            except Exception as e:
                print("Error _id to id: {}".format(e))

            bitts_dicts.append(dict(bitt_dict))  # convert bitts from a document type into a dictionary

        # sort by date created (reverse order)
        bitts_dicts.sort(key=lambda x: datetime.datetime.strptime(x['created'], '%Y-%m-%dT%H:%M:%S'), reverse=True)

        return bitts_dicts

    @classmethod
    def get_last_bitt_id(cls):
        last_bitt_id = redis.get(name="last-bitt-id")

        if last_bitt_id:
            last_bitt_id = last_bitt_id.decode()
        else:
            last_bitt = cls.get_all_bitts()[0]
            last_bitt_id = last_bitt["id"]
            redis.set(name="last-bitt-id", value=last_bitt_id)

        return last_bitt_id


# coding: utf-8
"""
1、找到未加密的参数
2、想办法把参数进行加密（必须参考网易逻辑），params，encSecKey
3、请求到网页，抓取评论信息
"""
import requests
# 需要安装pycrypto库，用于AES加密，安装命令：pip install pycryptodome
from Crypto.Cipher import AES
from base64 import b64encode
import json

url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
songId = 1325905146

# 请求方式：POST
# 经过解析发现：params _-> encText, encSecKey --> encSecKey
data = {
    "csrf_token": "",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "pageNo": "1",
    "pageSize": "20",
    "rid": f"R_SO_4_{songId}",
    "threadId": f"R_SO_4_{songId}"
}

# 这些参数都是服务与加密函数的，window.asrsea，其中i可以在网页中查到，然后写死
e = "010001"
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
g = "0CoJUm6Qyw8W8jud"
i = "PvLgW8IKGXQDWBsm"

def get_encSecKey():  # 由于i是固定的，所以encSecKey是固定的
    return "5757f6675f9df72e1d15570d55554f85378efb20855b54670e12b10fbd4339d48ec7156d91f5bc8c62082eba565720c7b1b1c0036589802cd1f884d2cf0cfe9a4070dbb203abd22d9f88dfc60b15ba171098d68431b7f54e65e24dd76a34917180025bf942d128ca281223b4eedca94c008deb0e8352576ae50e3e65e3b6387b"

def get_params(data):  # data默认是字符串
    first_enc = enc_params(data, g)
    second_enc = enc_params(first_enc, i)
    return second_enc  # 返回的就是params

def to_16(data):  # 转化成16的倍数，为下发的加密算法服务
    pad = 16 - len(data) % 16
    data += chr(pad) * pad
    return data

def enc_params(data, key):  # 加密过程
    iv = "0102030405060708"
    data = to_16(data)
    aes = AES.new(key=key.encode("utf-8"), IV=iv.encode("utf-8"), mode=AES.MODE_CBC)  # 创建加密器
    bs = aes.encrypt(data.encode("utf-8"))  # 加密，加密的内容长度必须是16的倍数
    return str(b64encode(bs), "utf-8")  # 转化位字符串返回

# 处理加密过程，window.asrsea
"""
    function a(a) {  # a=16，随机的16位字符串
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)  # 循环a次，即16次
            e = Math.random() * b.length,  # 随机数 * b的长度
            e = Math.floor(e),  # 取e的整数，e = "010001"，即1
            c += b.charAt(e);  # 取字符串中的xxx位置 b
        return c
    }
    # AES加密算法要三个参数：原文、密钥和偏移量，其中iv是偏移量，e是原文，所以反推c是加密的密钥，而c由b得到，所以b是密钥
    function b(a, b) {  # a：需要加密的数据
        var c = CryptoJS.enc.Utf8.parse(b) # b是密钥
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a) # 得到的结果e是通过对a进行加密得到的
          , f = CryptoJS.AES.encrypt(e, c, {  # c是加密的密钥
            iv: d,  # AES加密算法中的偏移量
            mode: CryptoJS.mode.CBC  # 模式是：CBC
        });
        return f.toString()
    }
    function c(a, b, c) {  # 函数c()不产生随机数
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    function d(d, e, f, g) {  d:数据，e:010001，f:很长， g:0CoJUm6Qyw8W8jud
        var h = {}
          , i = a(16);  # 往上找a，输出16位的随机值，在浏览器里边打短短的，可以找到i的值，"PvLgW8IKGXQDWBsm"，给它写死
        return h.encText = b(d, g), # d是原文，g是密钥
        h.encText = b(h.encText, i),  # 得到的就是params，加密过程，分两次进行加密，第一次是b(d, g)，之后将该加密得到的
        结果再次和i进行b(第一次加密的结果, i)加密，通过b函数分析，可以得到第一次加密的结果是原文，i是密钥
        h.encSecKey = c(i, e, f),  # 得到的就是encSecKey，其中e和f是定值，
        是写死的，唯一变化的是i，即a(16)，里边有随机值，如果将i固定写死，那么c()函数输出的值，也是定值，得到的encSecKey也是定值，以此来破解
        h
    }
    window.asrsea = d,
    var bKf6Z = window.asrsea(JSON.stringify(i8a), bva3x(["流泪", "强"]), bva3x(Tu8m.md), bva3x(["爱心", "女孩", "惊恐", "大笑"]));

    把bva3x(["流泪", "强"])丢到浏览器控制台，输出：010001
    把bva3x(Tu8m.md)丢到浏览器控制台，输出：
    00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7
    把bva3x(["爱心", "女孩", "惊恐", "大笑"])到浏览器控制台，输出：0CoJUm6Qyw8W8jud
    d: 数据, 010001, 上面那张长的数字和字母组合 , 0CoJUm6Qyw8W8jud
"""

# 发送请求
response = requests.post(url, data={
    "params": get_params(json.dumps(data)),
    "encSecKey": get_encSecKey()
})
print(response.json())

JS破解/网易云音乐热评/网易云音乐热评.py


import typing
from dataclasses import dataclass

import pytest
from dataclasses_json import dataclass_json
from typing_extensions import Annotated

from flytekit import LaunchPlan, task, workflow
from flytekit.core import context_manager
from flytekit.core.context_manager import CompilationState
from flytekit.core.promise import (
    VoidPromise,
    create_and_link_node,
    create_and_link_node_from_remote,
    translate_inputs_to_literals,
)
from flytekit.exceptions.user import FlyteAssertion
from flytekit.types.pickle.pickle import BatchSize

def test_create_and_link_node():
    @task
    def t1(a: typing.Union[int, typing.List[int]]) -> typing.Union[int, typing.List[int]]:
        return a

    with pytest.raises(FlyteAssertion, match="Cannot create node when not compiling..."):
        ctx = context_manager.FlyteContext.current_context()
        create_and_link_node(ctx, t1, a=3)

    ctx = context_manager.FlyteContext.current_context().with_compilation_state(CompilationState(prefix=""))
    p = create_and_link_node(ctx, t1, a=3)
    assert p.ref.node_id == "n0"
    assert p.ref.var == "o0"
    assert len(p.ref.node.bindings) == 1

    @task
    def t2(a: typing.Optional[int] = None) -> typing.Optional[int]:
        return a

    p = create_and_link_node(ctx, t2)
    assert p.ref.var == "o0"
    assert len(p.ref.node.bindings) == 0

def test_create_and_link_node_from_remote():
    @task
    def t1() -> None:
        ...

    with pytest.raises(FlyteAssertion, match="Cannot create node when not compiling..."):
        ctx = context_manager.FlyteContext.current_context()
        create_and_link_node_from_remote(ctx, t1, a=3)

    ctx = context_manager.FlyteContext.current_context().with_compilation_state(CompilationState(prefix=""))
    p = create_and_link_node_from_remote(ctx, t1)
    assert p.ref.node_id == "n0"
    assert p.ref.var == "placeholder"
    assert len(p.ref.node.bindings) == 0

    @task
    def t2(a: int) -> int:
        return a

    p = create_and_link_node_from_remote(ctx, t2, a=3)
    assert p.ref.var == "o0"
    assert len(p.ref.node.bindings) == 1

def test_create_and_link_node_from_remote_ignore():
    @workflow
    def wf(i: int, j: int):
        ...

    lp = LaunchPlan.get_or_create(wf, name="promise-test", fixed_inputs={"i": 1}, default_inputs={"j": 10})
    ctx = context_manager.FlyteContext.current_context().with_compilation_state(CompilationState(prefix=""))

    # without providing the _inputs_not_allowed or _ignorable_inputs, all inputs to lp become required,
    # which is incorrect
    with pytest.raises(FlyteAssertion, match="Missing input `i` type `<FlyteLiteral simple: INTEGER>`"):
        create_and_link_node_from_remote(ctx, lp)

    # Even if j is not provided it will default
    create_and_link_node_from_remote(ctx, lp, _inputs_not_allowed={"i"}, _ignorable_inputs={"j"})

    # value of `i` cannot be overriden
    with pytest.raises(
        FlyteAssertion, match="ixed inputs cannot be specified. Please remove the following inputs - {'i'}"
    ):
        create_and_link_node_from_remote(ctx, lp, _inputs_not_allowed={"i"}, _ignorable_inputs={"j"}, i=15)

    # It is ok to override `j` which is a default input
    create_and_link_node_from_remote(ctx, lp, _inputs_not_allowed={"i"}, _ignorable_inputs={"j"}, j=15)

@dataclass_json
@dataclass
class MyDataclass(object):
    i: int
    a: typing.List[str]

@pytest.mark.parametrize(
    "input",
    [2.0, MyDataclass(i=1, a=["h", "e"]), [1, 2, 3], ["foo"] * 5],
)
def test_translate_inputs_to_literals(input):
    @task
    def t1(a: typing.Union[float, MyDataclass, Annotated[typing.List[typing.Any], BatchSize(2)]]):
        print(a)

    ctx = context_manager.FlyteContext.current_context()
    translate_inputs_to_literals(ctx, {"a": input}, t1.interface.inputs, t1.python_interface.inputs)

def test_translate_inputs_to_literals_with_wrong_types():
    ctx = context_manager.FlyteContext.current_context()
    with pytest.raises(TypeError, match="Cannot convert"):

        @task
        def t1(a: typing.Union[float, typing.List[int]]):
            print(a)

        translate_inputs_to_literals(ctx, {"a": {"a": 3}}, t1.interface.inputs, t1.python_interface.inputs)

    with pytest.raises(TypeError, match="Cannot convert"):

        @task
        def t1(a: typing.Union[float, typing.Dict[str, int]]):
            print(a)

        translate_inputs_to_literals(ctx, {"a": [1, 2, 3]}, t1.interface.inputs, t1.python_interface.inputs)

    with pytest.raises(
        AssertionError,
        match="Outputs of a non-output producing task n0 cannot be passed to another task",
    ):

        @task
        def t1(a: typing.Union[float, typing.Dict[str, int]]):
            print(a)

        translate_inputs_to_literals(
            ctx,
            {"a": VoidPromise("n0")},
            t1.interface.inputs,
            t1.python_interface.inputs,
        )

def test_optional_task_kwargs():
    from typing import Optional

    from flytekit import Workflow

    @task
    def func(foo: Optional[int] = None):
        pass

    wf = Workflow(name="test")
    wf.add_entity(func, foo=None)

    wf()


import logging
from logging.config import dictConfig

import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration

def create_logger(config):
    SENTRY_DSN = config.get('SENTRY_DSN')

    if SENTRY_DSN:  # pragma: no cover
        sentry_logging = LoggingIntegration(
            level=logging.INFO,  # Capture info and above as breadcrumbs
            event_level=logging.ERROR  # Send errors as events
        )

        sentry_sdk.init(
            dsn=SENTRY_DSN,
            integrations=[sentry_logging]
        )

    LOGGING = {
        'version': 1,
        'formatters': {
            'verbose': {
                'format': '%(levelname)s[%(name)s] %(message)s'
            }
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose',
            }
        },
        'root': {
            'handlers': ['console'],
            'level': 'INFO',
        },
        '': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    }

    # JSON formatter for sending logs to ES
    LOG_FORMATTER_JSON = config.get('LOG_FORMATTER_JSON', False)
    if LOG_FORMATTER_JSON:  # pragma: no cover
        LOGGING['formatters']['json'] = {
            '()': 'intergov.json_log_formatter.JsonFormatter',
        }
        LOGGING['handlers']['console']['formatter'] = 'json'

    dictConfig(LOGGING)
    return logging.getLogger('shared-db-channel')


import argparse
import configparser
import re
import sys
from collections import OrderedDict

def ask_choice(prompt, choices):
    if not choices:
        return None
    lowered_choices = [o.lower().strip() for o in choices]
    while True:
        print(":: {prompt}".format(prompt=prompt))
        for i, option in enumerate(choices):
            print("   {i} {option}".format(i=i, option=option))
        answer = input("> ").strip().lower()
        if answer in lowered_choices:
            return answer
        try:
            if 0 <= int(answer) < len(choices):
                return choices[int(answer)]
        except:
            pass

def perform_subsitutions(text, variables):
    """Subsititues any occurances of ${NAME[:DEFAULT]} with the value from dictionary."""
    def evaluate_match(match):
        captures = match.groupdict()
        default = captures.get("default") or ""
        variable_name = captures.get("name") or ""
        if variable_name not in variables:
            return default
        return variables[variable_name]
    return re.sub(r"\$\{(?P<name>\w*):?(?P<default>.*?)\}",
                  evaluate_match,
                  text)

def parse_node_invocation(invocation):
    """Parse a node invocation like `switch_on [(param:value), ...]` into node_id and the variable dict."""
    node_id_and_maybe_variables_text = re.split(
        r"\s+", invocation.strip(), maxsplit=1)
    node_id = node_id_and_maybe_variables_text[0]
    variables = {}
    if len(node_id_and_maybe_variables_text) == 2:
        variables_text = node_id_and_maybe_variables_text[1]
        for variable_text in re.finditer(r"\((?P<variable>[^()]+)\)", variables_text):
            name, value = variable_text.group("variable").split(":")
            variables[name] = value
    return (node_id, variables)

class Node():
    """Represents a node in the decision tree."""
    def __init__(self, node_id, text, root, settings):
        self.node_id = node_id
        self.text = text
        if root is None:
            self.root = False
        elif root.strip().lower() == "true":
            self.root = True
        elif root.strip().lower() == "false":
            self.root = False
        else:
            raise ValueError(
                "node {node_id} had a root value of {root} which could not be parsed".format(
                    node_id=node_id,
                    root=root
                ))
        self.choice_to_node_invocation = OrderedDict()
        for name, value in settings.items():
            branch_match = re.match(r"if_(?P<choice>\w+)", name, re.I)
            if not branch_match:
                continue
            choice = branch_match.groupdict().get("choice")
            if not choice:
                raise ValueError(
                    "A choice text cannot be empty, if_<x> must have a non-empty x")
            self.choice_to_node_invocation[choice] = value

    def run(self, variables):
        """Run the node, returning the next node and invocation variables if any."""
        choices = []
        for choice, _ in self.choice_to_node_invocation.items():
            choices.append(choice)
        choices.append("exit")
        prompt = perform_subsitutions(self.text, variables)
        choice = ask_choice(prompt, choices=choices)
        if choice is None or choice == "exit":
            return None
        node_invocation = self.choice_to_node_invocation[choice]
        node_invocation = perform_subsitutions(node_invocation, variables)
        return parse_node_invocation(node_invocation)

    def __str__(self):
        return "Node(id={node_id}, text={text}, root={root}, choices={choices})".format(
            node_id=self.node_id,
            text=self.text,
            root=self.root,
            choices=self.choice_to_node_invocation
        )

class Scenario():
    """Represents a collection of nodes for a troubleshooting scenario."""
    def __init__(self, config_file_obj):
        config_parser = configparser.ConfigParser()
        config_parser.read_file(config_file_obj)
        self.node_id_to_node = {}
        for node_id, settings in config_parser.items():
            if node_id == configparser.DEFAULTSECT:
                continue
            if settings.get("text") is None:
                raise ValueError("Each node must contain a text setting")
            text = settings.get("text")
            if text is not None:
                del settings["text"]
            root = settings.get("root")
            if root is not None:
                del settings["root"]
            self.node_id_to_node[node_id] = Node(
                node_id,
                text,
                root,
                settings)

    def get_node(self, node_id):
        if not re.match(r"\w+", node_id, re.I):
            raise ValueError("Invalid node id {node_id}".format(node_id=node_id))
        return self.node_id_to_node.get(node_id)

    def get_root_node_id(self):
        for id, node in self.node_id_to_node.items():
            if node.root == True:
                return id
        return None

class Troubleshooter():
    """Create a troubleshooter which uses the given scenario to help users."""
    def __init__(self, scenario: Scenario):
        self.scenario = scenario

    def get_help(self):
        node_id, variables = self.scenario.get_root_node_id(), {}
        if not node_id:
            raise Exception("No root node in scenario")
        while node_id is not None:
            node = self.scenario.get_node(node_id)
            if node is None:
                raise Exception("Node with id {node_id} could not be found".format(node_id=node_id))
            next = node.run(variables)
            if next is None:
                return "OK"
            node_id, variables = next

def main(argv=None):
    argv = argv or sys.argv[1:] or ["--scenario", input("scenario filename: ")]
    parser = argparse.ArgumentParser("Troubleshooter")
    parser.add_argument("--scenario",
                        type=argparse.FileType("r"),
                        required=True)
    result = parser.parse_args(argv)
    troubleshooter = Troubleshooter(Scenario(result.scenario))
    troubleshooter.get_help()

if __name__ == "__main__":
    main()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('timers', '0007_timerrunner_paused_date'),
    ]

    operations = [
        migrations.AddField(
            model_name='timerrunner',
            name='resume_date',
            field=models.DateTimeField(blank=True, verbose_name='Fortsatte', null=True),
        ),
    ]