from Character import Character

class Hero(Character):
    def __init__(self, name, xp, level, gold, attack_power, defense, max_hp, hp, weapon, inventory) -> None:
        self.xp = xp
        self.level = level
        self.gold = gold
        self.inventory = inventory
        # This is essentially calling the init method on whatever parent / super class we specified
        # We only pass the things the Character class init method needs / is not covered by this subclass
        super().__init__( name, attack_power, defense, max_hp, hp, weapon)

    def check_level_up(self):
        print("Check to see if hero has leveled up")


from __future__ import annotations

import datetime
import decimal

import numpy as np
import pendulum.tz
import pytest
from pendulum import DateTime

from airflow.models.param import Param, ParamsDict
from airflow.serialization.serde import DATA, deserialize, serialize

class TestSerializers:
    def test_datetime(self):
        i = datetime.datetime(2022, 7, 10, 22, 10, 43, microsecond=0, tzinfo=pendulum.tz.UTC)

        s = serialize(i)
        d = deserialize(s)
        assert i.timestamp() == d.timestamp()

        i = DateTime(2022, 7, 10, tzinfo=pendulum.tz.UTC)
        s = serialize(i)
        d = deserialize(s)
        assert i.timestamp() == d.timestamp()

        i = datetime.date(2022, 7, 10)
        s = serialize(i)
        d = deserialize(s)
        assert i == d

        i = datetime.timedelta(days=320)
        s = serialize(i)
        d = deserialize(s)
        assert i == d

    @pytest.mark.parametrize(
        "expr, expected",
        [("1", "1"), ("52e4", "520000"), ("2e0", "2"), ("12e-2", "0.12"), ("12.34", "12.34")],
    )
    def test_encode_decimal(self, expr, expected):
        assert deserialize(serialize(decimal.Decimal(expr))) == decimal.Decimal(expected)

    def test_encode_k8s_v1pod(self):
        from kubernetes.client import models as k8s

        pod = k8s.V1Pod(
            metadata=k8s.V1ObjectMeta(
                name="foo",
                namespace="bar",
            ),
            spec=k8s.V1PodSpec(
                containers=[
                    k8s.V1Container(
                        name="foo",
                        image="bar",
                    )
                ]
            ),
        )
        assert serialize(pod)[DATA] == {
            "metadata": {"name": "foo", "namespace": "bar"},
            "spec": {"containers": [{"image": "bar", "name": "foo"}]},
        }

    def test_numpy(self):
        i = np.int16(10)
        e = serialize(i)
        d = deserialize(e)
        assert i == d

    def test_params(self):
        i = ParamsDict({"x": Param(default="value", description="there is a value", key="test")})
        e = serialize(i)
        d = deserialize(e)
        assert i["x"] == d["x"]

    def test_pandas(self):
        import pandas as pd

        i = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
        e = serialize(i)
        d = deserialize(e)
        assert i.equals(d)


from PIL import Image
import sys

def main():
    infile = sys.argv[1]

    image = Image.open(infile)
    inPixels = image.load()

    width = image.size[0]
    height = image.size[1]

    for i in range(width):
        for j in range(height):
            if inPixels[i, j] == (255, 255, 255, 255):
                inPixels[i, j] = (255, 255, 255, 0)

    image.save(infile)

if __name__ == "__main__":
    main()


from pathlib import Path

class Solution():
    def __init__(self) -> None:
        self.find_top_n_elves = 3
        self.current_elf_number = 1
        self.current_calories_sum = 0
        self.list_of_elves_and_calories: list[dict[str, int]] = []

    def solve(self) -> None:
        with open(f'{Path(__file__).resolve().parent}/day_01_input.txt',
                  encoding='utf-8') as input_file:
            for line in input_file:
                if len(line.strip()) == 0:
                    self.process_next_elf()
                else:
                    self.current_calories_sum += int(line)
            self.process_next_elf()

        self.list_of_elves_and_calories.sort(key=lambda x: x['calories'],
                                             reverse=True)

        total_calories = 0
        for i in range(self.find_top_n_elves):
            print(f'Elf #{self.list_of_elves_and_calories[i]["elf"]} '
                  f'with {self.list_of_elves_and_calories[i]["calories"]} '
                  f'calories')
            total_calories += self.list_of_elves_and_calories[i]["calories"]

        print(f'Total calories carried by the top {self.find_top_n_elves} ' +
              f'elves: {total_calories}')

    def process_next_elf(self) -> None:
        self.list_of_elves_and_calories.append({
            'elf': self.current_elf_number,
            'calories': self.current_calories_sum
        })

        self.current_calories_sum = 0
        self.current_elf_number += 1

Solution().solve()


from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from math import *

#Defina numero de Lados
size = 5

def defaultColor():
    #Magenta
    glColor3f(1,0,1)

def pyramid(size):
    #Desenha vertices e base
    glBegin(GL_LINE_LOOP)
    defaultColor()
    for i in range(size):
        #Conecta vertice
        glVertex3f(cos(2*pi*i/size) ,0,sin(2*pi*i/size))
    glEnd()

    #Conecta Vertices ao Topo
    for i in range(size):
        glBegin(GL_LINE_LOOP)
        defaultColor()
        #Vertice Topo
        glVertex3f(0,1,0)
        #vertice Base
        glVertex3f(cos(2*pi*i/size) ,0,sin(2*pi*i/size))
        glVertex3f(cos(2*pi*(i+1)/size) ,0,sin(2*pi*(i+1)/size))
        glEnd()

def draw():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glRotatef(2,1,3,0)
    pyramid(size)
    glutSwapBuffers()

def timer(i):
    glutPostRedisplay()
    glutTimerFunc(50,timer,1)

#Main
glutInit(sys.argv)
#Define o mode de Exibicao
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE)
#define tamanho da tela
glutInitWindowSize(800,600)
#Cria nome para Janela
glutCreateWindow("Multi-Pyramid")

#Chama funcao desenhar
glutDisplayFunc(draw)
glEnable(GL_MULTISAMPLE)
glEnable(GL_DEPTH_TEST)
glClearColor(0.,0.,0.,1.)

#Posiciona Camera
gluPerspective(45,800.0/600.0,0.1,50.0)
glTranslatef(0.0,0.0,-5)
glRotatef(45,1,1,1)

#Controle de velocidade
glutTimerFunc(50,timer,1)
glutMainLoop()

4 - Piramide Parametrizada/piramideParametrizada.py


from django.http import HttpResponse
from django.shortcuts import render
import rdflib
from rdflib import URIRef
import copy
import random
import pandas as pd
import json
import os
from DjangoTest.settings import BASE_DIR
import speech_recognition as sr

#engineDataPath = os.path.join(BASE_DIR, "polls/static/engineData/model_answer.csv")
#engineDataPath = os.path.join(BASE_DIR, "polls/static/engineData/demo.csv")
engineDataPath = os.path.join(BASE_DIR, "polls/static/engineData/wiseKB_answer.csv")

data = pd.read_csv(engineDataPath, names=['e1','r','e2','score'])

l_org = []
l = []
delSubject = None
delProperty = None
delObject = None
targetPropertyTriples = []
targetPropertyTriplesRandom = []
targetProperty = None
json_l = []

#dataPath = 'polls/static/data/display_.nt'
dataPath = 'polls/static/data/display_.nt'
dataCount = 5000

def ontologyDemo(request):
    global l_org, l, delObject, delProperty, delSubject, targetProperty, targetPropertyTriples, targetPropertyTriplesRandom
    l_org = []
    l = []
    delSubject = None
    delProperty = None
    delObject = None
    targetPropertyTriples = []
    targetPropertyTriplesRandom = []
    targetProperty = None
    return render(request, "../templates/ontologyDemo.html")

g = rdflib.Graph()

def readData(input_file):
    global l_org, l
    i = 0
    for line in input_file:
        #print(line)
        s, p, o = line.replace('\n', '').replace("concept:", '').replace("concept_", '').split('\t')
        s = s.replace(s.split('_')[0] + '_', '')
        o = o.replace(o.split('_')[0] + '_', '')
        if "latitudelongitude" not in p:
            i += 1
            l_org.append([s, p, o])
            l.append([s, p, o])
            # if i <= 200:
            #     l.append([s, p, o])

def ntDraw(data_l):
    nodes = dict()
    triples = []
    links = dict()
    node_index = 1
    link_index = 1

    for t in data_l:
        e1, r, e2 = t

        if e1 not in nodes:
            nodes[e1] = node_index
            node_index += 1

        if e2 not in nodes:
            nodes[e2] = node_index
            node_index += 1

        if r not in links:
            links[r] = link_index
            link_index += 1

        triples.append([e1, r, e2])

    myoutput = open(os.path.join(BASE_DIR, "polls/static/data/displayData.json"), 'w')

    myoutput.write('{ "nodes": [')

    i = 0
    for n in nodes:
        myoutput.write('{')
        myoutput.write('"name":"' + n + '",')
        myoutput.write('"label": "",')
        myoutput.write('"id":' + str(nodes[n]))
        myoutput.write('}')
        i += 1
        if i < len(nodes):
            myoutput.write(',')
    myoutput.write('],')

    myoutput.write('"links": [')
    i = 0
    for e1, r, e2 in triples:

        myoutput.write('{')
        source = nodes[e1]
        target = nodes[e2]
        myoutput.write('"source":"' + str(source) + '",')
        myoutput.write('"target": "' + str(target) + '",')
        myoutput.write('"type":' + '"' + r + '"')
        myoutput.write('}')
        i += 1
        if i < len(triples):
            myoutput.write(',')
    myoutput.write(']}')

    myoutput.close()

def graph(request):  ##### graph #####
    global l_org, l
    if request.method == 'POST':
        kb = request.POST.get('KB')
        content = True

        if content:
            if kb == 'nationality':
                dataPath = 'polls/static/data/display_nation.nt'
            elif kb == 'job':
                dataPath = 'polls/static/data/display_job.nt'
            elif kb == 'employer':
                dataPath = 'polls/static/data/display_emp.nt'
            filePath = os.path.join(BASE_DIR, dataPath)
            print(filePath)
            if '.nt' in filePath:
                input_file = open(filePath, 'r')
                readData(input_file)
                #ntDraw(l_org)
                if len(l_org) > dataCount:
                    ntDraw(random.sample(l_org, dataCount))
                else:
                    ntDraw(l_org)

            elif '.owl' in filePath:
                g.load(filePath)
                parsed_data = []
                filter_data = ['http://www.w3.org/2002/07/owl#NamedIndividual', 'domain', 'range',
                               'http://www.w3.org/2002/07/owl#Class', 'http://www.w3.org/2002/07/owl#Ontology',
                               'http://www.w3.org/2002/07/owl#ObjectProperty']

                for s, p, o in g:
                    x = str(s) + " " + str(p) + " " + str(o)
                    flag = False
                    for z in filter_data:
                        if z in x:
                            flag = True
                    if not flag:
                        if isinstance(s, URIRef) and isinstance(o, URIRef) and "subClassOf" in str(p):
                            parsed_data.append([s.split('#')[-1], p.split('#')[-1], o.split('#')[-1]])

                nodes = dict()
                triples = []
                links = dict()
                node_index = 1
                link_index = 1

                for t in parsed_data:
                    e1, r, e2 = t

                    if e1 not in nodes:
                        nodes[e1] = node_index
                        node_index += 1

                    if e2 not in nodes:
                        nodes[e2] = node_index
                        node_index += 1

                    if r not in links:
                        links[r] = link_index
                        link_index += 1

                    triples.append([e1, r, e2])

                myoutput = open(os.path.join(BASE_DIR, "polls/static/data/displayData.json"), 'w')

                myoutput.write('{ "nodes": [')

                i = 0

                for n in nodes:
                    myoutput.write('{')
                    myoutput.write('"name":"' + n + '",')
                    myoutput.write('"label": "",')
                    myoutput.write('"id":' + str(nodes[n]))
                    myoutput.write('}')
                    i += 1
                    if i < len(nodes):
                        myoutput.write(',')
                myoutput.write('],')

                myoutput.write('"links": [')
                i = 0

                for e1, r, e2 in triples:

                    myoutput.write('{')
                    source = nodes[e1]
                    target = nodes[e2]
                    myoutput.write('"source":"' + str(source) + '",')
                    myoutput.write('"target": "' + str(target) + '",')
                    myoutput.write('"type":' + '"' + r + '"')
                    myoutput.write('}')
                    i += 1
                    if i < len(triples):
                        myoutput.write(',')
                myoutput.write(']}')

                myoutput.close()

        return HttpResponse(content, content_type='text/html')

