import numpy as np
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
import seaborn as sb

df = pd.read_excel('indo_12_1.xls',skiprows=3, skipfooter=2, na_values=['-'])
df.rename(columns={'Unnamed: 0':'Provinsi'},inplace=True)
# print(df[df['Provinsi']=='INDONESIA'])
df = df.set_index('Provinsi')
df = df.interpolate()
df = df.transpose()
# print(df['Kepulauan Riau'])

x = list(df.index)
# print(x)

df1 = pd.DataFrame(x)
# df1.rename(columns={0:'Tahun'})
# print(df1)

model = linear_model.LinearRegression()
model.fit(df1,df['Kepulauan Riau'])
df2 = df1.append({0:2050},ignore_index=True)
# print(df2)
# print(model.coef_)
# print(model.intercept_)
# print(model.predict([[2050]]))
plt.plot(
    df1, df['Kepulauan Riau'], 'r-',
    df2, model.predict(df2), 'g--'
)
plt.grid(True)
plt.show()


#__Author__: Panax Wong
# encoding utf=8
# 定义展示系统功能的函数
def showUI():
    print("= " * 8,end="")
    print("  学员管理系统  ",end="")
    print("= " * 8,end="")
    print("")
    print(" 1.查看学员信息  ",end="")
    print(" 2.添加学员信息   ",end="")
    print(" 3.删除学员信息")
    print(" 4.修改学员信息  ",end="")
    print(" 5.退出管理系统",)
    print("= " * 23)

# 定义一个列表，用来存储学员信息
students = []

while True:
    # 展示可选择的功能
    showUI()

    # 提示用户进行功能选择
    key = input("请选择功能数字序号：")

    # 根据用户选择，运行相关功能
    if key == "1":
        print("您选择了查看学员信息")
        # 输出学员信息
        print("- " * 23)
        print("|id      |name       |age        |classid")
        for temp in students:
            print("%s      %s        %s         %s" % (temp['id'], temp['name'], temp['age'], temp['classid']))
        print("- " * 23)
    elif key == "2":
        print("您选择了添加学员信息功能")
        id = input("请输入学员学号(学号不可重复)：")
        name = input("请输入学员姓名：")
        age = input("请输入学员年龄:")
        classid = input("请输入学员的班级:")

        # 验证学号是否唯一
        i = 0
        leap = 0
        for temp in students:
            if temp['id'] == id:
                leap = 1
                break
            else:
                i = i + 1
        if leap == 1:
            print("您输入学号重复，添加失败！")
            break
        else:
            # 定义一个字典，存放单个学员信息
            stuInfo = {}
            stuInfo['name'] = name
            stuInfo['id'] = id
            stuInfo['age'] = age
            stuInfo['classid'] = classid

            # 将单个学员信息存入列表
            students.append(stuInfo)
            print("添加成功！")
    elif key == "3":
        print("您选择了删除学员功能")
        delId = input("请输入要删除的学员学号:")
        i = 0
        leap = 0
        for temp in students:
            if temp['id'] == delId:
                leap = 1
                break
            else:
                i = i + 1
        if leap == 0:
            print("没有此学生学号，删除失败！")
        else:
            del students[i]
            print("删除成功！")

    elif key == "4":
        print("您已选择修改学员信息功能")
        alterId = input("请输入你要修改学员的学号:")
        # 检查学号是否存在，然后再修改信息
        i = 0
        leap = 0
        for temp in students:
            if temp['id'] == alterId:
                leap = 1
                break
            else:
                i = i + 1
        if leap == 1:
            while True:
                alterNum = int(input(" 1.修改学号\n 2.修改姓名 \n 3.修改年龄 \n 4.修改班级 \n 5.退出修改\n"))
                if alterNum == 1:
                    newId = input("输入更改后的学号:")
                    # 修改后的学号要验证是否唯一
                    i = 0
                    leap1 = 0
                    for temp1 in students:
                        if temp1['id'] == newId:
                            leap1 = 1
                            break
                        else:
                            i = i + 1
                    if leap1 == 1:
                        print("输入学号不可重复，修改失败！")
                    else:
                        temp['id'] = newId
                        print("学号修改成功")
                elif alterNum == 2:
                    newName = input("输入更改后的姓名:")
                    temp['name'] = newName
                    print("姓名修改成功")
                elif alterNum == 3:
                    newAge = input("输入更改后的年龄:")
                    temp['age'] = newAge
                    print("年龄修改成功")
                elif alterNum == 4:
                    newClass = input("输入更改后的班级:")
                    temp['classid'] = newClass
                    print("班级修改成功")
                elif alterNum == 5:
                    break
                else:
                    print("输入错误请重新输入")
        else:
            print("没有此学号，修改失败！")
    elif key == "5":
        # 退出功能
        quitconfirm = input("亲，真的要退出么？😭~~(>_<)~~😭（请回复yes或者no）")
        if quitconfirm == 'yes':
            print("您已退出本系统，欢迎下次再次使用，谢谢")
            break;
        else:
            print("您输入的信息有误，请核实！")

    else:
        print("您再瞅瞅，输入的东东有误吧，要输入的是1到5的数字，本系统才支持哦")


"""Test ``sfini.execution.history``."""

from sfini.execution import history as tscr
import pytest
from unittest import mock
import datetime

@pytest.fixture
def timestamp():
    """An example time-stamp."""
    return datetime.datetime.utcnow() - datetime.timedelta(minutes=42)

@pytest.fixture
def input_():
    """Example input."""
    return {"a": 42, "b": "spam", "c": {"foo": [1, 2], "bar": None}}

class TestEvent:
    """Test ``sfini.execution.history.Event``."""
    @pytest.fixture
    def event(self, timestamp):
        """An example Event instance."""
        return tscr.Event(timestamp, "BlaSpammed", 2, previous_event_id=1)

    def test_init(self, event, timestamp):
        """Event initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.previous_event_id == 1

    def test_str(self, event, timestamp):
        """Event stringification."""
        res = str(event)
        assert "BlaSpammed" in res
        assert str(timestamp) in res
        assert "2" in res[:res.index(str(timestamp))]

    class TestRepr:
        """Event string representation."""
        def test_with_prev(self, event, timestamp):
            """Event has preceding event."""
            exp = "Event(%r, 'BlaSpammed', 2, previous_event_id=1)" % timestamp
            assert repr(event) == exp

        def test_no_prev(self, event, timestamp):
            """Event has no preceding event."""
            event.previous_event_id = None
            exp = "Event(%r, 'BlaSpammed', 2)" % timestamp
            assert repr(event) == exp

    class TestGetArgs:
        """Getting instantiation arguments from AWS API HistoryEvent."""
        def test_no_details(self, timestamp):
            """No event details."""
            # Setup environment
            type_keys = {}

            # Build input
            history_event = {
                "timestamp": timestamp,
                "type": "BlaSpammed",
                "id": 2,
                "previousEventId": 1}

            # Build expectation
            exp_args = (timestamp, "BlaSpammed", 2)
            exp_kwargs = {"previous_event_id": 1}
            exp_details = {}

            # Run function
            with mock.patch.object(tscr, "_type_keys", type_keys):
                res = tscr.Event._get_args(history_event)

            # Check result
            res_args, res_kwargs, res_details = res
            assert res_args == exp_args
            assert res_kwargs == exp_kwargs
            assert res_details == exp_details

        def test_with_details(self, timestamp):
            """Event comes with details."""
            # Setup environment
            type_keys = {"BlaSpammed": "BlaSpammedEventDetails"}

            # Build input
            history_event = {
                "timestamp": timestamp,
                "type": "BlaSpammed",
                "id": 2,
                "previousEventId": 1,
                "BlaSpammedEventDetails": {"a": 42, "b": "spam"}}

            # Build expectation
            exp_args = (timestamp, "BlaSpammed", 2)
            exp_kwargs = {"previous_event_id": 1}
            exp_details = {"a": 42, "b": "spam"}

            # Run function
            with mock.patch.object(tscr, "_type_keys", type_keys):
                res = tscr.Event._get_args(history_event)

            # Check result
            res_args, res_kwargs, res_details = res
            assert res_args == exp_args
            assert res_kwargs == exp_kwargs
            assert res_details == exp_details

        def test_no_previous_event(self, timestamp):
            """Event has no precedent."""
            # Setup environment
            type_keys = {}

            # Build input
            history_event = {
                "timestamp": timestamp,
                "type": "BlaSpammed",
                "id": 2}

            # Build expectation
            exp_args = (timestamp, "BlaSpammed", 2)
            exp_kwargs = {}
            exp_details = {}

            # Run function
            with mock.patch.object(tscr, "_type_keys", type_keys):
                res = tscr.Event._get_args(history_event)

            # Check result
            res_args, res_kwargs, res_details = res
            assert res_args == exp_args
            assert res_kwargs == exp_kwargs
            assert res_details == exp_details

    def test_from_history_event(self, timestamp):
        """Instantiation from AWS API history event."""
        # Setup environment
        args = (timestamp, "BlaSpammed", 2)
        kwargs = {"previous_event_id": 1}
        details = {"a": 42, "b": "spam"}
        ga_mock = mock.Mock(return_value=(args, kwargs, details))

        # Build input
        history_event = {"timestamp": timestamp, "type": "BlaSpammed", "id": 2}

        # Run function
        with mock.patch.object(tscr.Event, "_get_args", ga_mock):
            res = tscr.Event.from_history_event(history_event)

        # Check result
        assert isinstance(res, tscr.Event)
        assert res.timestamp == timestamp
        assert res.event_type == "BlaSpammed"
        assert res.event_id == 2
        assert res.previous_event_id == 1
        ga_mock.assert_called_once_with(history_event)

    def test_details_str(self, event):
        """Event details formatting."""
        assert not event.details_str

class TestFailed:
    """Test ``sfini.execution.history.Failed``."""
    @pytest.fixture
    def event(self, timestamp):
        """An example Failed instance."""
        return tscr.Failed(
            timestamp,
            "BlaSpammed",
            2,
            previous_event_id=1,
            error="spamError",
            cause="a spam occurred")

    def test_init(self, event, timestamp):
        """Failed initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.previous_event_id == 1
        assert event.error == "spamError"
        assert event.cause == "a spam occurred"

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            ({}, {}),
            ({"error": "spamError"}, {"error": "spamError"}),
            ({"cause": "a spam occurred"}, {"cause": "a spam occurred"}),
            (
                {"error": "spamError", "cause": "a spam occurred"},
                {"error": "spamError", "cause": "a spam occurred"})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"BlaFailed": "BlaFailedEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "BlaFailed",
            "id": 2,
            "previousEventId": 1,
            "BlaFailedEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "BlaFailed", 2)
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.Failed._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

    def test_details_str(self, event):
        """Failed details formatting."""
        assert event.details_str == "error: spamError"

