from aironDev import General_Register
import numpy as np
import openpyxl
import pandas as pd
from pandas import ExcelWriter
from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo

def Calculo_RCL(data,carga1,nombre,fecha):
    t88=Tabla88_Disp('aironDev/Tabla88.xlsx')
    t87=Tabla87_Disp('aironDev/Tabla87.xlsx')
    list_carga1=[]

    LCR=[]
    for i in t87:
        if(not(i in LCR)):
            LCR.append(t87[i]['LCR'])

    piv1=[]
    piv2=[]

    for i in LCR:
        piv1.append(i)
        info={
            'PesoI':0,
            'USDI':0,
            'Otras_1I':0,
            'Otras_2I':0,
            'Individual':0,
            'PesoC':0,
            'USDC':0,
            'Otras_1C':0,
            'Otras_2C':0,
            'Consolidado':0,
        }
        piv2.append(info)
    hash1 = {k:v for k, v in zip(piv1,piv2)}

    for i in data:
        cod=t87[i['Categoria']]['LCR']

        if(i['Nivel_Consolidacion']==1):
            if(i['Vencimiento_Contractual']==1):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['1_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

            elif(i['Vencimiento_Contractual']==2):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['2_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

            elif(i['Vencimiento_Contractual']==3):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

            elif(i['Vencimiento_Contractual']==4):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

            elif(i['Vencimiento_Contractual']==5):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

            elif(i['Vencimiento_Contractual']==6):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

            elif(i['Vencimiento_Contractual']==7):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoI']=hash1[cod]['PesoI']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDI']=hash1[cod]['USDI']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1I']=hash1[cod]['Otras_1I']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2I']=hash1[cod]['Otras_2I']+resultado
                hash1[cod]['Individual'] = hash1[cod]['Individual'] + resultado

        if(i['Nivel_Consolidacion']==2):

            if(i['Vencimiento_Contractual']==1):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['1_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

            elif(i['Vencimiento_Contractual']==2):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['2_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

            elif(i['Vencimiento_Contractual']==3):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

            elif(i['Vencimiento_Contractual']==4):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

            elif(i['Vencimiento_Contractual']==5):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

            elif(i['Vencimiento_Contractual']==6):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

            elif(i['Vencimiento_Contractual']==7):
                resultado=i['Flujo_Efectivo'] * t88[i['Categoria']]['3a7_RCL']
                if(i['Moneda']=='000'):
                    hash1[cod]['PesoC']=hash1[cod]['PesoC']+resultado
                elif(i['Moneda']=='013'):
                    hash1[cod]['USDC']=hash1[cod]['USDC']+resultado
                elif(i['Moneda']=='777'):
                    hash1[cod]['Otras_1C']=hash1[cod]['Otras_1C']+resultado
                elif(i['Moneda']=='888'):
                    hash1[cod]['Otras_2C']=hash1[cod]['Otras_2C']+resultado
                hash1[cod]['Consolidado'] = hash1[cod]['Consolidado'] + resultado

    for i in hash1:
        info={
            'Recalculo Auditorio':i,
            'CLP_Individual':hash1[i]['PesoI'],
            'USD_Individual':hash1[i]['USDI'],
            'Otras 1_Individual':hash1[i]['Otras_1I'],
            'Otras 2_Individual':hash1[i]['Otras_2I'],
            'Total Moneda 1':hash1[i]['Individual'],
            'CLP_Consolidado':hash1[i]['PesoC'],
            'USD_Consolidado':hash1[i]['USDC'],
            'Otras 1_Consolidado':hash1[i]['Otras_1C'],
            'Otras 2_Consolidado':hash1[i]['Otras_2C'],
            'Total Moneda 2':hash1[i]['Consolidado'],
        }
        list_carga1.append(info)

    cabecera=Cabecera_Cuadratura(carga1)
    limite=Ingresos_Lim(hash1['Ingresos'],hash1['Ingresos individual de riesgo < A3 o equivalente. (2)'],hash1['Egreso'],cabecera)
    list_carga1.append(limite)
    egresos=Egresos_Netos(limite,hash1['Egreso'])
    list_carga1.append(egresos)
    dif_caratula=Diferencia_Caratula(cabecera[1:],egresos)
    list_carga1.append(dif_caratula)
    General_Register.reporte_Vista(cabecera,list_carga1,nombre+'_'+fecha+'_RATIO')

def Egresos_Netos(limite,dato):
    info={
        'Recalculo Auditorio':'Egresos Netos',
        'CLP_Individual':dato['PesoI']-limite['CLP_Individual'],
        'USD_Individual':dato['USDI']-limite['USD_Individual'],
        'Otras 1_Individual':dato['Otras_1I']-limite['Otras 1_Individual'],
        'Otras 2_Individual':dato['Otras_2I']-limite['Otras 2_Individual'],
        'Total Moneda 1':dato['Individual']-limite['Total Moneda 1'],
        'CLP_Consolidado':dato['PesoC']-limite['CLP_Consolidado'],
        'USD_Consolidado':dato['USDC']-limite['USD_Consolidado'],
        'Otras 1_Consolidado':dato['Otras_1C']-limite['Otras 1_Consolidado'],
        'Otras 2_Consolidado':dato['Otras_2C']-limite['Otras 2_Consolidado'],
        'Total Moneda 2':dato['Consolidado']-limite['Total Moneda 2'],
    }
    return info

def Diferencia_Caratula(cabecera,egresos):
    data={
        'Recalculo Auditorio':'Diferencia contra carátula ',
    }
    for i in egresos:
        if(egresos[i]!='Egresos Netos'):
            data[i]=cabecera[0][i]-egresos[i]
    return data

def Ingresos_Lim(data1,data2,data3,cabecera):
    info={
        'Recalculo Auditorio':'Ingresos con Lim 75%',
    }

    info2={
        'Recalculo Auditorio':'Ingresos con Lim 75%',
    }

    info['CLP_Individual']=data1['PesoI']+data2['PesoI']
    info['USD_Individual']=data1['USDI']+data2['USDI']
    info['Otras 1_Individual']=data1['Otras_1I']+data2['Otras_1I']
    info['Otras 2_Individual']=data1['Otras_2I']+data2['Otras_2I']
    info['Total Moneda 1']=data1['Individual']+data2['Individual']
    info['CLP_Consolidado']=data1['PesoC']+data2['PesoC']
    info['USD_Consolidado']=data1['USDC']+data2['USDC']
    info['Otras 1_Consolidado']=data1['Otras_1C']+data2['Otras_1C']
    info['Otras 2_Consolidado']=data1['Otras_2C']+data2['Otras_2C']
    info['Total Moneda 2']=data1['Consolidado']+data2['Consolidado']

    info2['CLP_Individual']=(data3['PesoI'] * 0.75)
    info2['USD_Individual']=(data3['USDI'] * 0.75)
    info2['Otras 1_Individual']=(data3['Otras_1I'] * 0.75)
    info2['Otras 2_Individual']=(data3['Otras_2I'] * 0.75)
    info2['Total Moneda 1']=(data3['Individual'] * 0.75)
    info2['CLP_Consolidado']=(data3['PesoC'] * 0.75)
    info2['USD_Consolidado']=(data3['USDC'] * 0.75)
    info2['Otras 1_Consolidado']=(data3['Otras_1C'] * 0.75)
    info2['Otras 2_Consolidado']=(data3['Otras_2C'] * 0.75)
    info2['Total Moneda 2']=(data3['Consolidado'] * 0.75)

    for i in info:
        if(info[i] > info2[i]):
            info[i]=info2[i]

    return info

def Cabecera_Cuadratura(carga1):
    list_cabecera=[]
    info={
        'Recalculo Auditorio':'Activos_Liquidos',
        'CLP_Individual':carga1[0]['Activos_Liquidos'],
        'USD_Individual':carga1[1]['Activos_Liquidos'],
        'Otras 1_Individual':carga1[2]['Activos_Liquidos'],
        'Otras 2_Individual':carga1[3]['Activos_Liquidos'],
        'Total Moneda 1':carga1[4]['Activos_Liquidos'],
        'CLP_Consolidado':carga1[5]['Activos_Liquidos'],
        'USD_Consolidado':carga1[6]['Activos_Liquidos'],
        'Otras 1_Consolidado':carga1[7]['Activos_Liquidos'],
        'Otras 2_Consolidado':carga1[8]['Activos_Liquidos'],
        'Total Moneda 2':carga1[9]['Activos_Liquidos'],
    }
    list_cabecera.append(info)
    info={
        'Recalculo Auditorio':'Egresos_Netos',
        'CLP_Individual':carga1[0]['Egresos_Netos'],
        'USD_Individual':carga1[1]['Egresos_Netos'],
        'Otras 1_Individual':carga1[2]['Egresos_Netos'],
        'Otras 2_Individual':carga1[3]['Egresos_Netos'],
        'Total Moneda 1':carga1[4]['Egresos_Netos'],
        'CLP_Consolidado':carga1[5]['Egresos_Netos'],
        'USD_Consolidado':carga1[6]['Egresos_Netos'],
        'Otras 1_Consolidado':carga1[7]['Egresos_Netos'],
        'Otras 2_Consolidado':carga1[8]['Egresos_Netos'],
        'Total Moneda 2':carga1[9]['Egresos_Netos'],
    }
    list_cabecera.append(info)
    return list_cabecera

def Tabla88_Disp(excel):
    doc = openpyxl.load_workbook(excel)
    doc.get_sheet_names()
    hoja = doc.get_sheet_by_name('Hoja1')
    hoja.rows
    list_Cu=[]
    list_CM=[]
    for filas in hoja.rows:
        data2={
            '1_RCL':filas[1].value,
            '2_RCL':filas[2].value,
            '3a7_RCL':filas[3].value,
            '1_RFEN':filas[4].value,
            '2_RFEN':filas[5].value,
            '3_RFEN':filas[6].value,
            '4_RFEN':filas[7].value,
            '5_RFEN':filas[8].value,
            '6_RFEN':filas[9].value,
            '7_RFEN':filas[10].value,
        }
        list_Cu.append(str(filas[0].value))
        list_CM.append(data2)
    hash = {k:v for k, v in zip(list_Cu,list_CM)}
    return hash

def Tabla87_Disp(excel):
    doc = openpyxl.load_workbook(excel)
    doc.get_sheet_names()
    hoja = doc.get_sheet_by_name('Hoja1')
    hoja.rows
    list_Cu=[]
    list_CM=[]
    for filas in hoja.rows:
        data={
            'LCR':filas[1].value,
            'NSFR':filas[2].value,
            'Pais Domicilio':filas[3].value,
            'Tipo Flujo':filas[4].value,
            'Categoria':filas[5].value,
        }
        list_Cu.append(str(filas[0].value))
        list_CM.append(data)
    hash = {k:v for k, v in zip(list_Cu,list_CM)}
    return hash


import os
import json
import datetime
import discord

from src.filter_tool import find_url, scan_links

class LoliChan(discord.Client):

    def __init__(self, verbose=False):
        super(LoliChan, self).__init__()
        self.verbose = verbose
        with open('./data/defaults.json') as json_file:
            data = json.load(json_file)
            self.forbidden_tags = data["forbidden_tags"]
            self.allowed_roles = data["allowed_roles"]
            self.admin_cmd_tag = data["admin_cmd_tag"]
            self.cmd_tag = data["cmd_tag"]

    def admin_command_reader(self, content):
        def filterbot(message):
            def help_(message):
                msg = "**FILTERBOT HELP**\n> Available commands:\n```"
                padding = max(list(map(lambda x: len(x), list(ACCEPTABLE_COMMANDS.keys()))))+1
                for k, v in ACCEPTABLE_COMMANDS.items():
                    msg += '{message: <{padding}}| Usage - {description}\n'.format(message=k, padding=padding, description=v['desc'])
                msg += '```'
                return msg

            def not_allowed(message):
                msg = '**Forbidden Tags**\n```'
                padding = max(list(map(lambda x: len(x), list(self.forbidden_tags.keys()))))+1
                for k, v in self.forbidden_tags.items():
                    msg += '{message: <{padding}}| {values}\n'.format(message=k, padding=padding, values=", ".join(sorted(v)))
                msg += '```'
                return msg

            def add_(message):
                channel = message[0]
                to_add = " ".join(message[1:])
                if channel not in self.forbidden_tags.keys():
                    self.forbidden_tags[channel] = [to_add.lower()]
                else:
                    self.forbidden_tags[channel].append(to_add.lower())
                return not_allowed(message)

            def remove_(message):
                channel = message[0]
                to_add = " ".join(message[1:])
                if channel in self.forbidden_tags.keys():
                    while to_add in self.forbidden_tags[channel]:
                        self.forbidden_tags[channel].remove(to_add.lower())
                return not_allowed(message)

            ACCEPTABLE_COMMANDS = {
                'help' : {'func': help_, 'desc': self.admin_cmd_tag+'filterbot help'},
                'list' : {'func': not_allowed, 'desc': self.admin_cmd_tag+'filterbot list'},
                'add' : {'func': add_, 'desc': self.admin_cmd_tag+'filterbot add channel tag [for global tags, use all for channel]'},
                'remove' : {'func': remove_, 'desc': self.admin_cmd_tag+'filterbot remove channel tag [for global tags, use all for channel]'}
            }

            if len(message) == 0:
                return help_(message)

            cmd = message[0]
            if cmd in ACCEPTABLE_COMMANDS.keys():
                return ACCEPTABLE_COMMANDS[cmd]['func'](message[1:])
            else:
                return 'incorect command given, please choose command from list: {}'.format(", ".join(list(ACCEPTABLE_COMMANDS.keys())))

        def help_(message):
            msg = "**TOOLKIT HELP**\n> Available commands:\n```"
            padding = max(list(map(lambda x: len(x), list(ACCEPTABLE_COMMANDS.keys()))))+1
            for k, v in ACCEPTABLE_COMMANDS.items():
                msg += '{message: <{padding}}| Usage - {description}\n'.format(message=k, padding=padding, description=v['desc'])
            msg += '```'
            return msg

        def permission(message):
            def help_(message):
                msg = "**PERMISSION HELP**\n> Available commands:\n```"
                padding = max(list(map(lambda x: len(x), list(ACCEPTABLE_COMMANDS.keys()))))+1
                for k, v in ACCEPTABLE_COMMANDS.items():
                    msg += '{message: <{padding}}| Usage - {description}\n'.format(message=k, padding=padding, description=v['desc'])
                msg += '```'
                return msg

            def list_permission(message):
                return "**allowed roles:**  {}".format(", ".join(self.allowed_roles))

            def add_(message):
                to_add = " ".join(message)
                if to_add not in self.allowed_roles:
                    self.allowed_roles.append(to_add)
                return list_permission(message)

            def remove_(message):
                to_add = " ".join(message)
                while to_add in self.allowed_roles:
                    self.allowed_roles.remove(to_add)
                return list_permission(message)

            ACCEPTABLE_COMMANDS = {
                'help' : {'func': help_, 'desc': self.admin_cmd_tag+'permissions help'},
                'list' : {'func': list_permission, 'desc': self.admin_cmd_tag+'permissions list'},
                'add' : {'func': add_, 'desc': self.admin_cmd_tag+'permissions add role [CASE SENSITIVE]'},
                'remove' : {'func': remove_, 'desc': self.admin_cmd_tag+'permissions remove role [CASE SENSITIVE]'}
            }

            if len(message) == 0:
                return help_(message)

            cmd = message[0]
            if cmd in ACCEPTABLE_COMMANDS.keys():
                return ACCEPTABLE_COMMANDS[cmd]['func'](message[1:])
            else:
                return 'incorect command given, please choose command from list: {}'.format(", ".join(list(ACCEPTABLE_COMMANDS.keys())))

        ACCEPTABLE_COMMANDS = {
            'help' : {'func': help_, 'desc': self.admin_cmd_tag+'help'},
            'filterbot' : {'func': filterbot, 'desc': self.admin_cmd_tag+'filterbot'},
            'permissions' : {'func': permission, 'desc': self.admin_cmd_tag+'permissions'}
        }

        cmd = content.split(' ')[0]
        if cmd in ACCEPTABLE_COMMANDS.keys():
            return ACCEPTABLE_COMMANDS[cmd]['func'](content.split(' ')[1:])
        else:
            return 'incorect command given, please choose command from list: {}'.format(", ".join(list(ACCEPTABLE_COMMANDS.keys())))

    def command_reader(self, content):
        def help_(message):
            msg = "**TOOLKIT HELP**\n> Available commands:\n```"
            for k, v in ACCEPTABLE_COMMANDS.items():
                msg += '{}: Usage - {}\n'.format(k, v['desc'])
            msg += '```'
            return msg, None

        def schedule(message):
            available_text = {
                "monday" : './data/schedule/monday.jpg',
                "mon" : './data/schedule/monday.jpg',
                "tuesday" : './data/schedule/tuesday.jpg',
                "tue" : './data/schedule/tuesday.jpg',
                "wednesday" : './data/schedule/wednesday.jpg',
                "wed" : './data/schedule/wednesday.jpg',
                "thursday" : './data/schedule/thursday.jpg',
                "thu" : './data/schedule/thursday.jpg',
                "friday" : './data/schedule/friday.jpg',
                "fri" : './data/schedule/friday.jpg',
                "saturday" : './data/schedule/saturday.jpg',
                "sat" : './data/schedule/saturday.jpg',
                "sunday" : './data/schedule/sunday.jpg',
                "sun" : './data/schedule/sunday.jpg'
            }
            if message[0].lower() == 'today':
                current_time = datetime.datetime.utcnow() + datetime.timedelta(hours=8) #GMT+8
                day = current_time.weekday()
                dictionary = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
                return 'timing is based on SG time (GMT+8)', discord.File(available_text[dictionary[day]])
            elif len(message) > 1 or message[0].lower() not in available_text.keys():
                return 'incorrect format is given, please check {}help'.format(self.cmd_tag), None
            else:
                return 'timing is based on SG time (GMT+8)', discord.File(available_text[message[0].lower()])

        ACCEPTABLE_COMMANDS = {
            'help' : {'func': help_, 'desc': self.cmd_tag+'help'},
            'schedule' : {'func': schedule, 'desc': self.cmd_tag+'schedule day [day format is 3 letter day (e.g. thu) or full name (e.g. thursday) or today]'}
        }

        cmd = content.split(' ')[0]
        if cmd in ACCEPTABLE_COMMANDS.keys():
            return ACCEPTABLE_COMMANDS[cmd]['func'](content.split(' ')[1:])
        else:
            return 'incorect command given, please choose command from list: {}'.format(", ".join(list(ACCEPTABLE_COMMANDS.keys()))), None

    # initialization
    async def on_ready(self):
        print(f'{self.user} has connected to Discord!')
        activity = discord.Game(name="llc-help | llca-help")
        await self.change_presence(activity=activity)

    async def on_message(self, message):
        ### implement checks
        # prevent recursive case
        if message.author == self.user:
            return

        # admin command central
        if message.content[:len(self.admin_cmd_tag)] == self.admin_cmd_tag:
            not_found = True
            for role in message.author.roles:
                if role.name in self.allowed_roles and not_found:
                    reply = self.admin_command_reader(message.content[len(self.admin_cmd_tag):])
                    await message.channel.send(reply)
                    not_found = False
            if not_found:
                await message.channel.send(f"Hi {message.author.mention}, you do not have the required permission to use this command!")

        # regular command central
        if message.content[:len(self.cmd_tag)] == self.cmd_tag:
            reply, file_reply = self.command_reader(message.content[len(self.cmd_tag):])
            if reply is not None and file_reply is None:
                await message.channel.send(reply)
            elif file_reply is not None and reply is None:
                await message.channel.send(file=file_reply)
            else:
                await message.channel.send(reply, file=file_reply)

        # filter nhentai tags
        urls = find_url(message.content)
        if len(urls) > 0:
            if self.verbose:
                print('found some links', urls)
            # check to see if message contains bad content
            to_pass, problems = scan_links(urls, message.channel.name, self.forbidden_tags)

            # format problems
            if len(problems) == 1:
                msg = problems[0]
            elif len(problems) > 1:
                msg = ", ".join(problems[:-1]) + " and " + problems[-1]

            if to_pass:
                if self.verbose:
                    print('a problemmatic tag was found')
                await message.delete()
                # await message.channel.send(f"Hi {message.author.mention}, your nhentai link(s) contain(s) the {msg} tag(s), please do not send links with these tag(s)!")
                # await message.channel.send(f"H, hey {message.author.mention}... I, it's not like I want you to stop sending nhentai link(s) containing the {msg} tag(s)!")
                await message.channel.send(f"Hi {message.author.mention}, your link(s) contain(s) the {msg} tag(s), which is(are) against the discord ToS/channel rules. Please refrain from posting such links in the future.")


import numpy as np
from scipy.fftpack import fft
import matplotlib.pyplot as plt
from scipy.io import wavfile as wav

NTTF = 1000
names = [
	'saleem',
	'tariq',
	'khaled',
	'muhammed',
	'google'
]

def calc_fft(audio):
	fs, data = wav.read(audio)
	fft_out = fft(data)
	k = np.arange(len(data))
	T = len(data)/fs
	frqLabel = k/T
	fft_out = fft_out[:len(fft_out)//2-1]
	frqLabel= frqLabel[:len(frqLabel)//2-1]
	plt.plot(frqLabel, np.abs(fft_out))
	return np.abs(fft_out)

def calc_dist(s1, s2):
	return np.linalg.norm(s1 - s2)

def calc_avg_fft(s):
	return np.sum(s/len(s))

def avg_fft(name):
	avg_ffts = []
	plt.figure(name)
	for i in range(5):

		avg_ffts.append(
			calc_avg_fft(
				calc_fft('./dataset/' + name + str(i+1) + '.wav')
			)
		)

	return np.average(avg_ffts)

def normalized(a, axis=-1, order=2):
    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
    l2[l2==0] = 1
    return a / np.expand_dims(l2, axis)

def process_unknowns(known):
	n = len(known)
	result = []
	for i in range(n):
		dists = []
		avg_fft = calc_avg_fft(calc_fft('./dataset/unknown' + str(i+1) + '.wav'))
		for j in range(n):
			dists.append(calc_dist(known[j], avg_fft))
		result.append((1-normalized(dists))*100)
	return result

result = process_unknowns([
	avg_fft(names[0]),
	avg_fft(names[1]),
	avg_fft(names[2]),
	avg_fft(names[3]),
	avg_fft(names[4]),
])

for i in range(len(result)):
	print("unknown " + str(i+1) + ", matched "),
	for j in range(len(result[i][0])):
		print(str(int(result[i][0][j])) + "% with " + names[j] + " "),
	print

plt.show()


#!/usr/bin/env python
#Forked from https://github.com/ericminikel/minimal_representation and added to repo with permission from eric.minikel@prionalliance.org
'''
This script is a python implementation of the algorithm for variant
normalization described by Tan et al 2015:

Tan A, Abecasis GR, Kang HM. Unified representation of genetic variants.
Bioinformatics. 2015 Jul 1;31(13):2202-4. doi: 10.1093/bioinformatics/btv112.
Epub 2015 Feb 19. PubMed PMID: 25701572.

The authors have made a C++ implementation available in vt as vt normalize
And the source code is viewable here: https://github.com/atks/vt

For our purposes, we wanted a Python implementation so that we could
build end-to-end scripts in Python.

If you use this, please cite Tan et al 2015.

A note about when this is useful. In VCFs generated with GATK (or probably
other tools) from short read sequencing data, variants are already left-aligned
but may be non-minimal to the extent that indels overlap with other variants.
For those cases, minimal_representation.py is sufficient to convert variants
to minimal representation. However, genomic coordinates converted from HGVS
(we have encountered this when parsing the ClinVar XML dump) may be not only
non-minimal but also right-aligned rather than left-aligned, and may contain
hyphens. For those situations, use this script (or just run vt normalize).

Usage: normalize.py -R $b37ref < bad_file.txt > good_file.txt
'''

import sys
import pysam
import argparse
import gzip
import re

'''
An Error class for rare cases where REF == ALT (seen in ClinVar XML)
'''
class RefEqualsAltError(Exception):
		def __init__(self, value):
			self.value = value
		def __str__(self):
			return repr(self.value)

'''
An Error class for REF or ALT values that are not valid nucleotide sequences
'''
class InvalidNucleotideSequenceError(Exception):
		def __init__(self, value):
			self.value = value
		def __str__(self):
			return repr(self.value)

'''
An Error class for variants where the REF does not match the reference genome
'''
class WrongRefError(Exception):
		def __init__(self, value):
			self.value = value
		def __str__(self):
			return repr(self.value)

'''
An Error class for when Chromosome not in the reference genome
'''
class SequenceNotPresent(Exception):
		def __init__(self, value):
			self.value = value
		def __str__(self):
			return repr(self.value)

'''
An Error class for when Reference Sequence Unknown in the reference genome
'''
class SequenceUnknown(Exception):
		def __init__(self, value):
			self.value = value
		def __str__(self):
			return repr(self.value)

'''
Accepts a pysam FastaFile object pointing to the reference genome, and
chrom, pos, ref, alt genomic coordinates, and normalizes them.
'''
def normalize(pysam_fasta, chrom, pos, ref, alt):
	if chrom not in {'1', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '2', '20', '21', '22', '3', '4', '5', '6', '7', '8', '9', 'MT', 'X', 'Y'}:
		raise SequenceNotPresent('Invalid chromosome: %s %s %s %s'%(chrom, pos, ref, alt))
	pos = int(pos) # make sure position is an integer
	ref = ref.upper().strip()
	alt = alt.upper().strip()
	# Remove variants that contain invalid nucleotides
	if any(letter not in ['A','C','G','T','N','-'] for letter in ref + alt):
		raise InvalidNucleotideSequenceError('Invalid nucleotide sequence: %s %s %s %s'%(chrom, pos, ref, alt))
	# Time-saving shortcut for SNPs that are already minimally represented
	if len(ref) == 1 and len(alt) == 1 and ref in ['A','C','G','T'] and alt in ['A','C','G','T']:
		return chrom, pos, ref, alt
	# use blanks instead of hyphens
	if ref == '-' or ref == '.':
		ref = ''
	if alt == '-' or alt == '.':
		alt = ''
	# check whether the REF is correct
	true_ref = pysam_fasta.fetch(chrom, pos - 1, pos - 1 + len(ref)).upper()
	if ref != true_ref:
		x = re.search("^N+$", true_ref)
		wrong_pos = pysam_fasta.fetch(chrom, pos - 2, pos -2 + len(ref)).upper()
		if ref == wrong_pos:
			pos = pos-1
            # someone thought they had 0 based pos, but had 1 based.  Added 1 to get their stuff to 1 based, and now are off
			#raise WrongRefError('Incorrect POS value caused incorrect REF: %s %s %s %s (actual POS should be %s, pos-1 REF is %s)'%(chrom, pos, ref, alt, pos-1, wrong_pos))
			#print (ref + "\t==\t" + wrong_pos)
			pass
		elif x is not None:
			raise WrongRefError('Incorrect REF value: %s %s %s %s (actual REF should be %s )'%(chrom, pos, ref, alt, true_ref))
		else:
			raise SequenceUnknown('REF in provided fasta Unknown: %s %s %s %s Check Fasta file. )'%(chrom, pos, ref, alt))
	# Prevent infinte loops in cases where REF == ALT.
	# We have encountered this error in genomic coordinates from the ClinVar XML file
	if ref == alt:
		raise RefEqualsAltError('The REF and ALT allele are the same: %s %s %s %s'%(chrom, pos, ref, alt))

	# This first loop left-aligns and removes excess nucleotides on the right.
	# This is Algorithm 1 lines 1-6 from Tan et al 2015
	keep_working = True
	while keep_working:
		keep_working = False
		if len(ref) > 0 and len(alt) > 0 and ref[-1] == alt[-1]:
			ref = ref[:-1]
			alt = alt[:-1]
			keep_working = True
		if len(ref) == 0 or len(alt) == 0:
			preceding_base = pysam_fasta.fetch(chrom, pos-2, pos-1)
			ref = preceding_base + ref
			alt = preceding_base + alt
			pos = pos - 1
			keep_working = True
	# This second loop removes excess nucleotides on the left. This is Algorithm 1 lines 7-8.
	while len(ref) > 1 and len(alt) > 1 and ref[0] == alt[0]:
		ref = ref[1:]
		alt = alt[1:]
		pos = pos + 1
	return chrom, pos, ref, alt

'''
This function takes a tab-delimited file with a header line containing columns
named chrom, pos, ref, and alt, plus any other columns. It normalizes the
chrom, pos, ref, and alt, and writes all columns out to another file.  CNV have
"na" as ref/alt.  Ref==Alt should not be dismissed as Reference not perfect,
"N" is used in Reference Fasta
'''
def normalize_tab_delimited_file(in_file, out_file, reference_fasta, verbose, SKIP_ON_BASE_ERROR):
	infile=None
	if in_file.endswith(".gz"):
		try:
			infile = gzip.open(in_file, 'rt')
		except ValueError:
			# Workaround for Python 2.7 under Windows
			infile = gzip.open(in_file, "r")
	else:
		infile= open(in_file,'r')
	outfile=None
	if out_file.endswith(".gz"):
		outfile=gzip.open(out_file,'wt')
	else:
		outfile=open(out_file,'w')
	pysam_fasta = pysam.FastaFile(reference_fasta) # create a pysam object of the reference genome
	header = infile.readline() # get header of input file
	columns = [x.strip() for x in header.strip().upper().split('\t')]  # parse col names
	outfile.write('\t'.join(columns) + '\n') # write header line plus the CpG col to be generated
	counter = 0
	ref_equals_alt = 0
	wrong_ref = 0
	invalid_nucleotide = 0
	invalid_chrom = 0
	unknown_ref = 0
	for line in infile.readlines():
		if line.strip()=="":
			continue
		data = dict(zip(columns,[x.strip() for x in line.strip().split('\t')]))
		# fill the data with blanks for any missing data
		if data['ALT'] =="na" or data['REF']=="na":
			continue #CNVs in clinvar
		for column in columns:
			if column not in data.keys():
				data[column] = ''
		pos = int(data['POS'])
		try:
			data['CHROM'], pos, data['REF'], data['ALT'] = normalize(pysam_fasta, data['CHROM'], pos, data['REF'], data['ALT'])
		except RefEqualsAltError as e:
			#sys.stderr.write('\n'+str(e)+'\n')
			ref_equals_alt += 1
			if SKIP_ON_BASE_ERROR:
				continue
		except WrongRefError as e:
			sys.stderr.write('\n'+str(e)+'\n')
			wrong_ref += 1
			if SKIP_ON_BASE_ERROR:
				continue
		except SequenceUnknown as e:
			sys.stderr.write('\n'+str(e)+'\n')
			unknown_ref += 1
			if SKIP_ON_BASE_ERROR:
				continue
		except InvalidNucleotideSequenceError as e:
			sys.stderr.write('\n'+str(e)+'\n')
			invalid_nucleotide += 1
			if SKIP_ON_BASE_ERROR:
				continue
		except SequenceNotPresent as e:
			sys.stderr.write('\n'+str(e)+'\n')
			invalid_chrom += 1
			continue
		data['POS'] = str(pos)
		outfile.write('\t'.join([data[column] for column in columns]) + '\n')
		counter += 1
		if verbose and counter % 10000 == 0:
			sys.stderr.write("\r%s records processed\n"%(counter))
	outfile.close()
	infile.close()
	if verbose:
		sys.stderr.write("Final counts of variants discarded:\nREF == ALT: %s\nWrong REF: %s\nInvalid nucleotide: %s\n"%(ref_equals_alt, wrong_ref, invalid_nucleotide))

'''
Battery of test cases for normalize
'''
def test_normalize(pysam_fasta):
	sys.stdout.write(str(normalize(pysam_fasta, '7', 117199646, 'CTT', '-'))+'\n') # HGVS translation of CFTR p.F508del, should be ('7', 117199644, 'ATCT', 'A')
	sys.stdout.write(str(normalize(pysam_fasta, '13', 32914438, 'T', '-'))+'\n') # HGVS translation of a BRCA2 Ashkenazi founder variant, should be ('13', 32914437, 'GT', 'G')

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Python implementation of vt normalize')
	parser.add_argument('-R', '--reference_fasta', type=str, default='',
		help="Path to FASTA file of reference genome. Must be samtools faidx'ed")
	parser.add_argument('-i', '--infile', type=str, help="TSV file to be sorted", dest="infile")
	parser.add_argument('-o', '--outfile', type=str, help="File name of outfile", dest="outfile")
	args = parser.parse_args()
	normalize_tab_delimited_file(args.infile, args.outfile, args.reference_fasta)


import uuid
import collections
import typing

from cogs.utils.profiles.field_type import FieldType, TextField, ImageField, NumberField, BooleanField

class Field(object):
    """The abstract field object for a given template
    This itself does not store any user information, but rather the meta information associated with
    a field from a template
    """

    all_profile_fields: typing.Dict['profile_id', typing.List['Field']] = collections.defaultdict(list)
    all_fields: typing.Dict['field_id', 'Field'] = {}

    __slots__ = ("field_id", "index", "name", "prompt", "timeout", "field_type", "profile_id", "optional", "deleted")

    def __init__(self, field_id:uuid.UUID, name:str, index:int, prompt:str, timeout:int, field_type:FieldType, profile_id:uuid.UUID, optional:bool, deleted:bool):
        self.field_id = field_id
        self.index = index
        self.name = name
        self.prompt = prompt
        self.timeout = timeout
        self.field_type = field_type if isinstance(field_type, FieldType) else {
            '1000-CHAR': TextField(),
            'INT': NumberField(),
            'IMAGE': ImageField(),
            'BOOLEAN': BooleanField(),
        }[field_type]
        self.profile_id = profile_id
        self.optional = optional
        self.deleted = deleted

        self.all_profile_fields[self.profile_id].append(self)
        self.all_fields[self.field_id] = self


# coding=utf-8

from tornado import gen
from service.page.base import PageService

class RegionPageService(PageService):

    @gen.coroutine
    def get_region(self, conds, fields=None):

        '''
        获得多级城市信息
        :param conds:
        :param fields: 示例:
        conds = {
            "rid": rid
        }
        :return:
        '''

        city = yield self.region_ds.get_region(conds, fields)
        raise gen.Return(city)

    @gen.coroutine
    def get_regions(self, conds, fields=None, options=None, appends=None):

        '''
        获得多级城市列表信息
        :param conds:
        :param fields: 示例:
        conds = {
            "rid": rid
        }
        :return:
        '''

        city = yield self.region_ds.get_regions(conds, fields, options, appends)
        raise gen.Return(city)


# importing all needed libraries
import discord
import datetime
import json
import pickle
import yaml
import requests
import random
from discord.ext import commands, tasks
from discord import Embed, Member
from datetime import timedelta
from itertools import cycle
import things.UTILITIES as utl
import things.BOTUTL as butl

class RANK(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(aliases=['rank','ar','abrank','abr'])
    async def arank(self, ctx, usr : discord.Member=''):
        if usr == '':
            usr = ctx.author
        user = False
        page = 0
        rank = 1
        while user == False:
            data = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{ctx.guild.id}?page={page}').json()
            sdata = {}
            if data['players'] == []:
                await ctx.send('That user is not ranked!')
                break
            for i in data['players']:
                if i['username'] == f'{usr.name}' and i['discriminator'] == f'{usr.discriminator}':
                    sdata = i
                    srank = rank
                else:
                    rank += 1
            if sdata != {}:
                user = True
            page += 1
        if user == True:
            dxp = sdata['detailed_xp']
            pexp = dxp[0]/dxp[1]
            percentxp = round(round(pexp, 3) * 100, 1)
            percxp = round(round(pexp, 2) * 10)
            rankprogress = ''
            color = random.choice(['🟧','🟦','🟥','🟫','🟪','🟩','🟨'])
            for i in range(1, 11):
                if i <= percxp:
                    rankprogress += color
                else:
                    rankprogress += '⬜'

            if usr.nick == None:
                name = f'{usr.name}'
            else:
                name = f'{usr.nick} ({usr.name})'

            emb = discord.Embed(colour=discord.Color.blue(),title=f'Rank #**{srank}**')

            emb.add_field(inline = False, name = f"LEVEL **{sdata['level']}** (**{percentxp}**%)", value = f"**{sdata['level']}** {rankprogress} **{sdata['level'] + 1}**")
            emb.add_field(inline = True, name = '**EXP Level**', value = f'{dxp[0]} / {dxp[1]} XP')
            emb.add_field(inline = True, name = '**EXP Total**', value = f'{dxp[2]} XP')
            emb.add_field(inline = True, name = '**Messages**', value = f"{sdata['message_count']}")

            emb.set_author(name = f'{name}', icon_url = f"{usr.avatar_url}")

            await ctx.send(embed = emb)

    @commands.command(aliases=['rdash','adash','ad','rd','dashr','dr'])
    async def dashrank(self, ctx, usr : discord.Member=''):
        if usr == '' or utl.getadmin(ctx.author) == False:
            usr = ctx.author
        user = False
        user2 = False
        user3 = False
        page = 0
        rank = 1
        go = False
        sdata = {}
        adata = {}
        bdata = {}
        tempdata = {}

        while user == False:
            data = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{ctx.guild.id}?page={page}').json()
            if data['players'] == []:
                await ctx.send('That user is not ranked!')
                break
            for i in data['players']:
                if i['username'] == f'{usr.name}' and i['discriminator'] == f'{usr.discriminator}':
                    sdata = i
                    srank = rank
                    adata = tempdata
                    go = True
                else:
                    if go == True:
                        bdata = i
                        break
                    tempdata = i
                    rank += 1
            if sdata != {}:
                user = True
            if adata != {}:
                user2 = True
            if bdata != {}:
                user3 = True
            page += 1
        if user == True:
            dxp = sdata['detailed_xp']
            pexp = dxp[0]/dxp[1]
            percentxp = round(round(pexp, 3) * 100, 1)
            percxp = round(pexp*10)
            rankprogress = ''
            color = random.choice(['🟧','🟦','🟥','🟫','🟪','🟩','🟨'])
            for i in range(1, 11):
                if i <= percxp:
                    rankprogress += color
                else:
                    rankprogress += '⬜'

            if usr.nick == None:
                name = f'{usr.name}'
            else:
                name = f'{usr.nick} ({usr.name})'

            emb = discord.Embed(colour=discord.Color.purple(),title=f'Rank #**{srank}**')

            emb.add_field(inline = False, name = f"LEVEL **{sdata['level']}** (**{percentxp}**%)", value = f"**{sdata['level']}** {rankprogress} **{sdata['level'] + 1}**")
            emb.add_field(inline = True, name = '**EXP Level**', value = f'{dxp[0]} / {dxp[1]} XP')
            emb.add_field(inline = True, name = '**EXP Total**', value = f'{dxp[2]} XP')
            emb.add_field(inline = True, name = '**Messages**', value = f"{sdata['message_count']}")
            if user2 == True:
                axp = adata['detailed_xp']
                emb.add_field(inline = True, name = '**MSGs to Rank Up**', value = f'Abt. {round((axp[2]-dxp[2])/20)} Messages')
            else:
                emb.add_field(inline = True, name = '**MSGs to Rank Up**', value = f'0 Messages (Rank #1)')
            emb.add_field(inline = True, name = '**MSGs to Level Up**', value = f'Abt. {round((dxp[1]-dxp[0])/20)} Messages')

            hierarchy = ''

            if user2 == True:
                axp = adata['detailed_xp']
                hierarchy += f"#{srank-1} {adata['username']} (+{axp[2] - dxp[2]} XP | +{round((axp[2]-dxp[2])/20)} MSGs)\n"

            hierarchy += f"**#{srank} {usr.name} (+0 XP | +0 MSGs)**\n"

            if user3 == True:
                bxp = bdata['detailed_xp']
                hierarchy += f"#{srank+1} {bdata['username']} (-{dxp[2] - bxp[2]} XP | -{round((dxp[2]-bxp[2])/20)} MSGs)"

            emb.add_field(inline = False, name = '**Rank Hierarchy**', value = hierarchy)

            emb.set_author(name = f'{name}', icon_url = f"{usr.avatar_url}")

            await ctx.send(embed = emb)

    @commands.command(aliases=['alevels','al','ldrbrd','aleaderboard'])
    async def leaderboard(self, ctx):
        user = ctx.author
        user_ = False
        user_t5 = False
        userdata = {}
        data = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{ctx.guild.id}?page=0').json()
        page = 0

        try:
            top5 = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{ctx.guild.id}?page=0').json()['players'][:10]
        except IndexError:
            top5 = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{ctx.guild.id}?page=0').json()['players']

        for i in range(0,len(top5)):
            if top5[i]['username'] == f'{user.name}' and top5[i]['discriminator'] == f'{user.discriminator}':
                userdata = top5[i]
                userrank = i + 1
                rangel = i
                user_t5 = True
                user_ = True

        while user_ == False:
            data = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{ctx.guild.id}?page={page}').json()
            if data['players'] == []:
                await ctx.send('You are not ranked!')
                break
            for i in range(0, len(data['players'])):
                if data['players'][i]['username'] == f'{user.name}' and data['players'][i]['discriminator'] == f'{user.discriminator}':
                    userdata = data['players'][i]
                    userrank = i + 1
                    rangel = i
                    fulldata = data
            if userdata != {}:
                user_ = True
            page += 1

        emb = discord.Embed(colour = discord.Color.random(), title = f"**{data['guild']['name']}**", description = '==========================')
        emb.set_author(name = f'Leaderboard', url = f'https://mee6.xyz/leaderboard/{ctx.guild.id}')

        for i in range(0,len(top5)):
            if top5[i]['username'] == f'{user.name}' and top5[i]['discriminator'] == f'{user.discriminator}':
                emb.add_field(inline = False, name = f">> **#{i+1} {top5[i]['username']}** <<", value = f"Lvl {top5[i]['level']} / {top5[i]['detailed_xp'][2]} XP / {top5[i]['message_count']} MSGs")
            else:
                emb.add_field(inline = False, name = f"#**{i+1}** {top5[i]['username']}", value = f"Lvl {top5[i]['level']} / {top5[i]['detailed_xp'][2]} XP / {top5[i]['message_count']} MSGs")

        if user_ == True:
            if user_t5 != True:
                emb.add_field(inline = False, name = f"", value = f"==========================\n")

                emb.add_field(inline = False, name = f">> **#{userrank} {userdata['username']}** <<", value = f"Lvl {userdata['level']} / {userdata['detailed_xp'][2]} XP / {userdata['message_count']} MSGs")

        await ctx.send(embed = emb, content = f'<https://mee6.xyz/leaderboard/{ctx.guild.id}>')
def setup(bot):
    bot.add_cog(RANK(bot))


import django_filters
from django.contrib.auth.models import User
from django.db.models import Q
from posts.models import Offer, Signup, Activity
from users.models import ROLE_REFUGEE, ROLE_MENTOR, Language, LanguageSkill, UserProfile
from environment.models import City, Location
from posts.serializers import LanguageSerializer, CitySerializer, OfferSerializer, LocationSerializer, \
    ActivitySerializer, UserSerializer, LanguageSkillSerializer, ContactSerializer
from rest_framework import mixins
from rest_framework.exceptions import ValidationError
from rest_framework.filters import FilterSet, SearchFilter, DjangoFilterBackend, OrderingFilter
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK
from rest_framework.viewsets import GenericViewSet

class LanguageViewSet(mixins.RetrieveModelMixin,
                      mixins.ListModelMixin,
                      GenericViewSet):

    queryset = Language.objects.all()
    serializer_class = LanguageSerializer

class CityViewSet(mixins.RetrieveModelMixin,
                  mixins.ListModelMixin,
                  GenericViewSet):

    queryset = City.objects.all()
    serializer_class = CitySerializer

class LocationViewSet(mixins.RetrieveModelMixin,
                      mixins.ListModelMixin,
                      GenericViewSet):

    queryset = Location.objects.all()
    serializer_class = LocationSerializer

    filter_backends = (
        DjangoFilterBackend,
    )
    filter_fields = (
        'city',
    )

class ActivityViewSet(mixins.RetrieveModelMixin,
                      mixins.ListModelMixin,
                      GenericViewSet):

    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

class OfferFilter(FilterSet):
    begin_gte = django_filters.DateTimeFilter(name='begin', lookup_type='gte')
    begin_lte = django_filters.DateTimeFilter(name='begin', lookup_type='lte')
    gender = django_filters.CharFilter(name='user__profile__gender')

    class Meta:
        model = Offer
        fields = [
            'activity',
            'location',
            'user',
            'begin_gte',
            'begin_lte',
            'gender',
        ]

class OfferFilterBackend(DjangoFilterBackend):
    default_filter_set = OfferFilter

class OfferViewSet(mixins.RetrieveModelMixin,
                   mixins.ListModelMixin,
                   mixins.CreateModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin,
                   GenericViewSet):

    queryset = Offer.objects.all()
    serializer_class = OfferSerializer
    filter_backends = (
        OfferFilterBackend,
        SearchFilter,
        OrderingFilter,
    )
    filter_class = OfferFilter
    search_fields = (
        'description',
    )

    def perform_destroy(self, instance):
        if instance.user != self.request.user:
            raise ValidationError('Not your post')
        instance.delete()

    def perform_update(self, serializer):
        if serializer.instance.user != self.request.user:
            raise ValidationError('Not your post')
        serializer.save(user=self.request.user)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

class SignupViewSet(mixins.RetrieveModelMixin,
                    mixins.ListModelMixin,
                    mixins.CreateModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.DestroyModelMixin,
                    GenericViewSet):

    queryset = Signup.objects.all()
    serializer_class = OfferSerializer
    filter_backends = (
        DjangoFilterBackend,
    )
    filter_fields = (
        'user',
        'offer',
    )

    def get_queryset(self):
        return self.queryset.filter(user=self.request.user)

    def perform_destroy(self, instance):
        if instance.user != self.request.user:
            raise ValidationError('Not your signup')
        instance.delete()

    def perform_update(self, serializer):
        if serializer.instance.user != self.request.user:
            raise ValidationError('Not your signup')
        serializer.save(user=self.request.user)

    def perform_create(self, serializer):
        if not self.request.user.profile.approved:
            raise ValidationError('Your community manager needs to approve your user profile first!')
        serializer.save(user=self.request.user)

class LanguageSkillViewSet(mixins.RetrieveModelMixin,
                           mixins.ListModelMixin,
                           mixins.CreateModelMixin,
                           mixins.UpdateModelMixin,
                           mixins.DestroyModelMixin,
                           GenericViewSet):

    queryset = LanguageSkill.objects.all()
    serializer_class = LanguageSkillSerializer
    filter_backends = (
        DjangoFilterBackend,
    )
    filter_fields = (
        'user',
        'language',
    )

    def get_queryset(self):
        return self.queryset.filter(user=self.request.user)

    def perform_destroy(self, instance):
        if instance.user != self.request.user:
            raise ValidationError('Not your language')
        instance.delete()

    def perform_update(self, serializer):
        if serializer.instance.user != self.request.user:
            raise ValidationError('Not your language')
        serializer.save(user=self.request.user)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

class UserViewSet(mixins.UpdateModelMixin,
                  mixins.RetrieveModelMixin,
                  GenericViewSet):

    serializer_class = UserSerializer
    queryset = UserProfile.objects.all()

    def get_object(self):
        return self.request.user.profile

    def retrieve(self, request, *args, **kwargs):
        if not self.request.user.is_authenticated():
            return Response({'details': 'Not authenticated'})
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    def create(self, request, *args, **kwargs):
        if 'password' not in request.data:
            raise ValidationError('password required')
        if 'role' not in request.data or request.data['role'] not in [ROLE_REFUGEE, ROLE_MENTOR]:
            raise ValidationError('Invalid role')
        s = UserSerializer(data=request.data)
        s.is_valid(raise_exception=True)
        instance = s.create(s.validated_data)
        return Response(data=UserSerializer(instance).data, status=201)

class ContactViewSet(mixins.RetrieveModelMixin,
                     mixins.ListModelMixin,
                     GenericViewSet):

    queryset = UserProfile.objects.all()
    serializer_class = ContactSerializer

    def check_object_permissions(self, request, obj):
        super().check_object_permissions(request, obj)
        u = self.request.user
        my_offers = Offer.objects.filter(user=u)
        if obj.user == u:  # my own info ok
            return
        if Signup.objects.filter(offer__in=my_offers, user=obj.user).count() > 0:  # my customers ok
            return
        if Signup.objects.filter(user=u, offer__user=obj.user).count() > 0:  # my mentors ok
            return
        self.permission_denied(request, 'No permission for this contact info')

    def get_queryset(self):
        # TODO: limit to 1) users signed up to my offers 2) users whose offers I signed up to
        return self.queryset.all()


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import login, logout
from django.contrib.auth.forms import AuthenticationForm
from django.urls.base import reverse
from .forms import SignUpForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import UpdateView
from .models import User
from post.models import Post
from django.db.models import Q

# Create your views here.

def login_view(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return redirect('/')
    form = AuthenticationForm()
    return render(request, 'account/login.html', {'form': form})

def logout_view(request):
    logout(request)
    return redirect('/')

def signup_view(request):
    user_list = [user.username for user in User.objects.all()]
    users = " ".join(user_list)
    email_list = [user.email for user in User.objects.all()]
    emails = " ".join(email_list)
    print(users)
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()
    context = {
        'form': form,
         'users': users,
         'emails': emails,
    }
    return render(request, 'account/signup.html', context)

def profile_view(request, username):

    user = get_object_or_404(User, username=username)
    posts = Post.objects.filter(Q(user=user) & Q(
        is_archived=False)).order_by("-date")
    context = {
        'user': user,
        'posts': posts
    }
    return render(request, 'account/profile.html', context)

class ProfileUpdateView(LoginRequiredMixin, UpdateView):
    model = User
    fields = ['username', 'image', 'email', 'name', 'bio',
              'is_private', 'birth', 'gender', 'dark_mode']
    template_name = 'account/profile_update.html'

    def get_success_url(self):
        return reverse('account:profile', kwargs={'username': self.object.username})

def search_account(request):
    keyword = request.POST.get('search')
    accounts = [account for account in User.objects.all()
                if keyword in account.email.split('@')[0]
                or keyword in account.name
                or keyword in account.bio]
    return render(request, 'account/account_list.html', {'accounts': accounts})

def followers_list(request, username):
    user = get_object_or_404(User, username=username)
    accounts = [account for account in user.followers.all()]
    return render(request, 'account/account_list.html', {'accounts': accounts})

def following_list(request, username):
    user = get_object_or_404(User, username=username)
    accounts = [account for account in user.following.all()]
    return render(request, 'account/account_list.html', {'accounts': accounts})

def request_account(request, username):
    user = get_object_or_404(User, username=username)
    user.requests.add(request.user)
    user.save()
    return redirect('account:profile', user.username)

def accept_request(request, username):
    user = get_object_or_404(User, username=username)
    request.user.requests.remove(user)
    user.following.add(request.user)
    return redirect('account:profile', user.username)

def cancel_request(request, username):
    user = get_object_or_404(User, username=username)
    user.requests.remove(request.user)
    user.save()
    return redirect('account:profile', user.username)

def request_list(request):
    return render(request, 'account/request_list.html')

def follow_account(request, username):
    user = get_object_or_404(User, username=username)
    request.user.following.add(user)
    return redirect('account:profile', user.username)

def unfollow_account(request, username):
    user = get_object_or_404(User, username=username)
    request.user.following.remove(user)
    return redirect('account:profile', user.username)

def saved_list_view(request):
    user = request.user
    posts = reversed(user.saved.all())
    context = {
        'user': user,
        'posts': posts
    }
    return render(request, 'account/saved_list.html', context)

def archive_list_view(request):
    user = request.user
    posts = reversed(Post.objects.filter(Q(user=user) & Q(is_archived=True)))
    context = {
        'user': user,
        'posts': posts
    }
    return render(request, 'account/archive_list.html', context)


#!/usr/bin/env python3
# Usage: python3 mp3.py 1 100
import urllib.request as request

def f_html_from_url(s_url):
    import requests
    response = requests.get(s_url)
    s_html = response.text
    return s_html

def f_soup_from_html(s_html):
    import bs4
    soup = bs4.BeautifulSoup(s_html, "lxml")
    return soup

def f_mp3_url(n):
    urls = "https://talkpython.fm/episodes/show/{}/"
    url = urls.format(n)
    s_html = f_html_from_url(url)
    soup = f_soup_from_html(s_html)
    href = soup.find("a", class_="btn btn-default subscribe-btn").get("href")
    return href

def f_download(n):
    import os
    href = f_mp3_url(n)
    url = "https://talkpython.fm{}".format(href)
    ss = href.split("/")
    filename = "{}-{}".format(ss[-2], ss[-1])
    print("Accessing {}".format(url))
    request.urlretrieve(url, filename)
    print("Saved {}".format(os.path.abspath(filename)))

if __name__ == "__main__":
    import sys
    import time
    min_, max_ = int(sys.argv[1]), int(sys.argv[2])
    print("Processing from {} to {}.".format(min_, max_))
    for i in range(min_, max_ + 1):
        f_download(i)
        sec = 5
        print("Sleeping {} s.".format(sec))
        time.sleep(sec)
        print("Sleep done.")


import cv2
import numpy as np
from math import *

def crop_bbox(img, threshold=200, pad=1):
    height = img.shape[0]
    width = img.shape[1]
    x1, y1, x2, y2 = 0, 0, 0, 0
    # find black edge
    for i in range(height):
        if (img[i, :] <= threshold).any():
            y1 = i - pad
            if y1 < 0:
                y1 = 0
            break
    for i in range(height - 1, -1, -1):
        if (img[i, :] <= threshold).any():
            y2 = i + pad
            if y2 > height - 1:
                y2 = height - 1
            break
    for i in range(width):
        if (img[:, i] <= threshold).any():
            x1 = i - pad
            if x1 < 0:
                x1 = 0
            break
    for i in range(width - 1, -1, -1):
        if (img[:, i] <= threshold).any():
            x2 = i + pad
            if x2 > width - 1:
                x2 = width - 1
            break
    return img[y1:y2, x1:x2]

def no_crop_rotate(img, degree):
    height, width = img.shape[:2]
    heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
    widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))

    matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

    matRotation[0, 2] += (widthNew - width) / 2
    matRotation[1, 2] += (heightNew - height) / 2

    imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))

    return imgRotation

def erode(img):
    """
    make the black symbol thicker
    """
    kernel = np.ones((5, 5), np.uint8)
    img = cv2.erode(img, kernel, iterations=1)
    return img


from __future__ import print_function
from keras.callbacks import LambdaCallback
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation
from keras.layers import LSTM
from keras.optimizers import RMSprop
from keras.utils.data_utils import get_file
import numpy as np
import random
import sys
import io
import argparse

DEFAULT_TEXT_FILE='corpus.txt'
DEFAULT_MODEL_FILE='model.h5'
DEFAULT_EPOCHS=60

DESCRIPTION = """
Example script to build a model from example texts, and
generate synthetic texts from that model, using LSTMs.
At least 20 epochs are required before the generated text
starts sounding coherent.

It is recommended to run this script on GPU, as recurrent
networks are quite computationally intensive.
Make sure your corpus has at least ~100k characters.
~1M is better.

Based on a script from the Keras Team at
https://github.com/keras-team/keras/tree/master/examples
"""

parser = argparse.ArgumentParser(
    description=DESCRIPTION,
    epilog="",
    formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

parser.add_argument('-t', '--text', type=str, nargs='?', const=DEFAULT_TEXT_FILE,
                    required=True, help='text file from which to build the model')
parser.add_argument('-f', '--fit', action='store_true',
                    help='fit the model to the text file')
parser.add_argument('-e', '--epochs', type=int, nargs='?', const=DEFAULT_EPOCHS,
                    help='number of training epochs')
parser.add_argument('-s', '--save', type=str, nargs='?', const=DEFAULT_MODEL_FILE,
                    help='save the model to the given file')
parser.add_argument('-l', '--load', type=str, nargs='?',
                    help='load the model from the given file')
parser.add_argument('-g', '--generate', action='store_true',
                    help='generate examples from the model (either one built via -f, or one loaded via -l), taking seeds from the file given by -t')

args = parser.parse_args()

epochs=args.epochs
path=args.text

with io.open(path, encoding='utf-8') as f:
    text = f.read().lower()
print('corpus length:', len(text))

chars = sorted(list(set(text)))
print('total chars:', len(chars))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))

# cut the text in semi-redundant sequences of maxlen characters
maxlen = 40
step = 3
sentences = []
next_chars = []
for i in range(0, len(text) - maxlen, step):
    sentences.append(text[i: i + maxlen])
    next_chars.append(text[i + maxlen])
print('nb sequences:', len(sentences))

# Vectorize text and build the model
if args.fit:

  print('Vectorization...')
  x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
  y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
  for i, sentence in enumerate(sentences):
      for t, char in enumerate(sentence):
          x[i, t, char_indices[char]] = 1
      y[i, char_indices[next_chars[i]]] = 1

  # build the model: a single LSTM
  print('Build model...')
  model = Sequential()
  model.add(LSTM(128, input_shape=(maxlen, len(chars))))
  model.add(Dense(len(chars)))
  model.add(Activation('softmax'))

  optimizer = RMSprop(lr=0.01)
  model.compile(loss='categorical_crossentropy', optimizer=optimizer)

def sample(preds, temperature=1.0):
    # helper function to sample an index from a probability array
    preds = np.asarray(preds).astype('float64')
    preds = np.log(preds) / temperature
    exp_preds = np.exp(preds)
    preds = exp_preds / np.sum(exp_preds)
    probas = np.random.multinomial(1, preds, 1)
    return np.argmax(probas)

def on_epoch_end(epoch, logs):
    # Function invoked at end of each epoch. Prints generated text.
    print()
    print('----- Generating text after Epoch: %d' % epoch)
    generate_text()

def generate_text():
    # Create a seed and generate some examples
    start_index = random.randint(0, len(text) - maxlen - 1)
    for diversity in [0.2, 0.5, 1.0, 1.2]:
        print('----- diversity:', diversity)

        generated = ''
        sentence = text[start_index: start_index + maxlen]
        generated += sentence
        print('----- Generating with seed: "' + sentence + '"')
        sys.stdout.write(generated)

        for i in range(400):
            x_pred = np.zeros((1, maxlen, len(chars)))
            for t, char in enumerate(sentence):
                x_pred[0, t, char_indices[char]] = 1.

            preds = model.predict(x_pred, verbose=0)[0]
            next_index = sample(preds, diversity)
            next_char = indices_char[next_index]

            generated += next_char
            sentence = sentence[1:] + next_char

            sys.stdout.write(next_char)
            sys.stdout.flush()
        print()

print_callback = LambdaCallback(on_epoch_end=on_epoch_end)

# Fit the model
if args.fit:
  model.fit(x, y,
            batch_size=128,
            epochs=epochs,
            callbacks=[print_callback])

# Save the model
if args.save:
  print("saving to ", args.save)
  model.save(args.save)

# Load the model
if args.load:
  model = load_model(args.load)

# Generate examples
if args.generate:
  generate_text()


from django.core.management.base import BaseCommand
from tqdm import tqdm

from conditions.models import ConditionSet

class Command(BaseCommand):

    def handle(self, *args, **options):

        all_conditionsets = ConditionSet.objects.all()

        for conditionset in tqdm(all_conditionsets):

            # Re-generate the systematic name
            conditions_list = [(u'%s' % condition) for condition in
                               conditionset.conditions.order_by('type__tags__order', 'type__chebi_name',
                                                                'type__pubchem_name', 'type__name').all()]
            conditionset.systematic_name = u'%s' % ", ".join(conditions_list)

            conditionset.display_name = conditionset.systematic_name
            if conditionset.common_name:
                conditionset.display_name = conditionset.common_name

            conditionset.save()

        self.stdout.write('Successfully updated %d conditionsets.' % all_conditionsets.count())


# uncompyle6 version 3.7.4
# Python bytecode 2.6 (62161)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/uittenbroek/Projects/buildout-nuffic/src/collective.newrelic/collective/newrelic/patches/newrelic_transaction.py
# Compiled at: 2013-12-24 05:41:42
from collective.newrelic.utils import logger
from newrelic.api.transaction import Transaction
original__init__ = Transaction.__init__
original__exit__ = Transaction.__exit__

def patched__init__(self, *args, **kwargs):
    original__init__(self, *args, **kwargs)
    self._transaction_id = id(self)

Transaction.__init__ = patched__init__
logger.info('Patched newrelic.api.transaction:Transaction.__init__ to add _transaction_id')

def patched__exit__(self, *args, **kwargs):
    if self._transaction_id != id(self):
        logger.exception(('Checking my id: {0}  against {1}').format(self._transaction_id, id(self)))
        return
    original__exit__(self, *args, **kwargs)

Transaction.__exit__ = patched__exit__
logger.info('Patched newrelic.api.transaction:Transaction.__exit__ to check _transaction_id')


#-------------------------------------
# Project: Lightweight Industrial Image Classifier based on Federated Few-Shot Learning
# code is based on https://github.com/floodsung/LearningToCompare_FSL
#-------------------------------------

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.optim.lr_scheduler import StepLR
import numpy as np
import task_generator as tg
import os
import math
import argparse
import scipy as sp
import scipy.stats
from ecci_sdk import Client
import threading

parser = argparse.ArgumentParser(description="One Shot Visual Recognition")
parser.add_argument("-f","--feature_dim",type = int, default = 32)
parser.add_argument("-r","--relation_dim",type = int, default = 8)
parser.add_argument("-w","--class_num",type = int, default = 2)
parser.add_argument("-s","--sample_num_per_class",type = int, default = 1)
parser.add_argument("-b","--batch_num_per_class",type = int, default = 1)
parser.add_argument("-e","--episode",type = int, default= 500000)
parser.add_argument("-t","--test_episode", type = int, default = 100)
parser.add_argument("-l","--learning_rate", type = float, default = 0.001)
parser.add_argument("-g","--gpu",type=int, default=0)
parser.add_argument("-u","--hidden_unit",type=int,default=10)
args = parser.parse_args()

def mean_confidence_interval(data, confidence=0.95):
    a = 1.0*np.array(data)
    n = len(a)
    m, se = np.mean(a), scipy.stats.sem(a)
    h = se * sp.stats.t._ppf((1+confidence)/2., n-1)
    return m,h

def mini_imagenet_folders():
    metatrain_folder = './client3/train3'
    metaval_folder = './client3/train3'
    metatrain_folders = [os.path.join(metatrain_folder, label) \
                         for label in os.listdir(metatrain_folder) \
                         if os.path.isdir(os.path.join(metatrain_folder, label)) \
                         ]
    metaval_folders = [os.path.join(metaval_folder, label) \
                       for label in os.listdir(metaval_folder) \
                       if os.path.isdir(os.path.join(metaval_folder, label)) \
                       ]
    return metatrain_folders, metaval_folders

class CNNEncoder(nn.Module):
    """docstring for ClassName"""

    def __init__(self):
        super(CNNEncoder, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 32, kernel_size=3, padding=0),
            nn.BatchNorm2d(32, momentum=1, affine=True),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, padding=0),
            nn.BatchNorm2d(32, momentum=1, affine=True),
            nn.ReLU(),
            nn.MaxPool2d(2))

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        return out