def exp(request):  ##### exp #####
    if request.method == 'POST':
        print("exp")

        return HttpResponse("exp", content_type='text/html')

def get_depth(depth, kb_df, target_relation):
    # depth 0 : only target relation triples
    prev_df = kb_df[kb_df.p.str.match(target_relation)]
    prev_depth_node = []
    # depth 1 ~~~~
    for i in range(depth):
        depth_node = prev_depth_node + list(prev_df.s.values) + list(prev_df.o.values)
        depth_index = []

        for row, col in kb_df.iterrows():
            if (col['s'] in depth_node or col['o'] in depth_node ) :
                depth_index.append(row)

        # get triples
        depth_df = kb_df.iloc[depth_index]

        #update for next step
        prev_depth_node = depth_node
        prev_df = depth_df

    return prev_df.values.tolist()

def depth(request):  ##### depth select #####
    global l, l_org, targetProperty
    if request.method == 'POST':
        depth = int(request.POST.get('n')) #depth 숫자 받기
        #data = pd.read_csv(dataPath, sep='\t', names=['s', 'p', 'o'])
        data = []
        for line in open(dataPath, 'r'):
            s, p, o = line.replace('\n', '').replace("concept:", '').replace("concept_", '').split('\t')
            data.append([s, p, o])
        data = pd.DataFrame(data)
        data.columns = ['s', 'p', 'o']

        #print(data[0:1])
        for i in range(len(data)):
            data['s'][i] = data['s'][i].replace(data['s'][i].split('_')[0] + '_', '')
            data['o'][i] = data['o'][i].replace(data['o'][i].split('_')[0] + '_', '')

        l = get_depth(depth, data, targetProperty)
        ntDraw(l)
        result = [depth,len(l)]

        return HttpResponse(json.dumps(result), content_type='application/json')

def targetProperty(request):  ##### targetProperty select #####
    global l_org, l, targetPropertyTriples, targetPropertyTriplesRandom, targetProperty
    if request.method == 'POST':
        targetProperty = request.POST.get('targetProperty')
        del targetPropertyTriples[:]
        del targetPropertyTriplesRandom[:]

        for i in range(len(l_org)):
            if targetProperty == l_org[i][1]:
                targetPropertyTriples.append(l_org[i])
                flag = True

        l = targetPropertyTriples

        ntDraw(l)
        return HttpResponse(json.dumps(l), content_type='text/html')

def delTriple(request):  ##### Triple Delete & reDraw #####
    global l_org, l, delSubject, delProperty, delObject
    if request.method == 'POST':
        delSubject = request.POST.get('subject')
        delProperty = request.POST.get('property')
        delObject = request.POST.get('object')
        flag = False

        for i in range(len(l)):
            if l[i][0] == delSubject and l[i][2] == delObject:
                flag = True
                del l[i]
                break
        if flag:
            ntDraw(l)
        return HttpResponse(flag, content_type='text/html')

def runEngine(request):  #####  #####
    global l_org, l, data, delSubject, delProperty, delObject
    if request.method == 'POST':
        runData = data[data['e1'].str.contains(delSubject) & data['e2'].str.contains(delObject)].sort_values('score', ascending=False)
        #print("=========")
        print(runData)
        lst = []
        for i in range(len(runData)):
            tmp = []
            score = runData.values[i][3]
            e1 = runData.values[i][0]
            r = runData.values[i][1]
            e2 = runData.values[i][2]
            tmp.append(e1)
            tmp.append(r)
            tmp.append(e2)
            tmp.append(score)
            lst.append(tmp)

        l.append([lst[0][0].replace(lst[0][0].split('_')[0] + '_', ''), lst[0][1], lst[0][2].replace(lst[0][2].split('_')[0] + '_', '')])
        ntDraw(l)

        return HttpResponse(json.dumps(lst), content_type='application/json')

def objectReasoning(request):  ##### nell #####
    global l_org, l, data, delSubject, delProperty, delObject
    if request.method == 'POST':
        object = request.POST.get('object')
        threshold = request.POST.get('threshold')
        threshold = float(threshold)
        print(type(threshold),threshold)
        runData = data[data['e2'].str.contains(object)].sort_values('score', ascending=False)
        lst = []
        json_l = []
        tmp_l = copy.deepcopy(l)
        l = []
        for i in range(len(tmp_l)):
            if tmp_l[i][2] != object:
                l.append(tmp_l[i])
        for i in range(len(runData)):
            tmp = []
            score = runData.values[i][-1]
            e1 = runData.values[i][0]
            r = runData.values[i][1]
            e2 = runData.values[i][2]
            tmp.append(e1)
            tmp.append(r)
            tmp.append(e2)
            tmp.append(score)
            lst.append(tmp)

        # print(lst)
        for i in range(len(lst)):
            if lst[i][3] > threshold:
                l.append([lst[i][0].replace(lst[i][0].split('_')[0] + '_', ''), lst[i][1].replace('concept:', ''),
                          lst[i][2].replace(lst[i][2].split('_')[0] + '_', '')])
                json_l.append([lst[i][0].replace(lst[i][0].split('_')[0] + '_', ''), lst[i][1].replace('concept:', ''),
                               lst[i][2].replace(lst[i][2].split('_')[0] + '_', ''), lst[i][3]])
        print("=======")
        print(len(l))
        print(l)
        ntDraw(l)

        return HttpResponse(json.dumps(json_l), content_type='application/json')

def relationReasoning(request):  ##### nell #####
    global l_org, l, data, delSubject, delProperty, delObject, json_l
    if request.method == 'POST':
        relation = request.POST.get('relation')
        threshold = request.POST.get('threshold')
        threshold = float(threshold)
        print(type(threshold),threshold)
        runData = data[data['r'].str.contains(relation)].sort_values('score', ascending=False)
        lst = []
        result_l = []
        for i in range(len(runData)):
            tmp = []
            score = runData.values[i][-1]
            e1 = runData.values[i][0]
            r = runData.values[i][1]
            e2 = runData.values[i][2]
            tmp.append(e1)
            tmp.append(r)
            tmp.append(e2)
            tmp.append(score)
            lst.append(tmp)

        print('lstlen:',len(lst))
        print(lst)
        #l = []
        print('llen:',len(l))
        for i in range(len(lst)):
            if lst[i][3] > threshold:
                l.append([lst[i][0].replace(lst[i][0].split('_')[0] + '_', ''), lst[i][1].replace('concept:', ''),
                          lst[i][2].replace(lst[i][2].split('_')[0] + '_', '')])
                json_l.append([lst[i][0].replace(lst[i][0].split('_')[0] + '_', ''), lst[i][1].replace('concept:', ''),
                               lst[i][2].replace(lst[i][2].split('_')[0] + '_', '')])
                result_l.append([lst[i][0].replace(lst[i][0].split('_')[0] + '_', ''), lst[i][1].replace('concept:', ''),
                                 lst[i][2].replace(lst[i][2].split('_')[0] + '_', ''), lst[i][3]])
        print("=======")
        print(len(l))
        print(l)
        ntDraw(l)

        return HttpResponse(json.dumps(result_l), content_type='application/json')

def Reasoning(request):  ##### nell #####
    global l_org, l, data, delSubject, delProperty, delObject, json_l
    if request.method == 'POST':
        relation = request.POST.get('relation')
        threshold = request.POST.get('threshold')
        threshold = float(threshold)
        print(type(threshold),threshold)
        runData = data[data['r'].str.contains(relation)].sort_values('score', ascending=False)

        l_df = pd.DataFrame(l)
        l_df.columns = ['e1', 'r', 'e2']
        e1_lst = l_df.e1.values.tolist()
        e2_lst = l_df.e2.values.tolist()
        e = e1_lst + e2_lst
        e = list(set(e))
        reasoningData = runData[(runData.e1.isin(e)) & (runData.score >= threshold)]
        reasoningData_lst = reasoningData.values.tolist()
        reasoningData_lst2 = runData[runData.e1.isin(e)].values.tolist()
        withScore = []
        for i in range(len(reasoningData_lst)):
            if [reasoningData_lst[i][0], reasoningData_lst[i][1], reasoningData_lst[i][2]] not in l:
                l.append([reasoningData_lst[i][0], reasoningData_lst[i][1], reasoningData_lst[i][2]])
        ntDraw(l)
        return HttpResponse(json.dumps(reasoningData_lst2), content_type='application/json')

def movieReasoning_org(request):  ##### nell #####
    global l_org, l, data, delSubject, delProperty, delObject, json_l
    if request.method == 'POST':
        relation = request.POST.get('relation')
        threshold = request.POST.get('threshold')
        threshold = float(threshold)
        print(type(threshold),threshold)
        runData = data[data['r'].str.contains(relation)].sort_values('score', ascending=False)
        lst = runData.values.tolist()

        def processing(tmp):
            if len(tmp) == 3:
                return [tmp[0].replace(tmp[0].split('_')[0] + '_', ''), tmp[1],
                        tmp[2].replace(tmp[2].split('_')[0] + '_', '')]
            else:
                return [tmp[0].replace(tmp[0].split('_')[0] + '_', ''), tmp[1],
                        tmp[2].replace(tmp[2].split('_')[0] + '_', ''), tmp[3]]
        print('test1')
        withScore = []
        for i in range(len(l_org)):
            for j in range(len(lst)):
                if l_org[i][1] in lst[j][1]:
                    if l_org[i][0] in lst[j][0] or l_org[i][2] in lst[j][2]:
                        if processing([lst[j][0], lst[j][1], lst[j][2]]) not in l:
                            withScore.append(processing([lst[j][0], lst[j][1], lst[j][2], round(lst[j][3], 3)]))
                            if lst[j][-1] >= threshold:
                                l.append(processing([lst[j][0], lst[j][1], lst[j][2]]))
                    if l_org[i][2] in lst[j][0] or l_org[i][0] in lst[j][2]:
                        if processing([lst[j][0],lst[j][1],lst[j][2]]) not in l:
                            withScore.append(processing([lst[j][0], lst[j][1], lst[j][2], round(lst[j][3], 3)]))
                            if lst[j][-1] >= threshold:
                                l.append(processing([lst[j][0], lst[j][1], lst[j][2]]))
        print('test2')
        ntDraw(l)
        print('test3')
        return HttpResponse(json.dumps(withScore), content_type='application/json')

def inferredOnly(request):  #####  #####
    global l_org, l, data, delSubject, delProperty, delObject, json_l
    if request.method == 'POST':
        count = request.POST.get('count')
        if int(count)%2==0:
            ntDraw(l)
        else:
            ntDraw(json_l)

        return HttpResponse(json.dumps(json_l), content_type='application/json')

def listen():
    print('11')
    r = sr.Recognizer()
    print('22')
    mic = sr.Microphone()
    print('33')
    with mic as source:
        print('44')
        audio = r.listen(source)
    print('55')
    text = r.recognize_google(audio, language = 'ko-KR')
    print('인식한 문장: ',text)
    return text