class TestLambdaFunctionScheduled:
    """Test ``sfini.execution.history.LambdaFunctionScheduled``."""
    @pytest.fixture
    def event(self, timestamp, input_):
        """An example LambdaFunctionScheduled instance."""
        return tscr.LambdaFunctionScheduled(
            timestamp,
            "BlaSpammed",
            2,
            "lambdaResource:arn",
            previous_event_id=1,
            task_input=input_,
            timeout=60)

    def test_init(self, event, timestamp, input_):
        """LambdaFunctionScheduled initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.previous_event_id == 1
        assert event.task_input == input_
        assert event.timeout == 60

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            ({"resource": "lambdaResource:arn"}, {}),
            (
                {"resource": "lambdaResource:arn", "input": '"spamInput"'},
                {"task_input": "spamInput"}),
            (
                {"resource": "lambdaResource:arn", "timeoutInSeconds": 60},
                {"timeout": 60}),
            (
                {
                    "resource": "lambdaResource:arn",
                    "input": '"spamInput"',
                    "timeoutInSeconds": 60},
                {"task_input": "spamInput", "timeout": 60})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"LambdaScheduled": "LambdaScheduledEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "LambdaScheduled",
            "id": 2,
            "previousEventId": 1,
            "LambdaScheduledEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "LambdaScheduled", 2, "lambdaResource:arn")
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.LambdaFunctionScheduled._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

    def test_details_str(self, event):
        """LambdaFunctionScheduled details formatting."""
        assert event.details_str == "resource: lambdaResource:arn"

class TestActivityScheduled:
    """Test ``sfini.execution.history.ActivityScheduled``."""
    @pytest.fixture
    def event(self, timestamp, input_):
        """An example ActivityScheduled instance."""
        return tscr.ActivityScheduled(
            timestamp,
            "BlaSpammed",
            2,
            "lambdaResource:arn",
            previous_event_id=1,
            task_input=input_,
            timeout=60,
            heartbeat=10)

    def test_init(self, event, timestamp, input_):
        """Event initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.previous_event_id == 1
        assert event.task_input == input_
        assert event.timeout == 60
        assert event.heartbeat == 10

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            (
                {
                    "resource": "lambdaResource:arn",
                    "input": '"spamInput"',
                    "timeoutInSeconds": 60},
                {"task_input": "spamInput", "timeout": 60}),
            (
                {
                    "resource": "lambdaResource:arn",
                    "input": '"spamInput"',
                    "timeoutInSeconds": 60,
                    "heartbeatInSeconds": 10},
                {"task_input": "spamInput", "timeout": 60, "heartbeat": 10})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"ActivityScheduled": "ActivityScheduledEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "ActivityScheduled",
            "id": 2,
            "previousEventId": 1,
            "ActivityScheduledEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "ActivityScheduled", 2, "lambdaResource:arn")
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.ActivityScheduled._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

class TestActivityStarted:
    """Test ``sfini.execution.history.ActivityStarted``."""
    @pytest.fixture
    def event(self, timestamp):
        """An example ActivityStarted instance."""
        return tscr.ActivityStarted(
            timestamp,
            "BlaSpammed",
            2,
            "spamWorker",
            previous_event_id=1)

    def test_init(self, event, timestamp):
        """ActivityStarted initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.worker_name == "spamWorker"
        assert event.previous_event_id == 1

    def test_get_args(self, timestamp):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"ActivityStarted": "ActivityStartedEventDetails"}

        # Build input
        details = {"workerName": "spamWorker"}
        history_event = {
            "timestamp": timestamp,
            "type": "ActivityStarted",
            "id": 2,
            "previousEventId": 1,
            "ActivityStartedEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "ActivityStarted", 2, "spamWorker")
        exp_kwargs = {"previous_event_id": 1}
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.ActivityStarted._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

    def test_details_str(self, event):
        """ActivityStarted details formatting."""
        assert event.details_str == "worker: spamWorker"

class TestObjectSucceeded:
    """Test ``sfini.execution.history.ObjectSucceeded``."""
    @pytest.fixture
    def event(self, timestamp):
        """An example ObjectSucceeded instance."""
        return tscr.ObjectSucceeded(
            timestamp,
            "BlaSpammed",
            2,
            previous_event_id=1,
            output={"foo": [1, 2], "bar": None})

    def test_init(self, event, timestamp):
        """ObjectSucceeded initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.previous_event_id == 1
        assert event.output == {"foo": [1, 2], "bar": None}

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            ({}, {}),
            (
                {"output": '{"foo": [1, 2], "bar": null}'},
                {"output": {"foo": [1, 2], "bar": None}})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"BlaSucceeded": "BlaSucceededEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "BlaSucceeded",
            "id": 2,
            "previousEventId": 1,
            "BlaSucceededEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "BlaSucceeded", 2)
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.ObjectSucceeded._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

class TestExecutionStarted:
    """Test ``sfini.execution.history.ExecutionStarted``."""
    @pytest.fixture
    def event(self, timestamp, input_):
        """An example ExecutionStarted instance."""
        return tscr.ExecutionStarted(
            timestamp,
            "BlaSpammed",
            2,
            previous_event_id=1,
            execution_input=input_,
            role_arn="role:arn")

    def test_init(self, event, timestamp, input_):
        """ExecutionStarted initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.previous_event_id == 1
        assert event.execution_input == input_
        assert event.role_arn == "role:arn"

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            ({}, {}),
            ({"input": '"spamInput"'}, {"execution_input": "spamInput"}),
            ({"roleArn": "role:arn"}, {"role_arn": "role:arn"}),
            (
                {"input": '"spamInput"', "roleArn": "role:arn"},
                {"execution_input": "spamInput", "role_arn": "role:arn"})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"ExecutionStarted": "ExecutionStartedEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "ExecutionStarted",
            "id": 2,
            "previousEventId": 1,
            "ExecutionStartedEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "ExecutionStarted", 2)
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.ExecutionStarted._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

class TestStateEntered:
    """Test ``sfini.execution.history.StateEntered``."""
    @pytest.fixture
    def event(self, timestamp, input_):
        """An example StateEntered instance."""
        return tscr.StateEntered(
            timestamp,
            "BlaSpammed",
            2,
            "spamState",
            previous_event_id=1,
            state_input=input_)

    def test_init(self, event, timestamp, input_):
        """StateEntered initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.state_name == "spamState"
        assert event.previous_event_id == 1
        assert event.state_input == input_

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            ({"name": "spamState"}, {}),
            (
                {"name": "spamState", "input": '"spamInput"'},
                {"state_input": "spamInput"})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"StateEntered": "StateEnteredEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "StateEntered",
            "id": 2,
            "previousEventId": 1,
            "StateEnteredEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "StateEntered", 2, "spamState")
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.StateEntered._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

    def test_details_str(self, event):
        """StateEntered details formatting."""
        assert event.details_str == "name: spamState"

class TestStateExited:
    """Test ``sfini.execution.history.StateExited``."""
    @pytest.fixture
    def event(self, timestamp):
        """An example StateExited instance."""
        return tscr.StateExited(
            timestamp,
            "BlaSpammed",
            2,
            "spamState",
            previous_event_id=1,
            output={"foo": [1, 2], "bar": None})

    def test_init(self, event, timestamp):
        """StateExited initalisation."""
        assert event.timestamp == timestamp
        assert event.event_type == "BlaSpammed"
        assert event.event_id == 2
        assert event.state_name == "spamState"
        assert event.previous_event_id == 1
        assert event.output == {"foo": [1, 2], "bar": None}

    @pytest.mark.parametrize(
        ("details", "exp_kwargs"),
        [
            ({"name": "spamState"}, {}),
            (
                {
                    "name": "spamState",
                    "output": '{"foo": [1, 2], "bar": null}'},
                {"output": {"foo": [1, 2], "bar": None}})])
    def test_get_args(self, timestamp, details, exp_kwargs):
        """Getting instantiation arguments from AWS API HistoryEvent."""
        # Setup environment
        type_keys = {"StateExited": "StateExitedEventDetails"}

        # Build input
        history_event = {
            "timestamp": timestamp,
            "type": "StateExited",
            "id": 2,
            "previousEventId": 1,
            "StateExitedEventDetails": details}

        # Build expectation
        exp_args = (timestamp, "StateExited", 2, "spamState")
        exp_kwargs.update({"previous_event_id": 1})
        exp_details = details

        # Run function
        with mock.patch.object(tscr, "_type_keys", type_keys):
            res = tscr.StateExited._get_args(history_event)

        # Check result
        res_args, res_kwargs, res_details = res
        assert res_args == exp_args
        assert res_kwargs == exp_kwargs
        assert res_details == exp_details

    def test_details_str(self, event):
        """StateExited details formatting."""
        assert event.details_str == "name: spamState"

