import keras
import keras.layers as layers
import tensorflow as tf
from keras.models import Model
import numpy as np
import pandas as pd
import time
import argparse

from keras.layers import Input, Dense, Reshape, Flatten, Dropout, Add
from keras.layers import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model
from keras.optimizers import Adam, Adadelta
from keras.callbacks import CSVLogger, ModelCheckpoint
from weightnorm import AdamWithWeightnorm
from sklearn.preprocessing import StandardScaler
from keras.optimizers import Adam
from sklearn.metrics import mean_absolute_error as mae # tested with verison 0.20.3
from sklearn.metrics import mean_squared_error as mse # tested with verison 0.20.3

class GAN():
    def __init__(self,NumXgenes,NumYgenes):

        optimizer = AdamWithWeightnorm(lr=1e-3, beta_1=0.9,
                    beta_2=0.999,epsilon=1e-8)

        self.ld_genes_shape = NumXgenes
        self.target_genes_shape = NumYgenes
        ###############################################
        # hard coded hyperparameters below
        self.gen_hidden_units = 9000
        self.disc_hidden_units = 3000

        self.leak_value = 0.2
        ## lower bernoulli p means higher number of zeros
        self.init_bernoulli_p = 0.1
        self.final_bernoulli_p = 0.99
        self.u1 = 0.1
        self.u2 = 0.15

        ## lambdas
        self.lbda_adv = 0.1
        self.lbda_cons = 1
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss=self.custom_discriminator_loss(),
                                   optimizer=optimizer,
                                   metrics=['mae'])

        self.generator = self.build_generator(drop_frac = 0)
        self.generator_u1 = self.build_generator(drop_frac = self.u1)
        self.generator_u2 = self.build_generator(drop_frac = self.u2)
        self.generator_u1.trainable = False
        self.generator_u2.trainable = False

        z = Input(shape=(self.ld_genes_shape,))
        distrib = Input(shape=(self.target_genes_shape,))
        target_predicted_u1 = Input(shape=(1,))

        target_predicted = self.generator(z)

        self.discriminator.trainable = False
        lambda_mult = layers.Lambda(lambda x: tf.multiply(x[0],x[1]))
        target_predicted_masked = lambda_mult([distrib,target_predicted])

        validity_predicted = self.discriminator([z,target_predicted_masked])
        concat_out = layers.concatenate([target_predicted,validity_predicted],axis =1)

        self.combined = Model(inputs=[z,distrib,target_predicted_u1],
                        outputs=concat_out)
        self.combined.compile(loss=self.custom_generator_loss(),
              optimizer=optimizer)

    def custom_discriminator_loss(self):
        def total_loss(y_true,y_pred):
            validity_actual = y_true
            validity_predicted = y_pred
            ones_tensor = tf.ones(tf.shape(validity_actual))
            temp_tensor_ladv = tf.math.add(tf.math.square(tf.subtract(validity_actual,ones_tensor)),
                                tf.math.square(validity_predicted))
            L_adv = tf.reduce_sum(temp_tensor_ladv,0)/2
            return L_adv
        return total_loss

    def custom_generator_loss(self):
        def total_loss(y_true,y_pred):
            validity_actual = y_true[:,self.target_genes_shape:]
            validity_predicted = y_pred[:,self.target_genes_shape:]
            ones_tensor = tf.ones(tf.shape(validity_actual))
            L_adv = tf.math.square(tf.subtract(validity_predicted,ones_tensor))

            target_actual = y_true[:,:self.target_genes_shape]
            target_predicted = y_pred[:,:self.target_genes_shape]
            L_1 = tf.norm(target_actual-target_predicted,
                ord=1,axis = 1)

            return tf.reduce_sum(L_1) + tf.math.reduce_mean(self.lbda_adv*L_adv) \
                 + self.lbda_cons*tf.math.reduce_mean(self.combined.input[2])

        return total_loss

    def simple_model(self):
        units = [200,200,200]
        drps = [0.1,0.1,0.1]

        model = Sequential()
        for idx in range(len(units)):
            if idx == 0:
                model.add(Dense(units=units[idx],
                    input_dim=self.ld_genes_shape, activation='tanh',kernel_initializer='glorot_uniform'))
                model.add(Dropout(drps[idx]))
            else:
                model.add(Dense(units=units[idx], activation='tanh',kernel_initializer='glorot_uniform'))
                model.add(Dropout(drps[idx]))
        model.add(Dense(units=self.target_genes_shape,
                    activation='linear',kernel_initializer=keras.initializers.RandomUniform(-1e-4,1e-4)))
        return model

    def build_generator(self, drop_frac= 0):

        inp1 = Input(shape=(self.ld_genes_shape,))

        inp1_dp = Dropout(drop_frac)(inp1, training = True)

        h1 = Dense(self.gen_hidden_units)(inp1_dp)
        h1_dp = Dropout(drop_frac)(h1, training = True)
        h1_activated = LeakyReLU(alpha=self.leak_value)(h1_dp)

        h2 = Dense(self.gen_hidden_units)(h1_activated)
        h2_add = Add()([h2,h1_activated])
        h2_add_dp = Dropout(drop_frac)(h2_add, training = True)
        h2_activated = LeakyReLU(alpha=self.leak_value)(h2_add_dp)
        out = Dense(self.target_genes_shape)(h2_activated)

        return Model(inp1, out)

    def build_discriminator(self):

        model = Sequential()

        model.add(Dense(units=self.disc_hidden_units,
     		input_dim=self.ld_genes_shape+self.target_genes_shape,
     		kernel_initializer='glorot_uniform'))
        model.add(LeakyReLU(alpha=self.leak_value))

        model.add(Dense(units=1,
     			kernel_initializer='glorot_uniform'))
        model.add(LeakyReLU(alpha=self.leak_value))
        model.summary()
        ld_input = Input(shape=(self.ld_genes_shape,))
        target_input = Input(shape=(self.target_genes_shape,))
        inp_disc =layers.concatenate([ld_input,target_input],axis=-1)
        validity = model(inp_disc)
        return Model([ld_input,target_input], validity)

    def add_eval_to_df(self, df_eval,metric_values,metric_name,model_name,col_names,GPLID):
        num_genes = len(metric_values)
        gene_inds = list(np.arange(num_genes))
        fill_data = []
        fill_data.append([GPLID] * num_genes)
        fill_data.append([model_name] * num_genes)
        fill_data.append([metric_name] * num_genes)
        metric_values = list(metric_values)
        fill_data.append(metric_values)
        fill_data.append(gene_inds)
        fill_zip = list(zip(*fill_data))
        df_tmp = pd.DataFrame(fill_zip,columns=col_names)
        df_eval = pd.concat([df_eval,df_tmp])
        return df_eval

    def add_metric_values(self, preds):
        # make the coloumn names and initial df
        col_names = ['GPL','Model','Metric','Value','GeneIdx']
        df_eval = pd.DataFrame(columns=col_names)

        mae_values    = mae(ydata_aGPL,preds,multioutput='raw_values')
        df_eval = self.add_eval_to_df(df_eval, mae_values,'mae','GGAN',col_names,aGPL)
        rmse_values   = np.sqrt(mse(ydata_aGPL,preds,multioutput='raw_values')) # correct to have ytst_GL
        cvrmse_values = rmse_values/np.mean(ydata_aGPL,axis=0) # correct to have ytst_GL
        df_eval = self.add_eval_to_df(df_eval, cvrmse_values,'cvrmse','GGAN',col_names,aGPL)

        return df_eval

    def train(self, epochs, batch_size,
                X_train,Y_train,
                X_test,Y_test):
        np.random.seed(seed=0)
        total_gps = X_train.shape[0]

        best_model = ['',''] # this first is for the best epoch and the second ins the results df
        best_mae = 100000
        train_loss = []
        for epoch in range(epochs):
            ## set before each epoch
            self.generator_u1.set_weights(self.generator.get_weights())
            self.generator_u2.set_weights(self.generator.get_weights())
            all_IDs = np.arange(0,total_gps)

            np.random.shuffle(all_IDs)
            random_ids_to_put = total_gps%batch_size
            total_batches = total_gps//batch_size
            if random_ids_to_put>0:
                idx = np.random.randint(0, total_gps, random_ids_to_put)
                all_IDs = np.concatenate((all_IDs,idx))
                total_batches = total_gps//batch_size + 1
            ## would be a 1-D vector, need to be multiplied, adversial loss
            ## bernoulli_p need to change with each iteration or epoch
            self.bernoulli_p = self.init_bernoulli_p + \
                              (self.final_bernoulli_p-self.init_bernoulli_p)*(epoch/(epochs-1))
            complete_distrib = np.random.binomial(n=1, p=self.bernoulli_p,
                                        size=(total_gps,self.target_genes_shape))
            for batchNo in range(total_batches):

                ## gene profiles
                idx = all_IDs[batchNo*batch_size:(batchNo+1)*batch_size]
                z = X_train[idx]
                target_actual = Y_train[idx]
                distrib = complete_distrib[idx]

                target_actual_masked = np.multiply(distrib,target_actual)/self.bernoulli_p
                validity_actual = self.discriminator.predict([z,target_actual_masked]) ## not sure about this, confused

                target_predicted = self.generator.predict(z)
                target_predicted_masked = np.multiply(distrib,target_predicted)/self.bernoulli_p
                disc_loss= self.discriminator.train_on_batch([z,target_predicted_masked],validity_actual)

                ## for L_cons
                target_predicted_u1 = self.generator_u1.predict(z)
                target_predicted_u2 = self.generator_u2.predict(z)
                subtracted_noise = np.linalg.norm(target_predicted_u1-target_predicted_u2,
                                     axis = 1)**2
                concatenated_actual = np.concatenate((target_actual,validity_actual),axis = 1)
                ## for generator loss
                generator_loss = self.combined.train_on_batch([z,distrib,subtracted_noise],
                                  concatenated_actual)

            print("{} [D loss: {}] [G loss: {}]" .format(epoch, disc_loss, generator_loss))
            train_predicted = self.generator.predict(X_train)
            test_predicted = self.generator.predict(X_test)
            mae_train_loss = mae(Y_train,train_predicted)
            mae_test_loss = mae(Y_test,test_predicted)
            train_loss.append([epoch,mae_train_loss,mae_test_loss])
            df_train_loss = pd.DataFrame(train_loss,columns=['epoch','train_mae','test_mae'])
            df_train_loss.to_csv(CSVlogger_filename,header=True,index=False)
            print("at {}, MAE loss: {}" .format(epoch, mae_test_loss))
            if mae_test_loss < best_mae:
                df_eval = self.add_metric_values(test_predicted)
                best_model = [epoch,df_eval]
                best_model[1].to_csv(Evaluations_filename,sep='\t',header=True,index=False)
                df_train_loss.to_csv(CSVlogger_filename,header=True,index=False)
                best_mae = mae_test_loss
        return best_model, train_loss