def speak(x):
    os.system('say '+str(x))

def processing(t, rel):
    subjectDf = data[data.e1.str.contains(t) & data.r.str.contains(rel)]
    df = subjectDf[subjectDf.score == subjectDf.score.max()]
    if rel == 'nationality':
        text = df.e1.values[0] + '의 국적은 ' + df.e2.values[0] + ' 입니다.'
    elif rel == 'employer':
        text = df.e1.values[0] + '의 소속 팀은 ' + df.e2.values[0] + ' 입니다.'
    elif rel == 'job':
        text = df.e1.values[0] + '의 직업은 ' + df.e2.values[0] + ' 입니다.'
    return text

def stt(request):  #####  #####
    global  data
    if request.method == 'POST':
        name = request.POST.get('name')
        rel = request.POST.get('rel')
        print('aaaaaaaaaa')
        #text = listen()
        #print('1')
        text = processing(name, rel)
        #print('2')
        print(text)
        print(rel)
        speak(text)
        return HttpResponse(json.dumps([str(text)]), content_type='application/json')


import pprint

__author__ = 'ronanbrady2'

# Connects to 'udwrangle' DB and returns DB object
def connectDB() :
    from pymongo import MongoClient
    client = MongoClient("mongodb://localhost:27017")
    db = client.udwrangle
    return db

if __name__ == "__main__":
    db = connectDB()

    mapping = { "Ave": "Avenue",
            "Avevnue": "Avenue",
            "Cente": "Centre",
            "Center": "Centre",
            "Center": "Centre",
            "Donghmede": "Donaghmede",
            "Nouth": "North",
            "Roafd": "Road",
            "Sreet": "Street",
            "St": "Street",
            "street": "Street",
            "heights": "Heights",
            "lane": "Lane",
            "park": "Park",
            "place": "Place",
            "road": "Road"
            }

    correctedcount = 0
    for misspelling, correction in mapping.items():
        pprint.pprint(misspelling)
        regex = "\w " + misspelling + "$"
        results = db.openmap.find({"address.street" : { "$regex" : regex}})
        for obj in results:
            street = obj["address"]["street"]
            print(street)

            correctedstreet = street.replace(misspelling, correction)
            obj["address"]["street"] = correctedstreet
            print(correctedstreet)
            correctedcount += 1
            db.openmap.save(obj)
    print("total corrected: {0}".format(correctedcount))


#!/usr/bin/env python3

## perform alignment for word error rate and similar DTW problems.
## Inspired by https://martin-thoma.com/word-error-rate-calculation/

from builtins import zip
from builtins import str
from builtins import range
from builtins import object

import numpy
import random

# initialisation
class WER(object):
    def __init__(self, ref, hyp, subcost=4, inscost=3, delcost=3):
        cost = numpy.zeros((len(ref) + 1, len(hyp) + 1), dtype=numpy.uint16)
        dec = numpy.zeros((len(ref) + 1, len(hyp) + 1), dtype=numpy.uint8) ## 0123: csid, decision
        for i in range(1, len(ref)+1):
            cost[i, 0] = delcost * i
            dec[i, 0] = 3 ## del
        for j in range(1, len(hyp)+1):
            cost[0, j] = inscost * j
            dec[0, j] = 2 ## ins

        # computation
        for i in range(1, len(ref)+1):
            for j in range(1, len(hyp)+1):
                if ref[i-1] == hyp[j-1]:
                    cost[i, j] = cost[i-1, j-1]
                else:
                    sid = [cost[i-1, j-1] + subcost, cost[i, j-1] + inscost, cost[i-1, j] + delcost]
                    mini = numpy.argmin(sid)
                    cost[i, j] = sid[mini]
                    dec[i, j] = mini + 1

        ## backtrack
        self.refeval = []
        self.hypeval = []
        i, j = len(ref), len(hyp)
        self.nsub = self.nins = self.ndel = 0
        while i > 0 or j > 0:
            if dec[i, j] < 2: ## correct or subsitution
                d = "cs"[dec[i, j]]
                self.nsub += dec[i, j]
                i -= 1
                j -= 1
                self.refeval.insert(0, d)
                self.hypeval.insert(0, d)
            elif dec[i, j] == 2: ## insertion
                j -= 1
                self.hypeval.insert(0, "i")
                self.nins += 1
            else: ## deletion
                i -= 1
                self.refeval.insert(0, "d")
                self.ndel += 1
        self.nerr = self.nsub + self.nins + self.ndel

        self.ref = ref[:]
        self.hyp = hyp[:]

    def wer(self):
        return float(self.nerr) / len(self.ref)

    def align(self):
        refali = []
        hypali = []
        evalali = []
        ri = hi = 0
        while ri < len(self.refeval) or hi < len(self.hypeval):
            if ri < len(self.refeval) and self.refeval[ri] == 'd':
                hypali.append(None)
                refali.append(ri)
                evalali.append('d')
                ri += 1
            elif hi < len(self.hypeval) and self.hypeval[hi] == 'i':
                refali.append(None)
                hypali.append(hi)
                evalali.append('i')
                hi += 1
            else:
                refali.append(ri)
                hypali.append(hi)
                evalali.append(self.refeval[ri])
                ri += 1
                hi += 1
        return refali, hypali, evalali

    def pralign(self, inssym="*", delsym="*"):
        refali, hypali, evalali = self.align()
        prref = [str(self.ref[i]) if i is not None else delsym * len(str(self.hyp[j])) for i, j in zip(refali, hypali)]
        prhyp = [str(self.hyp[j]) if j is not None else inssym * len(str(self.ref[i])) for i, j in zip(refali, hypali)]
        return prref, prhyp, evalali

    def __repr__(self):
        ref, hyp, eva = self.pralign()
        lengths = [max(len(str(r)), len(str(h)), len(str(e))) for r, h, e in zip(ref, hyp, eva)]
        out = f"REF: {' '.join(('%%%ds' % l) % str(s) for l, s in zip(lengths, ref))}\n" + \
            f"HYP: {' '.join(('%%%ds' % l) % str(s) for l, s in zip(lengths, hyp))}\n" + \
            f"Eval {' '.join(('%%%ds' % l) % str(s) for l, s in zip(lengths, eva))}\n"
        return out

    @property
    def nref(self):
        return len(self.ref)

    @property
    def nhyp(self):
        return len(self.hyp)

class WERs:
    def __init__(self, subcost=4, inscost=3, delcost=3):
        self.subcost = subcost
        self.inscost = inscost
        self.delcost = delcost
        self.wers = list()
        for attr in ['nref', 'nhyp', 'ndel', 'nins', 'nsub', 'nerr']:
            setattr(self, attr, 0)

    def append(self, ref, hyp):
        wer = WER(ref, hyp, self.subcost, self.inscost, self.delcost)
        self.wers.append(wer)
        for attr in ['nref', 'nhyp', 'ndel', 'nins', 'nsub', 'nerr']:
            setattr(self, attr, getattr(self, attr) + getattr(wer, attr))

    def wer(self):
        return self.nerr / self.nref

    def align(self):
        return [wer.align() for wer in self.wers]

    @property
    def nutt(self):
        return len(self.wers)

def gentest(n=1000, v=100, p=0.25):
    ref = [random.randrange(v) for i in range(n)]
    hyp = ref[:]
    for i in range(int(p*n)):
        hyp[random.randrange(len(hyp))] = random.randrange(v) ## sub
        hyp.insert(random.randrange(len(hyp)), random.randrange(v)) ## insd
        hyp.pop(random.randrange(len(hyp))) ## del
    return ref, hyp


from http.client import ImproperConnectionState
from operator import imod
from pkgutil import ImpImporter
import re
from django.shortcuts import render
from .models import Book, Borrower
from . import forms
from . import models
from .forms  import NewBook_form
from django.core import validators
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.views.generic import View,TemplateView,ListView,DetailView,FormView
# Create your views here.

def landing(request):
    mydect2 = {'name2':'This is a landing page !!  '}
    return render(request, "landing_page.html",context=mydect2)

class IndexView(TemplateView):
    template_name = "landing_page.html"

class BooksListView(ListView):
    paginate_by = 5
    model = Book
#    template_name = 'books/bookslist.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
#        context['book'] = Book.objects.all()
        return context

class BooksDetailView(DetailView):
    context_object_name='book_detail'
    model = Book
    template_name = 'books/book_detail.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['boook'] = Book.objects.all()
        return context

def books(request):

    books_view = Book.objects.all()
    books_dict= {'book_key': books_view}
    return render(request, "books/books_page.html",context=books_dict)

#def form_name_view(request):
#    form = forms.Register_a_book()
#    return render(request, "books/register_a_book.html",context={'form':form})

def form_name_view(request):
    form = forms.NewBook_form()
    if request.method == "POST":
        form = NewBook_form(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return books(request)
        else:

            print(form.errors)

    return render(request, "books/register_a_book.html",context={'form':form})


from model.android import *
import numpy as np
import torch
from utils import IS_DEBUG
from torch.utils.data import DataLoader,Dataset
import torch.nn as nn
import torch.optim as optim
import os

my_device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")

def malware_loader(x, y):
    res = torch.cat((y.float(), x), dim = 1)
    data_loader = DataLoader(
        res, batch_size=2048,
        shuffle=True, collate_fn=my_collate_fn,
    )
    return data_loader

def my_collate_fn(batch):
    x = [data[1:] for data in batch]
    y = [data[:1].long() for data in batch]
    return torch.stack(x, dim=0), torch.stack(y, dim = 0)

def train_model():
    x = np.load('data/android_malware/x_train.npy')
    y = np.load('data/android_malware/y_train.npy')
    if IS_DEBUG:
        x = x[:1000, :500]
        y = y[:1000]
    data_num, feature_num = np.shape(x)
    print(data_num, feature_num)
    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.long)
    train_loader = malware_loader(x, y)
    model = Android_Poor(feature_num).to(my_device)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=0.01)
    model.train()
    for epoch in range(5):
        sum_loss = 0
        for i, (x, y) in enumerate(train_loader):
            print(i)
            optimizer.zero_grad()
            x = x.to(my_device)
            y = y.to(my_device).view([-1])
            pred_y = model(x)
            loss = criterion(pred_y, y)
            loss.backward()
            optimizer.step()
            sum_loss += loss
        print('loss is ', sum_loss/data_num)

    model.eval()

    x = np.load('data/android_malware/x_test.npy')
    y = np.load('data/android_malware/y_test.npy')
    if IS_DEBUG:
        x = x[:1000, :500]
        y = y[:1000]
    data_num, feature_num = np.shape(x)
    print(data_num, feature_num)
    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.long)
    train_loader = malware_loader(x, y)
    y_list, pred_list = [], []
    for (x, y) in train_loader:
        optimizer.zero_grad()
        x = x.to(my_device)
        y = y.to(my_device)
        pred_y = model(x)
        _, pred_y = torch.max(pred_y, dim=1)
        y_list.append(y)
        pred_list.append(pred_y)
    pred_list = torch.cat(pred_list, dim=0).detach().cpu().numpy().reshape([-1])
    y_list = torch.cat(y_list, dim=0).detach().cpu().numpy().reshape([-1])
    print(np.sum(pred_list == y_list)/data_num)
    path = './model_weight/android/'
    if not os.path.isdir(path):
        os.mkdir(path)
    torch.save(model.state_dict(), path + model.__class__.__name__ + '.h5')
    print('save model !', path + model.__class__.__name__ + '.h5')

if __name__ == '__main__':
    train_model()


import importlib.util
import multiprocessing