def main():
    ecci_client = Client()
    mqtt_thread = threading.Thread(target=ecci_client.initialize)
    mqtt_thread.start()
    ecci_client.wait_for_ready()
    FEATURE_DIM = args.feature_dim
    CLASS_NUM = args.class_num
    SAMPLE_NUM_PER_CLASS = args.sample_num_per_class
    BATCH_NUM_PER_CLASS = args.batch_num_per_class
    EPISODE = args.episode
    TEST_EPISODE = args.test_episode
    LEARNING_RATE = args.learning_rate
    GPU = args.gpu
    HIDDEN_UNIT = args.hidden_unit
    metatrain_folders,metatest_folders = mini_imagenet_folders()
    for episode in range(EPISODE):
        data_msg_queue = ecci_client.get_sub_data_payload_queue()
        data_msg = data_msg_queue.get()
        feature_encoder = data_msg['feature_encoder']
        task =tg.MiniImagenetTask(metatrain_folders, CLASS_NUM, SAMPLE_NUM_PER_CLASS, BATCH_NUM_PER_CLASS)  # task = tg.MiniImagenetTask(metatrain_folders,CLASS_NUM,5,10)
        sample_dataloader = tg.get_mini_imagenet_data_loader(task, num_per_class=SAMPLE_NUM_PER_CLASS, split="train",
                                                             shuffle=False)
        batch_dataloader = tg.get_mini_imagenet_data_loader(task, num_per_class=BATCH_NUM_PER_CLASS, split="test",
                                                            shuffle=True)
        samples,sample_labels = sample_dataloader.__iter__().next()
        batches,batch_labels = batch_dataloader.__iter__().next()
        sample_features = feature_encoder(Variable(samples).cuda(GPU))
        batch_features = feature_encoder(Variable(batches).cuda(GPU))
        payload = {"batch_labels": batch_labels, "sample_features": sample_features, "batch_features": batch_features}
        ecci_client.send_message(payload, "cloud")