def test_parse_history():
    """History event item processing."""
    # Setup environment
    event_mocks = {
        "type1": [mock.Mock(spec=tscr.Event), mock.Mock(spec=tscr.Event)],
        "type2": [mock.Mock(spec=tscr.Event), mock.Mock(spec=tscr.Event)],
        "type3": [mock.Mock(spec=tscr.Event)]}
    type_classes = {n: mock.Mock() for n in event_mocks}
    type_classes["type1"].from_history_event.side_effect = event_mocks["type1"]
    type_classes["type2"].from_history_event.side_effect = event_mocks["type2"]
    type_classes["type3"].from_history_event.side_effect = event_mocks["type3"]

    # Build input
    history_events = [
        {"type": "type1", "a": 42, "b": "spam"},
        {"type": "type2", "c": [1, 2], "b": "bla"},
        {"type": "type1", "a": 17, "b": "foo"},
        {"type": "type3", "d": None, "b": "bar"},
        {"type": "type2", "c": [3, 5], "b": "eggs"}]

    # Build expectation
    exp = [
        event_mocks["type1"][0],
        event_mocks["type2"][0],
        event_mocks["type1"][1],
        event_mocks["type3"][0],
        event_mocks["type2"][1]]
    exp_calls = {
        "type1": [
            mock.call.from_history_event(history_events[0]),
            mock.call.from_history_event(history_events[2])],
        "type2": [
            mock.call.from_history_event(history_events[1]),
            mock.call.from_history_event(history_events[4])],
        "type3": [mock.call.from_history_event(history_events[3])]}

    # Test function
    with mock.patch.object(tscr, "_type_classes", type_classes):
        res = tscr.parse_history(history_events)

    # Check result
    assert res == exp
    for k, c_mock in type_classes.items():
        assert c_mock.method_calls == exp_calls[k]


# import heapq

# def merge_sort(arr) :
#     if len(arr) <= 1 :
#         return arr

#     mid = len(arr) // 2
#     left = merge_sort(arr[:mid])
#     right = merge_sort(arr[mid:])
#     i = 0
#     j = 0
#     k = 0

#     while len(left) > i and len(right) > j :
#         if left[i] < right[j] :
#             arr[k] = left[i]
#             i += 1

#         else :
#             arr[k] = right[j]
#             j += 1

#         k += 1

#         # 왼쪽만 남은 경우
#     if len(left) == i :
#         while len(right) > j :
#             arr[k] = right[j]
#             j += 1
#             k += 1

#     # 오른쪽만 남은 경우
#     elif len(right) == j :
#         while len(left) > i :
#             arr[k] = left[i]
#             i += 1
#             k += 1

#     return arr

# N = int(input())
# arr = []
# for i in range(N) :
#     arr.append(int(input()))

# arr = merge_sort(arr)

# for _ in arr:
#     print(_)

# 위 코드는 O(NlogN) 문제다
# 하지만 속도 이슈로 인해 pypy3의 sorted를 쓰자
N = int(input())
arr = []

for i in range(N) :
    arr.append(int(input()))

for _ in sorted(arr):
    print(_)


# -*- coding:utf-8 -*-
from flask import g
from flask_restful import Resource, reqparse

from auth import basic_auth
from app.common.tool import set_return_val
from app.main.base.control.role import role_list, role_create, role_update, role_delete
from app.main.base import control

parser = reqparse.RequestParser()
parser.add_argument('name', type=str)
parser.add_argument('pgnum', type=int)
parser.add_argument('description', type=str)

class RoleManage(Resource):

    def get(self):
        """
         获取角色信息
         ---
         tags:
           - role
         summary: Add a new pet to the store
         parameters:
           - in: query
             name: name
             type: string
             description: 角色名
           - in: query
             name: pgnum
             type: string
             description: 页码
         responses:
           200:
            description: 获取角色信息
            schema:
             properties:
                ok:
                  type: boolean
                  default: 200
                  description: 状态
                code:
                  type: string
                msg:
                  type: string
                data:
                  type: array
                  items:
                    properties:
                      id:
                        type: string
                        default: 200
                        description: id
                      name:
                        default: admin
                        type: string
                        description: name
                      description:
                        default: admin
                        type: string
                        description: admin
         """
        args = parser.parse_args()

        if not args['pgnum']:
            pgnum = 1
        else:
            pgnum = args['pgnum']
        try:
            data, pg = control.role.role_list(name=args['name'], pgnum=pgnum)

        except Exception as e:
            return set_return_val(False, [], str(e), 1319), 400

        return set_return_val(True, data, 'Get the role information successfully', 1200, pg)

    @basic_auth.login_required
    def post(self):
        """
         创建角色信息
         ---
         tags:
           - role
         summary: Add a new pet to the store
         parameters:
           - in: query
             name: name
             type: string
             description: 角色名
           - in: query
             name: description
             type: string
             description: 描述
         responses:
           200:
            description: 创建角色信息
            schema:
             properties:
                ok:
                  type: boolean
                  default: 200
                  description: 状态
                code:
                  type: string
                msg:
                  type: string
                data:
                  type: array
                  items:
                    properties:
         """
        args = parser.parse_args()

        if not all([args['name'], args['description']]):
            raise Exception('Parameter error')
        try:
            role = control.role.role_create(name=args['name'], description=args['description'])
            id = role.id
        except Exception as e:
            control.event_logs.eventlog_create(type='role', result=False, resources_id='',
                                               event=unicode('创建新角色:%s' % args['name']), submitter=g.username)
            return set_return_val(False, [], str(e), 1319), 400
        control.event_logs.eventlog_create(type='role', result=True, resources_id=id,
                                           event=unicode('创建新角色:%s' % args['name']), submitter=g.username)
        return set_return_val(True, [], 'The role information is created successfully.', 1200)

    @basic_auth.login_required
    def put(self, id):
        """
         更新角色信息
         ---
         tags:
           - role
         parameters:
           - in: path
             type: integer
             format: int64
             name: id
             required: true
           - in: query
             name: name
             type: string
             description: 角色名
           - in: query
             name: description
             type: string
             description: 描述
         responses:
           200:
            description: 更新角色信息
            schema:
             properties:
                ok:
                  type: boolean
                  default: 200
                  description: 状态
                code:
                  type: string
                msg:
                  type: string
                data:
                  type: array
                  items:
                    properties:
         """
        args = parser.parse_args()
        try:
            name = control.role.role_update(role_id=int(id), name=args['name'], description=args['description'])
        except Exception as e:
            control.event_logs.eventlog_create(type='role', result=False, resources_id=id,
                                               event=unicode('更新角色信息'), submitter=g.username)
            return set_return_val(False, [], str(e), 1319), 400
        control.event_logs.eventlog_create(type='role', result=True, resources_id=id, event=unicode('更新角色:%s' % name),
                                           submitter=g.username)
        return set_return_val(True, [], 'Role information updated successfully', 1200)

    @basic_auth.login_required
    def delete(self, id):
        """
        删除角色信息
        ---
        tags:
          - role
        parameters:
          - in: path
            type: integer
            format: int64
            name: id
            required: true
        responses:
          200:
            description: 删除角色信息
            schema:
             properties:
                ok:
                  type: boolean
                  default: 200
                  description: 状态
                code:
                  type: string
                msg:
                  type: string
                data:
                  type: array
                  items:
                    properties:
          """
        try:
            name = control.role.role_delete(id)
        except Exception as e:
            control.event_logs.eventlog_create(type='role', result=False, resources_id=id,
                                               event=unicode('删除角色信息'), submitter=g.username)
            return set_return_val(False, [], str(e), 1319), 400
        control.event_logs.eventlog_create(type='role', result=True, resources_id=id, event=unicode('删除角色:%s' % name),
                                           submitter=g.username)
        return set_return_val(True, [], 'The role information was deleted successfully.', 1200)


import streamlit as st
from get_crypto import get_crypto_data
from PIL import Image

st.set_page_config(layout='wide')

image = Image.open('logo.png')

st.image(image, width=500)

st.title('Streamlit Crypto Price App')

st.markdown("""
This app retrieves chosen cryptocurrency prices from **Binance** !
""")

expander_bar = st.beta_expander("About")
expander_bar.markdown("""
* **Python libraries:** pandas, streamlit,
* **Inspired by:** [Data Professor](https://www.youtube.com/channel/UCV8e2g4IWQqK71bbzGDEI4Q).
* **Credit:** App is importing cryptocurrencies data from crypto exchanges thanks [ccxt](https://github.com/ccxt).
""")

col1 = st.sidebar
col2, col3 = st.beta_columns((4, 1))

col1.header('Options')

units = ['BTC', 'ETH', 'LSK', 'LTC']

price_unit = col1.selectbox('Select price unit', ('USDT', 'BUSD', 'EUR', 'BTC'))
currency_unit = col1.selectbox('Select currency', (units))
time = col1.selectbox('Select time resolution',
                      ('1d', '3d', '1w', '1M'))
col2.subheader("Crypto Data")
# '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h'
if price_unit == 'BTC' and currency_unit == 'BTC':
    col2 = None
else:
    get_data = f"{currency_unit}/{price_unit}"
    crypto_data = get_crypto_data(get_data, time_resolution=time)
    col2.dataframe(crypto_data)
    col2.subheader(get_data)
    col2.line_chart(data=crypto_data[["high", "low"]])


from django.urls import path
from .views import *

urlpatterns=[
    path('',dashboard,name='dashboard'),
    path('my_articles/',user_articles,name='my-articles'),
    path('ajouter-article/',AddArticle.as_view(),name='ajouter-article'),
    path('update-article/<int:pk>', UpdateArticle.as_view(),name='update-article'),
    path('delete-article/<int:pk>',DeleteArticle.as_view(),name='delete-article'),
    path('add-panier/<int:id>',add_panier,name='add_panier'),
    path('mon_panier/',panier,name='panier'),
    path('incrementer-quantite/<int:achat_id>/', ajouter_achat, name='incrementer_achat'),
    path('decrementer-quantite/<int:achat_id>/', retirer_achat, name='decrementer_achat'),
    path('generate-pdf/', generer_pdf, name='generate-pdf'),

]


import RPi.GPIO as GPIO
import datetime
import sys

GPIO.setwarnings(False)
# originally 11
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
pwm = GPIO.PWM(24, 500)  # PIN 12 = Board 32

def switch_off():
    pwm.ChangeDutyCycle(0)
    pwm.stop()

def switch_on(dc):
    print(dc)
    pwm.start(dc)

if __name__ == "__main__":
    x = datetime.datetime.now()
    y = x + datetime.timedelta(0, 5)
    switch_on(40)
    while datetime.datetime.now() < y:
        pass
    switch_off()


from opentrons import robot, labware, instruments
import json

