# -*- coding: utf-8 -*-
"""
@author: Daniel
@contact: 511735184@qq.com
@file: util.py
@time: 2017/5/31 16:08
"""
import os
import codecs
import json
import yaml

from utils.i18n import gettext

def load_config(config_path, loader=yaml.Loader):
    if config_path is None:
        return {}
    if not os.path.exists(config_path):
        raise RuntimeError(gettext(u"config.yml not found in {config_path}").format(config_path))
    if ".json" in config_path:
        with codecs.open(config_path, encoding="utf-8") as f:
            json_config = f.read()
        config = json.loads(json_config)
    else:
        with codecs.open(config_path, encoding="utf-8") as stream:
            config = yaml.load(stream, loader)
    return config

if __name__ == '__main__':
    config = load_config(config_path='config.yml')
    print(config)


from yargy import Parser
from yargy.tagger import Tagger
from yargy.predicates import tag

INSIDE = 'I'
OUTSIDE = 'O'

class MyTagger(Tagger):
    tags = {INSIDE, OUTSIDE}

    def __call__(self, tokens):
        for index, token in enumerate(tokens):
            yield token.tagged(
                OUTSIDE
                if index % 3 == 0
                else INSIDE
            )

def test_tagger():
    text = 'a b c d e f g'
    A = tag('I').repeatable()
    parser = Parser(A, tagger=MyTagger())

    matches = parser.findall(text)
    spans = [_.span for _ in matches]
    substrings = [
        text[start:stop]
        for start, stop in spans
    ]
    assert substrings == ['b c', 'e f']


import requests

from local.secrets import TIMESTATION_API_KEY

class Client:

    DEFAULT_OPTIONS = {
        'base_url': 'https://api.mytimestation.com/v1.1/',
    }

    def __init__(self, session=None, auth=None, **options):

        self.session = session or requests.Session()
        self.auth = auth
        self.headers = options.pop('headers', {})
        self.options = self._merge(self.DEFAULT_OPTIONS, options)

    def get(self, url, params={}, **options):
        """

        :param str url:
        :param dict params:
        :return:
        """
        response = requests.get(url, params, headers=self.headers)
        if response.status_code == 200:
            return response.json()
        else:
            self.handle_http_error(response)

    def post(self, url, params={}, **options):
        """

        :param str url:
        :param dict params:
        :return dict:
        """
        response = requests.post(url, params, headers=self.headers)
        if response.status_code == 201:
            return response.json()
        else:
            self.handle_http_error(response)

    def put(self, url, params={}, **options):
        """

        :param str url:
        :param dict params:
        :return dict:
        """
        response = requests.put(url, params, headers=self.headers)
        if response.status_code == 200:
            return response.json()
        else:
            self.handle_http_error(response)

    def delete(self, url, **options):
        """

        :param str url:
        :param dict params:
        :return dict:
        """
        response = requests.delete(url, headers=self.headers)
        if response.status_code == 200:
            return response.json()
        else:
            self.handle_http_error(response)

    @staticmethod
    def handle_http_error(response):
        error_message = response.json().get('error', {}).get('error_text', 'Unknown Error')
        raise KeyError(f"HTTP.Error.{response.status_code}: {error_message}")

    @staticmethod
    def _merge(*dicts):
        merged = {}
        for d in dicts:
            merged.update(d)
        return merged

if __name__ == '__main__':
    pass


import unittest

from ds2.graph import ( EdgeSetGraph,
                        UndirectedEdgeSetGraph,
                        AdjacencySetGraph,
                        UndirectedAdjacencySetGraph,
                        Digraph,
                        Graph,
                        )

class GeneralGraphTests:
    def testinit(self):
        G = self.Graph({},{})
        G = self.Graph({1,2,3}, {(1,2),(2,3)})

    def testinit_empty(self):
        G = self.Graph()

    def testinit_no_edges(self):
        G = self.Graph([1,2,3], ())

    def testlen(self):
        G = self.Graph({1,2,3}, {(1,2)})
        self.assertEqual(len(G), 3)
        G.addvertex(3)
        self.assertEqual(len(G), 3)
        G.addvertex(4)
        self.assertEqual(len(G), 4)
        G.addedge(2,3)
        self.assertEqual(len(G), 4)

    def testcontains(self):
        G = self.Graph([1,2,3,4], [(2,4), (1,3)])
        for v in [1,2,3,4]:
            self.assertTrue(v in G)
        self.assertTrue(0 not in G)
        self.assertTrue('2' not in G)

class DigraphTests(GeneralGraphTests):
    def testhasedge(self):
        G = self.Graph([1,2,3], [[1,2], [3,1]])
        self.assertTrue(G.hasedge(1,2))
        self.assertFalse(G.hasedge(2,1))
        self.assertTrue(G.hasedge(3,1))
        self.assertFalse(G.hasedge(1,3))

    def testremoveedge(self):
        G = self.Graph([1,2,3], [(1,2), (1,3), (2,1)])
        G.removeedge(1,2)
        self.assertTrue(G.hasedge(2,1))
        self.assertTrue(G.hasedge(1,3))
        self.assertFalse(G.hasedge(1,2))

    def testremoveedge_not_an_edge(self):
        G = self.Graph([1,2,3], [(1,2), (1,3), (2,1)])
        with self.assertRaises(KeyError):
            G.removeedge(3,1)

    def testvertices(self):
        G = self.Graph()
        G.addvertex('a')
        G.addvertex('b')
        self.assertEqual(set(G.vertices()), {'a', 'b'})
        G.addvertex('b')
        self.assertEqual(set(G.vertices()), {'a', 'b'})
        G.addvertex('c')
        self.assertEqual(set(G.vertices()), {'a', 'b', 'c'})

    def testedges(self):
        G = self.Graph([2,4,6], [(2,4), (4,6)])
        E = set(G.edges())
        self.assertEqual(len(E), 2)
        self.assertTrue((2,6) not in E and (6,2) not in E)
        G.addedge(2,6)
        E = set(G.edges())
        self.assertEqual(len(E), 3)
        self.assertTrue((2,4) in E)

class GraphTests(GeneralGraphTests):
    def testremoveedge_removesbothdirections(self):
        G = self.Graph({'a', 'b', 'c'}, {('a', 'b'), ('b','c')})
        G.removeedge('a', 'b')
        self.assertFalse(G.hasedge('b', 'a'))

    def testnbrs(self):
        G = self.Graph([2,4,6], [(2,4), (4,6)])
        self.assertEqual(set(G.nbrs(2)), {4})
        self.assertEqual(set(G.nbrs(4)), {2,6})
        self.assertEqual(set(G.nbrs(6)), {4})

    def testedges(self):
        G = self.Graph([2,4,6], [(2,4), (4,6)])
        E = list(G.edges())
        self.assertEqual(len(E), 2)

class TestEdgeSetGraph(unittest.TestCase, DigraphTests):
    Graph = EdgeSetGraph

class TestUndirectedEdgeSetGraph(unittest.TestCase, GraphTests):
    Graph = UndirectedEdgeSetGraph

class TestUndirectedAdjacencySetGraph(unittest.TestCase, GraphTests):
    Graph = UndirectedAdjacencySetGraph

class TestDigraph(unittest.TestCase, DigraphTests):
    Graph = Digraph

class TestGraph(unittest.TestCase, GraphTests):
    Graph = Graph

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


import cv2
import numpy as np
import face_recognition
import os

class FaceRec:
    def __init__(self, path):
        images = []
        self.avgfd = []
        self.classNames = []
        myList = os.listdir(path)
        print(myList)
        for cl in myList:
            curImg = cv2.imread(f'{path}/{cl}')
            images.append(curImg)
            self.classNames.append(os.path.splitext(cl)[0])
            print(self.classNames)

        def findEncodings(images):
            encodeList = []
            for img in images:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                encode = face_recognition.face_encodings(img)[0]
                encodeList.append(encode)
            return encodeList

        self.encodeListKnown = findEncodings(images)
        print('Encoding Complete')

    def recognize(self, img):
        imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
        imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

        facesCurFrame = face_recognition.face_locations(imgS)
        encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)

        for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
            # matches = face_recognition.\
            #     compare_faces(self.encodeListKnown, encodeFace)
            faceDis = face_recognition.\
                face_distance(self.encodeListKnown, encodeFace)
            # matchIndex = np.argmin(faceDis)

            self.avgfd.append(tuple([1-min(faceDis), img]))

        return img

    def clean_val(self):
        self.avgfd.clear()

    def get_result(self):
        print(self.avgfd)
        return max(self.avgfd, key=lambda i: i[0])


# coding:utf-8
'''
表格生成线条坐标
'''
import cv2
import numpy as np
import os
import pytesseract
import uuid

# 图像切割模块