if __name__ == '__main__':
    main()


# coding=utf-8
from __future__ import unicode_literals
from django.db import models
from administration.models import Profile

class Venue(models.Model):
    name = models.CharField(max_length=100)
    address = models.TextField(blank=True)
    phone = models.CharField(max_length=20)
    updated = models.DateTimeField(auto_now_add=True)
    updated_by = models.ForeignKey(Profile, related_name='updated_venues', null=True)
    created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(Profile, related_name='created_venues')

    def create(self, user, force_insert=False, force_update=False, using=None, update_fields=None):
        self.created_by = user
        self.save(force_insert, force_update, using, update_fields)

    def update(self, user, force_insert=False, force_update=False, using=None, update_fields=None):
        self.updated_by = user
        self.save(force_insert, force_update, using, update_fields)

    def __unicode__(self):
        return self.name

    def add_storage_product(self, sp):
        vp = VenueProduct.objects.filter(product_unit__product=sp.product_unit.product,
                                         product_unit__primary=True, venue=self).first()
        if not vp:
            pu = ProductUnit.objects.get(product=sp.product_unit.product, primary=True)
            vp = VenueProduct.objects.create(product_unit=pu, venue=self, quantity=0)
        vp.quantity += sp.quantity / float(sp.product_unit.relation)
        vp.update()