# metadata
metadata = {
    'protocolName': 'Dilution for UC-Davis',
    'author': 'Cedric K. Brinkmann, based on protocol from iGEM UC-Davis 2019',
    'description': 'A simple dilution protocol for the collaboration with UC-Davis',
}

with open("UC-Davis_Dilution_Protocol.json", "r") as infile:
    var_json = json.loads(infile.read())

### LABWARE ###
tiprack_01 = labware.load(var_json["Labware"][0]["labware"], var_json["Labware"][0]["Slot"])
falcon = labware.load(var_json["Labware"][1]["labware"], var_json["Labware"][1]["Slot"])
plate = labware.load(var_json["Labware"][2]["labware"], var_json["Labware"][2]["Slot"])

### Liquids ###
fluro_stock = falcon.wells( var_json["Liquids"][0]["Slot"])
pbs_stock = falcon.wells(var_json["Liquids"][1]["Slot"])

### Pipettes ###
p10 = instruments.P10_Single(mount='left')
p300 = instruments.P300_Single(
    mount='right',
    tip_racks=[tiprack_01])

p300.transfer(200,
    fluro_stock,
    plate.wells('A1', 'B1', 'C2', 'D1'),
    new_tip = 'once',
    strategy = 'arc')

wells = [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88,
    9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89,
    10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90,
    11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91
]

p300.transfer(
    100,
    pbs_stock,
    plate.wells(wells),
    new_tip = 'once',
    strategy = 'arc')

for j in range(4): # From A to D
    p300.drop_tip()
    p300.pick_up_tip()
    for i in range(11): # From well 1 to 12
        p300.transfer(100,
        plate.wells(i*8+j),
        plate.wells((i+1)*8+j),
        new_tip = 'never',
        strategy = 'direct',
        mix_after = (3, 50))


# -*- coding: utf-8 -*-
#from scrapy import log
from scrapy.contrib.spiders import XMLFeedSpider
from scrapy.selector import Selector
from ..items import AmazonDealsItem
from datetime import datetime

class DealsSpider(XMLFeedSpider):
    name = 'amazon_deals'
    allowed_domains = ['amazon.com']
    start_urls = ['http://rssfeeds.s3.amazonaws.com/goldbox']
    iterator = 'iternodes'  # This is actually unnecessary, since it's the default value
    itertag = 'item'

    def parse_node(self, response, node):
        item = AmazonDealsItem()
        item['title'] = ''.join(node.xpath('title/text()').extract())
        item['link'] = ''.join(node.xpath('link/text()').extract())
        desc_body = ''.join([str(a) for a in node.xpath('description/text()').extract()])
        item['desc'] = desc_body
        #Selector(text=desc_body,type='html').xpath('//text()').extract()
        item['pub_date'] = ''.join(node.xpath('pubDate/text()').extract())
        item['spider_metadata'] = { 'ts': datetime.utcnow(), 'status':'NEW' }
        return item


word_to_guess = 'apple'  # Word to be guessed.
user_word = '__ __ __ __ __'  # Word showing correct guesses of player so far.
guess = ''  # Word inputted by player
hint = 'Journey\'s Fruit from Heaven to Earth'  # Hint to word to be guessed.
lives = 6
exit = False

def game_intro():
    print(f'''
    Welcome to the Hangman Game.\n
    You try to guess a five-letter word correctly.\n
    You have six lives.\n
    You lose a life for every incorrect guess.\n
    \n
    ''')

def play_game():
    print(f'''
    Word: {user_word}\n
    Hint: {hint}\n
    Lives: {lives}\n
    ''')
    guess = input('Enter a letter: ')
    print('You entered letter: ', guess)
    check_len(guess)

def check_len(user_input):
    while len(user_input) != 1:
        user_input = input('Enter a single letter: ')

    else:
        guess = user_input

def validate_guess(letter, word):
    if letter in word:
        return (letter, word.index(letter))
    else:
        return False

game_intro()
if exit == False:
    play_game()
    print(validate_guess(guess, word_to_guess))


import requests
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from bs4 import BeautifulSoup
from pprint import pprint
import os

SPOTIFY_CLIENT_ID = os.environ.get("SPOTIFY_CLIENT_ID")
SPOTIFY_CLIENT_SECRET = os.environ.get("SPOTIFY_CLIENT_SECRET")
SPOTIPY_REDIRECT_URI = "http://example.com"

date = input("A que año te gustaría viajar? Escribe el año en este formato AAAA-MM-DD\n")
BILLBOARD_WEB = f"https://www.billboard.com/charts/hot-100/{date}/"

response = requests.get(url=BILLBOARD_WEB)
billboard = response.text

soup = BeautifulSoup(billboard, "html.parser")
# print(soup.find_all(name="div", class_="o-chart-results-list-row-container"))
top_songs = soup.find_all(name="div", class_="o-chart-results-list-row-container")
songs = [song.find(name="h3", id="title-of-a-story").getText().strip() for song in top_songs]
print(songs)

sp = spotipy.Spotify(
    auth_manager=SpotifyOAuth(
        scope="playlist-modify-private",
        redirect_uri="http://example.com",
        client_id=SPOTIFY_CLIENT_ID,
        client_secret=SPOTIFY_CLIENT_SECRET,
        show_dialog=True,
        cache_path="token.txt"
    )
)
user_id = sp.current_user()["id"]

song_uris = []
year = date.split("-")[0]

for song in songs:
    result = sp.search(q=f"track:{song} year:{year}", type="track")
    # pprint(result)
    try:
        uri = result["tracks"]["items"][0]["uri"]
        song_uris.append(uri)
    except IndexError:
        print(f"{song} doesn't exist in Spotify. #Skipped.")

playlist = sp.user_playlist_create(user=user_id, name=f"{date} Billboard 100", public=False)
playlist_id = playlist["id"]

sp.playlist_add_items(playlist_id=playlist_id, items=song_uris)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn
from itertools import chain

def plot_degree_dist(edge_list):
    deg_dict = {}
    for n, n_ in edge_list:
        deg_dict[n] = deg_dict.get(n,0) + 1
    degrees = deg_dict.values()
    max_k = max(degrees)
    ddist = [0] * max_k
    for d in degrees:
        ddist[d-1] += 1
    plt.bar(range(max_k), ddist)
    plt.show()

if __name__=="__main__":
    elist = [(0,1), (1,2), (1,0)]
    plot_degree_dist(elist)


from django.urls import path
from .views import HomePageView, NosotrosPageView, TyCPageView, ContactoPageView, send_email, PrivacidadPageView

urlpatterns = [
    path('', HomePageView.as_view(), name="home"),
    path('nosotros', NosotrosPageView.as_view(), name="nosotros"),
    path('terminosycondiciones', TyCPageView.as_view(), name="tyc"),
    path('contacto', ContactoPageView.as_view(), name="contacto"),
    path('friends', send_email, name='friend'),
    path('privacidad', PrivacidadPageView.as_view(), name='privacidad')
]


import gzip
import logging
import zlib
from os import environ
from typing import Dict, Optional, Any, Callable, List
from typing import Sequence, Mapping  # noqa: F401

from io import BytesIO
from time import sleep
from deprecated import deprecated

from opentelemetry.exporter.otlp.proto.common._internal import (
    _get_resource_data,
    _create_exp_backoff_generator,
)
from opentelemetry.exporter.otlp.proto.common._internal.metrics_encoder import (
    OTLPMetricExporterMixin,
)
from opentelemetry.exporter.otlp.proto.common.metrics_encoder import (
    encode_metrics,
)
from opentelemetry.exporter.otlp.proto.http import Compression
from opentelemetry.sdk.metrics._internal.aggregation import Aggregation
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import (  # noqa: F401
    ExportMetricsServiceRequest,
)
from opentelemetry.proto.common.v1.common_pb2 import (  # noqa: F401
    AnyValue,
    ArrayValue,
    KeyValue,
    KeyValueList,
)
from opentelemetry.proto.common.v1.common_pb2 import (  # noqa: F401
    InstrumentationScope,
)
from opentelemetry.proto.resource.v1.resource_pb2 import Resource  # noqa: F401
from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2  # noqa: F401
from opentelemetry.sdk.environment_variables import (
    OTEL_EXPORTER_OTLP_ENDPOINT,
    OTEL_EXPORTER_OTLP_CERTIFICATE,
    OTEL_EXPORTER_OTLP_HEADERS,
    OTEL_EXPORTER_OTLP_TIMEOUT,
    OTEL_EXPORTER_OTLP_COMPRESSION,
    OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
    OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
    OTEL_EXPORTER_OTLP_METRICS_HEADERS,
    OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
    OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
)
from opentelemetry.sdk.metrics.export import (
    AggregationTemporality,
    MetricExporter,
    MetricExportResult,
    MetricsData,
)
from opentelemetry.sdk.metrics.export import (  # noqa: F401
    Gauge,
    Histogram as HistogramType,
    Sum,
)
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.util.re import parse_env_headers

import requests
from opentelemetry.proto.resource.v1.resource_pb2 import (
    Resource as PB2Resource,
)

_logger = logging.getLogger(__name__)

DEFAULT_COMPRESSION = Compression.NoCompression
DEFAULT_ENDPOINT = "http://localhost:4318/"
DEFAULT_METRICS_EXPORT_PATH = "v1/metrics"
DEFAULT_TIMEOUT = 10  # in seconds