from darcyai.perceptor.coral.coral_perceptor_base import CoralPerceptorBase
from darcyai.perceptor.processor import Processor
from darcyai.utils import validate, validate_not_none, validate_type

_supported_processors = None

def get_supported_processors() -> dict:
    """
    Gets the list of supported processors.

    # Returns
    `dict` - The list of supported processors.

    # Examples
    ```python
    >>> get_supported_processors()
    {<Processor.CORAL_EDGE_TPU: 1>: {'is_available': True, 'coral_tpus': [{'type': 'usb', 'path':\
    '/sys/bus/usb/devices/2-2'}]}, <Processor.CPU: 2>: {'is_available': True, 'cpu_count': 4}}
    ```
    """
    global _supported_processors
    if _supported_processors is None:
        coral_tpus = CoralPerceptorBase.list_edge_tpus()
        _supported_processors = {
            Processor.CORAL_EDGE_TPU: {
                "is_available": len(coral_tpus) > 0,
                "coral_tpus": coral_tpus,
            },
            Processor.CPU: {
                "is_available": importlib.util.find_spec("tensorflow") is not None \
                    or importlib.util.find_spec("tflite_runtime") is not None,
                "cpu_count": multiprocessing.cpu_count(),
            },
        }

    return _supported_processors

def get_perceptor_processor(processor_preference:list) -> Processor:
    """
    Gets the processor for the perceptor.

    # Arguments
    processor_preference (list): The list of preferred processors.

    # Returns
    `Processor` - The processor for the perceptor.

    # Examples
    ```python
    >>> from darcyai.perceptor.processor import Processor
    >>> processor_preference = [Processor.CORAL_EDGE_TPU, Processor.CPU]
    >>> get_perceptor_processor(processor_preference)
    Processor.CORAL_EDGE_TPU
    ```
    """
    validate_processor_list(processor_preference)

    supported_processors = get_supported_processors()
    for processor in processor_preference:
        validate(processor in supported_processors,
                    f"processor {processor} is not valid")
        if supported_processors[processor]["is_available"]:
            return processor

    return None

def validate_processor_preference(processor_preference, model_path_required=True):
    """
    Validate the processor preference.

    # Arguments
    processor_preference: A dictionary of processor preference.
        The key is the processor. Values are dictionaries of model paths and labels.

    # Raises
    Exception: If the processor preference is invalid.

    # Examples
    ```python
    >>> from darcyai.perceptor.processor import Processor
    >>> processor_preference = {
            Processor.CORAL_EDGE_TPU: {
                "model_path": "/path/to/model.tflite",
                "labels_file": "/path/to/labels.txt",
            },
            Processor.CPU: {
                "model_path": "/path/to/model.tflite",
                "labels_file": "/path/to/labels.txt",
            },
        }
    >>> validate_processor_preference(processor_preference)
    ```
    """
    validate_not_none(processor_preference, "processor_preference is required.")
    validate_type(processor_preference, dict, "processor_preference must be a dictionary.")
    validate(len(processor_preference) > 0, "processor_preference must not be empty.")

    for processor, processor_preference_dict in processor_preference.items():
        validate_type(processor, Processor, "processor must be a Processor.")
        validate_type(processor_preference_dict,
                      dict,
                      "processor_preference_dict must be a dictionary.")

        if model_path_required:
            if "model_path" not in processor_preference_dict:
                raise Exception("The model path is required for the processor preference")
            else:
                validate_type(
                    processor_preference_dict["model_path"], str, "model_path must be a string.")

        if "labels" in processor_preference_dict:
            validate_type(
                processor_preference_dict["labels"], dict, "labels must be a dictionary.")

        if "labels_file" in processor_preference_dict:
            validate_type(
                processor_preference_dict["labels_file"], str, "labels_file must be a string.")

def validate_processor_list(processor_list):
    """
    Validate the processor list.

    # Arguments
    processor_list: A list of processors.

    # Raises
    Exception: If the processor preference is invalid.

    # Examples
    ```python
    >>> from darcyai.perceptor.processor import Processor
    >>> processor_list = [Processor.CORAL_EDGE_TPU, Processor.CPU]
    >>> validate_processor_list(processor_list)
    ```
    """
    validate_not_none(processor_list, "processor_list is required.")
    validate_type(processor_list, list, "processor_list must be a list.")
    validate(len(processor_list) > 0, "processor_list must not be empty.")

    for processor in processor_list:
        validate_type(processor, Processor, "processor must be a Processor.")


from selenium import webdriver
import os

options = webdriver.ChromeOptions()
options.add_argument("-headless")
drivePath = os.getenv("HOME") + "/Downloads/chromedriver"
browser = webdriver.Chrome(executable_path=drivePath, options=options)
url = "https://www.csie.ncnu.edu.tw/member/professor"

browser.get(url)
infos = browser.find_elements_by_class_name("personcard_wrap ")
for info in infos:
    tmp = info.text.split()
    print(tmp[0], tmp[3])
browser.close()


#!/usr/bin/python3
import math

class buildMaterial():
    """ Базовый класс строительных материалов"""

    def __init__(self, cost):
        self.cost = cost

class wallPapers(buildMaterial):
    """Класс обои, стоимость / ширина / длинна"""

    n = 'Обои'
    def __init__(self, cost, width, long):
        super().__init__(cost)
        self.w = width
        self.l = long
        self.area = self.w * self.l

class paint(buildMaterial):
    """Класс краска, стоимость / вес / расход"""

    n = 'Краска'
    def __init__(self, cost, weight, decreases):
        super().__init__(cost)
        self.w = weight
        self.d = decreases
        self.area = self.w / self.d

class laminate(buildMaterial):
    """Класс ламинат, стоимость / длинна / ширина / количество"""

    n = 'Ламинат'
    def __init__(self, cost, long, width, count):
        super().__init__(cost)
        self.l = long
        self.w = width
        self.c = count
        self.area = self.w * self.c * self.l

class room():
    """Класс комната, наименование / ширина / высота / длинна / ширна окна / ширина двери"""

    def __init__(self, name, width, height, long, windowWidht, doorWidth):
        self.n = name
        self.w = width
        self.h = height
        self.l = long
        self.wW = windowWidht
        self.dW = doorWidth
        self.wallArea = (self.w * 2 + self.l * 2 - self.wW - self.dW) * self.h
        self.ceilingFloorArea = (self.w * self.l)

    def doIt(self, object):
        self.area = self.wallArea if object.n == 'Обои' else self.ceilingFloorArea
        if object.area != 0:
            count = math.ceil(self.area / object.area)
        else:
            count = 0
        cost = count * object.cost
        return (object.n, object.cost, count, cost)

    def smeta (self, *objects):
        summ = 0
        print(('[%s: ширина: %.2f м, длинна:%.2f м, высота: %.2f м]')%(self.n ,self.w, self.l, self.h))
        for object in objects:
            itogCost = self.doIt(object)
            print(('%s \t\t %.2fx%.2f=%.2f \t руб.')%(itogCost))
            summ += itogCost[3]
        return summ

class apartment():
    """Класс квартира, комнаты"""

    def __init__(self, *rooms):
        self.room = list(rooms)

    def smeta(self, *object):
        summ = 0
        for room in self.room:
            summ += room.smeta(*object)
        print('_________________________________')
        print('Итого: \t\t\t\t %.2f руб '%(summ))

    def addRoom(self,room):
       self.room.append(room)

    def delRoom(self,room):
        self.room.remove(room)


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

###################
#    This package implements tools to build python package and tools.
#    Copyright (C) 2021, 2022, 2023  Maurice Lambert

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
###################

r"""
This package implements tools to build python package and tools.

>>> from PrintF import printf, ProgressBar
>>> printf("It's working !")
[+] It's working !
>>> printf("Is not working...", state="NOK")
[-] Is not working...
>>> printf("Oh no ! An exception is raised...", state="ERROR")
[!] Oh no ! An exception is raised...
>>> printf("It's running !", end="\n\n", state="INFO")
[*] It's running !

>>> printf("Please press enter to start the program...", start="\n", state="TODO")

[#] Please press enter to start the program...
>>> printf("Do you want to continue ?", pourcent=20, state="ASK"); print()
[?] Do you want to continue ?
[?] 20% |████                |
>>> printf("Step 1 OK", pourcent=20); printf("Step 2 OK", pourcent=55); print()
[+] Step 1 OK
[+] Step 2 OK
[+] 55% |███████████         |
>>> printf("Step 1 OK", pourcent=20); print(); printf("Step 2 OK", pourcent=55); print()
[+] Step 1 OK
[+] 20% |████                |
[+] Step 2 OK
[+] 55% |███████████         |
>>> printf("Step 2 OK", add_progressbar=False, pourcent=55); print()
[+] Step 2 OK
[+] 55%
>>> printf("Step 2 OK", oneline_progress=True, add_progressbar=False, pourcent=55); print()
[+] Step 2 OK 55%
>>> printf("Step 2 OK", oneline_progress=True, pourcent=55); print()
[+] Step 2 OK 55% |███████████         |
>>> import PrintF
>>> PrintF.STATES["TEST"] = ("[T]", "\x1b[37m")
>>> printf("Testing a custom state...", state="TEST")
[T] Testing a custom state...
>>> printf("Unknown", state="unknown")
[ ] Unknown
>>> custom_progress = ProgressBar("[", "]", "#", "-", 30)
>>> printf("Step 1 OK", progressbar=custom_progress, pourcent=20); print()
[+] Step 1 OK
[+] 20% [######------------------------]
>>>
"""

__version__ = "1.0.1"
__author__ = "Maurice Lambert"
__author_email__ = "mauricelambert434@gmail.com"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "mauricelambert434@gmail.com"
__description__ = """
This package implements tools to build python package and tools.
"""
license = "GPL-3.0 License"
__url__ = "https://github.com/mauricelambert/PythonToolsKit"

copyright = """
PythonToolsKit  Copyright (C) 2021, 2022, 2023  Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
"""
__license__ = license
__copyright__ = copyright

__all__ = ["printf", "STATES"]

from sys import argv
from typing import Union
from platform import system
from dataclasses import dataclass

@dataclass
class ProgressBar:
    start: str = "|"
    end: str = "|"
    character: str = "\u2588"
    empty: str = " "
    size: int = 20

STATES = {
    "OK": ("[+]", "\x1b[32m"),
    "NOK": ("[-]", "\x1b[33m"),
    "ERROR": ("[!]", "\x1b[31m"),
    "INFO": ("[*]", "\x1b[34m"),
    "TODO": ("[#]", "\x1b[35m"),
    "ASK": ("[?]", "\x1b[36m"),
}

states_get = STATES.get

def printf(
    string: str,
    state: str = "OK",
    pourcent: int = None,
    start: str = "",
    end: str = "\n",
    progressbar: ProgressBar = ProgressBar,
    add_progressbar: bool = True,
    oneline_progress: bool = False,
    **kwargs,
) -> None:
    """
    This function prints formatted and colored information and progression.
    """

    show, color = states_get(state) or ("[ ]", "\x1b[39m")

    progress_bar = ""
    has_pourcent = pourcent is not None

    if has_pourcent and add_progressbar:
        max_size = progressbar.size
        char_size = 100 / max_size
        pourcent_size = round(pourcent / char_size)
        if pourcent_size > max_size:
            pourcent_size = max_size
        progress_bar = (
            progressbar.start
            + (progressbar.character * pourcent_size)
            + (progressbar.empty * (max_size - pourcent_size))
            + progressbar.end
        )

    if has_pourcent:
        progress_bar = f"{pourcent}% {progress_bar}\x1b[39m{end}\x1b[F"
        if not oneline_progress:
            progress_bar = f"{color}{show} {progress_bar}"

    if oneline_progress:
        to_print = f"\x1b[K{start}{color}{show} {string} {progress_bar}"
    else:
        to_print = (
            f"\x1b[K{start}{color}{show} {string}\x1b[39m{end}"
            f"{progress_bar}"
        )

    print(to_print, flush=True, end="", **kwargs)