class VenueProduct(models.Model):
    venue = models.ForeignKey(Venue, related_name='products')
    product_unit = models.ForeignKey('ProductUnit', related_name='venues')
    quantity = models.FloatField(default=0)
    updated = models.DateTimeField(auto_now=True)

    def update(self):
        """Function that updates the quantity of product in different units
        """
        units = (self.quantity - VenueProduct.objects.get(pk=self.pk).quantity)/float(self.product_unit.relation)
        for vp in VenueProduct.objects.filter(product_unit__product=self.product_unit.product, venue=self.venue):
            vp.quantity += units * float(vp.product_unit.relation)
            vp.save()

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        """Override of save that stores a log
        """
        VenueProductLog(venue=self.venue, product=self.product_unit, quantity=self.quantity).save()
        super(VenueProduct, self).save(force_insert, force_update, using, update_fields)

    def __unicode__(self):
        return self.product_unit

class VenueProductLog(models.Model):
    venue = models.ForeignKey(Venue, related_name='product_logs')
    product = models.ForeignKey('ProductUnit', related_name='venue_logs')
    quantity = models.IntegerField(default=0)
    created = models.DateTimeField(auto_now_add=True)


import os
import time
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.utils import data
import torch.nn.functional as F
from models import ResNet, Unet, ResNet_UM, Unet_UM, ResNet_Mag, Unet_Mag, ResNet_Rot, Unet_Rot, ResNet_Scale, Unet_Scale
import matplotlib.pyplot as plt
from utils import train_epoch, eval_epoch, test_epoch, Dataset, get_lr, train_epoch_scale, eval_epoch_scale, test_epoch_scale, Dataset_scale
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