class OTLPMetricExporter(MetricExporter, OTLPMetricExporterMixin):

    _MAX_RETRY_TIMEOUT = 64

    def __init__(
        self,
        endpoint: Optional[str] = None,
        certificate_file: Optional[str] = None,
        headers: Optional[Dict[str, str]] = None,
        timeout: Optional[int] = None,
        compression: Optional[Compression] = None,
        session: Optional[requests.Session] = None,
        preferred_temporality: Dict[type, AggregationTemporality] = None,
        preferred_aggregation: Dict[type, Aggregation] = None,
    ):
        self._endpoint = endpoint or environ.get(
            OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
            _append_metrics_path(
                environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_ENDPOINT)
            ),
        )
        self._certificate_file = certificate_file or environ.get(
            OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
            environ.get(OTEL_EXPORTER_OTLP_CERTIFICATE, True),
        )
        headers_string = environ.get(
            OTEL_EXPORTER_OTLP_METRICS_HEADERS,
            environ.get(OTEL_EXPORTER_OTLP_HEADERS, ""),
        )
        self._headers = headers or parse_env_headers(headers_string)
        self._timeout = timeout or int(
            environ.get(
                OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
                environ.get(OTEL_EXPORTER_OTLP_TIMEOUT, DEFAULT_TIMEOUT),
            )
        )
        self._compression = compression or _compression_from_env()
        self._session = session or requests.Session()
        self._session.headers.update(self._headers)
        self._session.headers.update(
            {"Content-Type": "application/x-protobuf"}
        )
        if self._compression is not Compression.NoCompression:
            self._session.headers.update(
                {"Content-Encoding": self._compression.value}
            )

        self._common_configuration(preferred_temporality)

    def _export(self, serialized_data: str):
        data = serialized_data
        if self._compression == Compression.Gzip:
            gzip_data = BytesIO()
            with gzip.GzipFile(fileobj=gzip_data, mode="w") as gzip_stream:
                gzip_stream.write(serialized_data)
            data = gzip_data.getvalue()
        elif self._compression == Compression.Deflate:
            data = zlib.compress(bytes(serialized_data))

        return self._session.post(
            url=self._endpoint,
            data=data,
            verify=self._certificate_file,
            timeout=self._timeout,
        )

    @staticmethod
    def _retryable(resp: requests.Response) -> bool:
        if resp.status_code == 408:
            return True
        if resp.status_code >= 500 and resp.status_code <= 599:
            return True
        return False

    def export(
        self,
        metrics_data: MetricsData,
        timeout_millis: float = 10_000,
        **kwargs,
    ) -> MetricExportResult:
        serialized_data = encode_metrics(metrics_data)
        for delay in _create_exp_backoff_generator(
            max_value=self._MAX_RETRY_TIMEOUT
        ):

            if delay == self._MAX_RETRY_TIMEOUT:
                return MetricExportResult.FAILURE

            resp = self._export(serialized_data.SerializeToString())
            # pylint: disable=no-else-return
            if resp.status_code in (200, 202):
                return MetricExportResult.SUCCESS
            elif self._retryable(resp):
                _logger.warning(
                    "Transient error %s encountered while exporting metric batch, retrying in %ss.",
                    resp.reason,
                    delay,
                )
                sleep(delay)
                continue
            else:
                _logger.error(
                    "Failed to export batch code: %s, reason: %s",
                    resp.status_code,
                    resp.text,
                )
                return MetricExportResult.FAILURE
        return MetricExportResult.FAILURE

    def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
        pass

    @property
    def _exporting(self) -> str:
        return "metrics"

    def force_flush(self, timeout_millis: float = 10_000) -> bool:
        """Nothing is buffered in this exporter, so this method does nothing."""
        return True

@deprecated(
    version="1.18.0",
    reason="Use one of the encoders from opentelemetry-exporter-otlp-proto-common instead",
)
def get_resource_data(
    sdk_resource_scope_data: Dict[SDKResource, Any],  # ResourceDataT?
    resource_class: Callable[..., PB2Resource],
    name: str,
) -> List[PB2Resource]:
    return _get_resource_data(sdk_resource_scope_data, resource_class, name)

def _compression_from_env() -> Compression:
    compression = (
        environ.get(
            OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
            environ.get(OTEL_EXPORTER_OTLP_COMPRESSION, "none"),
        )
        .lower()
        .strip()
    )
    return Compression(compression)

def _append_metrics_path(endpoint: str) -> str:
    if endpoint.endswith("/"):
        return endpoint + DEFAULT_METRICS_EXPORT_PATH
    return endpoint + f"/{DEFAULT_METRICS_EXPORT_PATH}"


__author__ = 'Dylan'

from socket import *
import threading

ip = open("ip.info", "r")

host = ip.readline()

ip.close()

port = 5005

#Creating socket object
sockServer = socket()
#Binding socket to a address. bind() takes tuple of host and port.
sockServer.bind((host, port))
#Listening at the address
sockServer.listen(5) #5 denotes the number of clients can queue

lastCommand = "P 0|0|0|0|0 0"
lastProperties = "D 0|0|0|0|0|0|0|0|0|12|0|0|0"

def clientthread(connection):
    global lastProperties
    global lastCommand

    while True:
        data = connection.recv(1024)

        command = data.decode()
        commandSplit = command.split()

        if len(commandSplit) > 0:
            print(commandSplit)

            if commandSplit[0] == "C":
                print("CONTROLLER")

                lastCommand = commandSplit[1] + " " + commandSplit[2] + " " + commandSplit[3]

                print("lastCommand", lastCommand)

                connection.send(lastProperties.encode())
            elif commandSplit[0] == "D":
                print("DRONE")

                if commandSplit[1] != "SEND":
                    lastProperties = commandSplit[1] + " " + commandSplit[2]

                    connection.send(lastCommand.encode())
            else:
                print("NOT FOUND")

while True:
    print("new connection")

    connection, clientAddress = sockServer.accept()

    th1 = threading.Thread(target=clientthread, args=[connection])
    th1.start()

sockServer.close()


report_file = open("input.txt")
report_raw = report_file.read()
report_file.close()
report = report_raw.split("\n")

'''
Converts a string containing a command, to a list consisting of
direction (string) and number of steps to take in that direction (integer)
'''
def getCommand(str):
    size = len(str)
    direction = str[:size-2]
    steps = int(str[-1])
    return [direction, steps]

'''
Calculates horizontal position and depth with regards to input
'''
def calculatePosition(lst):
    horizontal = 0
    depth = 0
    for i in range(len(lst)):
        direction = getCommand(lst[i])[0]
        steps = getCommand(lst[i])[1]
        if direction == "forward":
            horizontal = horizontal + steps
        elif direction == "down":
            depth = depth + steps
        elif direction == "up":
            depth = depth - steps

    return [horizontal, depth]

'''
Calculates horizontal position, depth and aim with regards to input
'''
def calculatePositionAim(lst):
    horizontal = 0
    depth = 0
    aim = 0
    for i in range(len(lst)):
        direction = getCommand(lst[i])[0]
        steps = getCommand(lst[i])[1]
        if direction == "forward":
            horizontal = horizontal + steps
            depth = depth + (aim * steps)
        elif direction == "down":
            aim = aim + steps
        elif direction == "up":
            aim = aim - steps
    return [horizontal, depth]


#!/usr/bin/env python3

import os
import time
import datetime

errors_file = os.path.join(os.path.dirname(__file__), 'errors.txt')
events_file = os.path.join(os.path.dirname(__file__), 'events.txt')
email_file = os.path.join(os.path.dirname(__file__), 'email.txt')

def save_logs(error):
    if not os.path.exists(errors_file):
        open(errors_file, "w+")

    now_time = (time.strftime("%Y-%m-%d-%H:%M:%S"))
    with open(errors_file, 'a') as ef:
        ef.write(str(now_time))
        ef.write("\t")
        ef.write(str(error))
        ef.write("\n")
        ef.close()

def save_events_app(event):
    if not os.path.exists(events_file):
        open(events_file, "w+")

    now_time = (time.strftime("%Y-%m-%d-%H:%M:%S"))
    with open(events_file, 'a') as ef:
        ef.write(str(now_time))
        ef.write("\t")
        ef.write(str(event))
        ef.write("\n")
        ef.close()

def sent_emails(operation, number):

    if not os.path.exists(email_file):
        open(email_file, "w+")

    raw_create_time = os.path.getmtime(email_file)
    human_create_time =  datetime.datetime.fromtimestamp(raw_create_time).strftime('%Y%m%d')

    today_is = time.strftime('%Y%m%d')
    if int(today_is) > int(human_create_time):
        os.remove(email_file)
        open(email_file, "w+")
        if number != 1:
            number = 1

    if operation == "save":
        with open(email_file, 'w') as ef:
            ef.write(str(number))
            ef.close()
            return

    if operation == "read":
        with open(email_file, 'r') as ef:
            email_number = ef.read()
            if email_number == "":
                email_number = 0

            return email_number

def remove_all_logs():
    if os.path.exists(errors_file):
        os.remove(errors_file)
    if os.path.exists(events_file):
        os.remove(events_file)
    if os.path.exists(email_file):
        os.remove(email_file)
    return


'''Fitness evaluation for CNN topology strings'''
import concurrent.futures
import logging
import pickle
import time

import torch
import torch.nn as nn

from cga_src.hardware import HardwareManager
from cga_src.cnn import CNN
from cga_src.data import TorchDataset
from cga_src.base import InvalidTopologyError

class Evaluation:
    def __init__(self, accuracy, duration, size, fitness_punishment_per_hour=0):
        self.size = size
        self.accuracy = accuracy
        self.fitness = accuracy - duration * fitness_punishment_per_hour
        self.duration = duration