_build_file_template = """
load("@bsp4bazel-rules//private:wrap_executable.bzl", "wrap_executable")

wrap_executable(
    name = "{name}",
    executable_path = "{executable_path}",
    visibility = ["//visibility:public"],
)
"""

def _load_tool_impl(ctx):
    """Implementation of the load_tool rule."""

    if not ctx.attr.sha256:
        fail("Must specify a sha256")

    all_urls = []
    if ctx.attr.urls:
        all_urls = ctx.attr.urls

    ctx.file("WORKSPACE", "workspace(name = \"{name}\")\n".format(name = ctx.name))
    ctx.file("BUILD.bazel", _build_file_template.format(name = "bin", executable_path = ctx.attr.binary_path))

    download_info = None
    if ctx.attr.packaged:
        download_info = ctx.download_and_extract(
            url = all_urls,
            sha256 = ctx.attr.sha256,
            stripPrefix = ctx.attr.strip_prefix,
        )
    else:
        download_info = ctx.download(
            url = all_urls,
            output = ctx.attr.binary_path,
            sha256 = ctx.attr.sha256,
            executable = True,
        )

_load_tool_attrs = {
    "urls": attr.string_list(
        doc =
            """A list of URLs to a file that will be made available to Bazel.
Each entry must be a file, http or https URL. Redirections are followed.
URLs are tried in order until one succeeds, so you should list local mirrors first.
If all downloads fail, the rule will fail.""",
    ),
    "sha256": attr.string(
        doc = """The expected SHA-256 of the file downloaded.""",
    ),
    "packaged": attr.bool(default = True),
    "strip_prefix": attr.string(),
    "binary_path": attr.string(),
}

load_tool = repository_rule(
    implementation = _load_tool_impl,
    attrs = _load_tool_attrs,
)


#!/usr/bin/python
# -*- coding: utf8 -*-

import argparse
import os
import fnmatch
import re
import shutil

# PARSER

DEFAULT_MODE = 1
DEFAULT_WRAP = "__"

parser = argparse.ArgumentParser(
    description='Rename/remove files according to environment code before deploy'
)
parser.add_argument(
    '-e',
    '--environment',
    required=True,
    help='Selected environment'
)
parser.add_argument(
    '-p',
    '--path',
    required=True,
    help='Project root path'
)
parser.add_argument(
    '-w',
    '--wrap',
    type=str,
    nargs='?',
    const=DEFAULT_WRAP,
    default=DEFAULT_WRAP,
    help='Character wrapping environment name'
)
parser.add_argument(
    '-m',
    '--mode',
    type=int,
    nargs='?',
    const=DEFAULT_MODE,
    default=DEFAULT_MODE,
    help='Location of environment code in filename. 0 is before, 1 is just before the extension and 2 is at the end.'
)
parser.add_argument(
    '-v',
    '--verbose',
    action='store_true',
    help='Print what has been renamed or deleted'
)
args = parser.parse_args()

# Generate codes from wrap character and environment name
CODE_ENV = "{}{}{}".format(
    args.wrap,
    args.environment,
    args.wrap
)
CODE_WILDCARD_ENV = "{}{}{}".format(
    args.wrap,
    "*",
    args.wrap
)

# FUNCTIONS

def lremove(pattern, string):
    """
    Remove 'pattern' in 'string' if 'pattern' starts 'string'.
    """
    return re.sub('^%s' % pattern, '', string)

def rremove(pattern, string):
    """
    Remove 'pattern' in 'string' if 'pattern' ends 'string'.
    """
    return re.sub('%s$' % pattern, '', string)

def deletePath(path):
    """
    Same function to remove either file or folder
    """
    if os.path.exists(path):
        try:
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
        except:
            pass

def doBeginning(path, file_path, file_name, file_extension):
    """
    Rename or delete files when environment code is at the
    beginning of the file name
    """
    if fnmatch.fnmatch(file_name, CODE_ENV + "*"):
        new_file_name = lremove(CODE_ENV, file_name)
        new_path = file_path + "/" + new_file_name + file_extension
        if args.verbose:
            print("rename {} to {}".format(path, new_path))
        deletePath(new_path)
        os.rename(path, new_path)
    elif fnmatch.fnmatch(file_name, CODE_WILDCARD_ENV + "*"):
        if args.verbose:
            print("delete {}".format(path))
        deletePath(path)

def doBeforeExtension(path, file_path, file_name, file_extension):
    """
    Rename or delete files when environment code is before
    extension in file name
    """
    if fnmatch.fnmatch(file_name, "*" + CODE_ENV):
        new_file_name = rremove(CODE_ENV, file_name)
        new_path = file_path + "/" + new_file_name + file_extension
        if args.verbose:
            print("rename {} to {}".format(path, new_path))
        deletePath(new_path)
        os.rename(path, new_path)
    elif fnmatch.fnmatch(file_name, "*" + CODE_WILDCARD_ENV):
        if args.verbose:
            print("delete {}".format(path))
        deletePath(path)

def doEnd(path, file_path, file_name, file_extension):
    """
    Rename or delete files when environment code is at the
    end of file name
    """
    file_name_extension = file_name + file_extension
    if fnmatch.fnmatch(file_name_extension, "*" + CODE_ENV):
        new_file_name = rremove(CODE_ENV, file_name_extension)
        new_path = file_path + "/" + new_file_name
        if args.verbose:
            print("rename {} to {}".format(path, new_path))
        deletePath(new_path)
        os.rename(path, new_path)
    elif fnmatch.fnmatch(file_name_extension, "*" + CODE_WILDCARD_ENV):
        if args.verbose:
            print("delete {}".format(path))
        deletePath(path)

def processPath(path):
    """
    Process path and choose the good method
    """
    file_path = path.rpartition("/")[0]
    file_name = path.rpartition("/")[2]
    file_extension = ""
    if "." in file_name:
        file_extension = file_name.rpartition(".")[1] + file_name.rpartition(".")[2]
    file_name = rremove(file_extension, file_name)

    # Code at the beginning of the filename
    if args.mode == 0:
        doBeginning(path, file_path, file_name, file_extension)
    # Code just before extension
    elif args.mode == 1:
        doBeforeExtension(path, file_path, file_name, file_extension)
    # Code at the end of the filename
    elif args.mode == 2:
        doEnd(path, file_path, file_name, file_extension)

# PROCESS FILES

for root, dirs, files in os.walk(args.path, topdown=False):
    for name in files:
        processPath(os.path.join(root, name))
    for name in dirs:
        processPath(os.path.join(root, name))


import nltk
nltk.download('vader_lexicon')
from newspaper import Article
from nltk.sentiment.vader import SentimentIntensityAnalyzer

new_words = {
    'crushes': 10,
    'beats': 5,
    'misses': -5,
    'trouble': -10,
    'falls': -100,
    'bankrupt': -100,
    'fraud': -100,
    'crime': -100,
    'prison': -100,
    'embezzlement': -100,
    'money_trail':-100,
    'jail': -100,
    'sentenced':-100,
    'forfeiture': -100,
    'ill-gotten gain':-100,
    'laundering':-100
}

vader = SentimentIntensityAnalyzer()

vader.lexicon.update(new_words)
#does not work
#url2 = "https://www.bloomberg.com/news/articles/2020-08-01/apple-buys-startup-to-turn-iphones-into-payment-terminals?srnd=premium"

#works
#url2 = "https://hbswk.hbs.edu/item/white-collar-crime-enforcement"
#works
#url2 = 'https://www.straitstimes.com/singapore/courts-crime/doing-more-to-keep-child-sex-offenders-from-making-same-mistake'

najib_news = "https://www.theedgemarkets.com/article/najib-received-massive-rm2973-billion-says-prosecution-forfeiture-hearing"

article = Article(najib_news)
article.download()
article.parse()

news_text = article.text

ss = vader.polarity_scores(news_text)

print(ss)


class MarioTD:
    def __init__(self,env, rev_act_map):
        self.env = env
        self.rev_act_map = rev_act_map

    def step(self,action, reverse=True, render = False, return_state = False, return_reward = False):
        if reverse:
            action = self.rev_act_map[action]
        next_state, reward, done, info = self.env.step(action)
        if render:
            self.env.render()
        obs = f"pos_{info['x_pos']}_{info['y_pos']}"
        if done:
            if info['flag_get']:
                #print("Win")
                obs += "__win"
            else:
                #print(f"Game over at {obs}")
                obs += "__game_over"
        if return_state:
            if return_reward:
                return next_state,obs,reward
            else:
                return next_state,obs
        else:
            return obs

    def reset(self,return_state=False):
        state = self.env.reset()
        if return_state:
            return state
        else:
            return "Init"


"""
This module knows how to convert the output of the conllu module into feature
vectors understood by the nn module.
"""

from collections import defaultdict

import itertools
import json

import h5py

import numpy as np

from code.nn import EDGE_FEATURES