###### Hyperparameter ######
save_name = "ResNet_UM"
n_epochs = 1000
learning_rate = 0.001 # 0.0005 for mag_equ resnet; 0.0001 for scale_equ resnet
batch_size = 16
input_length = 24
train_output_length = 3 # 4 for all Unets
test_output_length = 10
lr_decay = 0.9
###########################

########## Data ###########
train_direc = ".../data_64/sample_"
valid_direc = ".../data_64/sample_"
test_direc = ".../data_64/sample_"
train_indices = list(range(0, 6000))
valid_indices = list(range(6000, 8000))
test_indices = list(range(8000, 10000))

train_set = Dataset(train_indices, input_length, 30, train_output_length, train_direc, True)
valid_set = Dataset(valid_indices, input_length, 30, train_output_length, valid_direc, True)
test_set = Dataset(test_indices, input_length, 30, test_output_length, test_direc, True)
# use Dataset_scale for scale equivariant models
# train_set = Dataset_scale(train_indices, input_length, 30, output_length, train_direc)
# valid_set = Dataset_scale(valid_indices, input_length, 30, output_length, train_direc)
# test_set = Dataset_scale(test_indices, input_length, 40, 10, test_direc)

train_loader = data.DataLoader(train_set, batch_size = batch_size, shuffle = True, num_workers = 8)
valid_loader = data.DataLoader(valid_set, batch_size = batch_size, shuffle = False, num_workers = 8)
test_loader = data.DataLoader(test_set, batch_size = batch_size, shuffle = False, num_workers = 8)
###########################