class Evaluator():
    '''
    Each evaluator is bound to a data set.
    It caches and evaluates fitnesses of CNN topology strings.
    '''

    def __init__(self,
                 data,
                 cache=None,

                 fitness_punishment_per_hour=0,
                 learning_rate_decay_points=None,
                 learning_rate=0.1,
                 momentum=0.9,
                 learning_rate_decay_factor=0.9,
                 ):
        self.cache = cache or {}
        self._data = data
        self._learning_rate_decay_points = learning_rate_decay_points or []
        self._learning_rate = learning_rate
        self._momentum = momentum
        self._learning_rate_decay_factor = learning_rate_decay_factor
        self.progress_cb = lambda: None
        self.fitness_punishment_per_hour = fitness_punishment_per_hour

        assert isinstance(self.cache, dict)
        assert isinstance(self._data, TorchDataset)

    def pickle(self, fname=None, wait=True):
        '''
        Returns a pickleable representation and optionally saves to file
        By default it will wait for all futures to complete
        '''

        pickleable_cache = {}
        # thread safe: don't iterate over self.cache directly
        for topology in list(self.cache.keys()):
            evaluation = self.cache[topology]['evaluation']

            if isinstance(evaluation, concurrent.futures.Future):
                if (evaluation.done() or wait):
                    evaluation = evaluation.result()
                else:
                    continue
            model = self.cache[topology]['model'] \
                .to('cpu') if self.cache[topology]['model'] else None

            pickleable_cache[topology] = {
                'evaluation': evaluation,
                'model': model
            }

        obj = {
            'cache': pickleable_cache,
            'learning_rate_decay_points': self._learning_rate_decay_points,
            'learning_rate': self._learning_rate,
            'momentum': self._momentum,
            'fitness_punishment_per_hour': self.fitness_punishment_per_hour,
            'learning_rate_decay_factor': self._learning_rate_decay_factor,
        }

        if fname:
            with open(fname, 'wb') as file:
                pickle.dump(obj, file)

        return obj

    @classmethod
    def from_pickle(cls, pckle, data):
        '''
        Restores a pickleable representation from either a file or an object
        '''

        if isinstance(pckle, str):
            with open(pckle, 'rb') as file:
                pckle = pickle.load(file)

        parsed_cache = {}
        cache = pckle['cache']
        for topology in cache:
            future = concurrent.futures.Future()

            # backwards compatability: old pickles had no 'evaluation'
            if 'evaluation' in cache[topology]:
                evaluation = cache[topology]['evaluation']
            else:
                evaluation = cache[topology]['fitness'], cache[topology]['fitness']

            # yet another backwards compatibility: create the object if it is a tuple
            if not isinstance(evaluation, Evaluation):
                fitness, accuracy = evaluation
                evaluation = Evaluation(accuracy, 0, 0)
                evaluation.fitness = fitness

            future.set_result(evaluation)
            parsed_cache[topology] = {
                'evaluation': future,
                'model': cache[topology]['model']
            }

        fitness_punishment_per_hour = 0 if 'fitness_punishment_per_hour' not in pckle else pckle[
            'fitness_punishment_per_hour']

        return cls(cache=parsed_cache,
                   data=data,
                   learning_rate_decay_points=pckle['learning_rate_decay_points'],
                   learning_rate=pckle['learning_rate'],
                   momentum=pckle['momentum'],
                   learning_rate_decay_factor=pckle['learning_rate_decay_factor'],
                   fitness_punishment_per_hour=fitness_punishment_per_hour,
                   )

    def get_evaluation(self, individual):
        '''
        Gets a tuple of (fitness, accuracy) of a topology as a promise.
        Call .result() to block for it
        '''

        if isinstance(individual, str):
            # this makes sure that the cache is not invalidated by small differences
            # when making manual changes on string topologies
            model = CNN.from_string(
                input_shape=self._data.example.shape,
                string=individual,
                num_classes=self._data.num_classes
            )
        else:
            model = individual

        if model.topology in self.cache:
            # this topology was previously trained for this number of epochs
            return self.cache[model.topology]['evaluation']

        evaluation = None
        # try to find a model that has been partially trained
        required_epoch = model.num_epochs
        for available_epoch in range(required_epoch - 1, -1, -1):
            partially_trained_individual = model.clone()
            partially_trained_individual.num_epochs = available_epoch
            if partially_trained_individual.topology in self.cache:
                # this topology was previously trained for a smaller number of epochs
                entry = self.cache[partially_trained_individual.topology]

                # edge case: find a model that finished training
                if not entry['evaluation'].done():
                    continue

                evaluation = entry['evaluation'].result()
                assert isinstance(evaluation, Evaluation)

                # resume training: query and clone the partially trained model with weights
                model = entry['model'].clone_with_weights()
                model.num_epochs = required_epoch

                evaluation = HardwareManager.add_job(self._train_test, {
                    'model': model,
                    'start_epoch': available_epoch,
                    'end_epoch': model.num_epochs,
                    'duration': evaluation.duration,
                })
                break

        if not evaluation:
            # this model has not been partially trained, so train from the beginning
            evaluation = HardwareManager.add_job(self._train_test, {
                'model': model,
                'start_epoch': 0,
                'end_epoch': model.num_epochs,
                'duration': 0,
            })

        self.cache[model.topology] = {
            'model': model,
            'evaluation': evaluation
        }

        def callback(_):
            time.sleep(1)
            self.progress_cb()

        evaluation.add_done_callback(callback)

        return evaluation

    def get_evaluations(self, population):
        '''Generator that yields pairs of (topology, evaluation)
        as they are evaluated by the hardware'''

        promises = [self.get_evaluation(individual)
                    for individual in population]
        future_mapping = {promises[i]: population[i]
                          for i in range(len(population))}

        for promise in concurrent.futures.as_completed(promises):
            topology = future_mapping[promise]
            evaluation = promise.result()

            yield topology, evaluation

    def _train_test(self, model, start_epoch, end_epoch, duration, device):

        try:
            model, duration_train = self._train(
                model, start_epoch, end_epoch, device
            )
            accuracy, duration_test = self._test(model, device)
            duration += (duration_train + duration_test) / 3600  # in hours

            model = model.to('cpu')  # try to free some space

            return Evaluation(accuracy, duration, model.size, self.fitness_punishment_per_hour)

        except RuntimeError as exc:
            if 'out of memory' in str(exc):
                logging.warning('Ran out of memory for GPU (%s), individual %s',
                                device, model.topology)
                return Evaluation(0, 0, model.size)

            if 'Given groups' in str(exc):
                raise InvalidTopologyError(
                    model.topology,
                    model._layers[-1].output_shape,  # pylint: disable=protected-access
                    exc.args[0]
                )

            raise exc

    def _train(self,
               model,
               start_epoch,
               end_epoch,
               device
               ):

        model = model.to(device)

        start_time = time.time()
        model.train()

        # Loss and optimizer
        criterion = nn.CrossEntropyLoss()
        optimizer = torch.optim.SGD(
            model.parameters(), lr=self._learning_rate,
            momentum=self._momentum
        )

        logging.debug('Start train on %s, start epoch %d, end epoch %d',
                      device, start_epoch, end_epoch)
        # Train the model

        learning_rate = self._learning_rate
        for epoch in range(start_epoch, end_epoch):
            if epoch in self._learning_rate_decay_points:
                learning_rate *= self._learning_rate_decay_factor
                # not sure if I am allowed to do this
                optimizer = torch.optim.SGD(
                    model.parameters(), lr=learning_rate, momentum=self._momentum)

            for i, (images, labels) in enumerate(self._data.train):
                images = images.to(device)
                labels = labels.to(device)

                # Forward pass
                outputs = model(images)
                loss = criterion(outputs, labels)

                # Backward and optimize
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

                if (i+1) % 300 == 0:
                    total_step = len(self._data.train)
                    logging.debug('Epoch [%d/%d], Step [%d/%d], Loss: %.4f}',
                                  epoch+1, end_epoch,
                                  i+1, total_step, loss.item())
        # Save the model checkpoint
        # torch.save(model.state_dict(), 'model.ckpt')

        return model, time.time() - start_time

    def _test(self, model, device):
        model = model.to(device)

        # Test the model
        model.eval()
        with torch.no_grad():
            correct = 0
            total = 0
            start_time = time.time()
            for images, labels in self._data.test:
                images = images.to(device)
                labels = labels.to(device)
                outputs = model(images)
                _, predicted = torch.max(outputs.data, 1)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()

        logging.debug('Ended train on %s', device)
        return correct / total, time.time() - start_time


n, c = map(int,input().split())
m = list(map(int,input().split()))
value = {}
order = {}
order_num = 1

for i in range(n):
  if m[i] not in value:
    value[m[i]] = 1
    order[m[i]] = order_num
    order_num += 1
  else:
    value[m[i]] += 1

value = sorted(value.items(), key=lambda x: x[1] ,reverse=True)

dict_ = {}
for j in value:
  dict_[order[j[0]]] = j

for k in dict_.values():
  for _ in range(k[1]):
    print(k[0], end=' ')


##########################################################################
####################  This class is for evaluating a trained model  ######
##########################################################################

# import libraries
import numpy as np
import pandas as pd
import os
from sklearn.metrics import confusion_matrix, roc_curve, auc, mean_absolute_error, mean_squared_error, classification_report, accuracy_score
from utils.data_manager import DataManager
from utils.plot_data import PlotData
from keras.models import model_from_json
from keras import backend as K
K.set_image_dim_ordering('tf')

# Model type
model_type = 'wow_128_10'

# Images width, height, channels
img_height = 128
img_width = 128
# number of test samples in the dataset
num_of_test_samples = 800

# for confusion matrix plotting
cm_plot_labels = ['cover', 'stego']

# test dataset
model_test_dataset = 'dataset_' + model_type

# path to saved model files
saved_model_weights_path = './trained_for_pred/' + \
    model_type + '/model/Best-weights.h5'
saved_model_arch_path = './trained_for_pred/' + \
    model_type + '/model/scratch_model.json'
test_data_dir = './datasets/' + model_test_dataset + '/test'

# paths to save outputs
save_plt_cm = './trained_for_pred/' + model_type + '/stats/scratch_model_cm.png'
save_plt_normalized_cm = './trained_for_pred/' + \
    model_type + '/stats/scratch_model_norm_cm.png'
save_plt_roc = './trained_for_pred/' + \
    model_type + '/stats/scratch_model_roc.png'
save_eval_report = './trained_for_pred/' + \
    model_type + '/stats/eval_report.txt'

train_log_data_path = './trained_for_pred/' + \
    model_type + '/model/log/model_train.csv'

save_plt_accuracy = './trained_for_pred/' + \
    model_type + '/stats/model_accuracy.png'

save_plt_loss = './trained_for_pred/' + \
    model_type + '/stats/model_loss.png'

save_plt_learning = './trained_for_pred/' + \
    model_type + '/stats/model_learning.eps'
# Cost function
model_loss_function = 'binary_crossentropy'

# define optimizers
model_optimizer_rmsprop = 'rmsprop'

# model metrics to evaluate training
model_metrics = ["accuracy"]

# batch size
batch_size = 16