class Extractor:
	"""
	Makes the first pass over the data, collecting what is necessary in order
	to determine the dimensions of the neural network. Provides the featurise_*
	methods (which would otherwise need to expect the UD version as an arg).

	Then makes the second pass over the data, collecting the samples (and
	possibly the targets) to be fed into the neural network.
	"""

	def __init__(self, ignore_forms=False, ignore_lemmas=False, ignore_morph=False):
		"""
		Constructor. The boolean flags could be used to disable the extraction
		of certain features.

		The pos_tags tuple lists all the possible POS tags that a node could
		belong to.

		The morph dict comprises the morphological features, both keys and
		values, found in the dataset during the reading phase.

		The lemmas dict provides unique IDs to the lemmas found in the dataset
		during the reading phase. ID 0 is used for unrecognised lemmas, hence
		the underscore. ID 1 is used for the non-standard root node lemma
		(\xa0). The same applies for the forms dict.
		"""
		self.ignore_forms = ignore_forms
		self.ignore_lemmas = ignore_lemmas
		self.ignore_morph = ignore_morph

		self.forms = defaultdict(lambda: len(self.forms))
		self.forms['_']  # unknown
		self.forms['\xa0']  # root

		self.lemmas = defaultdict(lambda: len(self.lemmas))
		self.lemmas['_']  # unknown
		self.lemmas['\xa0']  # root

		self.pos_tags = ('ROOT',)  # tuple of possible pos tags

		self.morph = {}  # key: tuple of possible values

	@classmethod
	def create_from_model_file(cls, model_fp):
		"""
		Returns a new Extractor instance with self.pos_tags, self.morph and
		self.lemmas loaded from the specified model file. The latter is
		expected to be a hdf5 file written or appended to by the method below.

		Raises an OSError if the file does not exist or cannot be read.
		"""
		f = h5py.File(model_fp, 'r')

		forms = json.loads(f['extractor'].attrs['forms'])
		lemmas = json.loads(f['extractor'].attrs['lemmas'])
		pos_tags = json.loads(f['extractor'].attrs['pos_tags'])
		morph = json.loads(f['extractor'].attrs['morph'])

		ignore_forms = json.loads(f['extractor'].attrs['ignore_forms'])
		ignore_lemmas = json.loads(f['extractor'].attrs['ignore_lemmas'])
		ignore_morph = json.loads(f['extractor'].attrs['ignore_morph'])

		f.close()

		extractor = Extractor(ignore_forms, ignore_lemmas, ignore_morph)

		for key, value in forms.items():
			extractor.forms[key] = value

		for key, value in lemmas.items():
			extractor.lemmas[key] = value

		extractor.pos_tags = tuple(pos_tags)
		extractor.morph = {key: tuple(value) for key, value in morph.items()}

		return extractor

	def write_to_model_file(self, model_fp):
		"""
		Appends to the specified hdf5 file, storing the instance's properties.
		Thus, an identical Extractor can be later restored using the above
		class method.

		Raises an OSError if the file cannot be written.
		"""
		f = h5py.File(model_fp, 'a')

		group = f.create_group('extractor')

		group.attrs['forms'] = json.dumps(dict(self.forms), ensure_ascii=False)
		group.attrs['lemmas'] = json.dumps(dict(self.lemmas), ensure_ascii=False)
		group.attrs['pos_tags'] = json.dumps(list(self.pos_tags), ensure_ascii=False)
		group.attrs['morph'] = json.dumps(dict(self.morph), ensure_ascii=False)

		group.attrs['ignore_forms'] = json.dumps(self.ignore_forms, ensure_ascii=False)
		group.attrs['ignore_lemmas'] = json.dumps(self.ignore_lemmas, ensure_ascii=False)
		group.attrs['ignore_morph'] = json.dumps(self.ignore_morph, ensure_ascii=False)

		f.flush()
		f.close()

	def read(self, dataset):
		"""
		Reads the data provided by the given conllu.Dataset instance and
		populates self.forms, self.lemmas, self.pos_tag, and self.morph.
		"""
		form_counts = defaultdict(lambda: 0)
		lemma_counts = defaultdict(lambda: 0)
		pos_tags = set(self.pos_tags)
		morph = defaultdict(set)

		for sent in dataset.gen_sentences():
			for word in sent:
				form_counts[word.FORM] += 1
				lemma_counts[word.LEMMA] += 1

				pos_tags.add(word.UPOSTAG)

				for key, values in word.FEATS.items():
					for value in values:
						morph[key].add(value)

		if not self.ignore_forms:
			for form, count in form_counts.items():
				if count > 1:
					self.forms[form]

		if not self.ignore_lemmas:
			for lemma, count in lemma_counts.items():
				if count > 1:
					self.lemmas[lemma]

		self.pos_tags = tuple(sorted(pos_tags))

		if not self.ignore_morph:
			self.morph = {key: tuple(sorted(value)) for key, value in morph.items()}

			if not self.morph:
				raise ValueError((
					'Could not find morphological features in the dataset; '
					'please use the --ignore-morph flag'))

	def get_vocab_sizes(self):
		"""
		Returns a {} containing: the number of form IDs, the number of lemma
		IDs, the number of POS tags, and the size of the vectors returned by
		the featurise_morph method.

		These are used as parameters for the form, lemma, and POS tag embedding
		and the morphology input layers of the neural network.

		This method assumes that the reading phase is already completed.
		"""
		morph_size = sum([len(value) for value in self.morph.values()])

		return {
			'forms': 0 if self.ignore_forms else len(self.forms),
			'lemmas': 0 if self.ignore_lemmas else len(self.lemmas),
			'morph': morph_size,
			'pos_tags': len(self.pos_tags) + 1}

	def featurise_form(self, form):
		"""
		Returns a positive integer uniquely identifying the given form. If the
		form has not been found during the reading phase, returns 0.
		"""
		if form not in self.forms:
			return 0

		return self.forms[form]

	def featurise_lemma(self, lemma):
		"""
		Returns a positive integer uniquely identifying the given lemma. If the
		lemma has not been found during the reading phase, returns 0.
		"""
		if lemma not in self.lemmas:
			return 0

		return self.lemmas[lemma]

	def featurise_pos_tag(self, pos_tag):
		"""
		Returns a positive integer uniquely identifying the given POS tag. If
		the POS tag has not been found during the reading phase, returns 0.
		"""
		try:
			return self.pos_tags.index(pos_tag) + 1
		except ValueError:
			return 0

	def featurise_morph(self, morph):
		"""
		Returns the feature vector corresponding to the given morphological
		features dict.

		The vector is a numpy array of 0s and 1s where each element represents
		a morphological feature. E.g. the output for "Animacy=Anim" should be a
		vector with its second element 1 and all the other elements zeroes.
		"""
		vector = []

		for feature, poss_values in sorted(self.morph.items()):
			small_vec = [0] * len(poss_values)

			if feature in morph:
				for index, value in enumerate(poss_values):
					if value in morph[feature]:
						small_vec[index] = 1

			vector += small_vec

		return np.array(vector, dtype='uint8')

	def extract(self, dataset, include_targets=False):
		"""
		Extracts and returns the samples, and possibly also the targets, from
		the given Dataset instance.

		Assumes that the latter is already read, i.e. this Extractor instance
		has already populated its pos_tags, lemmas, and morph properties.

		The samples comprise a dict where the keys are nn.EDGE_FEATURES (or a
		subset of those) and the values are numpy arrays. The targets are a
		numpy array of 0s and 1s.
		"""
		keys = list(EDGE_FEATURES)
		if self.ignore_forms:
			keys.remove('forms')
		if self.ignore_lemmas:
			keys.remove('lemmas')
		if self.ignore_morph:
			keys = itertools.filterfalse(lambda x: x.startswith('morph'), keys)

		samples = {key: [] for key in keys}
		targets = []

		for graph in dataset.gen_graphs():
			nodes = graph.nodes()
			edges = graph.edges()

			if not self.ignore_forms:
				forms = {i: self.featurise_form(graph.node[i]['FORM']) for i in nodes}

			if not self.ignore_lemmas:
				lemmas = {i: self.featurise_lemma(graph.node[i]['LEMMA']) for i in nodes}

			pos_tags = {i: self.featurise_pos_tag(graph.node[i]['UPOSTAG']) for i in nodes}
			pos_tags[-2] = 0
			pos_tags[-1] = 0
			pos_tags[len(nodes)] = 0
			pos_tags[len(nodes)+1] = 0

			if not self.ignore_morph:
				morph = {i: self.featurise_morph(graph.node[i]['FEATS']) for i in nodes}
				morph[-1] = self.featurise_morph('_')
				morph[len(nodes)] = self.featurise_morph('_')

			for a, b in itertools.permutations(nodes, 2):
				samples['B-A'].append(b-a)

				if not self.ignore_forms:
					samples['forms'].append([forms[a], forms[b]])

				if not self.ignore_lemmas:
					samples['lemmas'].append([lemmas[a], lemmas[b]])

				samples['pos'].append([
					pos_tags[a-2], pos_tags[a-1], pos_tags[a], pos_tags[a+1], pos_tags[a+2],
					pos_tags[b-2], pos_tags[b-1], pos_tags[b], pos_tags[b+1], pos_tags[b+2]])

				if not self.ignore_morph:
					samples['morph A-1'].append(morph[a-1])
					samples['morph A'].append(morph[a])
					samples['morph A+1'].append(morph[a+1])

					samples['morph B-1'].append(morph[b-1])
					samples['morph B'].append(morph[b])
					samples['morph B+1'].append(morph[b+1])

				if include_targets:
					targets.append((a, b) in edges)

		samples_ = {}
		for key, value in samples.items():
			if key in ['forms', 'lemmas']:
				samples_[key] = np.array(value, dtype='uint16')
			else:
				samples_[key] = np.array(value, dtype='uint8')
		del samples

		targets = np.array(targets, dtype='uint8')

		if include_targets:
			return samples_, targets
		else:
			return samples_


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

import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib
from actionlib_msgs.msg import *
import math
import time
import re
import yaml

class CMTFollower(object):
    def __init__(self):
        f = open('/home/robot/ros_workspace/SC0_ws/src/ocean_vision/config/tracker.yaml', 'r')
        config = yaml.load(f.read())
        self.FRAME_WIDTH = config['Video']['FRAME_WIDTH']
        self.FRAME_HEIGHT = config['Video']['FRAME_HEIGHT']
        self.dist_default = config['SmartCar']['dist_default']

        self.center_delta = 0
        self.dist_delta = 0
        self.if_track = False
        self.do_job = True

        rospy.init_node("cmt_tracker")

        self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)
        rospy.loginfo("wait for the action server to come up")
        self.move_base.wait_for_server(rospy.Duration(5))
        rospy.loginfo("waiting done")

        self.rate = rospy.get_param("~rate", 30)
        self.r = rospy.Rate(self.rate)

        rospy.on_shutdown(self.shutdown)
        self.run()

    def callback(self, data):
        # rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)
        cmd_message = re.findall(re.compile(r'\$\$.*?\&\&'), data.data)
        if len(cmd_message) > 0:
            cmd_para = re.findall(re.compile(r'[\d.]+'), cmd_message[-1])
            # print('LCH: the cmd para is ', cmd_para)
            if len(cmd_para)==4:
                centerx = float(cmd_para[0])
                dist = float(cmd_para[2])
                self.center_delta = (self.FRAME_WIDTH/2 - centerx)/float(self.FRAME_WIDTH)
                self.dist_delta = -(self.dist_default - dist)
                self.if_track = True
            else:
                rospy.loginfo("The target lost, need redetect ...")
                self.if_track = False

    def run(self):
        rospy.Subscriber('chatter', String, self.callback)
        while self.do_job:
            if self.if_track:
                goal = MoveBaseGoal()
                goal.target_pose.header.frame_id = 'base_link'
                goal.target_pose.header.stamp = rospy.Time.now()
                goal.target_pose.pose.position.x = self.dist_delta
                goal.target_pose.pose.position.y = self.center_delta
                goal.target_pose.pose.orientation.w = 1.0 #go forward
                print('LCH: the goal is ', goal)

                self.move_base.send_goal(goal)
                # success = self.move_base.wait_for_result(rospy.Duration(10))
                #
                # if not success:
                #     self.move_base.cancel_goal()
                #     rospy.loginfo("The base failed to move forward 3 meters for some reason")
                #     continue
                # else:
                #     # We made it!
                #     state = self.move_base.get_state()
                #     if state == GoalStatus.SUCCEEDED:
                #         rospy.loginfo("Hooray, the base moved 3 meters forward")

            else:
                rospy.loginfo("Holding")

            # self.r.sleep()
            time.sleep(2)

    def shutdown(self):
        rospy.loginfo("Stopping the robot...")
        self.do_job = False
        exit(0)

if __name__ == '__main__':
    try:
        CMTFollower()
        rospy.spin()
    except rospy.ROSInterruptException:
        rospy.loginfo("CMT follower node terminated.")


"""Training script for CheXpert"""
import argparse
import numpy as np
import pandas as pd
import torch
import tqdm
import util

from constants        import *
from args             import CheXpertTrainArgParser
from dataset.chexpert import get_dataloader
from logger           import Logger
from models           import CheXpertModel

# Reproducibility
torch.manual_seed(6)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)