### Model ###
model = nn.DataParallel(ResNet_UM(input_channels = input_length*2, output_channels = 2, kernel_size = 3).to(device))
#model = nn.DataParallel(Unet_Rot(input_frames = input_length, output_frames = 1, kernel_size = 3, N = 8).to(device))
#model = nn.DataParallel(ResNet_Scale(input_channels = input_length*2, output_channels = 2, kernel_size = 3).to(device))

optimizer = torch.optim.Adam(model.parameters(), learning_rate,betas=(0.9, 0.999), weight_decay=4e-4)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size= 1, gamma=lr_decay)
loss_fun = torch.nn.MSELoss()

min_mse = 100
train_mse = []
valid_mse = []
test_mse = []

for i in range(n_epochs):
    start = time.time()
    scheduler.step()

    model.train()
    # use train_epoch_scale/eval_epoch_scale for training scale equivariant models
    train_mse.append(train_epoch(train_loader, model, optimizer, loss_fun))
    model.eval()
    mse, _, _ = eval_epoch(valid_loader, model, loss_fun)
    valid_mse.append(mse)

    if valid_mse[-1] < min_mse:
        min_mse = valid_mse[-1]
        best_model = model
        torch.save(best_model, save_name + ".pth")
    end = time.time()

    # Early Stopping but train at least for 50 epochs
    if (len(train_mse) > 50 and np.mean(valid_mse[-5:]) >= np.mean(valid_mse[-10:-5])):
            break
    print(i+1,train_mse[-1], valid_mse[-1], round((end-start)/60,5), format(get_lr(optimizer), "5.2e"))