# Load architecture and weights of the model
def load_model():
    json_file = open(saved_model_arch_path, 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    loaded_model.load_weights(saved_model_weights_path)
    return loaded_model

def delete_file(filename):
    if os.path.exists(filename):
        os.remove(filename)
        pass

def main():
    # init DataManager class
    dataManager = DataManager(img_height, img_width)
    # init PlotData class
    plotData = PlotData()
    # load model
    print("===================== load model =========================")
    model = load_model()
    # get test data
    print("===================== load data =========================")
    test_data = dataManager.get_test_data(test_data_dir)
    # start the eval process
    print("===================== start eval =========================")
    y_true = test_data.classes
    # Confution Matrix and Classification Report
    Y_pred = model.predict_generator(test_data, num_of_test_samples // batch_size)
    y_pred = np.argmax(Y_pred, axis=1)
    # plot confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    plotData.plot_confusion_matrix(
        cm, cm_plot_labels, save_plt_cm, title='Confusion Matrix')
    plotData.plot_confusion_matrix(
        cm, cm_plot_labels, save_plt_normalized_cm, normalize=True, title='Normalized Confusion Matrix')
    # Compute ROC curve and ROC area for each class
    roc_auc = plotData.plot_roc(y_true, y_pred, save_plt_roc)
    mae = mean_absolute_error(y_true, y_pred)
    mse = mean_squared_error(y_true, y_pred)
    accuracy = accuracy_score(y_true, y_pred)

    print('mean absolute error: ' + str(mae))
    print('mean squared error: ' + str(mse))
    print('Area Under the Curve (AUC): ' + str(roc_auc))
    c_report = classification_report(
        y_true, y_pred, target_names=cm_plot_labels)
    print(c_report)
    delete_file(save_eval_report)
    with open(save_eval_report, 'a') as f:
        f.write('\n\n')
        f.write('******************************************************\n')
        f.write('**************   Evalaluation Report   ***************\n')
        f.write('******************************************************\n')
        f.write('\n\n')
        f.write('- Accuracy Score: ' + str(accuracy))
        f.write('\n\n')

        f.write('- Mean Absolute Error (MAE): ' + str(mae))
        f.write('\n\n')

        f.write('- Mean Squared Error (MSE): ' + str(mse))
        f.write('\n\n')

        f.write('- Area Under the Curve (AUC): ' + str(roc_auc))
        f.write('\n\n')

        f.write('- Confusion Matrix:\n')
        f.write(str(cm))
        f.write('\n\n')

        f.write('- Normalized Confusion Matrix:\n')
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        f.write(str(cm))
        f.write('\n\n')

        f.write('- Classification report:\n')
        f.write(str(c_report))

        f.close()

    train_validation = ['train', 'validation']
    data = pd.read_csv(train_log_data_path)
    acc = data['acc'].values
    val_acc = data['val_acc'].values
    loss = data['loss'].values
    val_loss = data['val_loss'].values

    # plot metrics to the stats dir
    plotData.plot_2d(acc, val_acc, 'epoch', 'accuracy',
                     'Model Accuracy', train_validation, save_plt_accuracy)
    plotData.plot_2d(loss, val_loss, 'epoch', 'loss',
                     'Model Loss', train_validation, save_plt_loss)
    plotData.plot_model_bis(data, save_plt_learning)

    '''
    # evalute model
    # compile the model
    print("==================== compile model ========================")
    model.compile(loss=model_loss_function, optimizer=model_optimizer_rmsprop, metrics=model_metrics)

    score = model.evaluate_generator(test_data, num_of_test_samples)
    print('Test Loss:', score[0])
    print('Test accuracy:', score[1])
    '''

if __name__ == "__main__":
    main()


from mappings import paths
import os

for actual_path, repo_path in paths:
    actual_path = os.path.expanduser(actual_path)
    if repo_path == '':
        repo_path = os.path.basename(actual_path)

    dest_dir = os.path.dirname(repo_path)
    if dest_dir != "" and not os.path.exists(dest_dir):
        os.makedirs(dest_dir)

    if os.path.exists(repo_path):
        # os.remove(repo_path)
        print(f'Ignoring {actual_path}')
        continue
    print(f'Linking {actual_path} to {repo_path}')
    os.link(actual_path, repo_path)


import tkinter as tk
from tkinter import ttk
import pandas as pd
import numpy as np
import plotly.express as px
df=pd.read_csv("tourist places.csv")
df=df.sort_values(by="Title")
import Graph_code as gc
import time
from tkinter import *
import trivago_tk

def triv2():

    # Creating tkinter window
    window = tk.Tk()
    window.title('Graph Analysis')
    window.geometry('500x500')
    window.resizable(False, False)
    #window.eval('tk::PlaceWindow . center')
    radio = IntVar()
    # label text for title
    ttk.Label(window, text = "Time v/s Average rate",
              font = ("Times New Roman", 15)).place(relx = 0.5, rely = 0.1, anchor=CENTER)

    # label
    ttk.Label(window, text = "Select City :").place(x = 20, y = 80)

    # Combobox creation
    city_chs_plot = tk.StringVar()
    citychoosen = ttk.Combobox(window, width = 27, textvariable = city_chs_plot)
    def graph():
        city_name=citychoosen.get()
        print(city_name)
        star=radio.get()
        star1=str(star)
        maxp = float(e2.get())
        gc.plot_graph(city_name,star1,maxp)
        #print(n.get())

    def goHome():
        window.destroy()
        trivago_tk.homeWindow()

    # Adding combobox drop down list
    citychoosen['values'] = tuple(df.iloc[:,0])
    #citychoosen['values'] = ("jaipur","pushkar","ajmer")

    citychoosen.place(x = 220, y = 80)
    citychoosen.current()

    l3=tk.Label(window,text="Number of Stars")
    l3.place(x=20,y=120)
    r1=tk.Radiobutton(window,text="1 star",variable=radio,value=1)
    r1.place(x=220,y=120)
    r2=tk.Radiobutton(window,text="2 star",variable=radio,value=2)
    r2.place(x=220,y=140)
    r3=tk.Radiobutton(window,text="3 star",variable=radio,value=3)
    r3.place(x=220,y=160)
    r4=tk.Radiobutton(window,text="4 star",variable=radio,value=4)
    r4.place(x=220,y=180)
    r5=tk.Radiobutton(window,text="5 star",variable=radio,value=5)
    r5.place(x=220,y=200)

    def entryOpen():
        valChk = checkbox1.get()
        if valChk == 1:
            e2.config(state='normal')
            e2.delete(0, 'end')
        else:
            e2.delete(0, 'end')
            e2.insert(0, -1)
            e2.config(state='disabled')

    checkbox1 = IntVar()
    cb1=Checkbutton(window,text="Check this button if you want to enter maximum price",variable=checkbox1,onvalue=1,offvalue=0,command=entryOpen).place(x=20,y=230)

    l2=Label(window,text="Maximum Price").place(x=20,y=260)

    maxPrice = IntVar(window, value=-1)
    e2=Entry(window, state='disabled', textvariable=maxPrice)
    e2.place(x=220,y=260)

    b1=tk.Button(window,text="Plot Graph",bg="green",fg="white",command=graph)
    b1.place(x=20,y=300)
    btn2=Button(window,text="Home",bg="green",fg="white",command=goHome).place(x=100,y=300)

    window.mainloop()

Trivago scrap and analysis/data_analysis_gui.py


import time
import numpy as np
import pickle
import math
from sklearn.cluster import KMeans

def load_split_data():
    file_path = 'data_split.p'
    f = open(file_path, 'rb')
    data = pickle.load(f)
    f.close()

    return data

def find_miu_using_knn(X, n):
    kmeans = KMeans(n_clusters=n)
    kmeans.fit(X)
    return kmeans

def calculate_euclidean_distance(x, y):
    total = 0
    for i in range(len(x)):
        total += math.pow(x[i] - y[i], 2)

    return math.sqrt(total)

def calculate_piecewise_fi(x, centroid):
    return calculate_euclidean_distance(x, centroid)

def calculate_cubic_fi(x, centroid):
    return math.pow(calculate_euclidean_distance(x, centroid), 3)

def calculate_thin_plate_fi(x, centroid):
    dist = calculate_euclidean_distance(x, centroid)
    try:
        return math.pow(dist, 2) * math.log(dist, 2)
    except:
        return 0.0

def calculate_gaussian_fi(x, centroid, covariance):
    covariance_inverse = np.linalg.inv(covariance)
    sub_T = np.subtract(x, centroid)
    sub = sub_T.reshape(-1, 1)

    cov_sub = np.dot(covariance_inverse, sub)
    subT_cov_sub = np.dot(sub_T, cov_sub)

    power = (-0.5) * subT_cov_sub
    return math.pow(math.e, power)

def calculate_covariance(X, miu, x_clusters):
    result_list = []
    for i in range(len(miu)):
        sigma = []
        centroid = miu[i]
        x_for_this_centroid = [X[index] for index, x in enumerate(x_clusters) if x == i]

        for x in x_for_this_centroid:
            sub_T = np.subtract(x, centroid)
            sub = sub_T.reshape(-1, 1)
            multiply = np.multiply(sub, sub_T)
            sigma.append(multiply)

        sigma_total = np.zeros((8, 8))
        for s in sigma:
            sigma_total = np.add(sigma_total, s)

        if np.count_nonzero(sigma_total) > 0:
            result = sigma_total / (len(x_for_this_centroid) - 1)
        else:
            result = sigma_total

        result_list.append(result)

    return result_list

def remove_singular_centroid_covariance(miu, covariances):
    new_miu = []
    new_covariances = []

    for i in range(len(miu)):
        if np.count_nonzero(covariances[i]) > 0:
            new_miu.append(miu[i])
            new_covariances.append(covariances[i])

    return new_miu, new_covariances

def train_rbf_model(X, Y, n_neuron_hidden_layer=10):
    start = time.time()
    kmeans = find_miu_using_knn(X, n_neuron_hidden_layer)
    miu = kmeans.cluster_centers_

    x_clusters = kmeans.labels_
    covariances = calculate_covariance(X, miu, x_clusters)
    # miu, covariances = remove_singular_centroid_covariance(miu, covariances)
    Z = []

    counter = 0
    for x in X:
        print(counter)
        counter += 1

        z = []
        for i in range(len(miu)):
            # fi = calculate_gaussian_fi(x, miu[i], covariances[i]) # scalar results
            # fi = calculate_piecewise_fi(x, miu[i])
            fi = calculate_cubic_fi(x, miu[i])
            # fi = calculate_thin_plate_fi(x, miu[i])
            z.append(fi)

        Z.append([1] + z) # the first one is the bias

    print(time.time() - start)
    Z = np.matrix(Z)
    ZT = Z.transpose()

    ZT_Z = np.dot(ZT, Z)
    ZT_Z_inverse = np.linalg.inv(ZT_Z)

    Y = np.array(Y).reshape(-1, 1)
    ZT_Y = np.dot(ZT, Y)
    W = np.dot(ZT_Z_inverse, ZT_Y)
    return W, miu, covariances

def predict_house_price(X, weights, miu, covariances):
    predictions = []
    for x in X:
        z = [1] # bias
        for i in range(len(miu)):
            # fi = calculate_gaussian_fi(x, miu[i], covariances[i])
            # fi = calculate_piecewise_fi(x, miu[i])
            fi = calculate_cubic_fi(x, miu[i])
            # fi = calculate_thin_plate_fi(x, miu[i])
            z.append(fi)

        prediction = sum([z[i] * weights[i] for i in range(len(z))])
        predictions.append(prediction[0, 0])

    return predictions

def calculate_mse(predictions, Y):
    squared_error = [math.pow(predictions[i] - Y[i], 2) for i in range(len(predictions))]
    total_squared_error = sum(squared_error)
    return total_squared_error / len(predictions)

if __name__:
    data = load_split_data()
    X_train = data['X_train']
    Y_train = data['Y_train']
    X_model_train = data['X_train_norm']
    Y_model_train = data['Y_train_norm']
    X_model_validation = data['X_validation_norm']
    Y_model_validation = data['Y_validation_norm']

    N_NEURON_HIDDEN_LAYER = [1, 2, 3, 4, 5, 10, 15]
    print('Settings:')
    print('N_NEURON_HIDDEN_LAYER: {}'.format(N_NEURON_HIDDEN_LAYER))

    final_result = []
    for n_neuron_hidden_layer in N_NEURON_HIDDEN_LAYER:
        print()
        print()
        print('N_NEURON_HIDDEN_LAYER: {}'.format(n_neuron_hidden_layer))
        weights, miu, covariances = train_rbf_model(
            X_model_train,
            Y_model_train,
            n_neuron_hidden_layer=n_neuron_hidden_layer
        )

        validation_predictions = predict_house_price(X_model_validation, weights, miu, covariances)
        validation_mse = calculate_mse(validation_predictions, Y_model_validation)
        print('Validation MSE: {}'.format(validation_mse))

        training_predictions = predict_house_price(X_model_train, weights, miu, covariances)
        training_mse = calculate_mse(training_predictions, Y_model_train)
        print('Training MSE: {}'.format(training_mse))

        result = {
            'n_neuron_hidden_layer': n_neuron_hidden_layer,
            'validation_predictions': validation_predictions,
            'validation_mse': validation_mse,
            'training_predictions': training_predictions,
            'training_mse': training_mse,
            'weights': weights,
            'miu': miu,
            'covariances': covariances
        }

        final_result.append(result)

    f = open('results/rbf_result_cubic_{}.p'.format(N_NEURON_HIDDEN_LAYER),'wb')
    pickle.dump(final_result, f)
    f.close()


import numpy as np
import random
import ray

class SumTree(object):

  def __init__(self, max_capacity, capacity_step):
    self.tree = np.zeros(2 * max_capacity - 1)
    self.buffer = np.zeros(max_capacity, dtype=object)
    self.max_capacity = max_capacity
    self.capacity_step = capacity_step
    self.capacity = capacity_step

    self.prev_capacity = 0
    self.num_memories = 0
    self.position = 0

  def add(self, priorities, history):
    for step, priority in enumerate(priorities):
      idx = self.position + self.max_capacity - 1
      self.buffer[self.position] = (step, history)
      self.update(idx, priority)

      if self.position >= self.prev_capacity:
        self.num_memories += 1

      self.position = (self.position + 1) % self.capacity

      if self.position == 0:
        self.prev_capacity = self.capacity
        self.capacity = min(self.max_capacity, (self.capacity + self.capacity_step))

  def update(self, idx, priority):
    change = priority - self.tree[idx]
    self.tree[idx] = priority

    while idx != 0:
      idx = (idx - 1) // 2
      self.tree[idx] += change

  def get_leaf(self, value):
    parent_index = 0

    while True:
      left_child_index = 2 * parent_index + 1
      right_child_index = left_child_index + 1

      if left_child_index >= len(self.tree):
        leaf_index = parent_index
        break
      else:
        if value <= self.tree[left_child_index]:
          parent_index = left_child_index
        else:
          value -= self.tree[left_child_index]
          parent_index = right_child_index

    buffer_index = leaf_index - self.max_capacity + 1
    step, history = self.buffer[buffer_index]

    return leaf_index, self.tree[leaf_index], step, history

  @property
  def total_priority(self):
    return self.tree[0]

@ray.remote
class PrioritizedReplay():

  def __init__(self, config):
    self.batch_size = config.batch_size
    self.beta_increment_per_sampling = config.beta_increment_per_sampling
    self.epsilon = config.epsilon
    self.alpha = config.alpha
    self.beta = config.beta
    self.stored_before_train = config.stored_before_train

    self.num_unroll_steps = config.num_unroll_steps
    self.td_steps = config.td_steps
    self.discount = config.discount
    n_steps = self.num_unroll_steps + self.td_steps
    self.discounts = np.array([self.discount**n for n in range(n_steps)], dtype=np.float32)

    self.action_space = config.action_space
    self.obs_space = config.obs_space
    self.target_length = config.num_unroll_steps + 1

    self.absorbing_policy = np.zeros(self.action_space, dtype=np.float32)

    capacity = config.window_size
    if config.window_step is not None:
      capacity_step = config.window_step
    else:
      capacity_step = config.window_size

    self.tree = SumTree(capacity, capacity_step)

    self.throughput = {'frames': 0, 'games': 0}

    if config.seed is not None:
      np.random.seed(config.seed)
      random.seed(config.seed+1)

  def add_initial_throughput(self, frames, games):
    self.throughput['frames'] += frames
    self.throughput['games'] += games

  def get_priorities(self, errors):
    return np.power((np.abs(errors) + self.epsilon), self.alpha)

  def save_history(self, history, ignore=None, terminal=False):
    if ignore is not None:
      errors = history.errors[:-ignore]
      priorities = self.get_priorities(errors) if errors else []
    else:
      priorities = self.get_priorities(history.errors)
    self.tree.add(priorities, history)

    self.throughput['frames'] += len(priorities)
    if terminal: self.throughput['games'] += 1

  def sample_batch(self):
    priorities = []
    batch_actions = []
    idxs = []

    batch_observations = np.zeros((self.batch_size, *self.obs_space), dtype=np.float32)
    target_policies = np.zeros((self.batch_size, self.target_length, self.action_space), dtype=np.float32)
    target_rewards = np.zeros((self.batch_size, self.target_length), dtype=np.float32)
    target_values = np.zeros((self.batch_size, self.target_length), dtype=np.float32)

    if self.beta < 1:
      self.beta = np.min([1., self.beta + self.beta_increment_per_sampling])

    priority_segment = self.tree.total_priority / self.batch_size
    for batch_idx in range(self.batch_size):
      s1, s2 = priority_segment * batch_idx, priority_segment * (batch_idx + 1)
      value = random.uniform(s1, s2)

      idx, priority, step, history = self.tree.get_leaf(value)

      priorities.append(priority)
      idxs.append(idx)

      batch_observations[batch_idx, :] = np.float32(history.observations[step])

      actions = history.actions[step:step + self.num_unroll_steps]
      while len(actions) < self.num_unroll_steps:
        actions.append(np.random.randint(self.action_space))
      batch_actions.append(actions)

      self.insert_target(batch_idx, history, step, target_rewards,
                                                   target_values,
                                                   target_policies)

    batch = (batch_observations, batch_actions, (target_rewards, target_values, target_policies))

    sampling_probabilities = priorities / self.tree.total_priority
    is_weights = np.power(self.tree.num_memories*sampling_probabilities, -self.beta)
    is_weights /= is_weights.max()
    return batch, idxs, is_weights

  def insert_target(self, batch_idx, history, step, target_rewards,
                                                    target_values,
                                                    target_policies):
    end_index = len(history.root_values)
    for idx, current_index in enumerate(range(step, step + self.num_unroll_steps + 1)):

      if current_index > 0 and current_index <= len(history.rewards):
        last_reward = history.rewards[current_index - 1]
      else:
        last_reward = 0

      if current_index < end_index:
        to_play = history.to_play[current_index]

        bootstrap_index = current_index + self.td_steps
        if bootstrap_index < end_index:
          value = history.root_values[bootstrap_index] * self.discount**self.td_steps
        else:
          value = 0

        rewards = history.rewards[current_index:bootstrap_index]
        if rewards:
          not_to_play = np.array(history.to_play[current_index:bootstrap_index]) != to_play
          rewards = np.array(rewards, dtype=np.float32)
          rewards[not_to_play] *= -1
          value += np.dot(rewards, self.discounts[:len(rewards)])

        target_policies[batch_idx, idx, :] = history.child_visits[current_index]
        target_rewards[batch_idx, idx] = last_reward
        target_values[batch_idx, idx] = value
      else:
        target_policies[batch_idx, idx, :] = self.absorbing_policy
        target_rewards[batch_idx, idx] = last_reward
        target_values[batch_idx, idx] = 0

  def update(self, idxs, errors):
    priorities = self.get_priorities(errors)
    for idx, priority in zip(idxs, priorities):
      self.tree.update(idx, priority)

  def size(self):
    return self.tree.num_memories

  def get_throughput(self):
    return self.throughput