def train(args):

    # get transformations
    train_trainsforms = util.transformation_composer(args, "train", "CheXpert")
    valid_trainsforms = util.transformation_composer(args, "valid", "CheXpert")

    # get dataloader
    train_loader = get_dataloader(args, train_trainsforms, "train", "CheXpert")
    valid_loader = get_dataloader(args, valid_trainsforms, "valid", "CheXpert")

    # get model and put on device
    model = CheXpertModel(
        model_name=args.model_name,
        num_classes=14
    )
    if args.device == "cuda":
        model = torch.nn.DataParallel(model, args.gpu_ids)
    model = model.to(args.device)

    # optimizer
    optimizer = util.set_optimizer(opt=args, model=model)

    # loss function
    loss_fn = torch.nn.BCEWithLogitsLoss(reduction="mean")

    # define logger
    logger = Logger(
        log_dir=args.log_dir,
        metrics_name=args.eval_metrics,
        args=args
    )

    # iterate over epoch
    global_step = 0
    model.train()
    for epoch in range(args.num_epoch):

        # training loop
        for inputs, targets in tqdm.tqdm(train_loader, desc=f"[epoch {epoch}]"):

            # validate every N training iteration
            if global_step % args.iters_per_eval == 0:
                model.eval()
                probs, gt = [], []
                with torch.no_grad():

                    # validation loop
                    for val_inputs, val_targets in valid_loader:

                        batch_logits = model(val_inputs.to(args.device))
                        batch_probs = torch.sigmoid(batch_logits)

                        probs.append(batch_probs.cpu())
                        gt.append(val_targets.cpu())

                # evaluate results
                metrics = util.evaluate(probs, gt, args.threshold)
                avg_metric, metric_dict = util.aggregate_metrics(metrics)

                # log image
                logger.log_iteration(metric_dict, global_step, "val")
                logger.log_dict(avg_metric, global_step, "val")
                logger.log_image(inputs, global_step)

                # save checkpoint
                logger.save_checkpoint(model, metrics, global_step)

                model.to(args.device)

            model.train()
            with torch.set_grad_enabled(True):

                # Run the minibatch through the model.
                logits = model(inputs.to(args.device))

                # Compute the minibatch loss.
                loss = loss_fn(logits, targets.to(args.device))

                logger.log_dict({"train/loss": loss}, global_step, "train")

                # Perform a backward pass.
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

            global_step += 1

if __name__ == "__main__":

    # get argumetns
    parser = CheXpertTrainArgParser()
    args = parser.parse_args()
    train(args)


import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
from sklearn.model_selection import StratifiedShuffleSplit, RepeatedStratifiedKFold
from sklearn.ensemble import AdaBoostClassifier,RandomForestClassifier
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
import time

def preprocess(df):
    df.drop(['id'], axis=1)
    for i in range(len(df)):
        if df.at[i, 'Previously_Insured'] == 0 and df.at[i, 'Vehicle_Damage'] == 'Yes':
            df.at[i, 'flag'] = 1
        else:
            df.at[i, 'flag'] = 0
    cdat = []
    ndat = []
    for i, c in enumerate(df.dtypes):
            if c == 'object':
                cdat.append(df.iloc[:, i])
            else:
                ndat.append(df.iloc[:, i])

    cdat = pd.DataFrame(cdat).transpose()
    ndat = pd.DataFrame(ndat).transpose()

    le = LabelEncoder()
    for i in cdat:
        cdat[i] = le.fit_transform(cdat[i])
    df = pd.concat([cdat, ndat], axis=1)

    return df

def run_cross_validation_on_trees(X, y, tree_depths, scoring='roc_auc'):
    cv_scores_list = []
    cv_scores_std = []
    cv_scores_mean = []
    accuracy_scores = []
    cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=5, random_state=1)
    for depth in tree_depths:
        tree_model = DecisionTreeClassifier(max_depth=depth, class_weight='balanced')
        cv_scores = cross_val_score(
            tree_model, X, y, cv=cv, scoring=scoring, n_jobs=-1)
        cv_scores_list.append(cv_scores)
        cv_scores_mean.append(cv_scores.mean())
        cv_scores_std.append(cv_scores.std())
        accuracy_scores.append(tree_model.fit(X, y).score(X, y))
    cv_scores_mean = np.array(cv_scores_mean)
    cv_scores_std = np.array(cv_scores_std)
    accuracy_scores = np.array(accuracy_scores)
    return cv_scores_mean, cv_scores_std, accuracy_scores

def plot_cross_validation_on_trees(depths, cv_scores_mean, cv_scores_std, accuracy_scores, title):
    fig, ax = plt.subplots(1, 1, figsize=(12, 6))
    ax.plot(depths, cv_scores_mean, '-o',
            label='mean cross-validation score', alpha=0.9)
    ax.fill_between(depths, cv_scores_mean-2*cv_scores_std,
                    cv_scores_mean+2*cv_scores_std, alpha=0.2)
    ylim = plt.ylim()
    ax.plot(depths, accuracy_scores, '-*', label='train score', alpha=0.9)
    ax.set_title(title, fontsize=16)
    ax.set_xlabel('Tree depth', fontsize=14)
    ax.set_ylabel('AUC Score', fontsize=14)
    ax.set_ylim(0.5, 1)
    ax.set_xticks(depths)
    ax.legend()
    plt.show()

def ti(t1, t2):
    if t2-t1 > 60:
        m = int((t2-t1)//60)
        s = (t2-t1) % 60
        if m > 1:
            print("Time required:", m, "minutes %.3f" % s, "seconds")
        else:
            print("Time required:", m, "minute %.3f" % s, "seconds")
    else:
        print("Time required:%.3f" % (t2-t1), "seconds")

df = pd.read_csv('train.csv')

df0 = preprocess(df)

X = df0.drop(['Response'], axis=1)
y = df0['Response']
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)

for train, test in sss.split(X, y):
    X_train, X_test = X.iloc[train], X.iloc[test]
    y_train, y_test = y.iloc[train], y.iloc[test]

sm_tree_depths = range(1, 30)
sm_cv_scores_mean, sm_cv_scores_std, sm_accuracy_scores = run_cross_validation_on_trees(
    X_train, y_train, sm_tree_depths)

depth0 = sm_cv_scores_mean.argmax()+1

print(sm_cv_scores_mean)
print("Best tree depth(after cross validation):", depth0)

plot_cross_validation_on_trees(sm_tree_depths, sm_cv_scores_mean, sm_cv_scores_std,
                               sm_accuracy_scores, 'Accuracy per decision tree depth on training data')


import os

import matplotlib.pyplot as plt
import seaborn as sns

sns.set(context='notebook',
        style='whitegrid',
        palette='deep',
        font='sans-serif',
        font_scale=1)

def fitness_plot(dir):
    print(os.getcwd())
    with open(dir + "loss.txt") as f:
        lines = f.readlines()
    fitness = [float(line[:-2]) for line in lines]

    plt.plot(fitness)
    plt.xlabel("Generations")
    plt.ylabel("Fitness")
    plt.savefig(dir + "fitness.png")
    plt.close()


import datetime
import os
import random
import sys
import numpy as np

import agents.image_input.AbstractBrain as AbstractBrain
from agents.networks.dueling_dqn_networks import build_dueling_cartpole_network, build_dueling_dqn_network

class Learning(AbstractBrain.AbstractLearning):

    def __init__(self, observations, actions, config):
        super().__init__(observations, actions, config)
        # use network suitable for classic control games
        if config['environment'] == 'CartPole-v1':
            self.network = \
                build_dueling_cartpole_network(self.state_space, self.action_space, self.config['learning_rate'])
        # use network suitable for Atari games
        else:
            self.network = build_dueling_dqn_network(self.state_space, self.action_space, self.config['learning_rate'])

        # set values for epsilon-greedy exploration
        self.e_greedy_formula = 'e = min(e_min, e - e_decay)'
        self.epsilon = self.config['epsilon']
        self.epsilon_decay = (self.config['epsilon'] - self.config['epsilon_min']) / self.config['epsilon_explore']

    def update_epsilon(self):
        if self.epsilon > self.config['epsilon_min']:
            self.epsilon = max(self.config['epsilon_min'], self.epsilon - self.epsilon_decay)

    def choose_action(self, state):
        policy = self.network.predict(np.expand_dims(state, axis=0))
        if random.random() <= self.epsilon:
            action = random.randrange(self.action_space)
        else:
            action = np.argmax(policy)
        return action, policy[0]

    def train_network(self, states, actions, rewards, next_states, dones, step):
        if step % self.config['network_train_frequency'] == 0:
            target = self.network.predict(states)
            target_next = self.network.predict(next_states)
            for i in range(self.config['batch_size']):
                if dones[i]:
                    target[i][actions[i]] = rewards[i]
                else:
                    # bellman equation
                    target[i][actions[i]] = rewards[i] + self.config['gamma'] * np.amax(target_next[i])

            self.network.fit(states, target, batch_size=len(dones), epochs=1, verbose=0)

        self.update_epsilon()

    # after some time interval update the target model to be same with model
    def update_target_model(self):
        self.network.set_weights(self.network.get_weights())

    def save_network(self, save_path, model_name, timestamp=None):
        # create folder for model, if necessary
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        # set timestamp if none was specified
        if timestamp is None:
            timestamp = str(datetime.datetime.now())
        # save model weights
        self.network.save_weights(save_path + model_name + '_' + timestamp + '.h5', overwrite=True)

    def load_network(self, save_path, model_name) -> None:
        if os.path.exists(save_path):
            self.network.load_weights(save_path + model_name)
            print('Loaded model ' + model_name + ' from disk')
        else:
            sys.exit("Model can't be loaded. Model file " + model_name + " doesn't exist at " + save_path + ".")

    def get_test_learner(self):
        test_learner = Learning(self.state_space, self.action_space, self.config)
        # use current network weights for testing
        test_learner.network.set_weights(self.network.get_weights())
        return test_learner


# 플로이드 ( 정답 )
# 알고리즘 : 플로이드 워셜
# 문제 의도는 플로이드 워셜이었으나 다익스트라 반복해 구현

import heapq

n = int(input())
m = int(input())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append([c, b])

distances = [[100000*(n+1) for _ in range(n+1)] for _ in range(n+1)]

for i in range(1, n+1):
    queue = []
    heapq.heappush(queue, [0, i])
    distances[i][i] = 0

    while queue:
        distance, now = heapq.heappop(queue)

        if distances[i][now] < distance:
            continue

        for d, to in graph[now]:
            if distances[i][to] > distance + d:
                distances[i][to] = distance + d
                heapq.heappush(queue, [distance + d, to])

for i in range(n+1):
    for j in range(n+1):
        if distances[i][j] == 100000*(n+1):
            distances[i][j] = 0

for i in range(1, n+1):
    print(" ".join(map(str, distances[i][1:])))


'''
Write a script that takes a string from the user and creates a dictionary of letter that exist
in the string and the number of times they occur. For example:

user_input = "hello"
result = {"h": 1, "e": 1, "l": 2, "o": 1}

'''
user_input = input("Type a word: ")
user_input_list = list(user_input)

from collections import Counter
cc = Counter(user_input_list)
dict1 = dict(cc)

print(dict1)


import torch
from torch import nn

from src.data.data import mnist

from models.model import MyAwesomeModel

def evaluate(model_checkpoint):
    print("Evaluating until hitting the ceiling")
    print(model_checkpoint)

    checkpoint = torch.load(model_checkpoint)
    model = MyAwesomeModel()
    model.load_state_dict(checkpoint["state_dict"])
    _, test_set = mnist()
    criterion = nn.NLLLoss()
    accuracy = 0
    loss = 0
    _, testloader = mnist()

    for images, labels in testloader:
        output = model.forward(images)

        labels = labels.type(torch.LongTensor)
        loss += criterion(output, labels).item()

        ps = torch.exp(output)

        temp = labels.data == ps.max(1)[1]

        accuracy += temp.type_as(torch.FloatTensor()).mean()

        print(
            "Test Loss: {:.3f}.. ".format(loss / len(testloader)),
            "Test Accuracy: {:.3f}".format(accuracy / len(testloader)),
        )


'''Scrape Billboard for hot 100 of certain year '''
import os
from dotenv import load_dotenv
from billboard_api import ScrapeBillboard
import search_spotify
import manage_playlist

load_dotenv()
# Set your Spotify API credentials
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
URL_REDIRECT = "http://localhost:8080/"
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
SPOTIFY_SEARCH_URL = "https://api.spotify.com/v1/search?"

BASE_URL = "https://www.billboard.com/charts/hot-100/"

SCRAPE_BILLBOARD = ScrapeBillboard(BASE_URL)