test_mse, preds, trues, loss_curve = test_epoch(test_loader, best_model, loss_fun)
torch.save({"preds": preds,
            "trues": trues,
            "test_mse":test_mse,
            "loss_curve": loss_curve},
            name + ".pt")


# -*- coding: utf-8 -*-
"""
functions to use GEE within Qgis python script
"""
import ee

import ee_plugin.utils

def addLayer(image: ee.Image, visParams=None, name='untitled', shown=True, opacity=1.0):
    """
        Mimique addLayer GEE function

        Uses:
            >>> from ee_plugin import Map
            >>> Map.addLayer(.....)
    """
    if not isinstance(image, ee.Image):
        err_str = "\n\nThe image argument in 'addLayer' function must be a 'ee.Image' instance."
        raise AttributeError(err_str)

    if visParams:
        image = image.visualize(**visParams)

    ee_plugin.utils.add_or_update_ee_image_layer(image, name, shown, opacity)


# ******************************************************************
# ******************** Universidad de los Andes ********************
# ********************   Fisica computacional   ********************
# ********************         Tarea 5          ********************
# ******************************************************************
# ***************   Andres Felipe Garcia Albarracin  ***************
# ***************         Andrea Rozo Mendez	     ***************
# ******************************************************************

# Librerias
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import pylab
import scipy
from scipy.fftpack import fft, fftfreq

# Lectura del archivo
c = np.loadtxt('monthrg2.txt')

# Vector de meses
(fil, col) = np.shape(c)
meses = []
for i in range(fil):
	meses = np.append(meses,[i])

anos = meses/12.0 + c[0,0]
# Vector del numero de manchas
manchas = (c[:,3])

# Grafica numero de manchas en funcion del tiempo
pylab.scatter(anos,manchas)
pylab.xlabel('$Ano$')
pylab.ylabel('$N(n)$')
pylab.title('Numero de manchas solares en funcion del anho')
pylab.grid(True)
pylab.savefig('GraficaManchas')

# Transformada de Fourier
n = fil
dt = 1 							# 1 mes de intervalo
fft_manchas = np.fft.fft(manchas)/n 			# Transformada de Fourier normalizada
freq = np.fft.fftfreq(n,dt)				# Vector de frecuencias

# Espectro de potencias
f2 = abs(fft_manchas)*abs(fft_manchas)

fig1 = plt.figure()
pylab.plot(freq,abs(f2))
pylab.xlabel('$f\ [1/mes]$')
pylab.ylabel('$(F\{N\})^2(f)\ [manchas.mes]^2$')
pylab.title('$\mathrm{Espectro\ de\ potencias\ del\ numero\ de\ manchas}$', fontsize=20)
pylab.grid(True)
pylab.savefig('PotenciaManchas')

# Espectro de frecuencias en funcion del periodo T
m = np.size(freq)
per = 1/(12*freq[m/(20*12):m/12])
f3 = f2[m/(20*12):m/12]
fig2 = plt.figure()
pylab.plot(per,abs(f3))
pylab.xlabel('$T\ [anho]$')
pylab.ylabel('$(F\{N\})^2(T)\ [manchas.mes]^2$')
pylab.title('$\mathrm{Espectro\ de\ potencias\ del\ numero\ de\ manchas}$', fontsize=20)
pylab.grid(True)
pylab.savefig('PotenciaManchasPeriodo')

# Filtro pasa bajas
fft_Filt = fft_manchas

freqCorte = 1/(12.0)
for i in range(len(freq)):
	if (abs(freq[i])>freqCorte):
		fft_Filt[i] = 0.0

f4 = abs(fft_Filt)*abs(fft_Filt)
fig3 = plt.figure()
pylab.plot(freq,abs(f4))
pylab.xlabel('$f\ [1/mes]$')
pylab.ylabel('$(F\{N\})^2(f)\ [manchas.mes]^2$')
pylab.title('$\mathrm{Espectro\ de\ potencias\ del\ numero\ de\ manchas\ con\ filtro}$', fontsize=20)
pylab.grid(True)
pylab.savefig('PotenciaManchasFiltradas')