if __name__ == '__main__':

    ############################################################################################################
    ############################################################################################################

    tic0 = time.time()

    # get the GPL to try from command line argument
    parser = argparse.ArgumentParser()
    parser.add_argument('-g','--aGPL',
                        default = 'GPL571',
                        type = str,
                        help = 'The GPL to do')
    args = parser.parse_args()
    aGPL = args.aGPL

    # set some paths
    fp_gpl570 = '../data/'
    fp_seek =  fp_gpl570 + '/Multiple_Platforms/'
    fp_save = '../reproduce_results/SEEK-save/'

    ############################################################################################################

    print('Load and standardize the data')
    # load the GPL data
    Xdata_aGPL = np.load(fp_seek + '%s_Xdata.npy'%aGPL)
    ydata_aGPL = np.load(fp_seek + '%s_ydata.npy'%aGPL)
    # load GPL570 gene inds
    GPL570_Xgene_inds = np.loadtxt(fp_seek + 'GPL570_%s_Xgenes_inds.txt'%aGPL,dtype=int)
    GPL570_ygene_inds = np.loadtxt(fp_seek + 'GPL570_ygenes_inds.txt',dtype=int)
    # load the GPL570data
    GPL570data = np.load(fp_gpl570 + 'Microarray_Trn_Exp.npy')
    # slice the GPL570 along gene axis
    Xdata_GPL570 = GPL570data[:,GPL570_Xgene_inds]
    ydata_GPL570 = GPL570data[:,GPL570_ygene_inds]

    # subset to 90 random samples
    sample_inds = np.loadtxt(fp_seek + '%s_90_Sample_Inds.txt'%aGPL,dtype=int)
    Xdata_aGPL = Xdata_aGPL[sample_inds,:]
    ydata_aGPL = ydata_aGPL[sample_inds,:]

    # standarize and format
    Xtrn = Xdata_GPL570
    ytrn = ydata_GPL570
    Xtst = Xdata_aGPL
    ytst = ydata_aGPL
    std_scale = StandardScaler().fit(Xtrn)
    Xtrn = std_scale.transform(Xtrn)
    Xtst = std_scale.transform(Xtst)

    ############################################################################################################
    print('Setting up some output file names')
    # # The directory to the file used to store the csv logger
    CSVlogger_filename   = fp_save  + '%s_GGAN_CSVlogger.csv'%aGPL
    # The directory to the file used to store the evaluations of the test set
    Evaluations_filename = fp_save + '%s_GGAN_evals.tsv'%aGPL

    ############################################################################################################
    print('Inializating GGAN')
    gan = GAN(Xtrn.shape[1], ytrn.shape[1])
    print('Starting to Train')
    best_model, train_loss = gan.train(epochs=200, batch_size=200,
              X_train=Xtrn,
              Y_train=ytrn,
              X_test = Xtst,
              Y_test = ytst)
    print('The best epoch was', best_model[0])
    print('\n\nThe script took %i minutes to run'%((time.time()-tic0)/60))


import setuptools
from setuptools import find_packages

__description__ = "The scrapper gets monthly crude volume data for UStrade."
__repo_name__ = "ea_csb_etl"

requirements = [
    'selenium',
    'pandas',
    'sqlalchemy',
    'psycopg2-binary',
    'sqlalchemy',
    'python-dotenv',
]
setuptools.setup(
    name=__repo_name__,
    version='1.0.0',
    description=__description__,
    url=f'https://github.com/energyaspects/{__repo_name__}.git',
    author='ByteIQ Data Engineering Team',
    author_email='',
    package_dir={"": "src"},
    packages=find_packages(where="src"),
    python_requires='>=3.6',
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Natural Language :: English",
        "Programming Language :: Python :: 3.6",
    ],
    install_requires=requirements,
    package_data={"": ["*"]},
    entry_points={
        'console_scripts': [
            "cpi_etl = macro.cpi_main.main:main",
            "gdp_etl = macro.gdp_main.main:main",
            "pcpi_etl = macro.pcpi_main.main:main",
            "retail_sales_etl = macro.retail_sales_main.main:main",
        ],
    },
)


from django.urls import path

from . import views

app_name = 'item_app'

urlpatterns = [
    # Оплата одного предмета
    path('item/<int:item_id>/', views.item, name='item'),
    path(
        'buy/<id>/',
        views.item_checkout_session,
        name='item_checkout_session'
    ),
    # Оплата заказа
    path('order/<int:order_id>/', views.order, name='order'),
    path(
        'buy-order/<id>/',
        views.order_checkout_session,
        name='order_checkout_session'
    ),
    # Странички успешной и неуспешной оплаты
    path('success/', views.PaymentSuccessView.as_view(), name='success'),
    path('failed/', views.PaymentFailedView.as_view(), name='failed'),
]


import json
import requests
import js2py

compare=js2py.require('compare-versions')

def package_json_comparev1v2(latest, current):
    return compare.satisfies(latest, current)

def package_json_parse(f):
    data=json.load(f)
    return data['dependencies']

def package_json_fetch_latest(name):
    url="https://registry.npmjs.org/{}".format(name)
    r=requests.get(url)
    response=r.text
    data=json.loads(response)
    return data["dist-tags"]['latest']

def package_json_check(modules):
    outdated=[]
    for name, version in modules.items():
        latest=package_json_fetch_latest(name)
        if package_json_comparev1v2(latest,version)==False:
            outdated.append((name,version,latest))
    return outdated

# with open('package.json','r') as f:
#     modules=package_json_parse(f)
#     outdated=package_json_check(modules)
#     print(outdated)


# the main process can wait a fixed number of seconds for all processes to finish.
# If all processes finish within the time, all is well, otherwise we can report that not all work could be finished on time.

from time import sleep
from random import random
from multiprocessing import Barrier, Process
from threading import BrokenBarrierError

# All process will eventually raise exception as some might not be able to complete on time. Hence exception is raised by all process.
# Changing 'value' to a lower number will easily give enough to for all to complete.
def task(barrier,number):
    value = random() * 10
    sleep(value)
    print(f'Process {number} done got {value}', flush=True)
    try:
        barrier.wait()
    except BrokenBarrierError:
        print(f'Process {number} Got Broken Barrier Exception..')

if __name__ == '__main__':
    barrier = Barrier(5+1)
    for i in range(5):
        worker = Process(target=task, args=(barrier,i))
        worker.start()
    print('Main process waiting on all threads')
    try:
        barrier.wait(timeout=5)
        print('All process have thier results')
    except BrokenBarrierError:
        print('Some process did not finish on time')


import sqlite3

try:
    with sqlite3.connect(r"c:\classroom\feb28\hr.db") as con:
        cur = con.cursor()

        id = int(input("Employee id            :"))
        salary = int(input("Employee new salary    :"))

        cur.execute("update employees set salary = ? where id = ?", (salary, id))

        if cur.rowcount == 1:
            print("Updated salary successfully!")
            con.commit()  # Commit transaction / Save changes
        else:
            print("Sorry! Employee Id not found!")
except Exception as ex:
    print("Sorry! Database error :", ex)


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

from django.db import models, migrations

class Migration(migrations.Migration):

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

    operations = [
        migrations.AddField(
            model_name='travel',
            name='did_not_travel',
            field=models.NullBooleanField(default=False),
        ),
    ]


from django.contrib import admin
from django.contrib.auth.models import User, Group
# Register your models here.
from .models import EmailList

admin.site.site_header = 'PODCAST'
admin.site.site_title = 'PODCAST'
admin.site.index_title = 'site administration'

@admin.register(EmailList)
class EmailListAdmin(admin.ModelAdmin):
    list_display = ['id', 'email', 'phone', 'pod_type', 'created', 'updated']
    list_filter = ['pod_type', 'email', 'phone']
    search_fields = ['phone', 'email']
    date_hierarchy = 'created'

admin.site.unregister(User)
admin.site.unregister(Group)


import numpy as np
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import DataLoader

def Dataloader(Dataset, batch_size, validation_split = .2, shuffle_dataset = True, random_seed = 42):
    dataset_size = len(Dataset)
    indices = list(range(dataset_size))
    train_split = int(np.floor(validation_split*dataset_size))

    np.random.seed(random_seed)
    np.random.shuffle(indices)

    validation_indices, train_indices = indices[:train_split], indices[train_split:]

    train_sampler, validation_sampler = [SubsetRandomSampler(train_indices),
                                         SubsetRandomSampler(validation_indices)]

    train_loader, validation_loader = [DataLoader(Dataset, batch_size = batch_size, sampler = train_sampler),
                                       DataLoader(Dataset, batch_size = batch_size, sampler = validation_sampler)]

    dataloaders = {"train": train_loader, "val": validation_loader}
    datasets_sizes = {"train": len(train_indices), "val": len(validation_indices)}

    return dataloaders, datasets_sizes


import pygame, time                      # import the pygame library which has many functions we will need later

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 500
pygame.init()                           # initializing the pygame window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))    # creating screen upon which we can place images

# importing images
red_dvd = pygame.image.load("/home/jeremy/Pictures/red_dvd.png")
blue_dvd = pygame.image.load("/home/jeremy/Pictures/blue_dvd.png")
green_dvd = pygame.image.load("/home/jeremy/Pictures/green_dvd.png")

# dictionary to deal with selecting color
color_select = {0: red_dvd, 1:blue_dvd, 2:green_dvd}

black = (0,0,0)                         # define black as black RGB color
x,y = 0,0                               # base x and y pixel coordinates are 0,0
dx,dy = 1,1                             # moving box will go one pixel at a time
color = 0                               # current color will be red

done = False                            # while loop exit condition

while not done:
    for event in pygame.event.get():    # whenever pygame recognizes an event (IE: mouse movement, clicking on the window x
        if event.type == pygame.QUIT:   # if user clicked the red box to close the window
            done = True                 # exit the loop
        if event.type == pygame.MOUSEBUTTONUP:  # if user clicks
            color += 1
    x += dx
    y += dy

    if x + dx < 0 or x + dx + 100 > SCREEN_WIDTH :
        dx = -dx
    if y + dy < 0 or y + dy + 100 > SCREEN_HEIGHT:
        dy = -dy

    screen.fill(black)                  # fill screen with black pixels

    screen.blit(color_select[color%3], (x,y))
    time.sleep(0.01)

    pygame.display.flip()               # update display with current picture


import pytest

from configs import MAIN_BRANCH
from services.github_service import GitHubRestApi
from utils.faker import random_str

def test_create_pull_request(user_data, repository, branch_for_repo_with_commit):
    branch, _ = branch_for_repo_with_commit
    gh_service = GitHubRestApi()

    pr_title = random_str()
    res = gh_service.create_pull_request(user_data["login"], repository["name"], branch["ref"], MAIN_BRANCH, pr_title)

    assert res.status_code == 201
    res = res.json()

    assert res["title"] == pr_title
    assert res["base"]["ref"] == MAIN_BRANCH
    assert res["head"]["ref"] == branch["ref"].split("/")[-1]

def test_create_pull_request_same_tree(user_data, repository, branch_for_repo):
    gh_service = GitHubRestApi()

    pr_title = random_str()
    res = gh_service.create_pull_request(user_data["login"], repository["name"], branch_for_repo["ref"], MAIN_BRANCH,
                                         pr_title)

    assert res.status_code == 422

def test_list_pull_requests(user_data, repository, open_pull_request):
    gh_service = GitHubRestApi()
    res = gh_service.list_pull_requests(user_data["login"], repository["name"])

    assert res.status_code == 200

    act_pr = None
    for pr in res.json():
        if pr["title"] == open_pull_request["title"]:
            act_pr = pr
            break

    assert act_pr
    assert act_pr["base"]["ref"] == open_pull_request["base"]["ref"]
    assert act_pr["head"]["ref"] == open_pull_request["head"]["ref"]

def test_list_pull_requests_fake_repo(user_data):
    gh_service = GitHubRestApi()
    res = gh_service.list_pull_requests(user_data["login"], random_str())

    assert res.status_code == 404

def test_close_pull_request(user_data, repository, open_pull_request):
    gh_service = GitHubRestApi()

    new_state = "closed"
    res = gh_service.update_pull_request(user_data["login"], repository["name"], open_pull_request["number"], new_state)
    assert res.status_code == 200

    res = res.json()
    assert res["state"] == new_state

def test_update_pr_wrong_state(user_data, repository, open_pull_request):
    gh_service = GitHubRestApi()

    res = gh_service.update_pull_request(user_data["login"], repository["name"], open_pull_request["number"],
                                         random_str())
    assert res.status_code == 200
    assert res.json()["state"] == "open"

@pytest.mark.skip("TODO: currently failing because of 'cannot review own pull request' validation")
def test_approve_pull_request(user_data, repository, open_pull_request):
    gh_service = GitHubRestApi()

    result = "APPROVE"
    res = gh_service.create_pr_review(user_data["login"], repository["name"], open_pull_request["number"], result)
    assert res.status_code == 200


"""
Description: Manages the invoices
             Support CRUD Operation of invoices
             Xero API
"""

import json
from connection import Connection
from util import send_get_request, parse_xml, send_put_request, send_post_request
from config import INVOICE_API_URL, INVOICE_CREATE