# ask user for date to get top 100 and return list with year and dict
artists_and_songs = SCRAPE_BILLBOARD.get_artists_songs()

# get the URI's of the songs
my_uris = search_spotify.get_song_uris(artists_and_songs[1])

year = artists_and_songs[0]
playlist_name = f"{year} Billboard 100"

# create a playlist
playlist_id = manage_playlist.create_playlist(manage_playlist.current_user_id, playlist_name)
response = manage_playlist.add_to_playlist(manage_playlist.current_user_id, playlist_id, my_uris)

if response['snapshot_id']:
    print(f"All good, playlist created with snapshot_id of : {response['snapshot_id']}")
else:
    print("Something went wrong wiht the request")


# # defining function.
# def print_codanics():
#     print("we are learning python with aammar")
#     print("we are learning python with aammar")
#     print("we are learning python with aammar")

# print_codanics()
#2
# def print_codanic():
#     text="we are learning  with aammar in codanics youtube channels"
#     print(text)
#     print(text)
#     print(text)

# print_codanic()

#3

# def print_codanics(text):
#     print(text)
#     print(text)
#     print(text)

# print_codanics("we are learning python with aammar")

# defining function with if elif and else statments

# def school_calculator(age, text):
#     if age==5:
#         print("hamad can join the school")
#     elif age>5:
#         print("hamad should go to higher school")
#     else:
#         print("hamad is still a baby")

# school_calculator(15, "hamad")

# defining a function  of future

def future_age(age):
    new_age= age+20
    return new_age
    print(new_age)

future_predicted_age=future_age(18)
print(future_predicted_age)


import tensorflow as tf

class Projection:
    """Projection to a scalar and through a sigmoid activation
    """

    def __init__(self, is_training, drop_rate=0., shape=1, activation=tf.nn.sigmoid, using_selu=True, scope=None):
        """
        Args:
            is_training: Boolean, to control the use of sigmoid function as it is useless to use it
                during training since it is integrate inside the sigmoid_crossentropy loss
            shape: integer, dimensionality of output space. Defaults to 1 (scalar)
            activation: callable, activation function. only used during inference
            scope: Projection scope.
        """
        super(Projection, self).__init__()
        self.is_training = is_training

        self.shape = shape
        self.drop_rate = drop_rate
        self.activation = activation if not using_selu else tf.nn.selu
        self.using_selu = using_selu
        self.scope = 'projection_layer' if scope is None else scope

    def __call__(self, inputs):
        with tf.variable_scope(self.scope):
            output = tf.layers.dense(inputs, units=self.shape,
                                     kernel_initializer=tf.contrib.layers.variance_scaling_initializer(factor=1.0, mode='FAN_IN') if self.using_selu else None,
                                     activation=None, name='dense_{}'.format(self.scope))

            if self.activation is not None:
                output = self.activation(output)

            if self.using_selu:
                if self.is_training:
                    output = tf.contrib.nn.alpha_dropout(output, keep_prob=(1 - self.drop_rate),
                                                         name='dropout_{}'.format(self.scope))

            else:
                output = tf.layers.dropout(output, rate=self.drop_rate, training=self.is_training,
                                           name='dropout_{}'.format(self.scope))

            return output


#!/usr/bin/python3
import pyglet
from pyglet.gl import *
from pyglet.window import mouse, key
from bez import *
import sys
from BezierCurve import BezierCurve
from util import *
import random

h = 1000
w = 1000
window = pyglet.window.Window( height=h, width=w )
order = 3 if len( sys.argv ) < 2 else int( sys.argv[1] )
selected = None
res = 10 if len( sys.argv ) < 3 else int( sys.argv[2] )
offset_w = w / 10
offset_h = h / 10
init_pts = list( [ ( int( random.uniform( offset_w, w - offset_w ) ), int( random.uniform( offset_h, h - offset_h ) ) ) for _ in range( order + 1 ) ] )
bez_curve = BezierCurve( order, init=init_pts )

@window.event
def on_mouse_drag( x, y, dx, dy, buttons, modifiers ):
    global selected
    global bez_curve
    if buttons == mouse.RIGHT:
        if selected is not None:
            bez_curve.move_ctrl( selected, x, y )

@window.event
def on_mouse_press( x, y, buttons, modifiers ):
    global selected
    global bez_curve
    if buttons == mouse.LEFT:
        for idx, pt in enumerate( bez_curve.control_coords() ):
            if pt_dist( pt, ( x, y ) ) <= 5.0:
                selected = idx
                return
        selected = None

@window.event
def on_draw():
    global bez_curve
    glClear( GL_COLOR_BUFFER_BIT )
    glPointSize( 10.0 )

    glBegin( GL_LINE_STRIP )

    glColor3f( 1.0, 0.0, 0.0 )
    for pt in bez_curve.rasterize( res ):
        glVertex2f( *pt )
    glEnd()

    glBegin( GL_POINTS )
    for idx, pt in enumerate( bez_curve.control_coords() ):
        if idx == selected:
            glColor3f( 0.0, 0.0, 1.0 )
        else:
            glColor3f( 0.0, 1.0, 0.0 )

        glVertex2f( *pt )
    glEnd()

pyglet.app.run()


#
# PySNMP MIB module HUAWEI-DAD-MIB (http://snmplabs.com/pysmi)
# ASN.1 source file:///Users/davwang4/Dev/mibs.snmplabs.com/asn1/HUAWEI-DAD-MIB
# Produced by pysmi-0.3.4 at Mon Apr 29 19:31:59 2019
# On host DAVWANG4-M-1475 platform Darwin version 18.5.0 by user davwang4
# Using Python version 3.7.3 (default, Mar 27 2019, 09:23:15)
#
OctetString, Integer, ObjectIdentifier = mibBuilder.importSymbols("ASN1", "OctetString", "Integer", "ObjectIdentifier")
NamedValues, = mibBuilder.importSymbols("ASN1-ENUMERATION", "NamedValues")
ValueSizeConstraint, ConstraintsIntersection, SingleValueConstraint, ConstraintsUnion, ValueRangeConstraint = mibBuilder.importSymbols("ASN1-REFINEMENT", "ValueSizeConstraint", "ConstraintsIntersection", "SingleValueConstraint", "ConstraintsUnion", "ValueRangeConstraint")
hwDatacomm, = mibBuilder.importSymbols("HUAWEI-MIB", "hwDatacomm")
InterfaceIndex, = mibBuilder.importSymbols("IF-MIB", "InterfaceIndex")
EnabledStatus, = mibBuilder.importSymbols("P-BRIDGE-MIB", "EnabledStatus")
ObjectGroup, ModuleCompliance, NotificationGroup = mibBuilder.importSymbols("SNMPv2-CONF", "ObjectGroup", "ModuleCompliance", "NotificationGroup")
TimeTicks, IpAddress, Bits, MibScalar, MibTable, MibTableRow, MibTableColumn, Counter64, ModuleIdentity, iso, Counter32, Unsigned32, ObjectIdentity, Gauge32, MibIdentifier, NotificationType, Integer32 = mibBuilder.importSymbols("SNMPv2-SMI", "TimeTicks", "IpAddress", "Bits", "MibScalar", "MibTable", "MibTableRow", "MibTableColumn", "Counter64", "ModuleIdentity", "iso", "Counter32", "Unsigned32", "ObjectIdentity", "Gauge32", "MibIdentifier", "NotificationType", "Integer32")
MacAddress, TextualConvention, TruthValue, DisplayString, RowStatus = mibBuilder.importSymbols("SNMPv2-TC", "MacAddress", "TextualConvention", "TruthValue", "DisplayString", "RowStatus")
hwDadMIB = ModuleIdentity((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246))
if mibBuilder.loadTexts: hwDadMIB.setLastUpdated('201109142130Z')
if mibBuilder.loadTexts: hwDadMIB.setOrganization('Huawei Technologies co.,Ltd.')
class PortIndex(TextualConvention, Integer32):
    status = 'current'
    subtypeSpec = Integer32.subtypeSpec + ValueRangeConstraint(0, 575)

hwDadTraps = MibIdentifier((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 1))
hwDadConformance = MibIdentifier((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 2))
hwDadConflictDetect = NotificationType((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 1, 1))
if mibBuilder.loadTexts: hwDadConflictDetect.setStatus('current')
hwDadConflictResume = NotificationType((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 1, 2))
if mibBuilder.loadTexts: hwDadConflictResume.setStatus('current')
hwDadCompliances = MibIdentifier((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 2, 1))
hwDadGroups = MibIdentifier((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 2, 2))
hwDadCompliance = ModuleCompliance((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 2, 1, 1)).setObjects(("HUAWEI-DAD-MIB", "hwDadTrapGroup"))

if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0):
    hwDadCompliance = hwDadCompliance.setStatus('current')
hwDadTrapGroup = NotificationGroup((1, 3, 6, 1, 4, 1, 2011, 5, 25, 246, 2, 2, 1)).setObjects(("HUAWEI-DAD-MIB", "hwDadConflictDetect"), ("HUAWEI-DAD-MIB", "hwDadConflictResume"))
if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0):
    hwDadTrapGroup = hwDadTrapGroup.setStatus('current')
mibBuilder.exportSymbols("HUAWEI-DAD-MIB", hwDadGroups=hwDadGroups, hwDadTrapGroup=hwDadTrapGroup, hwDadConflictDetect=hwDadConflictDetect, hwDadConflictResume=hwDadConflictResume, PortIndex=PortIndex, hwDadConformance=hwDadConformance, hwDadCompliance=hwDadCompliance, PYSNMP_MODULE_ID=hwDadMIB, hwDadCompliances=hwDadCompliances, hwDadMIB=hwDadMIB, hwDadTraps=hwDadTraps)


"""
Longest subarray that sums to zero.

Example:
arr = [15, -2, 2, -8, 1, 7, 10, 23]
Ouput: the longest subarray summing to 0 is [-2, 2, -8, 1, 7]

Example:
arr = [1, 2, 3]
Output: None

Created on Fri Jun 17 2022
"""
from typing import List
import argparse

# Idea: Use hash map
# Let prefix(i) = arr[0] + arr[1] + ... + arr[i]
#     prefix(j) = arr[0] + arr[1] + ... + arr[j], j > i
# If prefix(i) == prefix(j), it means
# arr[i+1] + ... + arr[j] = 0

# Algo:
# 1. Create extra space for array prefix[i], a var sum, a var max_len,
# and a hash map to store the sum-index pair
# 2. Iterate through the input array
# 3. For every index, update sum += arr[i]
# 4. Check every index to see if the current sum in the hash map
# - if yes: update max_len = max((current_index - index_in_hash), max_len)
# - else: put the sum in the hash map

def max_subarr(arr: List[int]) -> int:
    hashmap = dict()
    max_len = 0
    curr_sum = 0

    for i in range(len(arr)):
        curr_sum += arr[i]
        if arr[i] == 0 and max_len == 0:
            max_len = 1

        if curr_sum == 0:
            max_len += 1

        if curr_sum in hashmap:
            max_len = max(max_len, i - hashmap[curr_sum])
        else:
            hashmap[curr_sum] = i

    return max_len

arr = [15, -2, 2, -8, 1, 7, 10, 13]

print(max_subarr(arr))


def pilingup(d):
    left=0
    right=len(d)-1
    ans=[max(d[0],d[-1])]
    while left<right:
        large=max(d[left],d[right])
        if large>ans[0] and large>ans[-1]:
            return "No"
        if d[left]>=d[right]:
            large=d[left]
            ans.append(d[left])
            left+=1
        if d[left]<d[right]:
            large=d[right]
            ans.append(d[right])
            right-=1
    return "Yes"
for i in range(int(input())):
    n=int(input())
    lists=list(map(int,input().split()))
    print(pilingup(lists))