# Senal sin filtros
manchasLimpias = np.fft.ifft(fft_Filt)
fig4 = plt.figure()
pylab.scatter(anos,np.real(manchasLimpias))
pylab.xlabel('$Ano$')
pylab.ylabel('$N(n)$')
pylab.title('Numero de manchas solares en funcion del anho')
pylab.grid(True)
pylab.savefig('GraficaManchasLimpias')


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 11 11:18:58 2019
@author: Harish Julapalli
"""

import os
import nltk
from nltk.corpus import RegexpTokenizer as regextoken
import matplotlib.pyplot as plt
nltk.download('averaged_perceptron_tagger')

os.chdir("E:\\Python\\NLP\\")

def read_files(path, review_list):
    os.chdir(path)
    entries = os.listdir(path)
    for entry in entries:
        with open(entry,'r') as input:
            review_list.append(input.read())
    return review_list

positive_reviews_true = []
positive_reviews_true = read_files("E:\\Python\\NLP\\op_spam_v1.4\\pos_tru", positive_reviews_true)

positive_reviews_dec = []
positive_reviews_dec = read_files("E:\\Python\\NLP\\op_spam_v1.4\\pos_dec", positive_reviews_dec)

negative_reviews_true = []
negative_reviews_true = read_files("E:\\Python\\NLP\\op_spam_v1.4\\neg_tru", negative_reviews_true)

negative_reviews_dec = []
negative_reviews_dec = read_files("E:\\Python\\NLP\\op_spam_v1.4\\neg_dec", negative_reviews_dec)

#POSITIVE REVIEWS
def pos_tagger(review_list):
    tokenizer = regextoken(r'\w+')
    for review in review_list:
        words = tokenizer.tokenize(review)
        pos = nltk.pos_tag(words)
    print(pos)

pos_tagger(positive_reviews_true)


from typing import List

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def make_tree(nodes: List[int]) -> List[TreeNode]:
    v = []
    for n in nodes:
        v.append(TreeNode(n) if n is not None else None)

    size = len(v) - 1
    for i in range(len(v)):
        n = 2 * i + 1
        if n > size:
            break

        if v[i] is not None:
            v[i].left = v[n]
            n += 1
            if n > size:
                break
            v[i].right = v[n]

    return v


import sys
import urllib.request
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import uic
import sensing
import time
import matplotlib.pyplot as plt
from gasLeakage import *
import pandas as pd
import datetime

form_class = uic.loadUiType("sensor.ui")[0]

class WindowClass(QMainWindow, form_class) :
    def __init__(self) :
        super().__init__()
        self.setupUi(self)

        self.btn_1.clicked.connect(self.button1Function)

    def button1Function(self) :

        count =0
        temp = []
        temp2 = []
        gas = gasLeakage()

        while True:
            getData = gas.getSensorData()
            gas.controlFan(getData)

            print("[DEBUG] Smoke Sensor Value = %u"%(getData))

            data = [[datetime.datetime.now(), getData]]
            submission = pd.DataFrame(data)
            submission.to_csv('./Gas_DataSet.csv', header = False, mode = 'a', index = False)
            time.sleep(0.5)
            temp.append(getData)
            temp2.append(str(getData))
            count +=1
            if count >= 15:
                break

        auth = 0
        for element in temp:
            if element >= 200:
                auth = 1

        if auth == 1:
            self.textBrowser_2.setPlainText("위험상황입니다.")
        else:
            self.textBrowser_2.setPlainText("정상입니다.")
        self.textBrowser_3.setPlainText("정상입니다.")

        view = self.listView
        model = QStandardItemModel()
        for f in temp2:
            model.appendRow(QStandardItem(f))
        view.setModel(model)

        plt.plot(temp, color='red')
        plt.xlabel("Time")
        plt.ylabel("Gas_data")
        plt.draw()
        fig = plt.gcf()
        fig.set_figwidth(320/fig.dpi)
        fig.set_figheight(240/fig.dpi)
        fig.savefig("myfile.png")

        self.qPixmapFileVar = QPixmap()
        self.qPixmapFileVar.load("myfile.png")
        self.qPixmapFileVar = self.qPixmapFileVar.scaled(320,240)
        self.lbl_picture.setPixmap(self.qPixmapFileVar)

if __name__ == "__main__" :
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()


# @Author: Tushar Agarwal(tusharcoder) <tushar>
# @Date:   2017-06-13T10:05:29+05:30
# @Email:  tamyworld@gmail.com
# @Filename: models.py
# @Last modified by:   tushar
# @Last modified time: 2017-06-13T12:11:46+05:30

from django.db import models
from django.contrib.auth.models import User
from .app_settings import PROFILE_CHANGABLE_FIELDS
# Create your models here.

class UserProfile(models.Model):
    """
    Model to store the user profile
    """
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    emp_id = models.CharField(max_length=10)
    address = models.TextField(max_length=500)
    pan_no= models.CharField(max_length=12)
    position= models.CharField(max_length=20)

    def __str__(self):
        """
        return the string representation of the model
        """
        return self.name

    def updateProfile(self,**kwargs):
        """
        model to save the UserProfile
        """
        fields_to_change = set(kwargs.keys()).intersection(PROFILE_CHANGABLE_FIELDS)
        for field in fields_to_change:
            setattr(self,field,kwargs.get(field)[0])
        self.save()
        return True

class Project(models.Model):
    """Model to store the Projet details"""
    name=models.CharField(max_length=100)
    description=models.CharField(max_length=500)
    def __str__(self):
        """return the string representation of the MOdel Project"""
        return self.name

class WorkType(models.Model):
    """Model to store the type"""
    name=models.CharField(max_length=100)
    def __str__(self):
        """return the string representation of the MOdel Project"""
        return self.name

class Task(models.Model):
    """Model to store the task assigned to the user"""
    name=models.CharField(max_length=100)
    description=models.CharField(max_length=500)
    starttime=models.DateTimeField()
    endtime=models.DateTimeField()
    project=models.ForeignKey(Project)
    worktype=models.ForeignKey(WorkType)
    assigned_by=models.ForeignKey(User)
    is_approved = models.BooleanField(default=False)

    def __str__(self):
        return self.name


from __future__ import absolute_import, unicode_literals

import filecmp
import os
import re
import shutil
import sys
from glob import glob
from io import open

from ksconf.consts import KSCONF_DEBUG, SMART_CREATE, SMART_NOCHANGE, SMART_UPDATE
from ksconf.util.compare import file_compare

def _is_binary_file(filename, peek=2048):
    # https://stackoverflow.com/a/7392391/315892; modified for Python 2.6 compatibility
    textchars = bytearray(set([7, 8, 9, 10, 12, 13, 27]) | set(range(0x20, 0x100)) - set([0x7f]))
    with open(filename, "rb") as f:
        b = f.read(peek)
        return bool(b.translate(None, textchars))

_dir_exists_cache = set()

def dir_exists(directory):
    """ Ensure that the directory exists """
    # This works as long as we never call os.chdir()
    if directory in _dir_exists_cache:
        return
    if not os.path.isdir(directory):
        os.makedirs(directory)
    _dir_exists_cache.add(directory)

def smart_copy(src, dest):
    """ Copy (overwrite) file only if the contents have changed. """
    ret = SMART_CREATE
    if os.path.isfile(dest):
        if file_compare(src, dest):
            # Files already match.  Nothing to do.
            return SMART_NOCHANGE
        else:
            ret = SMART_UPDATE
            os.unlink(dest)
    shutil.copy2(src, dest)
    return ret

def _stdin_iter(stream=None):
    if stream is None:
        stream = sys.stdin
    for line in stream:
        yield line.rstrip()

def file_fingerprint(path, compare_to=None):
    stat = os.stat(path)
    fp = (stat.st_mtime, stat.st_size)
    if compare_to:
        return fp != compare_to
    else:
        return fp

def expand_glob_list(iterable, do_sort=False):
    for item in iterable:
        if "*" in item or "?" in item:
            glob_expanded = glob(item)
            if do_sort:
                glob_expanded.sort()
            for match in glob_expanded:
                yield match
        else:
            yield item

# This is a Splunk-style (props) stanza style glob:
# where '*' is a single path component, and '...' or '**' means any depth
_glob_to_regex = [
    ("**", r".*"),
    ("*", r"[^/\\]*"),
    ("?", r"."),
    ("...", r".*"),
    (".", r"\."),
]
_glob_to_regex_find = "({})".format("|".join(re.escape(r) for r, _ in _glob_to_regex))

def splglob_to_regex(pattern, re_flags=None):
    glob_to_regex = dict(_glob_to_regex)
    regex = re.sub(_glob_to_regex_find, lambda m: glob_to_regex[m.group()], pattern)
    # If NO anchors have been explicitly given, then assume full-match mode:
    if not re.search(r'(?<![\[\\])[$^]', regex):
        regex = "^{}$".format(regex)
    return re.compile(regex, flags=re_flags)

def splglob_simple(pattern):
    """ Return a splglob that either matches a full path or match a simple file """
    if "/" not in pattern:
        # Assume we've been given a simple file name:   app.conf, *.tgz
        pattern = "^.../{}$".format(pattern)
    else:
        pattern = "^{}$".format(pattern)
    return pattern

def relwalk(top, topdown=True, onerror=None, followlinks=False):
    """ Relative path walker
    Like os.walk() except that it doesn't include the "top" prefix in the resulting 'dirpath'.
    """
    if not top.endswith(os.path.sep):
        top += os.path.sep
    prefix = len(top)
    for (dirpath, dirnames, filenames) in os.walk(top, topdown, onerror, followlinks):
        dirpath = dirpath[prefix:]
        yield (dirpath, dirnames, filenames)

def file_hash(path, algorithm="sha256"):
    import hashlib
    h = hashlib.new(algorithm)
    with open(path, "rb") as fp:
        buf = True
        while buf:
            buf = fp.read(4096)
            h.update(buf)
    return h.hexdigest()

def _samefile(file1, file2):
    if hasattr(os.path, "samefile"):
        # Nix
        return os.path.samefile(file1, file2)
    else:
        # Windows
        file1 = os.path.normpath(os.path.normcase(file1))
        file2 = os.path.normpath(os.path.normcase(file2))
        return file1 == file2

class ReluctantWriter:
    """
    Context manager to intelligently handle updates to an existing file.  New content is written
    to a temp file, and then compared to the current file's content.  The file file will be
    overwritten only if the contents changed.
    """

    def __init__(self, path, *args, **kwargs):
        self.path = path
        self._arg = (args, kwargs)
        self._fp = None
        self._tmpfile = path + ".tmp"
        self.change_needed = None
        self.result = None

    def __enter__(self):
        args, kwargs = self._arg
        self._fp = open(self._tmpfile, *args, **kwargs)
        return self._fp

    def __exit__(self, exc_type, exc_val, exc_tb):
        # Don't do anything, other than try to close/delete the file, if an error occurred.
        try:
            self._fp.close()
        except Exception:
            raise
        if exc_type:
            if KSCONF_DEBUG in os.environ:
                # LOG that temp file is being kept
                pass
            else:
                os.unlink(self._tmpfile)
            return
        if not os.path.isfile(self.path):
            os.rename(self._tmpfile, self.path)
            self.change_needed = True
            self.result = "created"
        elif filecmp.cmp(self._tmpfile, self.path):
            os.unlink(self._tmpfile)
            self.change_needed = False
            self.result = "unchanged"
        else:
            os.unlink(self.path)
            os.rename(self._tmpfile, self.path)
            self.change_needed = True
            self.result = "updated"