class cutImage(object):
    def __init__(self, img, **settings):
        '''
        :param img: 输入图像
        :param bin_threshold: 二值化的阈值大小
        :param kernel: 形态学kernel
        :param iterations: 迭代次数
        :param areaRange: 面积范围 单元格的面积大小 此参数决定哪些是单元格，例如:[9000, 100000]
        :param border: 留边大小
        :param show: 是否显示结果图，默认是显示
        :param write: 是否把结果写到文件，默认是写入
        :param lang:ocr识别的语言，例如：'eng+tha'
        '''
        self.img = img

        self.bin_threshold = settings.get('pytesseract_bin_threshold', 127)
        self.kernel = settings.get(
            'pytesseract_kernel', np.ones((4, 4), np.uint8))
        self.iterations = settings.get('pytesseract_iterations', 1)
        self.areaRange = settings.get('pytesseract_areaRange', [10000, 100000])
        self.border = settings.get('pytesseract_border', 10)
        self.isDebug = settings.get('pytesseract_isDebug', False)
        self.lang = settings.get('pytesseract_lang', 'eng')

    def hitTest(self, p, x, y, w, h):
        '''
        : 得到包含在面里的点数组元素
        ：x y w h构成一个面
        '''
        x1 = x
        x2 = x + w
        y1 = y
        y2 = y + h

        px = p['x'] + p['w'] / 2
        py = p['y'] + p['h'] / 2

        return (px >= x1 and px <= x2) and (py >= y1 and py <= y2)

    def draw_points(self, img, pointArr):
        # 在坐标点上画点
        for point in pointArr:
            cv2.circle(
                img, (point['x'] + int(point['w'] / 2), point['y'] + int(point['h'] / 2)), 1, (0, 0, 255), 10)

        # cv2.imshow('point', img)
        cv2.imwrite('bitwiseAnd_point.png', img)
        # cv2.waitKey()

    def get_text(self):
        # 得到mask图和网格图
        mask, joint = detect_table(self.img)
        # 得到交叉点坐标
        pointArr = find_joint_points(joint)
        if pointArr is None:
            return None

        if self.img.shape[2] == 1:  # 灰度图
            img_gray = self.img
        elif self.img.shape[2] == 3:
            img_gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(
            img_gray, self.bin_threshold, 255, cv2.THRESH_BINARY_INV)  # 二值化
        img_erode = cv2.dilate(thresh, self.kernel, iterations=self.iterations)

        contours, hierarchy = cv2.findContours(
            img_erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        result = {}
        area_coord_roi = []

        npPointArr = np.array(pointArr)
        row, col = npPointArr.shape
        data = np.empty((row, col), dtype=object)

        for i in range(len(contours)):
            cnt = contours[i]
            area = cv2.contourArea(cnt)
            if area > self.areaRange[0] and area < self.areaRange[1]:
                x, y, w, h = cv2.boundingRect(cnt)
                roi = self.img[y+self.border:(y+h)-self.border,
                               x+self.border:(x+w)-self.border]
                area_coord_roi.append((area, (x, y, w, h), roi))

                text = None
                if self.isDebug == False:
                    text = pytesseract.image_to_string(
                        roi, lang=self.lang, config='--dpi 300')  # 读取文字
                    print('tesseract cell text:', text)

                if self.isDebug:
                    cv2.rectangle(
                        self.img, (x, y), (x + w, y + h), (0, 255, 0), 2)

                    cv2.imshow('roi_img', roi)
                    # cv2.imwrite(f'roi/roi_{x}_{y}_{w}_{h}.png', roi)
                    cv2.waitKey()

                # 根据坐标，将表格值写入数组中
                for row in pointArr:
                    pList = [p for p in row if self.hitTest(p, x, y, w, h)]
                    if len(pList) > 0:
                        firtP = pList[0]
                        cx = firtP['arrX']
                        cy = firtP['arrY']
                        data[cx][cy] = text

                        # None为''
                        for p in pList[1:]:
                            cx = p['arrX']
                            cy = p['arrY']
                            data[cx][cy] = ''

                        # 画点用来debug
                        if self.isDebug:
                            for point in pList:
                                cv2.circle(
                                    self.img, (point['x'] + int(point['w'] / 2), point['y'] + int(point['h'] / 2)), 1, (0, 0, 255), 10)

        if self.isDebug:
            cv2.imshow("marked-image-debug", self.img)
            cv2.imwrite("marked-image-debug.png", self.img)
            cv2.waitKey()

        return data.tolist()

def detect_table(img):
    '''
    : 探测表格，得到网格图和网格坐标点图
    : parame: src_img 原始图像
    : 返回：
    :   mask_img 网格图，由横线和竖线交叉得到的网格图片
    :   joints_img 坐标点图，网格的交点图片
    '''
    if len(img.shape) == 2:  # 灰度图
        gray_img = img
    elif len(img.shape) == 3:
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    thresh_img = cv2.adaptiveThreshold(
        ~gray_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2)
    h_img = thresh_img.copy()
    v_img = thresh_img.copy()
    scale = 15
    h_size = int(h_img.shape[1] / scale)

    if h_size == 0:
        return None, None

    h_structure = cv2.getStructuringElement(
        cv2.MORPH_RECT, (h_size, 1))  # 形态学因子
    h_erode_img = cv2.erode(h_img, h_structure, 1)

    h_dilate_img = cv2.dilate(h_erode_img, h_structure, 1)
    # cv2.imshow("h_erode",h_dilate_img)
    v_size = int(v_img.shape[0] / scale)
    if v_size == 0:
        return None, None

    v_structure = cv2.getStructuringElement(
        cv2.MORPH_RECT, (1, v_size))  # 形态学因子
    v_erode_img = cv2.erode(v_img, v_structure, 1)
    v_dilate_img = cv2.dilate(v_erode_img, v_structure, 1)

    mask_img = h_dilate_img+v_dilate_img
    joints_img = cv2.bitwise_and(h_dilate_img, v_dilate_img)

    # cv2.imshow("joints", joints_img)
    # cv2.imshow("mask", mask_img)
    return mask_img, joints_img

def find_table(img, mask_img, save=False, save_dir=None):
    '''
    : 查找表格
    : parame: img 图片
    : parame: contours
    : 返回 表格数组:
    : x 表格左上顶点的x值
    : y 表格坐上顶点的y值
    : w 表格宽度
    : h 表格高度
    '''
    contours, hierarchy = cv2.findContours(
        mask_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)
    tables = []
    for i in range(len(contours)):
        area0 = cv2.contourArea(contours[i])
        if area0 < 20:
            continue

        # =======================查找每个表的关节数====================
        epsilon = 0.1 * cv2.arcLength(contours[i], True)
        approx = cv2.approxPolyDP(contours[i], epsilon, True)  # 获取近似轮廓
        x, y, w, h = cv2.boundingRect(approx)

        table_path = None
        # 保存文件
        if save == True:
            table_img = img[y: y + h, x: x + w]
            if save_dir is None:
                save_dir = os.getcwd() + '/tmp/tables'
            # 临时文件夹不存在则创建
            if os.path.exists(save_dir) == False:
                os.makedirs(save_dir)
            uuid_id = uuid.uuid1()
            table_path = save_dir + '/' + str(uuid_id) + '.png'

            cv2.imwrite(table_path, table_img)

        tables.append({'x': x, 'y': y, 'w': w, 'h': h,
                       'table_path': table_path})

    return tables

def find_joint_points(joint):
    '''
    : 根据joint得到网格图信息
    ：parame: joint 坐标点图片数据
    ：返回：
    ：pointArr：包含坐标点信息的数组
    ：      arrX: 数组中的行索引
    :       arrY: 数组中的列索引
    :       x: x坐标
    :       y: y坐标
    :       w: 宽度
    :       h: 高度
    '''
    ys, xs = np.where(joint > 0)

    if len(xs) == 0 or len(ys) == 0:
        return None

    mylisty = []  # 纵坐标
    mylistx = []  # 横坐标

    # 通过排序，获取跳变的x和y的值，说明是交点，否则交点会有好多像素值值相近，我只取相近值的最后一点
    # 这个10的跳变不是固定的，根据不同的图片会有微调，基本上为单元格表格的高度（y坐标跳变）和长度（x坐标跳变）
    i = 0
    myxs = np.sort(xs)
    for i in range(len(myxs)-1):
        if(myxs[i+1]-myxs[i] > 10):
            mylistx.append(myxs[i])
        i = i+1
    mylistx.append(myxs[i])  # 要将最后一个点加入

    i = 0
    myys = np.sort(ys)
    # print(np.sort(ys))
    for i in range(len(myys)-1):
        if(myys[i+1]-myys[i] > 10):
            mylisty.append(myys[i])
        i = i+1
    mylisty.append(myys[i])  # 要将最后一个点加入

    pointArr = []

    # 循环y坐标，x坐标分割表格
    for i in range(len(mylisty) - 1):
        rowArr = []

        for j in range(len(mylistx) - 1):

            x1 = mylistx[j]
            x2 = mylistx[j + 1]
            y1 = mylisty[i]
            y2 = mylisty[i + 1]

            rowArr.append({'arrX': i, 'arrY': j, "x": x1, "y": y1,
                           'w': x2 - x1, 'h': y2-y1})
            j = j + 1

        pointArr.append(rowArr)
        i = i+1
    return pointArr

def extract_tables(img_path, **settings):
    '''
    抽取表格
    '''
    tables = []

    # 读取图片
    img = cv2.imread(img_path)

    mask, joints = detect_table(img)
    if mask is None or joints is None:
        return []

    # 寻找表格并且保存为图片
    table_images = find_table(img, mask, save=True)

    # 对表格图片进行分割并使用ocr得到文本
    kernel = np.ones((4, 4), np.uint8)

    # 循环表格抓取数据
    for tImg in table_images:
        table_path = tImg['table_path']

        # 切图并识别成表格
        table_img = cv2.imread(table_path)
        data = cutImage(table_img, **settings).get_text()

        if data is not None:
            tables.append(data)

        # 删除临时文件
        os.remove(table_path)

    return tables

if __name__ == '__main__':
    img_path = 'C:/Work/HxProjects/Wpbs/Crawler/pdf2tables/test_data/Jan-2010-page-2-300.png'

    isDebug = False
    # 切图并识别成表格
    settings = {
        'pytesseract_kernel': np.ones((4, 4), np.uint8),
        'pytesseract_bin_threshold': 127,
        'pytesseract_iterations': 1,
        'pytesseract_areaRange': [10000, 100000],
        'pytesseract_isDebug': isDebug,
        'pytesseract_border': 10,
        'pytesseract_lang': 'tha+eng'
    }

    tables = extract_tables(
        img_path, **settings)

    print(len(tables))

    if isDebug == False:
        for data in tables:
            for row in data:
                print(row)
            print('*********************************************************************')


import datetime as dt
import logging
import os.path
import sqlite3
import typing as tp
from contextlib import closing

class Database:
    def __init__(self):
        self.logger = logging.getLogger('discord')
        self.connection_url: str = os.path.abspath(os.path.join('db', 'corgi.db'))

        self.server_id_column: str = 'server_id'

        self.quotes_table_name: str = 'quotes'
        self.quote_column_name: str = 'quote'
        self.author_column_name: str = 'author'
        self.time_column_name: str = 'time'

        self.relation_table_name: str = 'relations'
        self.user_id_column = 'user_id'
        self.affection_column = 'affection'
        self.updated_time_column = 'last_update'

    def get_connection(self):
        try:
            self.logger.info(f'Connecting to quotes database at connection URL "{self.connection_url}"...')
            connection = sqlite3.connect(self.connection_url)
            self.logger.info(f'Connected successfully!')
            return connection
        except sqlite3.Error as e:
            self.logger.error(f'Could not connect to database. Error: {e}')
            return None

    def execute(self, sql_query: str, params: tp.Iterable, *args):
        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                cursor.execute(sql_query, params)
            connection.commit()

    def add_quote(self, quote: str, author: str, server_id: int, time: tp.Optional[float] = None):
        if time is None:
            time = dt.datetime.now().timestamp()

        self.execute(f'insert into {self.quotes_table_name} values(?, ?, ?, ?)', (quote, author, str(time), server_id))

    def get_random_quote(self, server_id: int):
        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                cursor.execute(
                    f'select * from {self.quotes_table_name} where {self.server_id_column} = ? order by random() limit 1;',
                    [server_id])
                quote = cursor.fetchone()
                return f'{dt.datetime.fromtimestamp(float(quote[2])):%B %d, %Y %H:%M:%S}: {quote[1]} said, "{quote[0]}"'

    def add_affection(self, user_id: int, delta_affection: int, server_id: int):
        sql_statement: str = f'select {self.affection_column} from {self.relation_table_name} where {self.user_id_column} = ? and {self.server_id_column} = ? limit 1;'
        self.logger.info(f'Connecting to relationship database at: "{self.connection_url}"')
        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                self.logger.info(f'Using SQL query for affection: "{sql_statement}"')
                cursor.execute(sql_statement, [user_id, server_id])
                row = cursor.fetchone()

            update_time: dt.datetime = dt.datetime.now()

            if row is None:
                insert_sql: str = f'insert into {self.relation_table_name} values(?, ?, ?, ?)'
                self.logger.info(f'Creating a new entry into the {self.relation_table_name} table!')
                with closing(connection.cursor()) as cursor:
                    cursor.execute(insert_sql, [user_id, delta_affection, update_time, server_id])
            else:
                current_affection: int = row[0]

                new_affection: int = current_affection + delta_affection

                with closing(connection.cursor()) as cursor:
                    cursor.execute(
                        f'update {self.relation_table_name} set {self.affection_column} = ?, {self.updated_time_column} = ? where {self.user_id_column} = ? and {self.server_id_column} = ?',
                        [new_affection, update_time, user_id, server_id])
            connection.commit()

    def get_most_loved(self, server_id: int, top_n: int = 10) -> tp.List[tp.Dict[str, int]]:
        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                cursor.execute(
                    f'select {self.user_id_column}, {self.affection_column} from {self.relation_table_name} where {self.server_id_column} = ? order by {self.affection_column} desc limit {top_n};',
                    [server_id])
                rows = cursor.fetchmany(top_n)
                output = []
                for row in rows:
                    output.append({'user_id': row[0], 'affection': row[1]})
        return output

    def get_affection(self, user_id: int, server_id: int) -> int:
        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                cursor.execute(
                    f'select {self.affection_column} from {self.relation_table_name} where {self.user_id_column} = ? and {self.server_id_column} = ? limit 1',
                    [user_id, server_id])
                row = cursor.fetchone()
                if row is None:
                    return 0
                affection: int = row[0]
                return affection

    def get_max_affection(self, server_id: int) -> int:
        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                max_name: str = 'max_affection'
                cursor.execute(
                    f'select max({self.affection_column}) as {max_name} from {self.relation_table_name} where {self.server_id_column} = ? limit 1',
                    [server_id])
                row = cursor.fetchone()
                max_affection: int = row[0]
                return max_affection

"""
This doesn't work but should give you an idea of how to make a select query.
    def get(self, sql_query: str):

        with closing(self.get_connection()) as connection:
            with closing(connection.cursor()) as cursor:
                cursor.execute(sql_query)
                yield cursor.fetchone()
"""


import numpy as np

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import cond
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import special_math_ops
from tensorflow.python.util import dispatch
from tensorflow.python.util.tf_export import tf_export

def _check_params(window_length, dtype):
  """Check window_length and dtype params.

  Args:
    window_length: A scalar value or `Tensor`.
    dtype: The data type to produce. Must be a floating point type.

  Returns:
    window_length converted to a tensor of type int32.

  Raises:
    ValueError: If `dtype` is not a floating point type or window_length is not
      a scalar.
  """
  if not dtype.is_floating:
    raise ValueError('dtype must be a floating point type. Found %s' % dtype)
  window_length = ops.convert_to_tensor(window_length, dtype=dtypes.int32)
  window_length.shape.assert_has_rank(0)
  return window_length

@tf_export('signal.kaiser_window')
@dispatch.add_dispatch_support
def kaiser_window(window_length, beta=12., dtype=dtypes.float32, name=None):
  """Generate a [Kaiser window][kaiser].

  Args:
    window_length: A scalar `Tensor` indicating the window length to generate.
    beta: Beta parameter for Kaiser window, see reference below.
    dtype: The data type to produce. Must be a floating point type.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of shape `[window_length]` of type `dtype`.

  [kaiser]:
    https://docs.scipy.org/doc/numpy/reference/generated/numpy.kaiser.html
  """
  with ops.name_scope(name, 'kaiser_window'):
    window_length = _check_params(window_length, dtype)
    window_length_const = tensor_util.constant_value(window_length)
    if window_length_const == 1:
      return array_ops.ones([1], dtype=dtype)
    # tf.range does not support float16 so we work with float32 initially.
    halflen_float = (
        math_ops.cast(window_length, dtype=dtypes.float32) - 1.0) / 2.0
    arg = math_ops.range(-halflen_float, halflen_float + 0.1,
                         dtype=dtypes.float32)
    # Convert everything into given dtype which can be float16.
    arg = math_ops.cast(arg, dtype=dtype)
    beta = math_ops.cast(beta, dtype=dtype)
    one = math_ops.cast(1.0, dtype=dtype)
    halflen_float = math_ops.cast(halflen_float, dtype=dtype)
    num = beta * math_ops.sqrt(nn_ops.relu(
        one - math_ops.square(arg / halflen_float)))
    window = math_ops.exp(num - beta) * (
        special_math_ops.bessel_i0e(num) / special_math_ops.bessel_i0e(beta))
  return window

@tf_export('signal.kaiser_bessel_derived_window')
@dispatch.add_dispatch_support
def kaiser_bessel_derived_window(window_length, beta=12.,
                                 dtype=dtypes.float32, name=None):
  """Generate a [Kaiser Bessel derived window][kbd].

  Args:
    window_length: A scalar `Tensor` indicating the window length to generate.
    beta: Beta parameter for Kaiser window.
    dtype: The data type to produce. Must be a floating point type.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of shape `[window_length]` of type `dtype`.

  [kbd]:
    https://en.wikipedia.org/wiki/Kaiser_window#Kaiser%E2%80%93Bessel-derived_(KBD)_window
  """
  with ops.name_scope(name, 'kaiser_bessel_derived_window'):
    window_length = _check_params(window_length, dtype)
    halflen = window_length // 2
    kaiserw = kaiser_window(halflen + 1, beta, dtype=dtype)
    kaiserw_csum = math_ops.cumsum(kaiserw)
    halfw = math_ops.sqrt(kaiserw_csum[:-1] / kaiserw_csum[-1])
    window = array_ops.concat((halfw, halfw[::-1]), axis=0)
  return window

@tf_export('signal.vorbis_window')
@dispatch.add_dispatch_support
def vorbis_window(window_length, dtype=dtypes.float32, name=None):
  """Generate a [Vorbis power complementary window][vorbis].

  Args:
    window_length: A scalar `Tensor` indicating the window length to generate.
    dtype: The data type to produce. Must be a floating point type.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of shape `[window_length]` of type `dtype`.

  [vorbis]:
    https://en.wikipedia.org/wiki/Modified_discrete_cosine_transform#Window_functions
  """
  with ops.name_scope(name, 'vorbis_window'):
    window_length = _check_params(window_length, dtype)
    arg = math_ops.cast(math_ops.range(window_length), dtype=dtype)
    window = math_ops.sin(np.pi / 2.0 * math_ops.pow(math_ops.sin(
        np.pi / math_ops.cast(window_length, dtype=dtype) *
        (arg + 0.5)), 2.0))
  return window

@tf_export('signal.hann_window')
@dispatch.add_dispatch_support
def hann_window(window_length, periodic=True, dtype=dtypes.float32, name=None):
  """Generate a [Hann window][hann].

  Args:
    window_length: A scalar `Tensor` indicating the window length to generate.
    periodic: A bool `Tensor` indicating whether to generate a periodic or
      symmetric window. Periodic windows are typically used for spectral
      analysis while symmetric windows are typically used for digital
      filter design.
    dtype: The data type to produce. Must be a floating point type.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of shape `[window_length]` of type `dtype`.

  Raises:
    ValueError: If `dtype` is not a floating point type.

  [hann]: https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
  """
  return _raised_cosine_window(name, 'hann_window', window_length, periodic,
                               dtype, 0.5, 0.5)

@tf_export('signal.hamming_window')
@dispatch.add_dispatch_support
def hamming_window(window_length, periodic=True, dtype=dtypes.float32,
                   name=None):
  """Generate a [Hamming][hamming] window.

  Args:
    window_length: A scalar `Tensor` indicating the window length to generate.
    periodic: A bool `Tensor` indicating whether to generate a periodic or
      symmetric window. Periodic windows are typically used for spectral
      analysis while symmetric windows are typically used for digital
      filter design.
    dtype: The data type to produce. Must be a floating point type.
    name: An optional name for the operation.

  Returns:
    A `Tensor` of shape `[window_length]` of type `dtype`.

  Raises:
    ValueError: If `dtype` is not a floating point type.

  [hamming]:
    https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
  """
  return _raised_cosine_window(name, 'hamming_window', window_length, periodic,
                               dtype, 0.54, 0.46)

def _raised_cosine_window(name, default_name, window_length, periodic,
                          dtype, a, b):
  """Helper function for computing a raised cosine window.

  Args:
    name: Name to use for the scope.
    default_name: Default name to use for the scope.
    window_length: A scalar `Tensor` or integer indicating the window length.
    periodic: A bool `Tensor` indicating whether to generate a periodic or
      symmetric window.
    dtype: A floating point `DType`.
    a: The alpha parameter to the raised cosine window.
    b: The beta parameter to the raised cosine window.

  Returns:
    A `Tensor` of shape `[window_length]` of type `dtype`.

  Raises:
    ValueError: If `dtype` is not a floating point type or `window_length` is
      not scalar or `periodic` is not scalar.
  """
  if not dtype.is_floating:
    raise ValueError('dtype must be a floating point type. Found %s' % dtype)

  with ops.name_scope(name, default_name, [window_length, periodic]):
    window_length = ops.convert_to_tensor(window_length, dtype=dtypes.int32,
                                          name='window_length')
    window_length.shape.assert_has_rank(0)
    window_length_const = tensor_util.constant_value(window_length)
    if window_length_const == 1:
      return array_ops.ones([1], dtype=dtype)
    periodic = math_ops.cast(
        ops.convert_to_tensor(periodic, dtype=dtypes.bool, name='periodic'),
        dtypes.int32)
    periodic.shape.assert_has_rank(0)
    even = 1 - math_ops.mod(window_length, 2)

    n = math_ops.cast(window_length + periodic * even - 1, dtype=dtype)
    count = math_ops.cast(math_ops.range(window_length), dtype)
    cos_arg = constant_op.constant(2 * np.pi, dtype=dtype) * count / n

    if window_length_const is not None:
      return math_ops.cast(a - b * math_ops.cos(cos_arg), dtype=dtype)
    return cond.cond(
        math_ops.equal(window_length, 1),
        lambda: array_ops.ones([window_length], dtype=dtype),
        lambda: math_ops.cast(a - b * math_ops.cos(cos_arg), dtype=dtype))


import os
import config
import pandas as pd
import utils

from sklearn.metrics import mean_squared_error

class ErrorReviewer(object):

    def __init__(self):

        self.full_results = self.load_results()
        self.calculate_errors()
        self.biggest_errors = self.get_biggest_errors()
        self.inspect_biggest_errors()
        # print(self.errors.shape, self.biggest_errors.shape, self.results.shape)
        # print(self.errors.head(), self.biggest_errors.head(), self.results.head())

    def calculate_errors(self):

        # check results as they stand
        y_gth = self.full_results[["orig_x", "orig_y"]]
        y_pred = self.full_results[["pred_x", "pred_y"]]
        mse = mean_squared_error(y_gth, y_pred)
        print("Mean Squared Error: " + str(mse))

        # now check after removing blinks
        self.results_no_blinks = self.full_results.copy(deep=True)
        self.results_no_blinks = self.results_no_blinks[self.results_no_blinks["orig_x"]>0]
        y_gth = self.results_no_blinks[["orig_x", "orig_y"]]
        y_pred = self.results_no_blinks[["pred_x", "pred_y"]]

        mse = mean_squared_error(y_gth, y_pred)
        print("Mean Squared Error: " + str(mse))

    def load_results(self):

        directory = os.path.join(config.model_save_folder, config.model_name)

        model_data = []

        for filepath in ["full_results"]:
            _ = os.path.join(directory, filepath+".csv")
            _df = pd.read_csv(_, encoding="utf-8")
            model_data.append(_df)

        return model_data[0]

    def get_biggest_errors(self):

        # all_errors = pd.concat([self.full_results["filenames"], self.full_results[["diff_lx", "diff_ly", "diff_rx", "diff_ry"]].abs()], axis=1)

        self.full_results["diff_x"] = self.full_results["pred_x"] - self.full_results["orig_x"]
        self.full_results["diff_y"] = self.full_results["pred_y"] - self.full_results["orig_y"]

        biggest_errors = self.full_results[
            (self.full_results["diff_x"] > 10) | (self.full_results["diff_x"] < -10) |
            (self.full_results["diff_y"] > 10) | (self.full_results["diff_y"] < -10)
        ][["filenames", "orig_x", "orig_y", "pred_x", "pred_y", "diff_x", "diff_y"]]

        # biggest_errors = biggest_errors[
        #     (self.full_results["diff_x"] > 5) | (self.full_results["diff_x"] < -5) |
        #     (self.full_results["diff_y"] > 5) | (self.full_results["diff_y"] < -5)
        # ][["filenames", "orig_x", "orig_y", "pred_x", "pred_y", "diff_x", "diff_y"]]

        print(biggest_errors.head())
        biggest_errors.to_csv("medium_errors.csv")

        biggest_errors.columns = ["filenames", "orig_x", "orig_y", "pred_x", "pred_y", "diff_x", "diff_y"]

        print(self.full_results.shape, biggest_errors.shape)

        return biggest_errors

    def inspect_biggest_errors(self):

        retrain_list, relabel_list = [], []

        row=0
        for img_filepath in self.biggest_errors["filenames"]:

            print(str(row) +"/"+ str(self.biggest_errors.shape[0]))

            full_img_filepath = os.path.join(os.getcwd(), "data", "processed", "cropped_imgs", img_filepath)
            eye_gth = (self.biggest_errors["orig_x"].iloc[row], self.biggest_errors["orig_y"].iloc[row])
            eye_preds = (self.biggest_errors["pred_x"].iloc[row], self.biggest_errors["pred_y"].iloc[row])

            retrain = utils.review_data(full_img_filepath, eye_gth, eye_preds)

            if retrain == 1:
                print("sdave")
                retrain_list.append([img_filepath, self.biggest_errors["orig_x"].iloc[row], self.biggest_errors["orig_y"].iloc[row]])
            elif retrain == 2:
                relabel_list.append([img_filepath, self.biggest_errors["orig_x"].iloc[row], self.biggest_errors["orig_y"].iloc[row]])

            row+=1

        retrain_list = pd.DataFrame(retrain_list)
        retrain_list.columns = ["filename", "orig_x", "orig_y"]
        retrain_list.to_csv("retrain.csv")

        try:
            relabel_list = pd.DataFrame(relabel_list)
            relabel_list.columns = ["filename", "orig_x", "orig_y"]
            relabel_list.to_csv("relabel.csv")
        except:
            print("No images to relabel")

if __name__ == '__main__':

    error_review = ErrorReviewer()


"""
Project by Clinton Anani
A simple intro to openCV
This project demonstrates reading images and videos, as
well as writing them to local storage

github: yendiDev
email: kceequan01@gmail.com

"""

import cv2

# load image
img = cv2.imread('lena.jpg', -1)

# show image
# cv2.imshow('Lena Image', img)
# cv2.waitKey(2000)
# cv2.destroyAllWindows()

# write image to new file
# cv2.imwrite('lena_copy.jpg', img)

# showing video
cap = cv2.VideoCapture(0)

# fourcc for video
# fourcc = cv2.VideoWriter_fourcc(*'XVID')

# video writer class
# writer = cv2.VideoWriter('recorded_video.avi', fourcc, 20.0, (640, 480))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

while cap.isOpened():
    return_val, frame = cap.read()
    if return_val:
        font = cv2.FONT_HERSHEY_SIMPLEX
        text = 'Width: '+str(cv2.CAP_PROP_FRAME_WIDTH) + ' Height: '+str(cv2.CAP_PROP_FRAME_HEIGHT)
        new_frame = cv2.putText(frame, text, (12, 312), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
        cv2.imshow('Frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
# writer.release()
cv2.destroyAllWindows()


'''Aquire and Prep Happy data'''
import pandas as pd
import numpy as np
import os

###################################################
################## ACQUIRE DATA ###################
###################################################

'''Reads happy into a pandas DataFrame from CSV'''
def get_happy_2015():
    filename = "2015.csv"

    os.path.isfile(filename)
    return pd.read_csv(filename)

def get_happy_2016():
    filename = "2016.csv"

    os.path.isfile(filename)
    return pd.read_csv(filename)

def get_happy_2017():
    filename = "2017.csv"

    os.path.isfile(filename)
    return pd.read_csv(filename)

def get_happy_2018():
    filename = "2018.csv"

    os.path.isfile(filename)
    return pd.read_csv(filename)

def get_happy_2019():
    filename = "2019.csv"

    os.path.isfile(filename)
    return pd.read_csv(filename)

###################################################
################## PREPARE DATA ###################
###################################################

def wrangle_happi():
    '''
    Reads happy into a pandas DataFrame from CSV
    drop columns, drop any rows with Null values,
    rename columns, and return cleaned DataFrames.
    '''
    # Acquire data
    df_2015 = get_happy_2015()
    df_2016 = get_happy_2016()
    df_2017 = get_happy_2017()
    df_2018 = get_happy_2018()
    df_2019 = get_happy_2019()

    # Drop columns
    df_2015.drop(columns=['Region', 'Standard Error', 'Dystopia Residual'], inplace=True)
    df_2016.drop(columns=['Region', 'Lower Confidence Interval', 'Upper Confidence Interval','Dystopia Residual'], inplace=True)
    df_2017.drop(columns=['Whisker.high', 'Whisker.low','Dystopia.Residual'], inplace=True)

    # Rename columns

    df_2015 = df_2015.rename(columns={'Family':'Social Support'})
    df_2015 = df_2015.rename(columns={'Happiness Score':'Happiness_Score'})
    df_2015 = df_2015.rename(columns={'Happiness Rank':'Happiness_Rank'})
    df_2015 = df_2015.rename(columns={'Trust (Government Corruption)':'Perceptions_Corruption_Gov'})
    df_2015 = df_2015.rename(columns={'Health (Life Expectancy)':'Health_Life_Expectancy'})
    df_2015 = df_2015.rename(columns={'Economy (GDP per Capita)':'Economy'})

    df_2016 = df_2016.rename(columns={'Economy (GDP per Capita)':'Economy'})
    df_2016 = df_2016.rename(columns={'Health (Life Expectancy)':'Health_Life_Expectancy'})
    df_2016 = df_2016.rename(columns={'Family':'Social Support'})
    df_2016 = df_2016.rename(columns={'Happiness Score':'Happiness_Score'})
    df_2016 = df_2016.rename(columns={'Happiness Rank':'Happiness_Rank'})
    df_2016 = df_2016.rename(columns={'Trust (Government Corruption)':'Perceptions_Corruption_Gov'})

    df_2017 = df_2017.rename(columns={'Happiness.Score':'Happiness_Score'})
    df_2017 = df_2017.rename(columns={'Happiness.Rank':'Happiness_Rank'})
    df_2017 = df_2017.rename(columns={'Family':'Social Support'})
    df_2017 = df_2017.rename(columns={'Economy..GDP.per.Capita.':'Economy'})
    df_2017 = df_2017.rename(columns={'Health..Life.Expectancy.':'Health_Life_Expectancy'})
    df_2017 = df_2017.rename(columns={'Trust..Government.Corruption.':'Perceptions_Corruption_Gov'})

    df_2018 = df_2018.rename(columns={'Country or region':'Country'})
    df_2018 = df_2018.rename(columns={'Healthy life expectancy':'Health_Life_Expectancy'})
    df_2018 = df_2018.rename(columns={'Overall rank':'Happiness_Rank'})
    df_2018 = df_2018.rename(columns={'Score':'Happiness_Score'})
    df_2018 = df_2018.rename(columns={'GDP per capita':'Economy'})
    df_2018 = df_2018.rename(columns={'Perceptions of corruption':'Perceptions_Corruption_Gov'})
    df_2018 = df_2018.rename(columns={'Freedom to make life choices':'Freedom'})

    df_2019 = df_2019.rename(columns={'Freedom to make life choices':'Freedom'})
    df_2019 = df_2019.rename(columns={'Country or region':'Country'})
    df_2019 = df_2019.rename(columns={'Healthy life expectancy':'Health_Life_Expectancy'})
    df_2019 = df_2019.rename(columns={'Overall rank':'Happiness_Rank'})
    df_2019 = df_2019.rename(columns={'Score':'Happiness_Score'})
    df_2019 = df_2019.rename(columns={'GDP per capita':'Economy'})
    df_2019 = df_2019.rename(columns={'Perceptions of corruption':'Perceptions_Corruption_Gov'})

    # Reindex columns to match
    column_titles = ['Country', 'Happiness_Rank', 'Happiness_Score', 'Economy', 'Social Support', 'Health_Life_Expectancy', 'Freedom', 'Perceptions_Corruption_Gov', 'Generosity']
    df_2017 = df_2017.reindex(columns=column_titles)
    df_2018 = df_2018.reindex(columns=column_titles)
    df_2019 = df_2019.reindex(columns=column_titles)

    # Adding Year columns
    df_2015['Year']= 2015
    df_2016['Year']= 2016
    df_2017['Year']= 2017
    df_2018['Year']= 2018
    df_2019['Year']= 2019

    return df_2015, df_2016, df_2017, df_2018, df_2019

def join_happy(df_2015, df_2016, df_2017, df_2018, df_2019):
    '''
    Function joins all clean dataframes.
    '''
    happy_data = [df_2015, df_2016, df_2017, df_2018, df_2019]
    happy_df = pd.concat(happy_data)
    happy_df.drop(columns=['Social Support'], inplace=True)
    happy_df.fillna(happy_df.mean(numeric_only=True).round(1), inplace=True)

    return happy_df


from typing import Optional

class FhirSenderException(Exception):
    def __init__(
        self,
        exception: Exception,
        url: str,
        json_data: str,
        response_text: Optional[str],
        response_status_code: Optional[int],
        message: str,
    ) -> None:
        self.exception: Exception = exception
        self.url: str = url
        self.data: str = json_data
        super().__init__(
            f"FHIR send failed to {url} {response_status_code}: {json_data}.  {message} {response_text}"
        )


from django.contrib import admin
from django.urls import include, path, re_path
from rest_framework import routers

from meraki.core.views import UserViewSet, GroupViewSet
from product.views import CategoryViewSet, ProductViewSet

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
    openapi.Info(
        title="E-commerce API",
        default_version="v1",
    ),
    public=True,
    permission_classes=[permissions.AllowAny],
)

router = routers.DefaultRouter()
router.register(r"users", UserViewSet)
router.register(r"groups", GroupViewSet)
router.register(r"product", ProductViewSet)
router.register(r"category", CategoryViewSet)

urlpatterns = [
    re_path(
        r"^swagger(?P<format>\.json|\.yaml)$",
        schema_view.without_ui(cache_timeout=0),
        name="schema-json",
    ),
    re_path(
        r"^swagger/$",
        schema_view.with_ui("swagger", cache_timeout=0),
        name="schema-swagger-ui",
    ),
    re_path(
        r"^redoc/$", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"
    ),
    path("", include(router.urls)),
    path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
    path("admin/", admin.site.urls),
    path("product/", include("product.urls")),
]


n,t = map(int,input().split())
pairs = []
for i in range(n):
    pairs.append(tuple(map(int,input().split())))

inventory = 0
result = pairs[0][0]
final_time = t
for i in range(n-1):
    print(pairs[i])
    # note next delivery time
    if pairs[i][0] > t: break
    if pairs[i+1][0] > t:
        pairs[i+1] = (t,-1)
        final_time = pairs[i][0]
    next_date = pairs[i+1][0]
    inventory += pairs[i][1]
    # calculate expected time to start starving
    exp = pairs[i][0] + inventory # plus 1??
    if exp <= next_date: # <=??
        # this is the date starving starts
        result += next_date - exp
        inventory = 0
        print("would starve")
    else:
        # shipment before starving starts
        inventory -= (next_date - pairs[i][0])
        print("would not starve")
    print(inventory)
    print()

# special case for the very end
print(result)


import argparse
from random import randrange
from multiprocessing import Process, Array

parser = argparse.ArgumentParser()
parser.add_argument("--input_file")
args = parser.parse_args()

def mergeSort(arr):
    mergeSort_(0, arr, 0, len(arr)-1)

def mergeSort_(id, arr, i, j):
    if j-i > 0:
        median = (i+j) // 2
        left_merge = Process(target=mergeSort_,
                         args=(0, arr, i, median))
        right_merge = Process(target=mergeSort_,
                              args=(0, arr, median+1, j))

        left_merge.start()
        right_merge.start()

        left_merge.join()
        right_merge.join()

        # when merging of child segements is finished
        # merge child segments to parent

        parent_merge = Process(target=merge, args=(0, arr, i, median, j))
        parent_merge.start()
        parent_merge.join()

def merge(id, arr, i, median, j):
    temp = [0 for _ in range(j-i+1)]

    index = 0
    l_index = i
    r_index = median+1

    while l_index <= median and r_index <= j:
        if arr[l_index] < arr[r_index]:
            temp[index] = arr[l_index]
            l_index += 1
        else:
            temp[index] = arr[r_index]
            r_index += 1
        index +=1

    while l_index <= median:
        temp[index] = arr[l_index]
        index += 1
        l_index += 1

    while r_index <= j:
        temp[index] = arr[r_index]
        index += 1
        r_index += 1

    for i_ in range(0, j-i+1):
        arr[i+i_] = temp[i_]

if __name__ == "__main__":
    with open(args.input_file, "r") as f:
        N = int(f.readline())

    # initialize arr with random values
    initial_arr = [randrange(10000) for _ in range(N)]
    arr = Array('i', initial_arr)
    print(list(arr))

    mergeSort(arr)

    print(list(arr))


import flask
from flask import Flask, render_template, request
from lib.preprocessing import *

val_dict = {'Gender': {'NaN': 1, 'male': 1, 'female': 0},
            'Married': {'NaN': 1, 'yes': 1, 'no': 0},
            'Dependents': {'NaN': 1},
            'Education': {'NaN': 1, 'graduated': 1, 'not graduated': 0},
            'Self_Employed': {'NaN': 0, 'yes': 1, 'no': 0},
            'ApplicantIncome': {'NaN': 4000},
            'CoapplicantIncome': {'NaN': 0},
            'LoanAmount': {'NaN': 10000},
            'Loan_Amount_Term': {'NaN': 360},
            'Credit_History': {'NaN': 1, 'yes': 1, 'no': 0},
            'Property_Area': {'NaN': 0, 'urban': 2, 'semi': 1, 'rural': 0},
            'TotalIncome': {'NaN': 4000}
            }

app = Flask(__name__)

@app.route('/')
def entry():
    return render_template('form.html', the_title='Loan Approval Prediction', form_title='Enter profile details')

@app.route('/analyse', methods=['GET', 'POST'])
def proc():
    test_val = create_df()
    name = request.form["name"]
    amount = request.form["LoanAmount"]
    for feature in test_val.columns:
        if(feature != "TotalIncome"):
            if request.form[feature]:
                if((feature == "ApplicantIncome") or (feature == "CoapplicantIncome") or (feature == "LoanAmount")):
                    test_val[feature] = str(int(request.form[feature])//70)
                else:
                    test_val[feature] = request.form[feature]
            else:
                test_val[feature] = 'NaN'
    fix_missing(test_val, val_dict)
    test_val["TotalIncome"] = test_val["ApplicantIncome"] + \
        test_val["CoapplicantIncome"]
    test_val = test_val.astype(float)
#    scaler = load_scaler()
#    test_val = scaler.transform(test_val)
    resp = {}
    model = load_model()
    pred = model.predict(test_val)
    resp['prediction'] = str(pred).strip('[]')
    status = pred
    return render_template('result.html', status=status, name=name, amount=amount)

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


import redis
import random
# 连接redis
# r = redis.StrictRedis(host='localhost',  # 主机ip
#                       port=6379,  # 端口
#                       db=0,  # 数据库索引
#                       password="",  # 密码
#                       decode_responses=True)  # 设置解码，因为使用get()方法获取出来的值是一个字节类型，这个设置为get()方法获取的值是字符串类型

# 使用连接池连接redis
def getredisconn():
    r = redis.StrictRedis(connection_pool=pool)
    return r
def getips(r=getredisconn()):
    key='proxies:universal'
    x=random.randint(0,800)
    ips=r.zrange(key,x,x+20)
    return ips


from django import forms
from django.db.models import fields
from django.forms import widgets
from .models import Artikel, Comment

class ArtikelForms(forms.ModelForm):
    class Meta:
        model = Artikel
        fields = ('judul', 'body', 'kategory', 'img')
        widgets = {
            "judul" : forms.TextInput(
                attrs={
                    'class' : 'form-control',
                    'type' : 'text',
                    'placeholder' : 'judul artikel',
                    'required' : True
                }),
            "body" : forms.Textarea(
                attrs={
                    'class' : 'form-control',
                    'cols' : '30',
                    'rows' : '10',
                    'required' : True
                }),
            "kategory" : forms.Select(
                attrs={
                    'class' : 'selectpicker',
                    'type' : 'text',
                    'required' : True,
                    'data-style' : 'btn-warning btn-outline',
                    'data-title' : 'Pilih Kategori',
                    'data-menu-style' : 'dropdown-blue',
                }),
            # "img" : forms.FileInput(
            #     attrs={
            #         'class' : 'figure-img img-fluid rounded',
            #         'type' : 'file',
            #         'required' : True
            #     }),
        }

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('nama', 'komen')


# Create an "arbitrary" 2d List
a = [[2, 3, 5], [1, 4, 7]]
print("Before: a =", a)

# Now find its dimensions
rows = len(a)
cols = len(a[0])

# And now loop over every element
# Here, we'll add one to each element,
# just to make a change we can easily see
for row in range(rows):
    for col in range(cols):
        # This code will be run rows*cols times, once for each
        # element in the 2d list
        a[row][col] += 1

# Finally, print the results
print("After:  a =", a)


from turtle import Screen, Turtle

WIDTH = 800
HEIGHT = 600

class ScreenSetup:
    """Class to initialize screen

    Attribute: screen -- > holds Screen object"""

    def __init__(self):
        #Screen Setting
        screen = Screen()
        screen.setup(width=WIDTH, height=HEIGHT)
        screen.bgcolor("black")
        screen.tracer(0)
        screen.title("Pong Game")
        self.screen = screen

        #center Line setting
        turtle = Turtle()
        turtle.hideturtle()
        turtle.color("white")
        turtle.penup()
        turtle.goto(0, -300)
        turtle.pendown()
        turtle.setheading(90)
        for _ in range(8):
            turtle.forward(40)
            turtle.penup()
            turtle.forward(40)
            turtle.pendown()


import time as t
import random as r
from spade.agent import Agent
from spade.behaviour import FSMBehaviour, State
from spade.message import Message
from spade import quit_spade

mreza = [['', '', ''], ['', '', ''], ['', '', '']]
igraci = []
zadnjiIgrao = " "

def ProvjeraJednakosti(a, b, c):
    return a == b and b == c and a != ''

def ProvjeraPobjednika():

    pobjednik = None

    for i in range(3):
        if (ProvjeraJednakosti(mreza[i][0], mreza[i][1], mreza[i][2])):
            pobjednik = mreza[i][0]

    for i in range(3):
        if (ProvjeraJednakosti(mreza[0][i], mreza[1][i], mreza[2][i])):
            pobjednik = mreza[0][i]

    if (ProvjeraJednakosti(mreza[0][0], mreza[1][1], mreza[2][2])):
        pobjednik = mreza[0][0]

    if (ProvjeraJednakosti(mreza[2][0], mreza[1][1], mreza[0][2])):
        pobjednik = mreza[2][0]

    praznaMjesta = 0
    for i in range(3):
        for j in range(3):
            if (mreza[i][j] == ''):
                praznaMjesta += 1

    if (pobjednik == None and praznaMjesta == 0):
        return 'Nerjeseno'
    else:
        return pobjednik

def pretvoriMrezu(temp):
    temp = temp.replace("[", "")
    temp = temp.replace("]", "")
    temp = temp.replace("'", "")
    temp = temp.replace(" ", "")
    temp = temp.split(",")

    mreza[0][0] = temp[0]
    mreza[0][1] = temp[1]
    mreza[0][2] = temp[2]
    mreza[1][0] = temp[3]
    mreza[1][1] = temp[4]
    mreza[1][2] = temp[5]
    mreza[2][0] = temp[6]
    mreza[2][1] = temp[7]
    mreza[2][2] = temp[8]

def crtanje():
    print(' {:^3}  | {:^3}  | {:^3} '.format(
        mreza[0][0], mreza[0][1], mreza[0][2]))
    print('-------------------')
    print(' {:^3}  | {:^3}  | {:^3} '.format(
        mreza[1][0], mreza[1][1], mreza[1][2]))
    print('-------------------')
    print(' {:^3}  | {:^3}  | {:^3} \n\n'.format(
        mreza[2][0], mreza[2][1], mreza[2][2]))

class Master(Agent):

    class KrizicKruzic(FSMBehaviour):

        async def on_start(self):
            print("Pripremam igru!")

        async def on_end(self):
            print("Igra gotova!")
            await self.agent.stop()

    class Inicijalizacija(State):
        async def run(self):

            global zadnjiIgrao

            msg = await self.receive(timeout=60)

            if (msg.get_metadata("intent") == "Prijava"):
                msg = msg.body
                igraci.append(msg)

            if (len(igraci) == 2):

                if r.randint(0, 10) % 2:
                    zadnjiIgrao = "0"

                    msg = Message(
                        to=igraci[0],
                        body="Igras",
                        metadata={
                            "intent": "Poredak",
                            "mreza": str(mreza)})

                    await self.send(msg)

                    msg = Message(
                        to=igraci[1],
                        body="Neigras",
                        metadata={
                            "intent": "Poredak"})

                    await self.send(msg)

                    self.set_next_state("Zapocni")

                else:
                    zadnjiIgrao = "1"

                    msg = Message(
                        to=igraci[1],
                        body="Igras",
                        metadata={
                            "intent": "Poredak",
                            "mreza": str(mreza)})
                    await self.send(msg)

                    msg = Message(
                        to=igraci[0],
                        body="Neigras",
                        metadata={
                            "intent": "Poredak"})

                    await self.send(msg)
                    self.set_next_state("Zapocni")

            elif len(igraci) > 2:
                igraci.clear
                self.set_next_state("Inicijalizacija")

            else:
                self.set_next_state("Inicijalizacija")

    class Zapocni(State):

        async def run(self):
            print("Inicijaliziram ploču")
            crtanje()
            self.set_next_state("Obrada")

    class Obrada(State):
        async def run(self):

            msg = await self.receive(timeout=60)

            if (msg.get_metadata("intent") == "Potez"):

                temp = msg.get_metadata("mreza")
                pretvoriMrezu(temp)
                crtanje()

                pobjednik = ProvjeraPobjednika()

                if pobjednik is None:
                    self.set_next_state("NastavakIgre")
                else:
                    self.set_next_state("ZavrsiIgru")

            else:
                self.set_next_state("Obrada")

    class NastavakIgre(State):
        async def run(self):

            global zadnjiIgrao

            if zadnjiIgrao == "1":
                zadnjiIgrao = "0"

                msg = Message(
                    to=igraci[0],
                    body="Igras",
                    metadata={
                        "intent": "Nastavak",
                        "mreza": str(mreza)})
                await self.send(msg)

            else:
                zadnjiIgrao = "1"

                msg = Message(
                    to=igraci[1],
                    body="Igras",
                    metadata={
                        "intent": "Nastavak",
                        "mreza": str(mreza)})
                await self.send(msg)

            self.set_next_state("Obrada")

    class ZavrsiIgru(State):
        async def run(self):

            pobjednik = ProvjeraPobjednika()

            if pobjednik == "Nerjeseno":
                print("Rezultat je nerjesen")
            else:
                print("Pobjedio je {}".format(pobjednik))

            for igrac in igraci:
                msg = Message(
                    to=igrac,
                    body="Gotova igra!",
                    metadata={
                        "intent": "Kraj",
                        "pobjednik": pobjednik})
                await self.send(msg)

    async def setup(self):

        print("Inicijalizacija igre!")

        fsm = self.KrizicKruzic()

        fsm.add_state(name="Inicijalizacija",
                      state=self.Inicijalizacija(), initial=True)
        fsm.add_state(name="Zapocni", state=self.Zapocni())
        fsm.add_state(name="Obrada", state=self.Obrada())
        fsm.add_state(name="NastavakIgre", state=self.NastavakIgre())
        fsm.add_state(name="ZavrsiIgru", state=self.ZavrsiIgru())

        fsm.add_transition(source="Inicijalizacija", dest="Inicijalizacija")
        fsm.add_transition(source="Inicijalizacija", dest="Zapocni")
        fsm.add_transition(source="Zapocni", dest="Obrada")
        fsm.add_transition(source="Obrada", dest="Obrada")
        fsm.add_transition(source="Obrada", dest="NastavakIgre")
        fsm.add_transition(source="NastavakIgre", dest="Obrada")
        fsm.add_transition(source="Obrada", dest="ZavrsiIgru")

        self.add_behaviour(fsm)

if __name__ == '__main__':

    master = Master("agent@localhost", "tajna")
    masterRunning = master.start()
    masterRunning.result()

    while master.is_alive():
        try:
            t.sleep(1)
        except KeyboardInterrupt:
            print("Prisilno gasenje!")
            break

    master.stop()

    quit_spade()


a=[]
b=[]
a=input("Entrer la phrase")

for i in range(0,len(a)):
    if a[i].isalpha()==True:
        b.append(a[i].lower())

a=b[::-1]

if a==b:
    print("C'est un palindrome !")
else:
    print("Ce n'est pas un palindrome.")


"""
PREMIS vocabularies following the national specifications
"""
from __future__ import unicode_literals

PREMIS_EVENT_OUTCOME_TYPES = [
    'success',
    'failure',
    '(:unac)',
    '(:unal)',
    '(:unap)',
    '(:unav)',
    '(:unkn)',
    '(:none)',
    '(:null)',
    '(:tba)',
    '(:etal)'
]

PREMIS_AGENT_TYPES = ['software', 'hardware', 'person', 'organization']


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

"""The setup script."""

from setuptools import find_packages
from setuptools import setup

with open('README.rst') as readme_file:
    readme = readme_file.read()

with open('HISTORY.rst') as history_file:
    history = history_file.read()

requirements = ['pytz']

setup_requirements = ['pytest-runner', ]

test_requirements = ['pytest', ]

setup(
    author="Sebastian Schaffer",
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
    ],
    description="Jsoner allows you to easily "
                "convert your classes to json and back.",
    long_description_content_type='text/x-rst',
    install_requires=requirements,
    license="MIT license",
    long_description=readme + '\n\n' + history,
    include_package_data=True,
    keywords='jsoner json celery django serialization',
    name='jsoner',
    packages=find_packages(include=['jsoner']),
    setup_requires=setup_requirements,
    test_suite='tests',
    tests_require=test_requirements,
    url='https://github.com/sschaffer92/jsoner',
    version='0.2.0',
    zip_safe=False,
    python_requires='>=3.4',
    project_urls={
        'Documentation': 'https://jsoner.readthedocs.io/en/latest/',
        'Source': 'https://github.com/sschaffer92/jsoner/',
        'Tracker': 'https://github.com/sschaffer92/jsoner/issues',
    },
)


"""
1060. Missing Element in Sorted Array
Given an integer array nums which is sorted in ascending order and all of its elements are unique and given also an integer k, return the kth missing number starting from the leftmost number of the array

Example1:
Input: nums = [4, 7, 9, 10], k = 1
Output: 5
Explanation: The first missing number is 5.

Example2:
Input: nums = [4, 7, 9, 10], k = 3
Output: 8
Explanation: The missing numbers are [5, 6, 8, ...], hence the third missing number is 8.

Example3:
Input: nums = [1, 2, 4], k = 3
Output: 6
Explanation: The missing numbers are [3, 5, 6, 7, ...], hence the third missing number is 6.

Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^7
nums is sorted in ascending order, and all the elements are unique.
1 <= k <= 10^8
"""

"""
Note:
1. Binary Search: O(logn) time | O(1) space
How many missing number between index 0 and index mid?
nums[mid] - nums[0] - mid
numOfMissingNumbers < k: left = mid + 1, otherwise right = mid
"""

from typing import List
import unittest
class Solution:
    def missingElement(self, nums: List[int], k: int) -> int:
        left, right = 0, len(nums)
        while left < right:
            mid = (left + right) // 2
            numOfMissingNumbers = nums[mid] - nums[0] - mid
            if numOfMissingNumbers < k:
                left = mid + 1
            else:
                right = mid
        return nums[0] + k + left - 1

# Unit Tests
funcs = [Solution().missingElement]

class TestMissingElement(unittest.TestCase):
    def testMissingElement1(self):
        for func in funcs:
            nums = [4, 7, 9, 10]
            k = 1
            self.assertEqual(func(nums=nums, k=k), 5)

    def testMissingElement2(self):
        for func in funcs:
            nums = [4, 7, 9, 10]
            k = 3
            self.assertEqual(func(nums=nums, k=k), 8)

    def testMissingElement3(self):
        for func in funcs:
            nums = [1, 2, 4]
            k = 3
            self.assertEqual(func(nums=nums, k=k), 6)

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


from collections import Counter

def canConstruct(ransomNote: str, magazine: str) -> bool:
    c1, c2 = Counter(ransomNote), Counter(magazine)

    # Counts the number of times a specific letter matched
    count = 0
    for letter in ransomNote:

        # Count only characters lower than letters in magazine
        if c1[letter] <= c2[letter]:
            count += 1

    length = len([i for i in ransomNote])
    if count == length:
        return True
    else:
        return False

ransomNote = "aa"
magazine = "aab"

print(canConstruct(ransomNote, magazine))


s=input()
n=len(s)
lng=[1]*n
vowels=['a','e','i','o','u']
lst=[]
for i in s:
    if i in vowels:
        lst.append(i)
print(lst)
for i in range(len(lst)):
    for j in range(i):
        if(lst[i]>=lst[j]):
            lng[i]=max(lng[j]+1,lng[i])

print(max(lng))


from django.http import HttpResponse
from .models import OrderInformation, OrderLineItem
from products.models import Product
from profiles.models import UserProfile
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.conf import settings
import json
import time

class StripeWH_Handler:
    # Handle Stripe Webhooks

    def __init__(self, request):

        self.request = request

    # sends user confirmation email
    def _sending_confirmation_email(self, order):

        custom_email_address = order.email_address
        subject = render_to_string(
            'checkout/email_confirmation/email_confirmation_subject.txt',
            {'order': order})
        body = render_to_string(
            'checkout/email_confirmation/email_confirmation_body.txt',
            {'order': order, 'contact_email_address': settings.DEFAULT_FROM_EMAIL})

        send_mail(
            subject,
            body,
            settings.DEFAULT_FROM_EMAIL,
            [custom_email_address]
        )

    # handle unknown webhook
    def handle_event(self, event):

        return HttpResponse(
            content=f'Unhandled webhook received: {event["type"]}',
            status=200)

    # handle the successful webhook payment
    def handle_payment_intent_succeeded(self, event):

        intent = event.data.object
        pid = intent.id
        bag = intent.metadata.bag
        save_info = intent.metadata.save_info

        billing_details = intent.charges.data[0].billing_details
        shipping_details = intent.shipping
        grand_total = round(intent.charges.data[0].amount / 100, 2)

        for field, value in shipping_details.address.items():
            if value == "":
                shipping_details.address[field] = None

        # updates the users profile information if save info is checked
        profile = None
        username = intent.metadata.username
        if username != 'AnonUser':
            profile = UserProfile.objects.get(user__username=username)
            if save_info:
                profile.default_full_name = shipping_details.name
                profile.default_street_name_1 = shipping_details.address.line1
                profile.default_street_name_2 = shipping_details.address.line2
                profile.default_town_or_city = shipping_details.address.city
                profile.default_county = shipping_details.address.state
                profile.default_postal_code = shipping_details.address.postal_code
                profile.save()

        order_exists = False
        attempt = 1
        while attempt <= 5:
            try:
                order = OrderInformation.objects.get(
                    full_name__iexact=shipping_details.name,
                    email_address__iexact=billing_details.email,
                    contact_number__iexact=shipping_details.phone,
                    street_name_1__iexact=shipping_details.address.line1,
                    street_name_2__iexact=shipping_details.address.line2,
                    town_or_city__iexact=shipping_details.address.city,
                    county__iexact=shipping_details.address.state,
                    postal_code__iexact=shipping_details.address.postal_code,
                    grand_total=grand_total,
                    original_shopping_bag=bag,
                    stripe_pid=pid,
                )
                order_exists = True
                break
            except OrderInformation.DoesNotExist:
                attempt += 1
                time.sleep(1)
        if order_exists:
            self._sending_confirmation_email(order)
            return HttpResponse(
                content=f'Webhook received: {event["type"]} | Success, Order already verifed in our databse',
                status=200)
        else:
            order = None
            try:
                order = OrderInformation.objects.create(
                    full_name=shipping_details.name,
                    user_profile=profile,
                    email_address=billing_details.email,
                    contact_number=shipping_details.phone,
                    street_name_1=shipping_details.address.line1,
                    street_name_2=shipping_details.address.line2,
                    town_or_city=shipping_details.address.city,
                    county=shipping_details.address.state,
                    postal_code=shipping_details.address.postal_code,
                    original_shopping_bag=bag,
                    stripe_pid=pid,
                )
                for item_id, item_data in json.loads(bag).items():
                    product = Product.objects.get(id=item_id)
                    if isinstance(item_data, int):
                        order_line_item = OrderLineItem(
                            order=order,
                            product=product,
                            quantity=item_data,
                        )
                        order_line_item.save()
                    else:
                        for size, quantity in item_data['item_by_size'].items():
                            order_line_item = OrderLineItem(
                                order=order,
                                product=product,
                                quantity=quantity,
                                product_size=size,
                            )
                            order_line_item.save()
            except Exception as e:
                if order:
                    order.delete()
                return HttpResponse(
                    content=f'Webhook received: {event["type"]} | Error {e}',
                    status=500)
        self._sending_confirmation_email(order)
        return HttpResponse(
            content=f'Webhook received: {event["type"]} | Success, Order created in webhook',
            status=200)

    # handle the failed webhook payment
    def handle_payment_intent_payment_failed(self, event):

        return HttpResponse(
            content=f'Webhook received: {event["type"]}',
            status=200)


from django.urls import path
from . import views

urlpatterns = [
    path('power/<int:input_number>', views.first_task, name='firstTask'),
    path('multiply/<int:first_number>/<int:second_number>', views.second_task, name='secondTask'),
    path('primes/<int:first_number>/<int:second_number>', views.third_task, name='thirdTask'),
    path('max/<int:first_number>/<int:second_number>/<int:third_number>', views.fourth_task, name='fourthTask'),
    path('power/', views.index, name='index'),
]


"""
This file keeps track of which radar lines are excluded from analyses because
of missing or corrupted data.
"""

import os.path as path

def blocklist(survey_path, line):
    """ Takes a survey filename and a line number, and compares to an index to
    determine whether or not the line should be blocked. Returns a boolean.
    """
    survey_name = path.basename(survey_name)

    if survey_name == "glacier1_08_utm.h5":
        if line <= 9:
            block = True

    else:
        block = False

    return block


# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import string
import math
import os
import re

from .process import load_word_list

"""
    url2features.simple: Basic text feature calculation.

    Calculate simple informative statistics about the URL.
    Such as the inferred depth using slash chars, presence of dates or
    indicators of common CMS
"""

########################################################################################
def simple_features(df, columns, add_prefix=True):
    """
        Given a pandas dataframe and a set of column names.
        calculate the simple text summary features and add them.
    """
    rez = df.copy()
    for col in columns:
        rez = add_simple_features(rez, col, add_prefix)
    return rez

########################################################################################
def add_simple_features(df, col, add_prefix=True):
    """
        Given a pandas dataframe and a column name.
        calculate the simple features
    """

    count = lambda l1,l2: sum([1 for x in l1 if x in l2])

    def simp_feats(x, col):
        if x[col]!=x[col]:
            length = -1
            punct = -1
            numeric = -1
            capital = -1
            depth = -1
        else:
            length = len(x[col])
            punct = count(x[col], string.punctuation)/length
            numeric = count(x[col], string.digits)/length
            capital = sum(1 for c in x[col] if c.isupper())/length
            depth = null_tolerant_depth(x[col])
        return length, punct, numeric, capital, depth

    if add_prefix:
        col_names = [col+"_length", col+"_punct", col+"_numeric", col+"_capital", col+"_path_depth"]
    else:
        col_names = ["url_length","url_punct", "url_numeric", "url_capital", "path_depth"]

    df[ col_names ] = df.apply(simp_feats, col=col, axis=1, result_type="expand")

    return df

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

def null_tolerant_len(x):
    if x != x:
        return 0
    else:
        return len(x)

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

def null_tolerant_depth(x):
    if x != x:
        return 0
    else:
        x = remove_protocol_and_trim(x)
        return len( str(x).split("/") )

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

def remove_protocol_and_trim(url):
    p = re.findall(r"^https?://", url)
    if len(p) > 0:
        url = url[len(p[0]):]
    e = re.findall(r"/\s*$", url)
    if len(e) > 0:
        url = url[:0-len(e[0])]
    return url


import serial
import paho.mqtt.client as mqtt

# MQTT 브로커 설정
broker_address = 'test.mosquitto.org'  # MQTT 브로커 주소
broker_port = 1883  # MQTT 브로커 포트

# Arduino 시리얼 포트 설정
port = 'tty.usbserial-1130'  # Arduino가 연결된 포트 이름
baudrate = 9600  # 시리얼 통신 속도

def on_connect(client, userdata, flags, rc):
    print('Connected to MQTT broker')

def on_publish(client, userdata, mid):
    print('Data published')

def read_data():
    try:
        ser = serial.Serial(port, baudrate)

        # MQTT 클라이언트 초기화
        client = mqtt.Client()
        client.on_connect = on_connect
        client.on_publish = on_publish
        client.connect(broker_address, broker_port)

        while True:
            data = ser.readline().strip()  # 시리얼 포트로부터 데이터 읽기
            if data:
                # 데이터 처리
                # 예: 데이터 출력
                print("Received data:", data.decode())
                client.publish("cow1/data", data.decode())

    except serial.SerialException:
        print("Serial port connection failed.")
    except KeyboardInterrupt:
        print("Program terminated.")

if __name__ == '__main__':
    read_data()


#!/usr/bin/env python2.3
#
# Copyright (C) 2006 British Broadcasting Corporation and Kamaelia Contributors(1)
#     All Rights Reserved.
#
# You may only modify and redistribute this under the terms of any of the
# following licenses(2): Mozilla Public License, V1.1, GNU General
# Public License, V2.0, GNU Lesser General Public License, V2.1
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
#     http://kamaelia.sourceforge.net/AUTHORS - please extend this file,
#     not this notice.
# (2) Reproduced in the COPYING file, and at:
#     http://kamaelia.sourceforge.net/COPYING
# Under section 3.5 of the MPL, we are using this text since we deem the MPL
# notice inappropriate for this file. As per MPL/GPL/LGPL removal of this
# notice is prohibited.
#
# Please contact us via: kamaelia-list-owner@lists.sourceforge.net
# to discuss alternative licensing.
# -------------------------------------------------------------------------
"""\
=======================
Convert Data to Strings
=======================

A simple component that takes data items and converts them to strings.

Example Usage
-------------

A simple pipeline::

    Pipeline( sourceOfNonStrings(),
              Stringify(),
              consumerThatWantsStrings(),
            ).activate()

How does it work?
-----------------

Send data items to this component's "inbox" inbox. They are converted to
strings using the str(...) function, and sent on out of the "outbox" outbox.

Anything sent to this component's "control" inbox is ignored.

This component does not terminate.
"""

from Axon.Component import component, scheduler

class Stringify(component):
   """\
   Stringify() -> new Stringify.

   A component that converts data items received on its "inbox" inbox to
   strings and sends them on out of its "outbox" outbox.
   """

   Inboxes = { "inbox"   : "Data items to convert to string",
               "control" : "NOT USED",
             }
   Outboxes = { "outbox" : "Data items converted to strings",
                "signal" : "NOT USED",
              }

   def __init__(self):
      """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
      super(Stringify, self).__init__() # !!!! Must happen, if this method exists
      self.activate()

   def mainBody(self):
      """Main loop body."""
      if self.dataReady("inbox"):
         theData = self.recv("inbox")
         self.send(str(theData), "outbox")
      return 1

__kamaelia_components__  = ( Stringify, )


# this script performs a manual training process on the XNLI dataset to enable more control

from args import parse_args
import logging
import os
import paddle
from experiments.xnli.xnli_utils import XNLI_Dataset, convert_examples, convert_example
from infoxlm_paddle import (
    InfoXLMTokenizer,
    InfoXLMModel,
    InfoXLMForSequenceClassification,
)
from utils import set_seed, get_writer, save_json, try_remove_old_ckpt, XNLI_LANGS
from argparse import Namespace
from paddle.amp import GradScaler, auto_cast
from paddle.metric import Accuracy
from paddle.optimizer import Adam, AdamW
import math
from tqdm.auto import tqdm

HERE = os.path.dirname(os.path.abspath(__file__))
PADDLE_WEIGHT = os.path.join(HERE, "model_checkpoints/converted_paddle")
SPM_MODEL = os.path.join(PADDLE_WEIGHT, "sentencepiece.bpe.model")

# print(model.roberta.embeddings.word_embeddings.weight.std())
# import pdb; pdb.set_trace()
logger = logging.getLogger(__name__)
args = parse_args()

tokenizer = InfoXLMTokenizer(
    sentencepiece_model_file=SPM_MODEL, do_lower_case=False, remove_space=True
)

if not args.quick_verify:
    base_model = InfoXLMModel.from_pretrained(PADDLE_WEIGHT)
    model = InfoXLMForSequenceClassification(base_model, num_classes=3, dropout=0.05)
    print(model.roberta.embeddings.word_embeddings.weight.std())
else:
    model = InfoXLMForSequenceClassification.from_pretrained(
        os.path.join(HERE, "model_checkpoints/finetuned_paddle")
    )

def as_tensor(t):
    return paddle.to_tensor(t)

@paddle.no_grad()
def evaluate(args, model, tokenizer, lang, metric, val=True):
    model.eval()
    metric.reset()
    splt = "dev" if val else "test"
    validation_loader = XNLI_Dataset(lang, split=splt)
    for batch in tqdm(
        validation_loader.get_batch_iterator(args.eval_batch_size), leave=False
    ):
        premises, hypotheses, labels = batch
        encoded_inputs = tokenizer(premises, hypotheses, padding=True)
        input_token_ids = paddle.to_tensor(encoded_inputs["input_ids"])
        logits = model(input_token_ids)
        correct = metric.compute(logits, as_tensor(labels))
        metric.update(correct)
    acc = metric.accumulate()
    model.train()
    return acc

def main(args):
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO,
        handlers=[
            logging.FileHandler(
                os.path.join(args.output_dir, "run.log"), mode="w", encoding="utf-8",
            )
        ],
    )
    logger.info("**********  Configuration Arguments **********")
    for arg, value in sorted(vars(args).items()):
        logger.info(f"{arg}: {value}")
    logger.info("**************************************************")

    set_seed(args)

    writer = get_writer(args)

    params = Namespace(
        min_count=0,
        tokens_per_batch=-1,
        max_batch_size=0,
        group_by_size=False,
        max_len=args.max_length,
        eval_batch_size=args.eval_batch_size,
        train_batch_size=args.train_batch_size,
    )

    train_dataset = XNLI_Dataset(args.train_lang, "train",)
    eval_dataset = XNLI_Dataset(args.eval_lang, "test",)

    opt_cls = AdamW if args.optimizer.lower() == "adamw" else Adam
    optimizer = opt_cls(
        learning_rate=args.learning_rate,
        beta1=0.9,
        beta2=0.999,
        epsilon=args.adam_epsilon,
        parameters=model.parameters(),
    )
    model.roberta.stop_gradient=True

    metric = Accuracy()

    if args.fp16:
        scaler = GradScaler(init_loss_scaling=args.scale_loss)

    num_update_steps_per_epoch = args.sentences_per_epoch // args.train_batch_size
    if args.max_train_steps > 0:
        args.num_train_epochs = math.ceil(
            args.max_train_steps / num_update_steps_per_epoch
        )
    else:
        args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
    total_batch_size = args.train_batch_size * args.gradient_accumulation_steps

    logger.info("********** Running training **********")
    logger.info(f"  Num Epochs = {args.num_train_epochs}")
    logger.info(f"  Instantaneous train batch size = {args.train_batch_size}")
    logger.info(f"  Instantaneous eval batch size = {args.eval_batch_size}")
    logger.info(f"  Total train batch size (w. accumulation) = {total_batch_size}")
    logger.info(f"  Gradient Accumulation steps = {args.gradient_accumulation_steps}")
    logger.info(f"  Total optimization steps = {args.max_train_steps}")

    save_json(vars(args), os.path.join(args.output_dir, "args.json"))
    progress_bar = tqdm(range(args.max_train_steps))
    global_steps = 0
    tr_loss, logging_loss = 0.0, 0.0
    max_val_acc = 0.0

    ce_loss = paddle.nn.CrossEntropyLoss(reduction="mean")

    dls = train_dataset.get_batch_iterator(args.train_batch_size)

    for epoch in range(args.num_train_epochs):
        step = 0
        for batch in dls:
            model.train()
            model.roberta.eval()
            with auto_cast(
                args.fp16, custom_white_list=["layer_norm", "softmax", "gelu"]
            ):
                premises, hypotheses, labels = batch
                # tokenize

                encoded_inputs = tokenizer(premises, hypotheses, padding=True, max_length=params.max_len)
                input_token_ids = paddle.to_tensor(encoded_inputs["input_ids"])
                logits = model(input_token_ids)
                loss = (
                    ce_loss(logits, as_tensor(labels))
                    / args.gradient_accumulation_steps
                )
                tr_loss += loss.item()
            if args.fp16:
                scaled = scaler.scale(loss)
                scaled.backward()
            else:
                loss.backward()

            if step % args.gradient_accumulation_steps == 0:
                if args.fp16:
                    scaler.step(optimizer)
                    scaler.update()
                else:
                    optimizer.step()
                optimizer.clear_grad(set_to_zero=False)
                progress_bar.update(1)
                global_steps += 1

                if (
                    args.logging_steps > 0 and global_steps % args.logging_steps == 0
                ) or global_steps == args.max_train_steps:
                    writer.add_scalar(
                        "loss",
                        (tr_loss - logging_loss) / args.logging_steps,
                        global_steps,
                    )
                    logger.info(
                        "global_steps {} loss: {:.8f}".format(
                            global_steps, (tr_loss - logging_loss) / args.logging_steps,
                        )
                    )
                    logging_loss = tr_loss

                if (
                    args.save_steps > 0 and global_steps % args.save_steps == 0
                ) or global_steps == args.max_train_steps:
                    logger.info("********** Running evaluating **********")
                    results_dict = {}
                    # val
                    val_avg_acc = 0
                    for lang in XNLI_LANGS:
                        val_acc = evaluate(
                            args, model, tokenizer, lang, metric, val=True
                        )
                        results_dict[f"val_{lang}_acc"] = val_acc
                        val_avg_acc += val_acc
                        logger.info(
                            f"##########  val_{lang}_acc {val_acc:.4f} ##########"
                        )

                    results_dict["val_avg_acc"] = val_avg_acc / 15
                    results_dict["val_other_acc"] = (
                        val_avg_acc - results_dict["val_en_acc"]
                    ) / 14
                    results_dict["val_gap_score"] = (
                        results_dict["val_en_acc"] - results_dict["val_other_acc"]
                    )

                    # test
                    test_avg_acc = 0
                    for lang in XNLI_LANGS:
                        # ds = XNLI_Dataset(lang, "test")
                        test_acc = evaluate(
                            args, model, tokenizer, lang, metric, val=False
                        )
                        results_dict[f"test_{lang}_acc"] = test_acc
                        test_avg_acc += test_acc
                        logger.info(
                            f"##########  test_{lang}_acc {test_acc:.4f} ##########"
                        )
                        # print(f"##########  test_{lang}_acc {test_acc} ##########")
                    results_dict["test_avg_acc"] = test_avg_acc / 15
                    results_dict["test_other_acc"] = (
                        test_avg_acc - results_dict["test_en_acc"]
                    ) / 14
                    results_dict["test_gap_score"] = (
                        results_dict["test_en_acc"] - results_dict["test_other_acc"]
                    )

                    for k, v in results_dict.items():
                        writer.add_scalar(f"eval/{k}", v, global_steps)
                        logger.info(f"  {k} = {v}")
                        print(f"  {k} = {v}")

                    val_avg_acc = results_dict["val_avg_acc"]
                    test_avg_acc = results_dict["test_avg_acc"]
                    val_gap_score = results_dict["val_gap_score"]
                    test_gap_score = results_dict["test_gap_score"]

                    if val_avg_acc >= max_val_acc:
                        max_val_acc = val_avg_acc
                        logger.info(
                            f"########## Step {global_steps} val_avg_acc {max_val_acc:.4f} test_avg_acc {test_avg_acc:.4f} ##########"
                        )
                        logger.info(
                            f"########## Step {global_steps} val_gap_score {-val_gap_score:.4f} test_gap_score {-test_gap_score:.4f}, the lower the better"
                        )

                    output_dir = os.path.join(
                        args.output_dir,
                        "ckpt",
                        f"step-{global_steps}-test_avg_acc-{test_avg_acc}-val_avg_acc-{val_avg_acc}",
                    )
                    os.makedirs(output_dir, exist_ok=True)
                    model.save_pretrained(output_dir)
                    try_remove_old_ckpt(args.output_dir, topk=args.topk)

                    logger.info("********** Evaluating Done **********")

            if global_steps >= args.max_train_steps:
                return

            step += 1
            # if step >= args.sentences_per_epoch // args.train_batch_size:
            #     print(f"Epoch {epoch} resample!")
            #     dls = train_dataset.get_batch_iterator
            #     break

if __name__ == "__main__":
    args = parse_args()
    main(args)


# -*- coding: utf-8 -*-
"""
Created on Thu Jul 15 16:08:25 2021

@author: whmcilha
Drawing a triangle in opengl with vertex attribute (colour)

"""

import pyglet
from pyglet import gl
import pyshaders
from cauldron import VertexArrayObject

window = pyglet.window.Window(width=600, height=600)

# SHADERS - color and location

def setup_program():

    vertex_shader = '''
        #version 300 es
        precision mediump float;
        layout(location = 0) in vec4 position;
        layout(location = 1) in vec4 color;

        out vec4 vcolor ;

        void main()
        {
            gl_Position = position;
            vcolor = color;
        }
    '''

    fragment_shader = '''
        #version 300 es
        precision mediump float;

        in vec4 vcolor;
        out vec4 fColor;

        void main()
        {
             fColor = vcolor;
        }
    '''

    program = pyshaders.from_string(vertex_shader, fragment_shader)
    program.use()
    return program

program = setup_program()

# VERTICES

VAO = VertexArrayObject()
# vertices
vertices = VAO.createBuffer(data=((-0.6, -0.5, 0.1), (0.6, -0.5, 0.1), (0.0, 0.5, 0.1)))
vertices.connectToShader(location=0)
# colors
colors = VAO.createBuffer(data=((1,0,0),(0,1,0), (0,0,1)))
colors.connectToShader(location=1)

# RUN

@window.event
def on_draw():
    gl.glClearColor(0.5, 0.6, 0.7, 1.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    VAO.drawArrays()

gl.glEnable(gl.GL_MULTISAMPLE) # antialiasing
gl.glEnable(gl.GL_DEPTH_TEST) # 3d
pyglet.app.run()