class Invoices:
    """
    Creates a data pipeline for the Xero Invoices
    """

    def __init__(self):
        # This is a connection object which is ready to consume the API
        self.conn = Connection()

    def get(self):
        """
        Retrieves invoices
        :return: List of invoices
        """
        # Refresh token
        self.conn.get_or_refresh_token()
        headers = {'authorization': "Bearer %s" % self.conn.token,
                   'Content-Type': 'application/json',
                   'Xero-tenant-id': self.conn.tenant_id}
        res = send_get_request(url=INVOICE_API_URL, headers=headers)
        # Parses the XML to Python Dictionary
        invoices = parse_xml(res.text)
        invoices = invoices['Response']['Invoices']['Invoice']

        return invoices

    def post(self):
        """
        Updates invoices
        :return:
        """
        pass

    def put(self):
        """
        Creates Invoices
        :return:
        """
        # Refresh token
        self.conn.get_or_refresh_token()
        headers = {'authorization': "Bearer %s" % self.conn.token,
                   'Content-Type': 'application/json',
                   'Xero-tenant-id': self.conn.tenant_id}

        res = send_put_request(url=INVOICE_API_URL, headers=headers, data=json.dumps(INVOICE_CREATE))
        # print(res.text)
        invoices = parse_xml(res.text)
        invoices = invoices['Response']['Invoices']['Invoice']
        return invoices

# inv = Invoices()
# invoices_list = inv.get()

# print("Fetching Invoices...")
# print(f"Invoice Count: {len(invoices_list)}")
# print(invoices_list)

# print("Creating Invoices")
# invoices_list = inv.put()
# print(invoices_list)

# invoices_list = inv.get()
# print(f"Invoice Count: {len(invoices_list)}")

from config import *

def get_or_refresh_token():
    """
    Gets or Refresh token
    :return: token
    """
    # headers = {'authorization': "Basic %s" % b64_client_id_secret,
    #            'Content-Type': 'application/x-www-form-urlencoded'}
    data = {}

    data['grant_type'] = "client_credentials"
    data['client_id'] = CLIENT_ID
    data['client_secret'] = CLIENT_SECRET

    res = send_post_request(url=TOKEN_URI, headers=None, data=json.dumps(data))
    json_result = json.loads(res.text)
    print(json_result)
get_or_refresh_token()


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

import os
import shutil
import urllib.request
import urllib.error
import urllib.parse

from fabric.api import local, lcd

from .base import BuilderBase
from buildtools.defines import (PLUGINS_DIR,
                                PLUGINS_LIST)
from buildtools.versions import (readAppInfo,
                                 getPluginVersionsPath,
                                 downloadAppInfo)

class BuilderPlugins(BuilderBase):
    """
    Create archives with plug-ins
    """
    def __init__(self,
                 updatedOnly=False,
                 build_dir=PLUGINS_DIR,
                 plugins_list=PLUGINS_LIST):
        super(BuilderPlugins, self).__init__(build_dir)
        self._all_plugins_fname = u'outwiker-plugins-all.zip'
        self._plugins_list = plugins_list
        self._updatedOnly = updatedOnly

    def get_plugins_pack_path(self):
        return self._getSubpath(self._all_plugins_fname)

    def clear(self):
        super(BuilderPlugins, self).clear()
        self._remove(self.get_plugins_pack_path())

    def _build(self):
        # Path to archive with all plug-ins
        full_archive_path = self.get_plugins_pack_path()

        for plugin in self._plugins_list:
            # Path to plugin.xml for current plugin
            xmlplugin_path = getPluginVersionsPath(plugin)

            localAppInfo = readAppInfo(xmlplugin_path)
            assert localAppInfo is not None
            assert localAppInfo.currentVersion is not None

            skip_plugin = False

            # Check for update
            if self._updatedOnly:
                url = localAppInfo.updatesUrl
                try:
                    siteappinfo = downloadAppInfo(url)
                    if localAppInfo.currentVersion == siteappinfo.currentVersion:
                        skip_plugin = True
                except (urllib.error.URLError, urllib.error.HTTPError):
                    pass

            # Archive a single plug-in
            if not skip_plugin:
                version = str(localAppInfo.currentVersion)
                archive_name = u'{}-{}.zip'.format(plugin, version)

                # Subpath to current plug-in archive
                plugin_dir_path = self._getSubpath(plugin)

                # Path to future archive
                archive_path = self._getSubpath(plugin, archive_name)
                os.mkdir(plugin_dir_path)
                shutil.copy(xmlplugin_path, plugin_dir_path)

                # Archive a single plug-in
                with lcd("plugins/{}".format(plugin)):
                    local('7z a -r -aoa -xr!*.pyc -xr!.ropeproject -x!doc "{}" ./*'.format(archive_path))

            # Add a plug-in to full archive
            with lcd("plugins/{}".format(plugin)):
                local('7z a -r -aoa -xr!*.pyc -xr!.ropeproject -w../ "{}" ./*'.format(full_archive_path))

    def _getSubpath(self, *args):
        """
        Return subpath inside current build path (inside 'build' subpath)
        """
        return os.path.join(self.build_dir, *args)


"""blog URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf.urls import url,include
from django.conf import settings
from posts import views as v1

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', v1.index,name='home'),
    path('search/', v1.search,name='search'),
    path('blog/',v1.blog,name='post-list'),
    path('<int:pk>',v1.PostDetail.as_view()),
    path('pcreate/',v1.PostAdd.as_view()),
    path('update/<int:pk>',v1.PostUpdate.as_view()),
    path('delete/<int:pk>',v1.PostDelete.as_view()),
    path('ckeditor/', include('ckeditor_uploader.urls')),

    path('sports/',v1.sports),
    path('business/',v1.business),
    path('food/',v1.food),
    path('travel/',v1.travel),
    path('gym/',v1.gym),
    path('gamming/',v1.gamming),

    path('draft/',v1.draft),
    path('authform/',v1.AuthorAdd),

    path('authorpost/',v1.view,name='post-list'),
    path('post/<id>/',v1.post,name='post-detail'),

    path('signup/',v1.Signup,name='signup'),

    path('logout/',v1.logout_view),
    path('accounts/',include('django.contrib.auth.urls')),
]
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)


import json
import boto3
from urllib.parse import unquote_plus

def lambda_handler(event, context):
    # parse the S3 triggered event
    record = event['Records'][0]

    # extract bucket from event
    bucket = record['s3']['bucket']['name']

    # extract key from event
    key = unquote_plus(record['s3']['object']['key'])

    ## for debugging
    # bucket = "fundmapper"
    # key = "04-StagingTables/series_data/202006/part-00000-39604d54-0f41-44be-8850-fafdc5733262-c000.snappy.parquet"

    # create s3 resource
    s3_resource = boto3.resource('s3')

    dest_prefix = "05-FinalTables"
    prefix, dataset, date, filename = key.split("/")
    print(bucket)
    # copy file from S3
    (s3_resource.Object(bucket, f"{dest_prefix}/{dataset}/{date}/{dataset}_{date}.parquet")
     .copy_from(CopySource=f"{bucket}/{key}"))

    # Delete the former file
    (s3_resource.Object(bucket, key)
     .delete())


import coalpy.gpu as g
import numpy as np
import math
import functools
from . import prefix_sum as gpu_prefix_sum

def prefix_sum(input_data, is_exclusive = False):
    accum = 0
    output = []
    for i in range(0, len(input_data), 1):
        if is_exclusive:
            output.append(accum)
            accum += input_data[i]
        else:
            accum += input_data[i]
            output.append(accum)
    return output

def test_cluster_gen(is_exclusive = False):
    buffersz = 8529
    input_data = np.array([x  for x in range(0, buffersz, 1)], dtype='i')
    test_input_buffer = g.Buffer(format = g.Format.R32_UINT, element_count = buffersz)

    reduction_buffers = gpu_prefix_sum.allocate_args(buffersz)

    cmd_list = g.CommandList()
    cmd_list.upload_resource(source = input_data, destination = test_input_buffer)
    output = gpu_prefix_sum.run(cmd_list, test_input_buffer, reduction_buffers, is_exclusive)

    g.schedule(cmd_list)

    dr = g.ResourceDownloadRequest(resource = output)
    dr.resolve()

    result = np.frombuffer(dr.data_as_bytearray(), dtype='i')
    result = np.resize(result, buffersz)
    expected = prefix_sum(input_data, is_exclusive)
    correct_count = functools.reduce(lambda x, y: x + y, [1 if x == y else 0 for (x, y) in zip(result, expected)])
    return True if correct_count == len(input_data) else False

def run_test(nm, fn):
    result = fn()
    print(nm + " : " + ("PASS" if result else "FAIL"))

def test_cluster_gen_inclusive():
    return test_cluster_gen(is_exclusive = False)

def test_cluster_gen_exclusive():
    return test_cluster_gen(is_exclusive = True)

if __name__ == "__main__":
    run_test("test prefix sum inclusive", test_cluster_gen_inclusive)
    run_test("test prefix sum exclusive", test_cluster_gen_exclusive)


'''
bfs는 가장 가까운 물고기를 찾기 위한 수단
bfs를 만들어 놓고, 계속 반복해서 돌려야 한다.
'''

import sys
from collections import deque
import heapq
input = sys.stdin.readline

n = int(input())

board = [[]*n for _ in range(n)]
dsize = 2
eat = 0
second = 0

dr = [-1,0,1,0]
dc = [0,-1,0,1]

now = (-1,-1)
for i in range(n):
    board[i] = list(map(int, input().split()))
    for j in range(n):
        if not board[i][j]: continue
        if board[i][j]==9:
            now = (i,j)
            board[i][j] = 0

def bfs(row,col,size):
    # 가장 가까운 물꼬기를 찾기 위한 bfs
    visited = [[False]*n for _ in range(n)]
    togo = deque([(row,col,0)])
    fishes = []
    while(togo):
        nowr, nowc, dist = togo.popleft()
        for i in range(4):
            nr = nowr + dr[i]
            nc = nowc + dc[i]
            if 0<=nr<n and 0<=nc<n:
                if visited[nr][nc]: continue
                if board[nr][nc]<=size:
                    # 빈칸 방문
                    visited[nr][nc] = True
                    togo.append((nr,nc,dist+1))
                    if 0<board[nr][nc]<size:
                        # 먹을 수 있음
                        heapq.heappush(fishes, (dist+1, nr, nc))

    return fishes

while(True):
    nowr, nowc = now
    fishes = bfs(nowr, nowc, dsize)
    if not fishes:
        break
    # 물고기 먹음
    (dist, fr, fc) = heapq.heappop(fishes)
    second += dist
    board[fr][fc], board[nowr][nowc] = 0,0
    now = (fr, fc)
    eat+=1
    if eat==dsize:
        eat = 0
        dsize+=1

print(second)


import requests
from bs4 import BeautifulSoup
import csv
import os

homeUrl = "http://books.toscrape.com/"

def allUrls(homeUrl):# retourne toutes les catégories
    page = requests.get(homeUrl)
    soup = BeautifulSoup(page.content, 'lxml')
    urls = []
    for a in soup.find('ul',{'class':'nav nav-list'}).li.find_all('a'):
        url = a.get('href').replace('catalogue/','http://books.toscrape.com/catalogue/')
        urls.append(url)
    categs = urls[1:]
    return categs
categs = allUrls(homeUrl)

def oneCateg(categs):# retourne une catégorie
    booksCategorie = ''
    for cat in categs:
        booksCategorie = cat
        return booksCategorie

booksCategorie = oneCateg(categs)

def catBooks(booksCategorie):# complete une liste (livresUrl) avec toutes les url d'une catégorie
    page = requests.get(booksCategorie)
    soup = BeautifulSoup(page.content,'lxml')

    # vérifit si une pagination est existante
    if soup.find('ul', class_ = 'pager'):
        nombreDePage = int(soup.find('li', class_='current').text[40])
    else:
        nombreDePage = 1

    # boucle sur chaque livre pour compléter son url en fonction de la pagination
    iterPages = 0
    livresUrl = []

    while iterPages < nombreDePage:
        for livre in soup.find_all('article'):
            livreUrl = livre.h3.a.get('href').replace('../../../', 'http://books.toscrape.com/catalogue/')
            livresUrl.append(livreUrl)
        iterPages += 1
        if iterPages >= 1 and nombreDePage > 1:
            nextPage = booksCategorie.replace('index.html','page-' + str(iterPages + 1) + '.html')
            page = requests.get(nextPage)
            soup = BeautifulSoup(page.content,'lxml')
    return livresUrl

livresUrl = catBooks(booksCategorie)

def oneBook(livresUrl):# retourne un livre de la liste des url d'une catgorie
    unLivre = ''
    for book in livresUrl:
        unLivre = book
        return unLivre

unLivre = oneBook(livresUrl)

def genData(unLivre,booksCategorie):# complete une liste avec toute les data du livre et sa categorie
    datas = []
    def prodUrl(unLivre):# retourne l'url du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        prods = unLivre
        return prods

    prods = prodUrl(unLivre)
    datas.append(prods)

    def upc(unLivre):# retourne le code du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        for upc in soup.find_all('tr'):
            if 'UPC' in upc.text:
                upcs = upc.td.text
        return upcs

    upcs = upc(unLivre)
    datas.append(upcs)

    def title(unLivre):# retourne le titre du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        for title in soup.find_all('h1'):
            titre = title.text
        return titre

    titre = title(unLivre)
    datas.append(titre)

    def ttc(unLivre):# retourne le prix ttc du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        for ttc in soup.find_all('tr'):
            if 'excl' in ttc.text:
                ttcs = ttc.td.text
        return ttcs

    ttcs = ttc(unLivre)
    datas.append(ttcs)

    def ht(unLivre):# retourne le prix ht du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        for ht in soup.find_all('tr'):
            if 'excl' in ht.text:
                hts = ht.td.text
        return hts

    hts = ht(unLivre)
    datas.append(hts)

    def dispo(unLivre):# retourne le nombre de livres disponibles
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        for dispo in soup.find_all('tr'):
            if 'Availability' in dispo.text:
                dispos = dispo.td.text.split(' ')[2].replace('(','')
        return dispos

    dispos = dispo(unLivre)
    datas.append(dispos)

    def descript(unLivre):# retourne un résumé du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        desc = soup.find_all('p',)[3]
        descripts = desc.text
        return descripts

    descripts = descript(unLivre)
    datas.append(descripts)

    def categorie(booksCategorie):# retourne la catégorie du livre
        page = requests.get(booksCategorie)
        soup = BeautifulSoup(page.content,'lxml')
        for cat in soup.ul.find_all('li'):
            if 'Home' not in cat.text and 'Books' not in cat.text:
                categorie = cat.text
        return categorie

    categorie = categorie(booksCategorie)
    datas.append(categorie)

    def eval(unLivre):# retourne l'appréciation du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        for star in soup.find_all('p',limit = 4):
            try:
                note = star['class']
                if 'star-rating' in note:
                    note = note[1]

            except KeyError:
                pass
        return note

    note = eval(unLivre)
    datas.append(note)

    def img(unLivre):# retourne l'url de l'image du livre
        page = requests.get(unLivre)
        soup = BeautifulSoup(page.content,'lxml')
        img = soup.find('img')
        image = img['src'].replace('../..','http://books.toscrape.com')
        return image

    image = img(unLivre)
    datas.append(image)

    def path(categorie):# crée l'arborescence des dossiers et retourne le chemin relatif
        cat = categorie
        cat = ''.join([x for x in cat if x.isalnum()])
        path = 'data/' + cat + '/images/'
        # créer les repertoires du path
        if not os.path.exists(path):
            os.makedirs(path)
        return path

    path = path(categorie)
    datas.append(path)

    def imgLivre(image, path, titre):# définit les paramètres de création du fichier image du livre et crée le fichier
        imageGet = requests.get(image)
        soup = BeautifulSoup(imageGet.content,'lxml')
        titre = ''.join([x for x in titre if x.isalnum()]) + '.jpg'
        open(path + titre, 'wb').write(imageGet.content)
        return imageGet

    imageGet = imgLivre(image, path, titre)
    datas.append(imageGet)

    return datas

datas = genData(unLivre, booksCategorie)

def main():# appele les fonctions sus-jacente  en leur passant les parametres nécessaires en arguments et crée les fichiers csv
    categs = allUrls(homeUrl)
    for cat in categs:
        booksCategorie = catBooks(cat)
        page = requests.get(cat)
        soup = BeautifulSoup(page.content,'lxml')
        for oneCat in soup.ul.find_all('li'):
            if 'Home' not in oneCat.text and 'Books' not in oneCat.text:
                categorie = oneCat.text

        # création du fichier csv pour chaque catégorie en complètant uniquement l'entête
        en_tete = ['product_page_url' , 'universal_product_code' , 'title' , 'price_including_tax' , 'price_excluding_tax' , 'number_available' ,'product_description' , 'category' , 'review_rating' , 'image_url']
        categorie = ''.join([x for x in categorie if x.isalnum()])
        csvFile = categorie + '.csv'
        path = 'data/' + categorie + '/'
        if not os.path.exists(path):
            os.makedirs(path)
        with open(path + csvFile, 'w', errors = 'replace') as csvFile:
            scribe = csv.writer(csvFile , delimiter = ";")
            scribe.writerow(en_tete)

        for book in booksCategorie:# récupère les data de chaque livre et complète le fichier csv de chaque catégorie
                livres = oneBook(booksCategorie)
                datas = genData(book, cat)
                page = requests.get(cat)
                soup = BeautifulSoup(page.content,'lxml')
                for oneCat in soup.ul.find_all('li'):
                    if 'Home' not in oneCat.text and 'Books' not in oneCat.text:
                        categorie = oneCat.text
                categorie = ''.join([x for x in categorie if x.isalnum()])
                csvFile = categorie + '.csv'
                path = 'data/' + categorie + '/'
                with open(path + csvFile, 'a', errors = 'replace') as csvFile:
                    scribe = csv.writer(csvFile , delimiter = ";")
                    scribe.writerow(datas[0:-2])

main()

print("Traitement termine avec succes ! ...")


from typing import List

"""
https://leetcode.cn/problems/max-dot-product-of-two-subsequences/

请你返回 nums1 和 nums2 中两个长度相同的 非空 子序列的最大点积。

数组的非空子序列是通过删除原数组中某些元素（可能一个也不删除）后剩余数字组成的序列，但不能改变数字间相对顺序。
比方说，[2,3,5] 是 [1,2,3,4,5] 的一个子序列而 [1,5,3] 不是。

"""
def maxDotProduct(nums1: List[int], nums2: List[int]) -> int:
    """
    f[i][j] 表示只考虑数组nums1的前i个元素以及数组nums2的前j个元素时,
    可以得到的两个长度相同的非空子序列的最大点积。

    可以考虑每个数组中的最后一个元素:
    1. 选择成为点积。
    2. 不选择成为点积。
    """
    m, n = len(nums1), len(nums2)
    f = [[0] * n for _ in range(m)]

    for i in range(m):
        for j in range(n):
            xij = nums1[i] * nums2[j]
            f[i][j] = xij
            if i > 0:
                f[i][j] = max(f[i][j], f[i - 1][j])
            if j > 0:
                f[i][j] = max(f[i][j], f[i][j - 1])
            if i > 0 and j > 0:
                f[i][j] = max(f[i][j], f[i - 1][j - 1] + xij)

    return f[m - 1][n - 1]

动态规划/两个子序列的最大点积.py

两个子序列的最大点积.py


from django.core.urlresolvers import reverse
from django.utils.html import format_html

from wagtail.wagtailadmin.search import SearchArea
from wagtail.wagtailcore import hooks
from wagtail.wagtailsnippets.permissions import user_can_edit_snippet_type

from wagtail.contrib.modeladmin.options import (
    ModelAdmin,
    ThumbnailMixin,
    modeladmin_register
)

from people.models import Person

class PeopleSearchArea(SearchArea):
    def is_shown(self, request):
        return user_can_edit_snippet_type(request.user, Person)

@hooks.register('register_admin_search_area')
def register_people_search_area():
    return PeopleSearchArea(
        'Team Members',
        reverse('people_person_modeladmin_index'),
        classnames='icon icon-group',
        order=150
    )

class PersonThumbnailMixin(ThumbnailMixin):
    thumb_image_field_name = 'photo'

class PeopleAdmin(ModelAdmin):
    model = Person
    menu_icon = 'group'
    menu_label = 'Team'
    menu_order = 300
    list_display = ('profile_photo', 'full_name', 'role_and_production')
    list_filter = ('role', 'production')
    list_per_page = 50
    search_fields = [
        'first_name',
        'last_name',
        'role',
        'biography',
        'production__title',
    ]

    def profile_photo(self, obj): # pylint: disable=no-self-use
        return PersonThumbnailMixin().admin_thumb(obj)
    profile_photo.short_description = "photo"

    def role_and_production(self, obj): # pylint: disable=no-self-use
        if obj.production and obj.role:
            return format_html(
                '{} at {}',
                obj.role,
                reverse('wagtailadmin_pages:edit', args=(obj.production.id,)),
                obj.production
            )
        elif obj.production:
            return format_html(
                '{}',
                reverse('wagtailadmin_pages:edit', args=(obj.production.id,)),
                obj.production
            )
        elif obj.role:
            return obj.role
        else:
            return ""
    role_and_production.admin_order_field = '-production'

    def full_name(self, obj): # pylint: disable=no-self-use
        return "%s %s" % (
            obj.first_name,
            obj.last_name
        )
    full_name.short_description = 'name'
    full_name.admin_order_field = 'last_name'

modeladmin_register(PeopleAdmin)


import numpy as np
from scipy.stats import norm
import euro_option_pricer as eop

def imp_vol(Type,S0,K,T,r,q,V,tol=1e-8,sigmadiff=1,iter_num=200):
    sigma=np.sqrt(2*abs((np.log(S0/K)+(r-q)*(T))/(T)))
    n=1
    if str.upper(Type)=='C':
        while sigmadiff>=tol and n<iter_num:
            c_bs=eop.euro_opt('C',S0,K,T,sigma,r,q)
            c=c_bs.value()
            d1=c_bs.d1_d2()[0]
            c_vega=S0*np.exp(q*(-T))*np.sqrt(T)*norm.pdf(d1)
            increment=(c-V)/c_vega
            sigma=sigma-increment
            n=n+1
            sigmadiff=abs(increment)
    elif str.upper(Type)=='P':
        while sigmadiff>=tol and n<iter_num:
            p_bs=eop.euro_opt('P',S0,K,T,sigma,r,q)
            p=p_bs.value()
            d1=p_bs.d1_d2()[0]
            p_vega=S0*np.exp(q*(-T))*np.sqrt(T)*norm.pdf(d1)
            increment=(p-V)/p_vega
            sigma=sigma-increment
            n=n+1
            sigmadiff=abs(increment)
    return sigma


import aiogram.types

from Infotron.Const import *

# Creating reply keyboard buttons
button_finansy = aiogram.types.KeyboardButton(text="Фінанси")
button_qat = aiogram.types.KeyboardButton(text="Питання щодо навчання")
button_admissions = aiogram.types.KeyboardButton(text='Питання щодо вступу')
button_mfaq = aiogram.types.KeyboardButton(text='Найчастіші запитання')
button_settlement = aiogram.types.KeyboardButton(text='Електроне поселення в гуртожиток')
button_support = aiogram.types.KeyboardButton(cfg['button_new_question'])
button_back = aiogram.types.KeyboardButton("Змінити факультет")
# Creating a reply keyboard markup and adding buttons to it
keyboard_menu = aiogram.types.ReplyKeyboardMarkup(resize_keyboard=True)
keyboard_menu.add(button_mfaq).row(button_qat).add(button_finansy, button_admissions).add(button_support, button_back)
# Creating a reply keyboard markup for faculty selection
menu_faculty = aiogram.types.ReplyKeyboardMarkup()
# Adding buttons for each faculty from the database
cafe = aiogram.types.KeyboardButton(text='Кафе')
# Отримати список імен з таблиці question_answer_faculty
cur.execute("SELECT name FROM question_faculty")
menu_faculty = aiogram.types.ReplyKeyboardMarkup(resize_keyboard=True)
for name in cur.fetchall():
    button = aiogram.types.KeyboardButton(text=str(name[0]))
    menu_faculty.add(cafe).add(button)

# Оновлення функції create_inline_keyboard
def create_inline_keyboard(text, f_id):
    """Створення внутрішнього клавішного макету для вибору запитань"""

    menu_question = aiogram.types.InlineKeyboardMarkup()
    # Додавання кнопок для кожного запитання з вказаним типом і факультетом з бази даних
    cur.execute("SELECT text FROM question_question WHERE type = %s and faculty_id = %s", (text, f_id))
    for reply in cur.fetchall():
        button = aiogram.types.InlineKeyboardButton(text=reply[0], callback_data=reply[0])
        menu_question.add(button)
    return menu_question

def keybord_kafe():
    menu_cafe = aiogram.types.ReplyKeyboardMarkup(resize_keyboard=True)
    main_menu = aiogram.types.KeyboardButton("Головного меню")
    cur.execute("SELECT type FROM cafe_student_type_product")
    for type in cur.fetchall():
        button = aiogram.types.KeyboardButton(text=str(type[0]))
        menu_cafe.add(button).add(main_menu)
    return menu_cafe

# Оновлення функції create_inline_keyboard_answer
def create_inline_keyboard_answer(text, f_id):
    """Створення внутрішнього клавішного макету для вибору запитань"""

    keyboard = aiogram.types.InlineKeyboardMarkup()
    # Додавання кнопок для кожного запитання з вказаним типом і факультетом з бази даних
    cur.execute("SELECT text FROM question_question WHERE type = %s and faculty_id = %s", (text, f_id))
    for reply in cur.fetchall():
        button = aiogram.types.InlineKeyboardButton(text=reply[0], callback_data=reply[0])
        keyboard.add(button)
    return keyboard

# Оновлення функції create_inline_keyboard_cafe
def create_inline_keyboard_cafe(text):
    """Створення внутрішнього клавішного макету для вибору запитань"""

    keyboard = aiogram.types.InlineKeyboardMarkup()
    cur.execute("SELECT name, cost FROM cafe_student_product WHERE type_product_id = %s", (text,))
    for name, cost in cur.fetchall():
        button = aiogram.types.InlineKeyboardButton(text=f'{name} = {cost}грн.',
                                                    callback_data=f"product:{name}:{cost}")
        keyboard.add(button)
    return keyboard

keyboard_order = aiogram.types.InlineKeyboardMarkup()
basket = aiogram.types.InlineKeyboardButton(text='Мій кошик', callback_data='my_basket')
confirm_order = aiogram.types.InlineKeyboardButton(text='Підтвердити замовлення', callback_data='confirm_order')
keyboard_order.add(basket, confirm_order)


import numpy as np
from scipy import signal as scipy_signal

# this breaks the spectrogram up and keeps it from blowing up memory since i only have 16 gigs... :(
def spectrogram(signal, size=64, sample_rate=11025):
	signal_length = len(signal)
	segment_length = 1000000
	stride_length = segment_length - (size - 1)
	i = 0

	stft_segments = []

	while i + segment_length < signal_length:
		segment = signal[i:i + segment_length]
		_, _, spec = scipy_signal.spectrogram(segment, sample_rate, nperseg=size, noverlap=size-1, mode='magnitude')
		stft_segments.append(spec)
		i += stride_length

	final_segment = signal[i:]

	_, _, spec = scipy_signal.spectrogram(final_segment, sample_rate, nperseg=size, noverlap=size-1, mode='magnitude')
	stft_segments.append(spec)

	return np.concatenate(stft_segments, axis=1)


from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
  username = {
    "user": "Eugene"
  }
  return render_template("index.html", title="Lab 2", username=username)

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


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

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from sklearn.metrics import r2_score,mean_absolute_error
from sklearn.tree import DecisionTreeRegressor

def main():
    #准备数据
    boston = load_boston()
    #获取特征集
    features = boston.data
    #获取房价
    prices = boston.target
    #随机抽取33%的数据作为测试集，其他为训练结合
    train_features, test_features, train_labels, test_labels = train_test_split(features, prices, test_size=0.33,random_state=0)
    #创建CART回归树
    dtr = DecisionTreeRegressor()
    #拟合构造CART回归树
    dtr.fit(train_features,train_labels)
    #预测测试集合中的房价
    predict_price = dtr.predict(test_features)
    #结果评价
    # 回归树二乘偏差均值
    print(mean_squared_error(test_labels,predict_price))
    #回归树绝对值偏差均值
    print(mean_absolute_error(test_labels,predict_price))

if __name__ == "__main__":
    main()


"""
This module contains tools and functions to monitor performance of a model
"""
from typing import Dict, Any, Union
import pandas as pd
from pathlib import Path

from starter import common
from sklearn.metrics import fbeta_score, precision_score, recall_score

class PerformanceEvaluator:
    def __init__(self, dataframe: pd.DataFrame, label_column: str, prediction_column: str = 'pred'):
        """

        Args:
            dataframe: the input dataset. It must include the label and prediction columns
            label_column: column of the binary encoded labels
            prediction_column: column of the binary encoded predictions
        """
        self.df = dataframe
        self.label_column = label_column
        self.prediction_column = prediction_column

    def compute_performance_on_slice(self, feature: str) -> Dict[Any, common.BinaryConfusionMatrix]:
        """ Slices the data based on the input feature and compute the common.BinaryConfusionMatrix for each slice

        Args:
            feature: the feature (column name) used to generate the data slices

        Returns:
            a confusion matrix for each category of the selected feature.
            The output is provided as dictionary with
            {key: value} ---> {category for the slice: confusion matrix on the category}

        """
        label_column = self.label_column
        prediction_column = self.prediction_column
        return {x[0]: common.BinaryConfusionMatrix(common.confusion_matrix_df(x[1], label_column, prediction_column))
                for x in self.df.groupby(feature)}

    def get_summary_slice(self, feature: str, output_file: Union[str, None]) -> pd.DataFrame:
        """ Merges together the performance on each category for the input feature.
        Each category has a different number of samples.
        To get the total number of sample for each category is sufficient to sum up the number of true/false
        positives/negatives (in the output dataframe ['tp', 'fp', 'tn', 'fn']

        Args:
            feature: the feature (column name) used to generate the data slices
            output_file: if not None, writes the summary to the specified file

        Returns:
            the summary of the performance for each category of the given slice (as pd.DataFrame)

        """
        cm_dict = self.compute_performance_on_slice(feature)
        slice_perf = pd.concat(list(map(lambda x: x.to_df(), cm_dict.values())), ignore_index=True)
        slice_perf.index = cm_dict.keys()

        if output_file:
            if not Path(output_file).parent.is_dir():
                Path(output_file).mkdir(parents=True)
            slice_perf.to_csv(output_file)
        return slice_perf

def compute_model_metrics(y, preds):
    """
    Validates the trained machine learning model using precision, recall, and F1.

    Inputs
    ------
    y : np.array
        Known labels, binarized.
    preds : np.array
        Predicted labels, binarized.
    Returns
    -------
    precision : float
    recall : float
    fbeta : float
    """
    fbeta = fbeta_score(y, preds, beta=1, zero_division=1)
    precision = precision_score(y, preds, zero_division=1)
    recall = recall_score(y, preds, zero_division=1)
    return precision, recall, fbeta


from django.shortcuts import render
from django.http import HttpResponse
from .models import  Impulses, Currency, Candles, BigOrders,OrdersRealtime

from .db_request import getCandlesDF, getOrders
from .math_methods import getChartWithImpulse,impulse_long,getChartWithOrders

from datetime import datetime, timedelta

import numpy as np

def index(request):
    result = []
    currency = Currency.objects.all()

    list_TF = [5,15,30,60]
    other_result = []
    for curr in currency:
        listImpulses = []
        isHaveImpulse = False
        for TF in list_TF:
            currImp = 0
            count_after_imp = 0
            # ПРОХОДИМСЯ ПО ИМПУЛЬСАМ
            imp_list = Impulses.objects.filter(symbol = curr, tf = TF)
            for imp in imp_list:
                if imp.isOpen:
                    diff = datetime.now() - imp.dateEnd
                    count_after_imp = np.round(diff.total_seconds() / 60 / TF,0)
                    isHaveImpulse = True
                    currImp = imp
            listImpulses.append({'impulse' : currImp, 'count' :  count_after_imp, 'TF': TF})
        # ЗАПИСЫВАЕМ ОРДЕРА
        orderLMin = 0
        if OrdersRealtime.objects.filter(symbol = curr, type = 'bids').exists():
            orderL = OrdersRealtime.objects.get(symbol_id=curr, type='bids')
            orderLMin = np.round((orderL.dateEnd - orderL.dateStart).total_seconds() / 60, 0)
        orderSMin = 0
        if OrdersRealtime.objects.filter(symbol=curr, type='asks').exists():
            orderS = OrdersRealtime.objects.get(symbol_id=curr, type='asks')
            orderSMin = np.round((orderS.dateEnd - orderS.dateStart).total_seconds() / 60, 0)
        if isHaveImpulse:
            result.append({'name' : curr.name, 'impInfo' : listImpulses, 'orderL' : orderLMin,'orderS': orderSMin })
        else:
            other_result.append({'name': curr.name, 'impInfo': listImpulses, 'orderL' : orderLMin, 'orderS': orderSMin})

    result.extend(other_result)
    return render(request, 'screener/currency_table.html', {'result':result, 'listTF' : list_TF})

def single_currency(request, name):
    print(name)
    return render(request, 'screener/single_currency.html', {'name' : name})

def test_view(request,name, tf, type):
    currency = Currency.objects.get(name = name)
    df = getCandlesDF(currency, tf)
    imp = Impulses.objects.get(symbol = currency, tf = tf, type = type)
    #bigOrders = getOrders(currency)
    chart = getChartWithImpulse(df, imp, tf)
    return render(request, 'screener/test_view.html', {'chart': chart})

def chartOrder(request,name, type):
    chart = getChartWithOrders(name)
    return render(request, 'screener/test_view.html', {'chart': chart})


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

import scipy.__config__ as cf

'''Usage:
       python setup.py build_ext --inplace --fcompiler=gnu95'''

'''Function obtained from http://www.peterbe.com/plog/uniqifiers-benchmark'''
def f7(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if x not in seen and not seen_add(x)]

def configuration(parent_package='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('gsvd',parent_package,top_path)
    blinfo = cf.get_info('blas_opt')
    lpinfo = cf.get_info('lapack_opt')
    atinfo = cf.get_info('atlas_threads')
    atbinfo = cf.get_info('atlas_blas_threads')

    libs = []
    include = []
    libdirs = []
    def_mac = []

    for inf in (lpinfo,atbinfo,blinfo,atinfo):
        if len(inf.keys()):
            if 'libraries' in inf.keys():
                libs += inf['libraries']
            if 'include_dirs' in inf.keys():
                include += inf['include_dirs']
            if 'library_dirs' in inf.keys():
                libdirs += inf['library_dirs']
            if 'define_macros' in inf.keys():
                def_mac += inf['define_macros']

    libs = f7(libs)
    include = f7(include)
    libdirs = f7(libdirs)
    def_mac = f7(def_mac)

#    print libs
#    print include
#    print libdirs
#    print def_mac

    config.add_extension('gensvd',
            ['gensvd.pyf','dggsvd.f'],
            libraries=libs,
            include_dirs=include,
            library_dirs=libdirs,
            define_macros= def_mac)
    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(configuration=configuration)

############################################################
# Program is part of GIAnT v1.0                            #
# Copyright 2012, by the California Institute of Technology#
# Contact: earthdef@gps.caltech.edu                        #
############################################################


"""
COSC2671: Assignment 2, 2018
@author a small world
@description To read the final graphml file and compute & visualise the basic measures of a graph.
			 It also calculates the clustering coefficient of the initial (starting) ego-centric users

"""

import networkx as nx
import matplotlib.pyplot as plt

G = nx.read_graphml('all_nodes.graphml')
starting_users = ['pydatasci', 'RProgramming', 'DataCamp',
				  'R_programming', 'rstudio', 'pycharm',
				  'kaggle']
top_n = 20

def compute_centrality(g, centrality_type, n):

	if centrality_type == 'eigen':
		centrality = nx.eigenvector_centrality_numpy(g)
	elif centrality_type == 'degree':
		centrality = nx.degree_centrality(g)
	else:
		centrality = nx.katz_centrality_numpy(g)

	# Return the top 10 accounts by highest centrality
	ordered_accounts = tuple(centrality.items())
	ordered_accounts = sorted(ordered_accounts, reverse=True, key = lambda x: x[1])

	for item in ordered_accounts[0:n]:
		print('{}: {} centrality of {}'.format(centrality_type, item[0], item[1]))

	return centrality.values()

print('Obtaining {} most central nodes....'.format(top_n))
print('\n\tDegree centrality')
degree  = compute_centrality(G, 'degree', n=top_n)

print('\n\tEigenvector centrality')
eigen = compute_centrality(G, 'eigen', n=top_n)

# print('\n\tKatz centrality')
# katz  = compute_centrality(G, 'katz', n=top_n)

print('Visualising of degree distributions....')
plt.subplot(1,3,1)
plt.hist(list(eigen))
plt.xlabel('Eigenvector centrality')
plt.ylabel('Degree')

# plt.subplot(1,3,2)
# plt.hist(list(katz))
# plt.title('Degree')
# plt.xlabel('Katz centrality')

plt.subplot(1,3,3)
plt.hist(list(degree))
plt.xlabel('Degree centrality')

plt.show()

# Visualisation of in- and out degree distributions

if nx.is_directed(G):
	print('The graph is directed.')
	print(nx.in_degree_centrality(G))
	print(nx.out_degree_centrality(G))

# Global clustering coefficient or transitivity of a graph
# Remember to convert to undirected graph
print('\nClustering coefficients of Ego users')

# Getting average clustering for the starting users
starting_users_clustering = []
for users in starting_users:
	x = nx.clustering(G.to_undirected(), users)
	starting_users_clustering.append(x)
	print(x)

print('Average clustering for G is {}'.format(nx.average_clustering(G.to_undirected())))
print('\nTransitivity')
print(nx.transitivity(G.to_undirected()))

# Distances: short path between userName1 and userName2
# print(nx.shortest_path(G, userName1, userName2))
# print(nx.shortest_path_length(G, userName1, userName2))

# Diameter and eccentricity are applicable when the graph is strongly connected
# number of strongly/weakly connected components
if nx.is_strongly_connected(G):
	print('G is strongly connected')
	print(sorted(nx.strongly_connected_components(G)))
	print(nx.number_connected_components(G))
	print(nx.eccentricity(G)) # Eccentricity of a node: the largest distance between n and all other nodes.
	print(nx.diameter(G))     # Diameter: maximum distance between any pair of nodes

elif nx.is_weakly_connected(G):
	print('G is weakly connected')

# If the graph is weakly connected it only has one weakly connected component.

print('Closeness centrality')

# TODO: to visualise the closeness

# TODO: number of bridges
# TODO: robustness, Node connectivity
# TODO: Betweenness centraliy

# TODO: Compare centrality


import airsim

boxsize = 50
stripewidth = 10
x = -50
z = -30

path = []
xs = []
distance = 0
while x < boxsize:
	distance += boxsize
	path.append(airsim.Vector3r(x, boxsize, z))
	x += stripewidth
	xs.append(x)
	distance += stripewidth
	path.append(airsim.Vector3r(x, boxsize, z))
	distance += boxsize
	path.append(airsim.Vector3r(x, -boxsize, z))
	x += stripewidth
	xs.append(x)
	distance += stripewidth
	path.append(airsim.Vector3r(x, -boxsize, z))
	distance += boxsize

# print(path)
# path_x_vals = [0, -50]
# path_y_vals = [0, 50]
# path_z_vals = [-30, -30]

path_x_vals = [-50]
path_y_vals = [50]
path_z_vals = [-30]
for p in path:
	x_val = p.x_val
	y_val = p.y_val
	z_val = p.z_val
	path_x_vals.append(x_val)
	path_y_vals.append(y_val)
	path_z_vals.append(z_val)
	print(f"\n{p}")
print(xs)
print(x)

# path_x_vals.append(0)
# path_y_vals.append(0)
# path_z_vals.append(-30)

import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

fig = plt.figure()
ax = plt.axes(projection='3d')

ax.plot3D(path_x_vals, path_y_vals, path_z_vals, 'black', label="scanning flight path")
# ax.plot3D([0], [0], [-30], 'green')
# ax.scatter3D(path_x_vals, path_y_vals, path_z_vals, c=path_z_vals, cmap='Greens')
ax.scatter3D([0], [0], [-30], color="green", label="survey flight path origin point")
ax.plot3D([0, 0], [0, 0], [0, -30], "--k", label="take-off")
ax.plot3D([0, path_x_vals[0]], [0, path_y_vals[0]], [path_z_vals[0], path_z_vals[0]], ":k", label="flight to origin point")
ax.scatter3D(path_x_vals, path_y_vals, path_z_vals, color="red", label="rotational maneuvers")
ax.plot3D([0, path_x_vals[-1]], [0, path_y_vals[-1]], [path_z_vals[-1], path_z_vals[-1]], "-.k", label="return to origin")
# ax.scatter3D([0], [0], [-30], color="green")
ax.axes.set_zlim3d(bottom=0, top=-60)
# plt.plot(path_x_vals, path_y_vals)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.text(0, 0, -30, "O", color='green', fontsize=12)
# ax.text(path_x_vals[0], path_y_vals[0], path_z_vals[0], "P1", color='green', fontsize=12)
ax.text(path_x_vals[1], path_y_vals[1], path_z_vals[1], "P1", color='green', fontsize=12)
ax.text(path_x_vals[2], path_y_vals[2], path_z_vals[2], "P2", color='green', fontsize=12)
ax.text(path_x_vals[3], path_y_vals[3], path_z_vals[3], "P3", color='green', fontsize=12)
ax.text(path_x_vals[4], path_y_vals[4], path_z_vals[4], "P4", color='green', fontsize=12)
# ax.text2D(0, 0, -30, "O (0, 0, -30)", color='green')
plt.legend()
# ax.label()
plt.show()


from collections import defaultdict
from enum import Enum
import logging
import sys
from typing import Dict, Optional, Sequence

from gym import Env, spaces

import numpy as np

from pyhtzee import Pyhtzee
from pyhtzee.classes import Category, PyhtzeeException, Rule

log = logging.getLogger(__name__)

class GameType(Enum):
    SUDDEN_DEATH = 0,
    RETRY_ON_WRONG_ACTION = 1

def get_score(score: Optional[int]) -> int:
    return score if score is not None else -1

def get_dice_face_counts(dice: Sequence[int]) -> Dict[int, int]:
    faces: Dict[int, int] = defaultdict(int)
    for die in dice:
        faces[die] += 1
    return faces

class YahtzeeSingleEnv(Env):
    metadata = {'render.modes': ['human']}

    def __init__(self,
                 rule: Rule = Rule.YAHTZEE_FREE_CHOICE_JOKER,
                 game_type: GameType = GameType.RETRY_ON_WRONG_ACTION,
                 seed=None):
        self.pyhtzee = Pyhtzee(seed=seed)
        self.rule = rule
        self.game_type = game_type
        self.action_space = spaces.Discrete(44)
        self.observation_space = spaces.Tuple((
            spaces.Discrete(13),  # round
            spaces.Discrete(4),  # sub-round
            spaces.Box(low=1, high=6, shape=(1,), dtype=np.uint8),  # die 1
            spaces.Box(low=1, high=6, shape=(1,), dtype=np.uint8),  # die 2
            spaces.Box(low=1, high=6, shape=(1,), dtype=np.uint8),  # die 3
            spaces.Box(low=1, high=6, shape=(1,), dtype=np.uint8),  # die 4
            spaces.Box(low=1, high=6, shape=(1,), dtype=np.uint8),  # die 5
            spaces.Box(low=-1, high=5, shape=(1,), dtype=np.int16),  # aces
            spaces.Box(low=-1, high=10, shape=(1,), dtype=np.int16),  # twos
            spaces.Box(low=-1, high=15, shape=(1,), dtype=np.int16),  # threes
            spaces.Box(low=-1, high=20, shape=(1,), dtype=np.int16),  # fours
            spaces.Box(low=-1, high=25, shape=(1,), dtype=np.int16),  # fives
            spaces.Box(low=-1, high=30, shape=(1,), dtype=np.int16),  # sixes
            spaces.Box(low=-1, high=30, shape=(1,), dtype=np.int16),  # three of a kind
            spaces.Box(low=-1, high=30, shape=(1,), dtype=np.int16),  # four of a kind
            spaces.Box(low=-1, high=25, shape=(1,), dtype=np.int16),  # full house
            spaces.Box(low=-1, high=30, shape=(1,), dtype=np.int16),  # small straight
            spaces.Box(low=-1, high=40, shape=(1,), dtype=np.int16),  # large straight
            spaces.Box(low=-1, high=30, shape=(1,), dtype=np.int16),  # chance
            spaces.Box(low=-1, high=50, shape=(1,), dtype=np.int16),  # yahtzee
            spaces.Box(low=-1, high=35, shape=(1,), dtype=np.int16),  # upper bonus
            spaces.Box(low=-1, high=1200, shape=(1,), dtype=np.int16),  # yahtzee bonus
        ))

    def get_observation_space(self):
        pyhtzee = self.pyhtzee
        return (
            pyhtzee.round,
            pyhtzee.sub_round,
            pyhtzee.dice[0],
            pyhtzee.dice[1],
            pyhtzee.dice[2],
            pyhtzee.dice[3],
            pyhtzee.dice[4],
            get_score(pyhtzee.scores.get(Category.ACES)),
            get_score(pyhtzee.scores.get(Category.TWOS)),
            get_score(pyhtzee.scores.get(Category.THREES)),
            get_score(pyhtzee.scores.get(Category.FOURS)),
            get_score(pyhtzee.scores.get(Category.FIVES)),
            get_score(pyhtzee.scores.get(Category.SIXES)),
            get_score(pyhtzee.scores.get(Category.THREE_OF_A_KIND)),
            get_score(pyhtzee.scores.get(Category.FOUR_OF_A_KIND)),
            get_score(pyhtzee.scores.get(Category.FULL_HOUSE)),
            get_score(pyhtzee.scores.get(Category.SMALL_STRAIGHT)),
            get_score(pyhtzee.scores.get(Category.LARGE_STRAIGHT)),
            get_score(pyhtzee.scores.get(Category.CHANCE)),
            get_score(pyhtzee.scores.get(Category.YAHTZEE)),
            get_score(pyhtzee.scores.get(Category.UPPER_SECTION_BONUS)),
            get_score(pyhtzee.scores.get(Category.YAHTZEE_BONUS)),
        )

    def sample_action(self):
        action = self.pyhtzee.sample_action()
        log.info(f'Sampled action: {action}')
        return action

    def step(self, action: int):
        pyhtzee = self.pyhtzee
        try:
            reward = pyhtzee.take_action(action)
            finished = pyhtzee.is_finished()
            valid_move = True
        except PyhtzeeException:
            valid_move = False
            reward = 0
            if self.game_type == GameType.SUDDEN_DEATH:
                log.info('Invalid action, terminating round.')
                finished = True
            else:  # retry on wrong action
                log.info('Invalid action, step ignored.')
                finished = False

        log.info(f'Finished step. Reward: {reward}, Finished: {finished}')
        debug_info = {
            'valid_move': valid_move,
        }
        return self.get_observation_space(), reward, finished, debug_info

    def reset(self):
        self.pyhtzee = Pyhtzee()

    def render(self, mode='human', close=False):
        dice = self.pyhtzee.dice
        outfile = sys.stdout
        outfile.write(f'Dice: {dice[0]} {dice[1]} {dice[2]} {dice[3]} {dice[4]} '
                      f'Round: {self.pyhtzee.round}.{self.pyhtzee.sub_round} '
                      f'Score: {self.pyhtzee.get_total_score()}\n')


from __future__ import unicode_literals, annotations
import discord
from discord.ext.commands import Bot
import random
import asyncio

admins = []

def load_admin_commands(bot: Bot, fixed_response: str, name: str) -> str:
    # fixed response = "I work and speak only for my MASTER!"

    # ==== Say Command ====
    help_string = "Admin Commands: \n"

    global admins

    command = ("bite", "The " + name + " bites the target.")

    @bot.command(name=command[0], help=command[1])
    async def bite(context, arg: str):
        if context.message.author.id in admins:
            await context.message.channel.send("*The Goose Bites " + arg + "," +
                                               " die heathen!*")
        else:
            await context.message.channel.send("...")
            await asyncio.sleep(3)
            await context.message.channel.send("*The Goose bites " +
                                               context.message.author.name +
                                               " instead!*")

    help_string += "\t" + ": ".join(command) + "\n"

    command = ("admin", "Emily can grant admin.")

    @bot.command(name=command[0], help=command[1])
    async def admin(context, target: discord.User):
        if context.message.author.id == 397458781665230848:
            if target.id not in admins:
                admins.append(target.id)
                await context.message.channel.send("Successfully granted " +
                                                   target.name
                                                   + " admin for this session.")
            else:
                await context.message.channel.send(target.name +
                                                   " is already registered.")

    help_string += "\t" + ": ".join(command) + "\n"

    command = ("remove_admin", "Emily can remove admin.")

    @bot.command(name=command[0], help=command[1])
    async def remove_admin(context, target: discord.User):
        if context.message.author.id == 397458781665230848:
            if target.id in admins:
                admins.remove(target.id)
                await context.message.channel.send("Successfully removed " +
                                                   target.name
                                                   + " admin for this session.")
            else:
                await context.message.channel.send(target.name +
                                                   " is not admin.")

    help_string += "\t" + ": ".join(command) + "\n"

    return help_string


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

import numpy as np
import copy
import matplotlib as mpl
import matplotlib.pyplot as plt
import time
import HMM
import GaussianHMM as ghmm

print ("Starting convergence experiment...")

start_time = time.time()

#
# rtol=1e-3 is ideal for dA=0.0, sigval=0.1
# rtol=1e-4 is ideal for dA=0.1, sigval=0.1
# rtol=1e-5 is ideal for dA=0.2, sigval=0.1
#
# rtol=1e-5 (или больше) for dA=0.1, sigval=0.2
# rtol=1e-6 (или больше) for dA=0.1, sigval=0.4

#
# research params
#

n_of_launches = 1
K = 100
T = 100
T_for_dist = 500
K_for_dist = 100
hmms0_size = 5
max_iter = 10000
is_using_true_hmm_for_hmms0 = False
rtol_range = np.array([1e-1**i for i in range(1, 8)])
is_gaps_places_different = True
n_of_gaps = int(T * 0)
algorithm = 'marginalization'
#algorithm = 'viterbi'
dA = 0.0
dtau = 0.1
dmu = 0.1
dsig = 0.1
sig_val = 0.1

#
# true HMM parameters
#
pi = np.array([0.3, 0.4, 0.3])
a = np.array([[0.1+dA, 0.7-dA, 0.2],
              [0.2, 0.2+dA, 0.6-dA],
              [0.8-dA, 0.1, 0.1+dA]])
tau = np.array([[0.3+dtau, 0.4-dtau, 0.3],
                [0.3, 0.4+dtau, 0.3-dtau],
                [0.3-dtau, 0.4, 0.3+dtau]])
mu = np.array([
              [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]],
              [[3.0, 3.0], [4.0, 4.0], [5.0, 5.0]],
              [[6.0, 6.0], [7.0, 7.0], [8.0, 8.0]],
              ])
mu[:, :, 0] -= dmu
mu[:, :, 1] += dmu
Z = (mu[0,0]).size
N, M = tau.shape
sig = np.empty((N,M,Z,Z))
for n in range(N):
    for m in range(M):
        sig[n,m,:,:] = np.eye(Z) * (sig_val + dsig)
hmm = ghmm.GHMM(N, M, Z, mu, sig, pi=pi, a=a, tau=tau)

# is_using_true_hmm_for_hmms0
if is_using_true_hmm_for_hmms0:
    hmm0 = copy.deepcopy(hmm)
    hmms0 = [hmm0]
else:
    hmms0 = None

# generate gaps positions
def gen_gaps(seqs):
    np.random.seed(1)
    to_dissapears = []
    if is_gaps_places_different:
        # gaps are in different places in sequences
        for k in range(K):
            to_dissapear = np.arange(T)
            np.random.shuffle(to_dissapear)
            to_dissapears.append(to_dissapear)
    else:
         # gaps are at the same place in sequences
        to_dissapear = np.arange(T)
        np.random.shuffle(to_dissapear)
        for k in range(K):
            to_dissapears.append(to_dissapear)

    # mark some elements as availiable and others as missing
    avails = [np.full(seqs[k].shape[0], True, dtype=np.bool) for k in range(K)]
    for k in range(K):
        avails[k][to_dissapears[k][:n_of_gaps]] = False
        seqs[k][to_dissapears[k][:n_of_gaps]] = np.nan
    return seqs, avails

#
# experiment
#
iters = np.zeros_like(rtol_range, dtype=np.int)
ps = np.zeros_like(rtol_range)
pi_norms = np.zeros_like(rtol_range)
a_norms = np.zeros_like(rtol_range)
tau_norms = np.zeros_like(rtol_range)
mu_norms = np.zeros_like(rtol_range)
sig_norms = np.zeros_like(rtol_range)
dists = np.zeros_like(rtol_range)

sum_p_true = 0.0
for n_of_launch in range(n_of_launches):
    print ("n_of_launch = {}".format(n_of_launch))
    print
    seqs, state_seqs = hmm.generate_sequences(K, T, seed=n_of_launch)

    p_true = hmm.calc_loglikelihood(seqs)
    sum_p_true += p_true

    seqs_original = copy.deepcopy(seqs)

    seqs, avails = gen_gaps(seqs)

    step = 0
    for rtol in rtol_range:
        # train hmm
        np.random.seed(n_of_launch)
        hmm_trained, p_max, iter_best, n_of_best = \
            ghmm.train_best_hmm_baumwelch(seqs, hmms0_size, N, M, Z, hmms0=hmms0,
                                          rtol=rtol, max_iter=max_iter, avails=avails,
                                          algorithm=algorithm)
        p = hmm_trained.calc_loglikelihood(seqs_original)
        diff_pi = np.linalg.norm(hmm_trained._pi - hmm._pi)
        diff_a = np.linalg.norm(hmm_trained._a - hmm._a)
        diff_tau = np.linalg.norm(hmm_trained._tau - hmm._tau)
        diff_mu = np.linalg.norm(hmm_trained._mu - hmm._mu)
        diff_sig = np.linalg.norm(hmm_trained._sig - hmm._sig)
        np.random.seed(n_of_launch)
        dist = HMM.calc_symmetric_distance(hmm_trained, hmm,
                                           T_for_dist, K_for_dist)
        ps[step] += p
        pi_norms[step] += diff_pi
        a_norms[step] += diff_a
        tau_norms[step] += diff_tau
        mu_norms[step] += diff_mu
        sig_norms[step] += diff_sig
        iters[step] += iter_best
        dists[step] += dist
        print("rtol = {}".format(rtol))
        print("p = {}".format(p))
        print("p_max = {}".format(p_max))
        print("p_true = {}".format(p_true))
        print("diff_pi = {}".format(diff_pi))
        print("diff_a = {}".format(diff_a))
        print("diff_tau = {}".format(diff_tau))
        print("diff_mu = {}".format(diff_mu))
        print("diff_sig = {}".format(diff_sig))
        print("dist = {}".format(dist))
        # print (str(hmm_trained))
        print("iter_best = {}".format(iter_best))
        print("n_of_best = {}".format(n_of_best))
        print(("--- {:.1f} minutes ---".format((time.time()-start_time) / 60)))

        print
        step += 1

sum_p_true /= n_of_launches
iters //= n_of_launches
ps /= n_of_launches
pi_norms /= n_of_launches
a_norms /= n_of_launches
tau_norms /= n_of_launches
mu_norms /= n_of_launches
sig_norms /= n_of_launches
dists = dists / n_of_launches

xs = np.arange(rtol_range.size)

# draw plots
# plot all this
mpl.rcdefaults()
font = {'family': 'Verdana',
        'weight': 'normal'}
mpl.rc('font',**font)
mpl.rc('font', size=12)
plt.figure(figsize=(1920/96, 1000/96), dpi=96)

labels = ["{}, 1e-{}".format(iters[i], xs[i]+1) for i in range(len(rtol_range))]
print (labels)

suptitle = u"Исследование сходимости алгоритма Баума-Велша. "\
           u"Число скрытых состояний N = {}, число смесей M = {}, "\
           u"Размерность наблюдений Z = {}, \n"\
           u"число обучающих последовательностей K = {}, "\
           u"максимальная длина обучающих последовательностей Tmax = {}, \n"\
           u"число начальных приближений = {}, "\
           u"число запусков эксперимента n_of_launches = {}, dA={}, sig_val={}\n"\
           u"в качестве начального приближения взята истинная модель = {}, "\
           u"число пропусков={}, алгоритм={}" \
           .format(N, M, Z, K, T, hmms0_size, n_of_launches, dA, sig_val,
                   is_using_true_hmm_for_hmms0, n_of_gaps, algorithm)
xlabel = u"Число итераций"

plt.suptitle(suptitle)

ax1 = plt.subplot(121)
plt.ylabel(u"Логарифм правдоподобия")
plt.xlabel(xlabel)
plt.plot(xs, ps, '-', label=u'Обученная модель')
plt.plot(xs, np.full(len(xs), sum_p_true), '--', label=u'Истинная модель')
plt.xticks(xs, labels)
plt.gca().xaxis.grid(True)
plt.legend(loc='lower right')

#ax2 = plt.subplot(322)
#plt.ylabel(r"$||\Pi - \Pi^*||$")
#plt.xlabel(xlabel)
#plt.plot(xs, pi_norms, '-')
#plt.xticks(xs, labels)
#plt.gca().xaxis.grid(True)
#
#ax3 = plt.subplot(323, sharex=ax1)
#plt.ylabel(r"$||A - A^*||$")
#plt.xlabel(xlabel)
#plt.plot(xs, a_norms, '-')
#plt.xticks(xs, labels)
#plt.gca().xaxis.grid(True)
#
#ax4 = plt.subplot(324, sharex=ax2)
#plt.ylabel(r"$||\tau - \tau^*||$")
#plt.xlabel(xlabel)
#plt.plot(xs, tau_norms, '-')
#plt.xticks(xs, iters)
#plt.gca().xaxis.grid(True)
#
#ax5 = plt.subplot(325, sharex=ax1)
#plt.ylabel(r"$||\mu - \mu^*||$")
#plt.xlabel(xlabel)
#plt.plot(xs, mu_norms, '-')
#plt.xticks(xs, labels)
#plt.gca().xaxis.grid(True)
#
#ax6 = plt.subplot(326, sharex=ax2)
#plt.ylabel(r"$||\Sigma - \Sigma^*||$")
#plt.xlabel(xlabel)
#plt.plot(xs, sig_norms, '-')
#plt.xticks(xs, labels)
#plt.gca().xaxis.grid(True)

ax5 = plt.subplot(122, sharex=ax1)
plt.ylabel(r"$D_s(\lambda, \lambda^*)$")
plt.xlabel(xlabel)
plt.plot(xs, dists, '-')
plt.xticks(xs, labels)
plt.gca().xaxis.grid(True)

plt.show()

filename = "out/ghmm_research_convergence_N={}_M={}_Z={}_K={}_T={}_hmms0_size={}_launches={}_"\
           "truehmm0={}_nofgaps={}_alg={}"\
            .format(N, M, Z, K, T, hmms0_size, n_of_launches,
                    is_using_true_hmm_for_hmms0, n_of_gaps, algorithm)
plt.savefig(filename+".png")


import tensorflow as tf
import tensorflow_addons as tfa
from collections.abc import Iterable

def upsample(x: tf.Tensor, use_conv: bool = False) -> tf.Tensor:
    batch_size, height, width, channels = x.shape
    x = tf.keras.layers.UpSampling2D(interpolation='nearest')(x)
    if use_conv:
        x = tf.keras.layers.Conv2D(channels, 3, padding='same')(x)
    assert(x.shape == [batch_size, height * 2, width * 2, channels])
    return x

def downsample(x: tf.Tensor, use_conv: bool = False):
    batch_size, height, width, channels = x.shape
    if use_conv:
        x = tf.keras.layers.Conv2D(channels, 3, strides=2, padding='same')(x)
    else:
        x = tf.keras.layers.AveragePooling2D(strides=2, padding='same')(x)
    assert(x.shape == [batch_size, height // 2, width // 2, channels])
    return x

def attention_block(x: tf.Tensor) -> tf.Tensor:
    """
    Implementing self-attention block, as mentioned in
    https://arxiv.org/pdf/1809.11096.pdf
    """

    x = tfa.layers.GroupNormalization(groups=32, axis=-1)(x)

    q = AttentionVectorLayer()(x)
    v = AttentionVectorLayer()(x)
    k = AttentionVectorLayer()(x)

    h = tf.keras.layers.Attention()([q, v, k])

    return x + h

def deep_resblock(
        x: tf.Tensor,
        noise_embedding: tf.Tensor,
        n_out_channels: bool = None,
        dropout: float = 0.) -> tf.Tensor:
    """
    Reuse the resblock definitions from https://arxiv.org/pdf/1809.11096.pdf
    Slight tweak to use ConditionalInstanceNormalization and pre-activation
    as described in https://arxiv.org/pdf/1907.05600.pdf
    """
    if not n_out_channels:
        n_out_channels = x.shape[-1]

    track_a = tf.keras.layers.Conv2D(n_out_channels, 1, padding='same')(x)

    track_b = tfa.layers.GroupNormalization(groups=32, axis=-1)(x)
    track_b = tf.keras.activations.swish(track_b)
    track_b = tf.keras.layers.Conv2D(n_out_channels, 1, padding='same')(track_b)
    track_b = ConditionalInstanceNormalization()([track_b, noise_embedding])
    track_b = tf.keras.activations.swish(track_b)
    track_b = tf.keras.layers.Conv2D(n_out_channels, 3, padding='same')(track_b)
    track_b = tf.keras.layers.Dropout(dropout)(track_b)
    track_b = tfa.layers.GroupNormalization(groups=32, axis=-1)(x)
    track_b = tf.keras.activations.swish(track_b)
    track_b = tf.keras.layers.Conv2D(n_out_channels, 3, padding='same')(track_b)
    track_b = ConditionalInstanceNormalization()([track_b, noise_embedding])
    track_b = tf.keras.activations.swish(track_b)
    track_b = tf.keras.layers.Conv2D(n_out_channels, 1, padding='same')(track_b)

    return track_a + track_b

def resblock(
        x: tf.Tensor,
        noise_embedding: tf.Tensor,
        n_out_channels: bool = None,
        dropout: float = 0.) -> tf.Tensor:
    """
    Reuse the resblock definitions from https://arxiv.org/pdf/1809.11096.pdf
    Slight tweak to use ConditionalInstanceNormalization and pre-activation
    as described in https://arxiv.org/pdf/1907.05600.pdf
    """
    if not n_out_channels:
        n_out_channels = x.shape[-1]

    track_a = tf.keras.layers.Conv2D(n_out_channels, 1, padding='same')(x)

    track_b = tfa.layers.GroupNormalization(groups=32, axis=-1)(x)
    track_b = tf.keras.activations.swish(track_b)
    track_b = tf.keras.layers.Conv2D(n_out_channels, 3, padding='same')(track_b)
    track_b = tf.keras.layers.Dropout(dropout)(track_b)
    track_b = ConditionalInstanceNormalization()([track_b, noise_embedding])
    track_b = tf.keras.activations.swish(track_b)
    track_b = tf.keras.layers.Conv2D(n_out_channels, 3, padding='same')(track_b)

    return track_a + track_b

def up_deep_resblock(x: tf.Tensor, skip_x: tf.Tensor, noise_embedding: tf.Tensor, n_out_channels: bool = None,
        dropout: float = 0.) -> tf.Tensor:
    if not n_out_channels:
        n_out_channels = x.shape[-1]

    skip_x = skip_x / tf.math.sqrt(2.) # downscale the skip connection by 1 / sqrt(2)
    x = tf.keras.layers.Concatenate(axis=-1)([x, skip_x])

    return deep_resblock(x, noise_embedding, n_out_channels=n_out_channels, dropout=dropout)

def up_resblock(x: tf.Tensor, skip_x: tf.Tensor, noise_embedding: tf.Tensor, n_out_channels: bool = None,
        dropout: float = 0.) -> tf.Tensor:
    if not n_out_channels:
        n_out_channels = x.shape[-1]

    skip_x = skip_x / tf.math.sqrt(2.)
    x = tf.keras.layers.Concatenate(axis=-1)([x, skip_x])

    return resblock(x, noise_embedding, n_out_channels=n_out_channels, dropout=dropout)

class AttentionVectorLayer(tf.keras.layers.Layer):
    """
    Building the query, key or value for self-attention
    from the feature map
    """
    def __init__(self, **kwargs):
        super(AttentionVectorLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.n_channels = input_shape[-1]
        self.w = self.add_weight(
            shape=(self.n_channels, self.n_channels),
            initializer=tf.keras.initializers.VarianceScaling(scale=1., mode='fan_avg', distribution='uniform'),
            trainable=True,
            name='attention_w'
        )
        self.b = self.add_weight(
            shape=(self.n_channels,),
            initializer='zero',
            trainable=True,
            name='attention_b'
        )

    def call(self, x: tf.Tensor) -> tf.Tensor:
        return tf.tensordot(x, self.w, 1) + self.b

    def get_config(self) -> dict:
        return {}

    @classmethod
    def from_config(cls, config):
        return cls(**config)

class ConditionalInstanceNormalization(tf.keras.layers.Layer):
    """
    The goal of conditional instance normalization is to make the
    model aware of the amount of noise to be removed (i.e how many
    steps in the noise diffusion process are being considered). The
    implementation was informed by the appendix in
    https://arxiv.org/pdf/1907.05600.pdf and implementation at
    https://github.com/ermongroup/ncsn/blob/master/models/cond_refinenet_dilated.py
    """
    def __init__(self, **kwargs):
        super(ConditionalInstanceNormalization, self).__init__(**kwargs)

    def build(self, input_shape):
        self.batch_size = input_shape[0][0]
        self.height = input_shape[0][1]
        self.width = input_shape[0][2]
        self.n_channels = input_shape[0][3]
        self.embedding_dim = input_shape[1][1]
        self.w1 = self.add_weight(
            shape=(self.embedding_dim, self.n_channels),
            initializer=tf.keras.initializers.RandomNormal(mean=1., stddev=0.02),
            trainable=True,
            name='conditional_w1'
        )
        self.b = self.add_weight(
            shape=(self.embedding_dim, self.n_channels),
            initializer="zero",
            trainable=True,
            name='conditional_b'
        )
        self.w2 = self.add_weight(
            shape=(self.embedding_dim, self.n_channels),
            initializer=tf.keras.initializers.RandomNormal(mean=1., stddev=0.02),
            trainable=True,
            name='conditional_w2'
        )

    def call(self, inputs: Iterable) -> tf.Tensor:
        x, noise_embedding = inputs
        feature_map_means = tf.math.reduce_mean(x, axis=(1, 2), keepdims=True)
        feature_map_std_dev = tf.math.reduce_std(x, axis=(1, 2), keepdims=True) + 1e-5
        m = tf.math.reduce_mean(feature_map_means, axis=-1, keepdims=True)
        v = tf.math.reduce_std(feature_map_means, axis=-1, keepdims=True) + 1e-5
        gamma = tf.expand_dims(tf.expand_dims(tf.tensordot(noise_embedding, self.w1, 1), 1), 1)
        beta = tf.expand_dims(tf.expand_dims(tf.tensordot(noise_embedding, self.b, 1), 1), 1)
        alpha = tf.expand_dims(tf.expand_dims(tf.tensordot(noise_embedding, self.w2, 1), 1), 1)
        instance_norm = (x - feature_map_means) / feature_map_std_dev
        x = gamma * instance_norm + beta + alpha * (feature_map_means - m) / v
        return x

    def get_config(self) -> dict:
        return {}

    @classmethod
    def from_config(cls, config):
        return cls(**config)

class NoiseEmbedding(tf.keras.layers.Layer):
    """
    Implementation of noise embedding taken from Wavegrad
    implementation at https://github.com/lmnt-com/wavegrad/blob/master/src/wavegrad/model.py
    """
    def __init__(self, dim, **kwargs):
        super(NoiseEmbedding, self).__init__(**kwargs)
        self.dim = dim

    def call(self, inputs: tf.Tensor) -> tf.Tensor:
        assert(len(inputs.shape) == 2)
        half_dim = self.dim // 2
        encoding = tf.range(half_dim, dtype=tf.float32) * tf.math.log(1e4) / half_dim
        encoding = tf.math.exp(encoding * -1)
        encoding = inputs * tf.expand_dims(encoding, 0)
        encoding = tf.concat([tf.math.sin(encoding), tf.math.cos(encoding)], -1)
        assert(encoding.shape == (inputs.shape[0], self.dim))
        return encoding

    def get_config(self) -> dict:
        return {
            'dim': self.dim
        }

    @classmethod
    def from_config(cls, config):
        return cls(**config)


from unicodedata import name
from django.urls import path
from .import orders,sales_return

app_name = 'orders'

urlpatterns = [
 path('manage_orders',orders.manage_orders,name='manage_orders'),
 path('manage_car',orders.manage_car,name='manage_car'),
 path('manage_car/<str:pk>/update/',orders.edit_car,name='edit_car'),
 path('add_orders/<str:pk>/',orders.add_orders,name='add_orders'),
 path('approve_order/<str:pk>/',orders.approve_order,name='approve_order'),
 path('comfirm_order/<str:pk>/',orders.comfirm_order,name='comfirm_order'),
 path('order_summery/<str:pk>/',orders.order_summery,name='order_summery'),
 path('remove_item/<str:pk>/',orders.remove_item,name='remove_item'),

 path('creat_returns/<str:pk>/',sales_return.creat_returns,name='creat_returns'),
 path('add_return_sales/<str:pk>/',sales_return.add_return_sales,name='add_return_sales'),
 path('remove_return_item/<str:pk>/',sales_return.remove_return_item,name='remove_return_item'),
 path('manage_returns/',sales_return.manage_returns,name='manage_returns'),
 path('approve_return/<str:pk>/',sales_return.approve_return,name='approve_return'),
 path('comfirm_return/<str:pk>/',sales_return.comfirm_return,name='comfirm_return'),
 path('return_summery/<str:pk>/',sales_return.return_summery,name='return_summery'),
 path('add_return_saless/<str:pk>/',orders.add_return_saless,name='add_return_saless'),
 path('close_shitf_sales/<str:pk>/',orders.close_shitf_sales,name='close_shitf_sales'),
 path('add_sales_amount/<str:pk>/',orders.add_sales_amount,name='add_sales_amount'),

 ]