import pytest

import numpy as np
from xarray import DataArray

from analysis.analyse import check_temperature, reactor_survived

class TestMeltingTemperature:
    def test_melted(self):
        melted = check_temperature(3500)
        assert melted

    def test_not_melted(self):
        melted = check_temperature(1500)
        assert not melted

    def test_unphysical_temperature(self):
        with pytest.raises(ValueError):
            check_temperature(-100)

@pytest.fixture()
def example_temp_data():
    temp = DataArray(
        name='T',
        data=np.linspace(250, 3500, 10), dims=['time'],
        coords={'time': ('time', np.linspace(1, 10, 10))}
    )
    return temp

def test_reactor_survival(example_temp_data):
    survived = reactor_survived(example_temp_data)
    assert not survived


from django.test import TestCase
from django.utils import timezone

from .models import (
        Collector, Agent, Project, Job,
        InstalledJob, RequiredJobArgument,
        OptionalJobArgument, JobInstance,
)
from .base_models import ValuesType, OpenbachFunctionParameter

class ProjectCheckerMixin:
    def assertProjectCompliant(self, expected, actual):
        self.assertEqual(expected['name'], actual['name'])
        self.assertEqual(expected.get('description', ''), actual['description'])
        self.assertCountEqual(expected.get('owners', []), actual['owners'])

        for entity in expected.get('entity', []):
            for actual_entity in actual['entity']:
                if entity['name'] == actual_entity['name']:
                    self.assertEqual(entity['description'], actual_entity['description'])
                    break
            else:
                self.fail('Found unknown entity in actual project: {}'.format(entity))

        for scenario in expected.get('scenario', []):
            for actual_scenario in actual['scenario']:
                if scenario['name'] == actual_scenario['name']:
                    self.assertScenarioCompliant(scenario, actual_scenario)
                    break
            else:
                self.fail('Found unknown scenario in actual project: {}'.format(scenario))

    def assertScenarioCompliant(self, expected, actual):
        self.assertDictEqual(expected.get('arguments', {}), actual['arguments'])
        self.assertDictEqual(expected.get('constants', {}), actual['constants'])
        self.assertEqual(expected.get('description', ''), actual['description'])
        self.assertEqual(expected['name'], actual['name'])

        expected_openbach_functions = expected.get('openbach_functions', [])
        actual_openbach_functions = actual['openbach_functions']
        self.assertEqual(len(expected_openbach_functions), len(actual_openbach_functions))

        for expected, actual in zip(expected_openbach_functions, actual_openbach_functions):
            expected = expected.copy()
            expected_id = expected.pop('id')
            self.assertEqual(expected_id, actual['id'])
            self.assertEqual(expected.pop('label', str(expected_id)), actual['label'])

            expected_wait = expected.pop('wait', {})
            actual_wait = actual.get('wait', {})
            self.assertEqual(
                    expected_wait.get('time', 0),
                    actual_wait.get('time', 0))
            self.assertCountEqual(
                    expected_wait.get('launched_ids', []),
                    actual_wait.get('launched_ids', []))
            self.assertCountEqual(
                    expected_wait.get('finished_ids', []),
                    actual_wait.get('finished_ids', []))

            openbach_function, = expected
            expected_function = expected[openbach_function]
            actual_function = expected[openbach_function]
            self.assertCountEqual(expected_function, actual_function)

class OpenbachFunctionArgumentTestCase(TestCase):
    def _check_conversions(self, field, argument, value, serialized):
        self.assertEqual(field.to_python(argument), value)
        self.assertEqual(field.get_prep_value(argument), serialized)

    def _check_interpolation(self, field, argument, expected, **parameters):
        actual = field.validate_openbach_value(argument, parameters)
        self.assertEqual(actual, expected)

    def _prepare_fields(self, *types, **named_types):
        for type_ in types:
            yield OpenbachFunctionParameter(type=type_)

        for name, expected in named_types.items():
            field = OpenbachFunctionParameter.from_type(name)
            self.assertEqual(field.type, expected)
            yield field

    def test_arguments_int(self):
        expected = {ValuesType.INTEGER.value: int}
        for field in self._prepare_fields(int, **expected):
            self._check_conversions(field, 3, 3, '3')
            self._check_conversions(field, '3', 3, '3')
            self._check_conversions(field, 3.0, 3, '3')
            self._check_interpolation(field, '$test', 3, test=3)
            self._check_interpolation(field, '$test', 3, test='3')

    def test_arguments_int_list(self):
        expected = {
                ValuesType.JOB_INSTANCE_ID.value: [int],
                ValuesType.SCENARIO_INSTANCE_ID.value: [int],
        }
        for field in self._prepare_fields([int], **expected):
            self._check_conversions(field, [1, 2, 3], [1, 2, 3], '1 2 3')
            self._check_conversions(field, '1 2 3', [1, 2, 3], '1 2 3')
            self._check_conversions(field, '12 3', [12, 3], '12 3')
            self._check_conversions(field, '123', [123], '123')
            self._check_conversions(field, 123, [123], '123')
            self._check_interpolation(field, '1 $test 3', [1, 2, 3], test=2)
            self._check_interpolation(field, '1 $test 3', [1, 2, 3], test='2')
            self._check_interpolation(field, ['1', '$test', 3], [1, 2, 3], test=2)

    def test_arguments_none(self):
        expected = {ValuesType.NONE_TYPE.value: type(None)}
        for field in self._prepare_fields(type(None), **expected):
            self._check_conversions(field, None, None, None)
            self._check_conversions(field, '', True, 'True')
            self._check_conversions(field, 'None', True, 'True')
            self._check_conversions(field, 'True', True, 'True')
            self._check_conversions(field, True, True, 'True')
            self._check_conversions(field, False, True, 'True')
            self._check_conversions(field, 'False', True, 'True')
            self._check_interpolation(field, '$test', True, test=3)

class JobTestCase(TestCase):
    def setUp(self):
        Job.objects.create(name='test_job')

    def test_arguments_create(self):
        job = Job.objects.get(name='test_job')
        arg = RequiredJobArgument.objects.create(
                name='first', type='int',
                subcommand=job.subcommands.get(name=None),
                count='1', rank=0)
        self.assertTrue(arg.check_count(1))
        self.assertFalse(arg.check_count(0))
        for i in range(2, 10):
            self.assertFalse(arg.check_count(i))

        arg = RequiredJobArgument.objects.create(
                name='second', type='str',
                subcommand=job.subcommands.get(name=None),
                count='3-6', rank=1)
        for i in range(3, 7):
            self.assertTrue(arg.check_count(i))
        for i in range(3):
            self.assertFalse(arg.check_count(i))
        for i in range(7, 10):
            self.assertFalse(arg.check_count(i))

        RequiredJobArgument.objects.create(
                name='third', type='ip',
                subcommand=job.subcommands.get(name=None),
                count='+', rank=2)

        arg = OptionalJobArgument.objects.create(
                name='optional', type='bool',
                subcommand=job.subcommands.get(name=None),
                count='*', flag='-o')
        for i in range(10):
            self.assertTrue(arg.check_count(i))

        OptionalJobArgument.objects.create(
                name='flags', type='None',
                subcommand=job.subcommands.get(name=None),
                count='0', flag='-f')

        OptionalJobArgument.objects.create(
                name='star', type='int',
                subcommand=job.subcommands.get(name=None),
                count='*', flag='-s')

        default_subcommand = job.subcommands.get(name=None)
        self.assertEqual(default_subcommand.required_arguments.count(), 3)
        self.assertEqual(default_subcommand.optional_arguments.count(), 3)

class InstalledJobTestCase(TestCase):
    def setUp(self):
        Job.objects.create(name='test_job')
        collector = Collector.objects.create(address='172.20.34.45')
        Agent.objects.create(
                address='172.20.34.45', name='Openbach_Agent',
                reachable=True, collector=collector)

    def test_install_job(self):
        agent = Agent.objects.get(name='Openbach_Agent')
        job = Job.objects.get(name='test_job')
        installed_job = InstalledJob.objects.create(
                agent=agent, job=job,
                severity=1, local_severity=1)
        self.assertEqual(installed_job.job, job)
        self.assertEqual(installed_job.agent, agent)

class JobInstanceTestCase(TestCase):
    def setUp(self):
        job = Job.objects.create(name='test_job')
        collector = Collector.objects.create(address='172.20.34.45')
        agent = Agent.objects.create(
                address='172.20.34.45', name='Openbach_Agent',
                reachable=True, collector=collector)
        InstalledJob.objects.create(
                agent=agent, job=job,
                severity=1, local_severity=1)
        RequiredJobArgument.objects.create(
                name='first', type='int',
                subcommand=job.subcommands.get(name=None),
                count='1', rank=0)
        RequiredJobArgument.objects.create(
                name='second', type='str',
                subcommand=job.subcommands.get(name=None),
                count='3-6', rank=1)
        OptionalJobArgument.objects.create(
                name='optional', type='ip',
                subcommand=job.subcommands.get(name=None),
                count='*', flag='-o')
        OptionalJobArgument.objects.create(
                name='flags', type='None',
                subcommand=job.subcommands.get(name=None),
                count='0', flag='-f')
        OptionalJobArgument.objects.create(
                name='star', type='int',
                subcommand=job.subcommands.get(name=None),
                count='*', flag='-s')

    def test_job_instance(self):
        now = timezone.now()
        installed_job = InstalledJob.objects.all()[0]
        job_instance = JobInstance.objects.create(
                job_name=installed_job.job.name,
                agent_name=installed_job.agent.name,
                agent=installed_job.agent,
                collector=installed_job.agent.collector,
                update_status=now,
                start_date=now,
                periodic=False)

        job_instance.configure({
            'first': 42,
            'second': ['riri', 'fifi', 'loulou'],
            'optional': ['8.8.8.8', '127.0.0.1', '192.168.0.1'],
        })
        job_instance.save()

class ProjectTestCase(ProjectCheckerMixin, TestCase):
    def setUp(self):
        self.project_json = {
                "name": "OpenSAND",
                "description": "OpenSAND Plateform",
                "entity": [{
                    "name": "Sat",
                    "description": "The satellite",
                    "agent": {
                        "name": "openbach-controller",
                        "address": "172.20.34.39",
                        "username": "opensand",
                        "collector": "172.20.34.39",
                    },
                    "networks": ["emu"],
                }, {
                    "name": "gw",
                    "description": "Gateway",
                    "agent": None,
                    "networks": ["emu", "lan_gw"],
                }, {
                    "name": "st1",
                    "description": "Satellite terminal 1",
                    "agent": None,
                    "networks": ["emu", "lan_st"],
                }, {
                    "name": "st2",
                    "description": "Satellite terminal 2",
                    "agent": None,
                    "networks": ["emu"],
                }, {
                    "name": "ws1",
                    "description": "Workstation 1",
                    "agent": None,
                    "networks": ["lan_st"],
                }, {
                    "name": "ws2",
                    "description": "Workstation 2",
                    "agent": None,
                    "networks": ["lan_st"],
                }, {
                    "name": "ws3",
                    "description": "Workstation 3",
                    "agent": None,
                    "networks": ["lan_gw"],
                }, {
                    "name": "ws4",
                    "description": "Workstation 4",
                    "agent": None,
                    "networks": []
                }],
                "network": ["emu", "lan_gw", "lan_st"],
                "scenario": [{
                    "name": "Ping between machines",
                    "description": "First scenario (for test)",
                    "arguments": {},
                    "constants": {},
                    "openbach_functions": [{
                        "id": 1,
                        "start_job_instance": {
                            "entity_name": "Sat",
                            "fping": {
                                "destination_ip": "172.20.0.83",
                            },
                            "offset": 5,
                        },
                        "wait": {
                            "time": 5,
                        },
                    }, {
                        "id": 2,
                        "start_job_instance": {
                            "entity_name": "Sat",
                            "hping": {
                                "destination_ip": "172.20.0.83",
                            },
                            "offset": 5,
                        },
                        "wait": {
                            "time": 5,
                        },
                    }],
                }, {
                    "name": "Congestion tests",
                    "description": "2 Iperf servs queried by 2 iperf clients",
                    "arguments": {},
                    "constants": {},
                    "openbach_functions": [{
                        "id": 1,
                        "start_job_instance": {
                            "entity_name": "Sat",
                            "pep": {
                                "sat_network": "opensand",
                                "pep_port": 3000,
                            },
                            "offset": 0,
                        },
                    }, {
                        "id": 2,
                        "start_job_instance": {
                            "entity_name": "gw",
                            "iperf": {
                                "mode": "-s",
                                "udp": True,
                                "port": 5000,
                            },
                            "offset": 0,
                        },
                    }, {
                        "id": 3,
                        "start_job_instance": {
                            "entity_name": "gw",
                            "iperf": {
                                "mode": "-s",
                                "port": 5001,
                            },
                            "offset": 0,
                        },
                    }, {
                        "id": 4,
                        "start_job_instance": {
                            "entity_name": "gw",
                            "tcpprobe_monitoring": {
                                "port": 5001,
                                "interval": 10,
                                "path": "/tcp/tcpprobe.out",
                            },
                            "offset": 0,
                        },
                    }, {
                        "id": 5,
                        "start_job_instance": {
                            "entity_name": "gw",
                            "rate_monitoring": {
                                "interval": 1,
                                "chain": "-A INPUT",
                                "jump": "ACCEPT",
                                "in_interface": "eth0",
                                "protocol": "tcp",
                                "source_port": 5001,
                            },
                            "offset": 0,
                        },
                    }, {
                        "id": 6,
                        "start_job_instance": {
                            "entity_name": "ws1",
                            "iperf": {
                                "mode": "-c 192.168.0.7",
                                "udp": True,
                                "port": 5000,
                            },
                            "offset": 0,
                        },
                        "wait": {
                            "time": 5,
                            "launched_ids": [1, 2, 3, 4, 5],
                        },
                    }, {
                        "id": 7,
                        "start_job_instance": {
                            "entity_name": "ws1",
                            "iperf": {
                                "mode": "-c 192.168.0.7",
                                "port": 5001,
                            },
                            "offset": 0,
                        },
                        "wait": {
                            "time": 5,
                            "launched_ids": [1, 2, 3, 4, 5],
                        },
                    }, {
                        "id": 8,
                        "stop_job_instances": {
                            "openbach_function_ids": [5, 6],
                        },
                        "wait": {
                            "time": 500,
                            "launched_ids": [6, 7],
                        },
                    }, {
                        "id": 9,
                        "stop_job_instances": {
                            "openbach_function_ids": [1, 2, 3, 4, 5],
                        },
                        "wait": {
                            "launched_ids": [8],
                        },
                    }],
                }],
        }

    def _create_jobs(self):
        Job.objects.create(name='fping')
        Job.objects.create(name='hping')

    def _create_arguments(self):
        for job in Job.objects.filter(name__iendswith='ping'):
            RequiredJobArgument.objects.create(
                    rank=0, name='destination_ip',
                    subcommand=job.subcommands.get(name=None),
                    type='ip', count='1')

    def _create_scenario_prerequisites(self):
        # Scenario 1
        self._create_jobs()
        self._create_arguments()

        # Scenario 2
        pep = Job.objects.create(name='pep')
        iperf = Job.objects.create(name='iperf')
        tcp = Job.objects.create(name='tcpprobe_monitoring')
        rate = Job.objects.create(name='rate_monitoring')

        RequiredJobArgument.objects.create(
                subcommand=pep.subcommands.get(name=None),
                rank=0, name='sat_network',
                type='str', count='1')
        OptionalJobArgument.objects.create(
                subcommand=pep.subcommands.get(name=None),
                flag='-p', name='pep_port',
                type='int', count='1')

        OptionalJobArgument.objects.create(
                subcommand=iperf.subcommands.get(name=None),
                flag='-s', name='mode',
                type='None', count='0')
        OptionalJobArgument.objects.create(
                subcommand=iperf.subcommands.get(name=None),
                flag='-u', name='udp',
                type='None', count='0')
        OptionalJobArgument.objects.create(
                subcommand=iperf.subcommands.get(name=None),
                flag='-p', name='port',
                type='int', count='1')

        RequiredJobArgument.objects.create(
                subcommand=tcp.subcommands.get(name=None),
                rank=0, name='port',
                type='int', count='1')
        OptionalJobArgument.objects.create(
                subcommand=tcp.subcommands.get(name=None),
                flag='-i', name='interval',
                type='int', count='1')
        OptionalJobArgument.objects.create(
                subcommand=tcp.subcommands.get(name=None),
                flag='-p', name='path',
                type='str', count='1')

        RequiredJobArgument.objects.create(
                subcommand=rate.subcommands.get(name=None),
                rank=0, name='interval',
                type='int', count='1')
        RequiredJobArgument.objects.create(
                subcommand=rate.subcommands.get(name=None),
                rank=1, name='chain',
                type='str', count='1')
        OptionalJobArgument.objects.create(
                subcommand=rate.subcommands.get(name=None),
                flag='-j', name='jump',
                type='str', count='1')
        OptionalJobArgument.objects.create(
                subcommand=rate.subcommands.get(name=None),
                flag='-i', name='in_interface',
                type='str', count='1')
        OptionalJobArgument.objects.create(
                subcommand=rate.subcommands.get(name=None),
                flag='-p', name='protocol',
                type='str', count='1')
        OptionalJobArgument.objects.create(
                subcommand=rate.subcommands.get(name=None),
                flag='--sport', name='source_port',
                type='int', count='1')

    def _test_project_fails_and_get_context_manager(self):
        name = self.project_json['name']
        description = self.project_json['description']
        project = Project.objects.create(name=name, description=description)
        with self.assertRaises(Project.MalformedError) as cm:
            project.load_from_json(self.project_json)
        return cm

    def test_scenario_fail_with_no_job(self):
        cm = self._test_project_fails_and_get_context_manager()
        self.assertEqual(
                cm.exception.error['section'],
                'scenario.Ping between machines')
        self.assertEqual(
                cm.exception.error['message']['error'],
                'No such job in the database')
        self.assertEqual(
                cm.exception.error['message']['offending_entry'],
                'openbach_functions.0.start_job_instance.fping')

    def test_scenario_fail_with_no_job_argument(self):
        self._create_jobs()
        cm = self._test_project_fails_and_get_context_manager()
        self.assertEqual(
                cm.exception.error['section'],
                'scenario.Ping between machines')
        self.assertEqual(
                cm.exception.error['message']['error'],
                'The configured job does not accept the given argument')
        self.assertEqual(
                cm.exception.error['message']['offending_entry'],
                'openbach_functions.0.start_job_instance.fping.destination_ip')

    def test_scenario_succed_with_job_arguments_but_second_fail(self):
        self._create_jobs()
        self._create_arguments()
        cm = self._test_project_fails_and_get_context_manager()
        self.assertEqual(
                cm.exception.error['section'],
                'scenario.Congestion tests')
        self.assertEqual(
                cm.exception.error['message']['error'],
                'No such job in the database')
        self.assertEqual(
                cm.exception.error['message']['offending_entry'],
                'openbach_functions.0.start_job_instance.pep')

    def test_success(self):
        self._create_scenario_prerequisites()
        name = self.project_json['name']
        description = self.project_json['description']
        project = Project.objects.create(name=name, description=description)
        project.load_from_json(self.project_json)
        self.assertProjectCompliant(self.project_json, project.json)


from utils import data_transform
import metrics
import numpy as np

class LinearRegression:

	def __init__(self, solve_by='ols', regularized=False):
		self.solve_by = solve_by
		self.regularized = regularized

	def train(self, features, labels):
		X = data_transform.insert_ones(features)
		y = labels

		if self.solve_by == 'ols':
			self.weights = self.__ols(X, y)

		elif self.solve_by == 'gdesc':
			self.weights = self.__gradient_descent(X, y)

	def __ols(self, X, y):
		return np.matmul(np.matmul(np.linalg.pinv(np.matmul(X.T, X)), X.T), y)

	def __gradient_descent(self, X, y, learning_rate=10e-2, num_iter=1000, stopping_threshold=10e-4):
		# initial weights
		n = X.shape[0]
		weights = np.zeros(X.shape[1])

		for _iter in range(num_iter):
			gradients = (1 / n) * np.matmul(X.T, np.dot(X, weights) - y)	# vectorized gradient calculation

			old_weights = weights
			weights = weights - learning_rate * gradients 	# gradient descent
			weight_difference = np.linalg.norm(weights - old_weights)

			if weight_difference <= stopping_threshold:		# break early if we're doing well
				break

		return weights

	def predict(self, features):
		X = data_transform.insert_ones(features)
		predictions = np.matmul(X, self.weights)
		return predictions

	def compare_sklearn(self, training_features, training_labels, test_features, test_labels, my_mse, my_r2):

		# do linear regression with sklearn
		from sklearn.linear_model import LinearRegression
		skmodel = LinearRegression()
		skmodel.fit(training_features, training_labels)
		skpreds = skmodel.predict(test_features)

		# get sklearn mse and r2 score
		skmse = metrics.mean_squared_error(test_labels, skpreds)
		skr2 = metrics.r2_score(test_labels, skpreds)

		print("Your model's MSE: {}\nsklearn's MSE: {}".format(my_mse, skmse))
		print()
		print("Your model's R2 score: {}\nsklearn's R2 score: {}".format(my_r2, skr2))


#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2020/5/27 19:57
Desc: 新浪财经-股票-机构持股
http://vip.stock.finance.sina.com.cn/q/go.php/vComStockHold/kind/jgcg/index.phtml
"""
from mssdk.utils import demjson
import pandas as pd
import requests

def stock_institute_hold(quarter: str = "20051") -> pd.DataFrame:
    """
    新浪财经-股票-机构持股一览表
    http://vip.stock.finance.sina.com.cn/q/go.php/vComStockHold/kind/jgcg/index.phtml
    :param quarter: 从 2005 年开始, {"一季报":1, "中报":2 "三季报":3 "年报":4}, e.g., "20191", 其中的 1 表示一季报; "20193", 其中的 3 表示三季报;
    :type quarter: str
    :return: 机构持股一览表
    :rtype: pandas.DataFrame
    """
    url = "http://vip.stock.finance.sina.com.cn/q/go.php/vComStockHold/kind/jgcg/index.phtml?symbol=%D6%A4%C8%AF%BC%F2%B3%C6%BB%F2%B4%FA%C2%EB"
    params = {
        "p": "1",
        "num": "5000",
        "reportdate": quarter[:-1],
        "quarter": quarter[-1],
    }
    r = requests.get(url, params=params)
    temp_df = pd.read_html(r.text)[0]
    temp_df["证券代码"] = temp_df["证券代码"].astype(str).str.zfill(6)
    del temp_df["明细"]
    temp_df.columns = ['证券代码', '证券简称', '机构数', '机构数变化', '持股比例', '持股比例增幅', '占流通股比例', '占流通股比例增幅']
    return temp_df

def stock_institute_hold_detail(stock: str = "600433", quarter: str = "20201") -> pd.DataFrame:
    """
    新浪财经-股票-机构持股详情
    http://vip.stock.finance.sina.com.cn/q/go.php/vComStockHold/kind/jgcg/index.phtml
    :param stock: 股票代码
    :type stock: str
    :param quarter: 从 2005 年开始, {"一季报":1, "中报":2 "三季报":3 "年报":4}, e.g., "20191", 其中的 1 表示一季报; "20193", 其中的 3 表示三季报;
    :type quarter: str
    :return: 指定股票和财报时间的机构持股数据
    :rtype: pandas.DataFrame
    """
    url = "http://vip.stock.finance.sina.com.cn/q/api/jsonp.php/var%20details=/ComStockHoldService.getJGCGDetail"
    params = {
        "symbol": stock,
        "quarter": quarter,
        }
    r = requests.get(url, params=params)
    text_data = r.text
    json_data = demjson.decode(text_data[text_data.find("{"):-2])
    big_df = pd.DataFrame()
    for item in json_data["data"].keys():
        inner_temp_df = pd.DataFrame(json_data["data"][item]).T.iloc[:-1, :]
        inner_temp_df.reset_index(inplace=True)
        big_df = big_df.append(inner_temp_df, ignore_index=True)
    if not big_df.empty:
        big_df["index"] = big_df["index"].str.split("_", expand=True)[0]
        big_df.rename(columns={"index": "institute"}, inplace=True)
        big_df = big_df.iloc[:, :12]
        big_df.columns = ["持股机构类型",
                          "持股机构代码",
                          "持股机构简称",
                          "持股机构全称",
                          "持股数",
                          "最新持股数",
                          "持股比例",
                          "最新持股比例",
                          "占流通股比例",
                          "最新占流通股比例",
                          "持股比例增幅",
                          "占流通股比例增幅",
                          ]
        big_df["持股机构类型"] = big_df["持股机构类型"].str.replace("fund", "基金")
        big_df["持股机构类型"] = big_df["持股机构类型"].str.replace("socialSecurity", "全国社保")
        big_df["持股机构类型"] = big_df["持股机构类型"].str.replace("qfii", "QFII")
        return big_df
    else:
        return None

if __name__ == '__main__':
    stock_institute_hold_df = stock_institute_hold(quarter="20201")
    print(stock_institute_hold_df)

    stock_institute_hold_detail_df = stock_institute_hold_detail(stock="300003", quarter="20201")
    print(stock_institute_hold_detail_df)


"""
The REST interface for ERP distribution center resources.
"""
import server.services.distribution_centers as distribution_center_service
import server.services.shipments as shipment_service
from flask import g, request, Response, Blueprint
from server.web.utils import logged_in, check_null_input

distribution_centers_v1_blueprint = Blueprint('distribution_centers_v1_api', __name__)

@distribution_centers_v1_blueprint.route('/distribution-centers', methods=['GET'])
@logged_in
def get_distribution_centers():
    """
    Get all distribution center objects.

    :return: [{
        "id": "D2",
        "address": {Address},
        "contact": {Contact}
    }, {...}]

    """

    distribution_centers = distribution_center_service.get_distribution_centers(token=g.auth['loopback_token'])
    return Response(distribution_centers,
                    status=200,
                    mimetype='application/json')

@distribution_centers_v1_blueprint.route('/distribution-centers/<string:dc_id>', methods=['GET'])
@logged_in
def get_distribution_center(dc_id):
    """
    Retrieve a single distribution center object.

    :param dc_id:   The distribution center's id

    :return: {
        "id": "D2",
        "address": {Address},
        "contact": {Contact}
    }

    """
    check_null_input((dc_id, 'distribution center to retrieve'))

    distribution_center = distribution_center_service.get_distribution_center(token=g.auth['loopback_token'],
                                                                              dc_id=dc_id)
    return Response(distribution_center,
                    status=200,
                    mimetype='application/json')

@distribution_centers_v1_blueprint.route('/distribution-centers/<string:dc_id>/shipments', methods=['GET'])
@logged_in
def get_distribution_centers_shipments(dc_id):
    """
    Retrieve all shipments originating from the specified distribution center.

    :param dc_id:   The distribution center's id

    :return: [{
        "id": "123",
        "status": "SHIPPED",
        "createdAt": "2015-11-05T22:00:51.692765",
        "updatedAt": "2015-11-08T22:00:51.692765",
        "deliveredAt": "2015-11-08T22:00:51.692765",
        "estimatedTimeOfArrival": "2015-11-07T22:00:51.692765",
        "currentLocation": {Address},
        "fromId": "123",
        "toId:": "123"
    }, {...}]

    """
    check_null_input((dc_id, 'distribution center whose shipments you want to retrieve'))
    status = request.args.get('status')

    shipments = shipment_service.get_shipments(token=g.auth['loopback_token'],
                                               dc_id=dc_id,
                                               status=status)
    return Response(shipments,
                    status=200,
                    mimetype='application/json')

@distribution_centers_v1_blueprint.route('/distribution-centers/<string:dc_id>/inventory', methods=['GET'])
@logged_in
def get_distribution_center_inventory(dc_id):
    """
    Retrieve all inventory at the specified distribution center.

    :param dc_id:   The distribution center's id

    :return: [{
        "id": "123",
        "quantity": 10,
        "productId": "123",
        "locationId": "123",
        "locationType": "DistributionCenter"
    }, {...}]
    """
    check_null_input((dc_id, 'distribution center whose inventory you want to retrieve'))

    inventory = distribution_center_service.get_distribution_center_inventory(token=g.auth['loopback_token'],
                                                                              dc_id=dc_id)
    return Response(inventory,
                    status=200,
                    mimetype='application/json')


from Bio.Blast import NCBIWWW
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import generic_dna

# read nucleotides
handle = open("myseqs.fna", "rU")
records = list(SeqIO.parse(handle, "fasta"))
handle.close()

# translate nucleotides to proteins
for i in range(0,len(records)):
    records[i].seq = records[i].seq.translate()

# define output file
save_file = open("biopython_blast.xml", "w")

# blast over the internet
for record in records:
    result_handle = NCBIWWW.qblast("blastp", "swissprot", record, expect=0.00001, format_type='XML', hitlist_size=5)
    save_file.write(result_handle.read())
    result_handle.close()

save_file.close()


import matplotlib.pyplot as pit
import matplotlib.animation as animation
import threading
import numpy as np
import serial

#from mpl_toolkits.mplot3d import Axes3D

serialData = []
serialData.append([0.0])
serialData.append([0.0])

def optenerdatos(out_dato):
    with serial.Serial("COM3", 9600) as arduino:
        while True:
            val = arduino.readline().decode('ascii')
            try:
                out_dato[1].append(float(val))
                if len(out_dato[1]) > 200:
                    out_dato[1].pop(0)
                    print(out_dato[1])
            except:
                pass

dataColector = threading.Thread(target = optenerdatos, args = (serialData,))
dataColector.start()

def actualizar(num, hl, data):
    dx = np.array(range(len(data[1])))
    dy = np.array(data[1])
    hl.set_data(dx, dy)
    return hl,

fig = pit.figure()
hl, = pit.plot(serialData[0], serialData[1])
pit.ylim(-7, 7)
pit.xlim(0, 200)

line_ani = animation.FuncAnimation(fig, actualizar, fargs=(hl, serialData), interval = 50, blit= False)

pit.show()
dataColector.join()


from fastapi import status
from fastapi.testclient import TestClient

from fastapi_demo import app

client = TestClient(app)

def test_delete(existing_sauce):
    resp = client.delete(f"/sauces/{existing_sauce.id}")
    assert resp.status_code == status.HTTP_204_NO_CONTENT
    assert client.get(f"/sauces/{existing_sauce.id}").status_code == status.HTTP_404_NOT_FOUND

def test_get_all(existing_sauce):
    resp = client.get("/sauces/")
    assert resp.status_code == status.HTTP_200_OK
    content = resp.json()
    assert isinstance(content, list)
    assert existing_sauce.data in content

def test_get_one(existing_sauce):
    resp = client.get(f"/sauces/{existing_sauce.id}")
    assert resp.status_code == status.HTTP_200_OK
    content = resp.json()
    assert existing_sauce.data == content

def test_get_nonexistent():
    bad_id = 2000001
    resp = client.get(f"/sauces/{bad_id}")
    assert resp.status_code == status.HTTP_404_NOT_FOUND
    error = resp.json()["detail"]
    assert str(bad_id) in error
    assert "does not exist" in error

def test_create(new_sauce):
    resp = client.post(f"/sauces/{new_sauce.id}", json=new_sauce.data)
    assert resp.status_code == status.HTTP_201_CREATED
    assert new_sauce.data.items() <= resp.json().items()
    assert client.get(f"/sauces/{new_sauce.id}").status_code == status.HTTP_200_OK

def test_create_gen_id(new_sauce):
    resp = client.post("/sauces/", json=new_sauce.data)
    assert resp.status_code == status.HTTP_201_CREATED
    assert new_sauce.data.items() <= resp.json().items()

def test_create_already_exists(existing_sauce, new_sauce):
    resp = client.post(f"/sauces/{existing_sauce.id}", json=new_sauce.data)
    assert resp.status_code == status.HTTP_400_BAD_REQUEST
    error = resp.json()["detail"]
    assert str(existing_sauce.id) in error
    assert "already exists" in error

def test_create_bad_data():
    resp = client.post(
        "/sauces/",
        json={
            "name": "bad",
            "brand": "bad",
            "scoville_scale": "not a number",
        },
    )
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    content = resp.json()["detail"]
    assert content[0]["type"] == "int_parsing"
    assert content[0]["loc"] == ["body", "scoville_scale"]

def test_create_missing_data():
    resp = client.post(
        "/sauces/",
        json={
            "brand": "incomplete",
        },
    )
    assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    content = resp.json()["detail"]
    assert content[0]["type"] == "missing"
    assert content[0]["loc"] == ["body", "name"]

def test_update(existing_sauce_2):
    updates = {
        "name": "updated name",
        "brand": "updated brand",
        "scoville_scale": 12,
        "flavor_notes": ["Bright"],
    }
    resp = client.put(f"/sauces/{existing_sauce_2.id}", json=updates)
    resp.status_code == status.HTTP_200_OK
    assert {**existing_sauce_2.data, **updates}.items() <= resp.json().items()
    assert client.get(f"/sauces/{existing_sauce_2.id}").json() == resp.json()

def test_update_incomplete_data(existing_sauce_2):
    updates = {
        "flavor_notes": ["Bright"],
    }
    resp = client.put(f"/sauces/{existing_sauce_2.id}", json=updates)
    resp.status_code == status.HTTP_200_OK
    assert {**existing_sauce_2.data, **updates}.items() <= resp.json().items()
    assert client.get(f"/sauces/{existing_sauce_2.id}").json() == resp.json()

def test_bad_update(existing_sauce_2):
    updates = {
        "name": "updated name",
        "brand": "updated brand",
        "bottle_size": -3,
    }
    resp = client.put(f"/sauces/{existing_sauce_2.id}", json=updates)
    resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
    content = resp.json()["detail"]
    assert content[0]["type"] == "greater_than"
    assert content[0]["loc"] == ["body", "bottle_size"]

def test_update_does_not_exist():
    nonexistent_id = 789
    updates = {
        "name": "updated name",
        "brand": "updated brand",
        "bottle_size": 10,
    }
    resp = client.put(f"/sauces/{nonexistent_id}", json=updates)
    assert resp.status_code == status.HTTP_404_NOT_FOUND
    content = resp.json()["detail"]
    assert str(nonexistent_id) in content
    assert "does not exist" in content


"""
35. 搜索插入位置
给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
示例 1:
输入: [1,3,5,6], 5
输出: 2

示例 2:
输入: [1,3,5,6], 2
输出: 1

示例 3:
输入: [1,3,5,6], 7
输出: 4

示例 4:
输入: [1,3,5,6], 0
输出: 0
"""
from typing import List
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        ans = 0
        for i in range(len(nums)):
            if target <= nums[i]:
                return i
        return len(nums)

    def searchInsert_2(self, nums: List[int], target: int) -> int:
        # 上面的遍历方法提交之后，时间空间都不好，因为这是有序的，那么我们可以用二分查找啊
        nums_len = len(nums)
        left = 0
        right = nums_len - 1

        if target > nums[-1]:
            return nums_len
        while left < right:
            mid = (left + right) // 2
            if target == nums[mid]:
                return mid
            elif target > nums[mid]:
                left = mid+1
            elif target < nums[mid]:
                right = mid
        return left

solu = Solution()
nums = [1,3,5,6]
target = 0
ans = solu.searchInsert_2(nums, target)
print(ans)


from events import Event
from collections import deque
from packet import LinkStatePacket

class Link(object):
    def __init__(self, event_manager, i, source, dest, rate, delay,
                 buffer_capacity, debug=True):
        self.debug = debug

        self.em = event_manager
        self.i = i
        self.source = source  # Each end is either a router or a host
        self.dest = dest
        self.rate = rate  # bits per second
        self.delay = delay  # seconds

        self.usable = True  # Whether the link accepts new packets
        self.buffer = deque()
        self.buffer_usage = 0
        self.buffer_capacity = buffer_capacity  # bits

        self.interval_usage = 0 #total buffer usage since last linkstate req

    def set_usable(self, usable_status):
        #  can simulate a physical link cut
        self.usable = usable_status

    def on_packet_entry(self, t, p):
        if not self.usable:
            if self.debug:
                print("t={}: Link {} disabled.".format(round(t, 6), self))
            return  # Packet loss.
        if self.buffer_usage + p.size > self.buffer_capacity:
            if self.debug:
                print("t={}: Link {} lost a packet.".format(round(t, 6), self))
            ### Per-link packet loss ###
            self.em.log_it('LINK|{}'.format(self.i), 'T|{}|LOSS|1'.format(t))
            return  # Packet loss.

        self.buffer.append(p)
        self.update_buffer_usage(t, p.size)
        self.interval_usage += p.size

        if self.debug and type(p) is not LinkStatePacket:
            print('t={}, Link {} enqueueing packet into buffer: {}'.
                  format(round(t, 6), self, p.i))

        if len(self.buffer) == 1: # First packet
            self.transmit_next_packet(t)

    def transmit_next_packet(self, t):
        # Only pay the transmission delay here.
        p = self.buffer[0]
        self.em.enqueue(LinkBufferRelease(t + p.size / self.rate, self))

    def on_buffer_release(self, t):
        p = self.buffer.popleft()
        self.update_buffer_usage(t, -p.size)

        self.em.enqueue(LinkExit(t + self.delay, self, p))
        if len(self.buffer) > 0:
            self.transmit_next_packet(t)

    def update_buffer_usage(self, t, amount):
        self.buffer_usage += amount
        ### Per-link buffer occupancy ###
        self.em.log_it('LINK|{}'.format(self.i), 'T|{}|BUFF|{}'.format(t, amount))

    def on_packet_exit(self, t, exiting_packet):
        if self.debug and type(exiting_packet) is not LinkStatePacket:
            print("t={}: Link {} packet exit, details: {}".
                    format(round(t, 6), self, exiting_packet))

        self.dest.on_reception(t, exiting_packet)
        ### Per-link flow rate ###
        self.em.log_it('LINK|{}'.format(self.i), 'T|{}|FLOW|{}'.format(t, exiting_packet.size))

    def __str__(self):
        return "{} ({} -> {})".format(self.i, self.source.i, self.dest.i)

    def __eq__(self, other):
        return self.i == other.i if isinstance(other, Link) else False

    def __hash__(self):
        return hash(self.i)

class LinkBufferRelease(Event):
    # When a packet is released from the link's buffer.
    def __init__(self, t, link):
        super().__init__(t)
        self.link = link

    def run(self):
        self.link.on_buffer_release(self.t)

class LinkExit(Event):
    # When a packet reaches the end of a link.
    def __init__(self, t, link, packet):
        super().__init__(t)
        self.link = link
        self.packet = packet

    def run(self):
        self.link.on_packet_exit(self.t, self.packet)

class LinkSetUsable(Event):
    def __init__(self, t, link, usable):
        super().__init__(t)
        self.link = link
        self.usable = usable

    def run(self):
        self.link.set_usable(self.usable)


import os
from opt import get_opts
import torch
from torch import nn
from collections import defaultdict
from einops import repeat
from torch.utils.data import DataLoader
from datasets import dataset_dict
from torchvision import transforms as T
# models
# from models.nerf import *
from models.nerf_warp import *
from models.rendering_warp import *
# from models.encoder import *
from models.discriminator import discriminator
from random import randint
# from models.vae_components import EncoderBlock,ResNetEncoder
from torch.nn import functional as F
# optimizer, scheduler, visualization
from utils import *
# losses
from losses import loss_dict
# from stage1.unet_components import Enc
from datasets.ray_sample import FlexGridRaySampler,ImgToPatch
# metrics
from metrics import *
# pytorch-lightning
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.loggers import TestTubeLogger

torch.manual_seed(0)

class NeRFSystem(LightningModule):
    def __init__(self, hparams):
        super().__init__()
        self.hparams = hparams
        self.discriminators = []
        self.use_D = hparams.use_D
        self.use_contrastive = hparams.contrastive
        self.valid,self.fake = torch.ones(1, 1),torch.zeros(1,1)
        # self.loss = loss_dict['depth'](coef=1)
        self.loss = loss_dict['nerft'](coef=1).to(self.device)
        if self.use_contrastive:
            self.contrastive_loss = loss_dict['contrastive']()
        self.mutex_loss = loss_dict['mutex']()
        self.x_kernel = torch.Tensor([[1,0,-1],[2,0,-2],[1,0,-1]])[None,None,:,:]
        self.models_to_train = []
        self.enc_out_dim=self.hparams.enc_out_dim
        self.latent_dim=self.hparams.latent_dim
        self.img2patch = ImgToPatch(FlexGridRaySampler(hparams.sample_wh,img_wh=hparams.img_wh,scale_anneal=5e-4), hparams.sample_wh)

        if len(self.use_D) > 0:
            self.discriminator = discriminator()
            self.discriminators += [self.discriminator]

        self.embedding_xyz = PosEmbedding(hparams.N_emb_xyz-1, hparams.N_emb_xyz)
        self.embedding_dir = PosEmbedding(hparams.N_emb_dir-1, hparams.N_emb_dir)
        self.embeddings = {'xyz': self.embedding_xyz,
                           'dir': self.embedding_dir}
        if hparams.encode_t:
            self.embedding_t = torch.nn.Embedding(hparams.N_vocab, hparams.N_tau)
            self.embeddings['t'] = self.embedding_t
            self.models_to_train += [self.embedding_t]

        self.nerf_coarse = NeRFWarp('coarse',
                                in_channels_xyz=6*hparams.N_emb_xyz+3,
                                in_channels_dir=6*hparams.N_emb_dir+3,
                                in_channels_f=self.latent_dim)
        self.models = {'coarse': self.nerf_coarse}
        if hparams.N_importance > 0:
            self.nerf_fine = NeRFWarp('fine',
                                  in_channels_xyz=6*hparams.N_emb_xyz+3,
                                  in_channels_dir=6*hparams.N_emb_dir+3,
                                  encode_appearance=hparams.encode_a,
                                  in_channels_a=hparams.N_a,
                                  encode_transient=hparams.encode_t,
                                  in_channels_t=hparams.N_tau,
                                  beta_min=hparams.beta_min,
                                  in_channels_f=self.latent_dim)
            self.models['fine'] = self.nerf_fine
        self.models_to_train += [self.models]

    def get_progress_bar_dict(self):
        items = super().get_progress_bar_dict()
        items.pop("v_num", None)
        return items

    def forward(self, rays, ts,proj_mat, full_code_B=None,pixels_i=None, **kwargs): # TODO
        """Do batched inference on rays using chunk."""
        B = rays.shape[0]
        results = defaultdict(list)
        for i in range(0, B, self.hparams.chunk):
            rendered_ray_chunks = \
                render_rays(self.models,
                            self.embeddings,
                            rays[i:i+self.hparams.chunk],
                            ts[i:i+self.hparams.chunk],
                            self.hparams.N_samples,
                            self.hparams.use_disp,
                            self.hparams.perturb,
                            self.hparams.noise_std,
                            self.hparams.N_importance,
                            self.hparams.chunk, # chunk size is effective in val mode
                            self.train_dataset.white_back,
                            proj_mat=proj_mat,
                            full_code_B=full_code_B,
                            pixels_i=pixels_i[:,i:i+self.hparams.chunk],
                            **kwargs)

            for k, v in rendered_ray_chunks.items():
                results[k] += [v]

        for k, v in results.items():
            results[k] = torch.cat(v, 0)
        return results

    def setup(self, stage):
        dataset = dataset_dict[self.hparams.dataset_name]
        kwargs = {'root_dir': self.hparams.root_dir}
        kwargs['perturbation'] = self.hparams.data_perturb
        kwargs['img_wh'] = tuple(self.hparams.img_wh)
        kwargs['sample_wh'] = self.hparams.sample_wh
        kwargs['img2patch'] = self.img2patch
        kwargs['use_edge'] = self.hparams.use_edge
        self.train_dataset = dataset(split='train', **kwargs)
        self.val_dataset = dataset(split='val', **kwargs)
        if self.train_dataset.use_edge:
            print("Using Recurring Edge Constraint (REC).")
        # self.images_dataset = dataset_dict['wholeImage']

    def configure_optimizers(self):
        self.optimizer_G = get_optimizer(self.hparams, self.models_to_train)
        scheduler_G = get_scheduler(self.hparams, self.optimizer_G)
        if len(self.use_D):
            hparams = self.hparams
            hparams.lr=1e-4
            self.optimizer_D = get_optimizer(hparams, self.discriminators)
            scheduler_D = get_scheduler(hparams, self.optimizer_D)
            return [self.optimizer_G,self.optimizer_D], [scheduler_G,scheduler_D]
        return [self.optimizer_G],[scheduler_G]

    def train_dataloader(self):
        return DataLoader(self.train_dataset,
                          shuffle=True,
                          num_workers=4,
                          batch_size=self.hparams.batch_size,
                          pin_memory=True)

    def val_dataloader(self):
        return DataLoader(self.val_dataset,
                          shuffle=False,
                          num_workers=4,
                          batch_size=1, # validate one image (H*W rays) at a time
                          pin_memory=True)

    def training_step(self, batch, batch_nb,optimizer_idx=0):
        if self.hparams.use_patch:
            rays, rgbs, ts, original = batch['rays'], batch['rgbs'], batch['ts'], batch['original']
            full_code_B = batch['full_code_B']
            pixels_i = batch['pixels_i'].flatten(1,2)[:,:,None,:]
            # depth_gt = batch['depth_gt'].transpose(-1,-2).view(self.hparams.batch_size,1,self.hparams.sample_wh,self.hparams.sample_wh)
            proj_mat = batch['proj_mat']
            if 'edge' in batch.keys():
                edge = batch['edge'].transpose(-1,-2).view(self.hparams.batch_size,3,self.hparams.sample_wh,self.hparams.sample_wh)
            else:
                edge = None
            rays = rays.squeeze()
            rgbs = rgbs.transpose(-1,-2).view(self.hparams.batch_size,3,self.hparams.sample_wh,self.hparams.sample_wh)
            ts = ts.squeeze()
            original = original.transpose(-1,-2).view(self.hparams.batch_size,3,self.hparams.sample_wh,self.hparams.sample_wh)
        results = self(rays, ts,proj_mat,full_code_B,pixels_i=pixels_i)
        ts=ts.view(self.hparams.batch_size,1,self.hparams.sample_wh,self.hparams.sample_wh)
        # if 'B' in self.use_D or (self.use_contrastive and optimizer_idx == 0):
        for key in results.keys():
            shape = results[key].shape
            C = 1 if len(shape)==1 else shape[1]
            if shape[0] == self.hparams.batch_size*self.hparams.sample_wh ** 2:
                results[key] = results[key].t().view(C,self.hparams.batch_size,self.hparams.sample_wh,self.hparams.sample_wh).transpose(0,1)
        img_pred_static = results['_rgb_fine_static']
        img_pred_transient = results['_rgb_fine_transient']
        if optimizer_idx == 0 or len(self.use_D)==0: # Train generator
            loss_d = self.loss(results, rgbs, BG_edge=edge)
            pred = results['rgb_fine']
            loss = sum(l for l in loss_d.values())
            mut_loss = self.mutex_loss(img_pred_static*(1-results['beta']),img_pred_transient*results['beta'],self.x_kernel.to(self.device))
            loss += mut_loss
            self.log(f'mut/',mut_loss,prog_bar=True)

            for k, v in loss_d.items():
                self.log(f'train/{k}', v, prog_bar=True)
            self.log('train/loss', loss)
            with torch.no_grad():
                psnr_ = psnr(pred, original)
                if self.global_step % 250 == 0:
                    stack = torch.cat([img_pred_static,img_pred_transient*results['beta'],pred, original],0) # (3, 3, H, W)
                    self.logger.experiment.add_images('train/Bpred_Rpred_Mixpred_GT', stack, self.global_step)
            self.log('lr', get_learning_rate(self.optimizer_G))
            self.log('train/psnr', psnr_, prog_bar=True)

        return loss

    def validation_step(self, batch, batch_nb):
        rays, rgbs, ts, original = batch['rays'], batch['rgbs'], batch['ts'], batch['original']
        full_code_B = batch['full_code_B']
        proj_mat = batch['proj_mat']
        rays = rays[0]
        rgbs = rgbs.squeeze() # (H*W, 3)
        ts = ts[0]
        results = defaultdict(list)
        index_w, index_h = torch.meshgrid([torch.linspace(-1,1,self.hparams.img_wh[0]),
                                         torch.linspace(-1,1,self.hparams.img_wh[1])])
        pixels_i = torch.dstack([index_w,index_h]).flatten(0,1)[None,:,None,:].to(self.device)
        results = self(rays, ts, proj_mat, full_code_B,pixels_i=pixels_i, output_transient=False)
        log = dict()
        typ = 'fine' if 'rgb_fine' in results else 'coarse'
        W, H = self.hparams.img_wh
        stack = torch.stack([results[f'rgb_fine'].T.view(3,H,W), original.T.view(3,H,W)]) # (2, 3, H, W)
        self.logger.experiment.add_images('val/pred_GT', stack, self.global_step)

        psnr_ = psnr(results[f'rgb_{typ}'].unsqueeze(0), original)
        log['val_psnr'] = psnr_

        return log

    def validation_epoch_end(self, outputs):
        mean_psnr = torch.stack([x['val_psnr'] for x in outputs]).mean()
        self.log('val/psnr', mean_psnr, prog_bar=True)

def main(hparams):
    system = NeRFSystem(hparams)
    os.makedirs(f'ckpts/{hparams.exp_name}/',exist_ok=True)
    checkpoint_callback = \
        ModelCheckpoint(filepath=os.path.join(f'ckpts/{hparams.exp_name}/',
                                               '{epoch:d}'),
                        monitor='val/psnr',
                        mode='max',
                        save_top_k=1,
                        save_last=True)
        # save_top_k = -1:all models; 0:no model; k: top k models on monitor

    logger = TestTubeLogger(save_dir="logs",
                            name=hparams.exp_name,
                            debug=False,
                            create_git_tag=False,
                            log_graph=False)

    trainer = Trainer(max_epochs=hparams.num_epochs,
                      checkpoint_callback=checkpoint_callback,
                      resume_from_checkpoint=hparams.ckpt_path,
                      logger=logger,
                      weights_summary=None,
                      progress_bar_refresh_rate=hparams.refresh_every,
                      gpus=hparams.gpus,
                      gradient_clip_val=0.5,
                      accelerator='ddp' if (',' in hparams.gpus) else None,
                      num_sanity_val_steps=1,
                      benchmark=True,
                      profiler="simple" if len(hparams.gpus)==1 else None)
                    #   profiler="simple" if hparams.num_gpus==1 else None)

    trainer.fit(system)

if __name__ == '__main__':
    hparams = get_opts()
    main(hparams)


import http

import flask

from advertisements.controller import category_controller

blueprint = flask.Blueprint("category", __name__)

@blueprint.route("/category", methods=("GET",))
def get():
    return {
        "data": [
            {
                "id": category.id,
                "name": category.name,
            }
            for category in category_controller.fetch_all()
        ]
    }, 200


import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer, TfidfTransformer
from string import punctuation
import nltk
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import pickle
from sklearn.externals import joblib
import os

#stop word for cleaning pitches that were input by users
stopwords = stopwords.words( 'english' ) + list(punctuation)
stemmer = PorterStemmer()

def run_model(input_pitch, input_amount, input_exchange, input_gender, input_category):
    #Define veriables from page POST method
    dealloc = "Deal_Status_model.pkl"
    dealvoc = "Deal_Status_vocab.pkl"
    sharkloc = "Deal_Shark 1_model.pkl"
    sharkvoc = "Deal_Shark 1_vocab.pkl"
    path = os.path.join("Shark_Data","static","py",dealloc)
    vocpath = os.path.join("Shark_Data","static","py",dealvoc)
    sharkpath = os.path.join("Shark_Data","static","py",sharkloc)
    sharkvocpath = os.path.join("Shark_Data","static","py",sharkvoc)

    #Load the trained models
    #This model predicts whether a deal would be made or not
    deal_model = joblib.load(path)

    #This model predicts who would make the deal
    shark_model = joblib.load(sharkpath)

    #Deal Model
    #Creates the vector for the pitch word input
    transformer = TfidfTransformer()
    loaded_vectorizer = TfidfVectorizer(decode_error="replace",vocabulary=pickle.load(open(vocpath, "rb")))
    tfidf = transformer.fit_transform(loaded_vectorizer.fit_transform(np.array(input_pitch)))
    input_df = pd.DataFrame(tfidf.toarray(), columns=loaded_vectorizer.get_feature_names())

    #Adds the numerical and catagorical factors to the table
    input_df.loc[0,"Amount_Asked_For"] = input_amount
    input_df.loc[0,"Exchange_For_Stake"] = input_exchange
    input_df.loc[0,f"Gender_{input_gender}"] = 1
    input_df.loc[0,f"Category_{input_category}"] = 1

    #If the model predicts 1, then a deal has been made, 0 no deal
    dealprediction = deal_model.predict(input_df)

    #If a deal has been made, run the second model to predict who might pick it up
    if (dealprediction[0] == 1):

        #Shark Model
        transformer = TfidfTransformer()
        loaded_vectorizer = TfidfVectorizer(decode_error="replace",vocabulary=pickle.load(open(sharkvocpath, "rb")))
        tfidf = transformer.fit_transform(loaded_vectorizer.fit_transform(np.array(input_pitch)))
        input_df_shark = pd.DataFrame(tfidf.toarray(), columns=loaded_vectorizer.get_feature_names())

        input_df_shark.loc[0,"Amount_Asked_For"] = input_amount
        input_df_shark.loc[0,"Exchange_For_Stake"] = input_exchange
        input_df_shark.loc[0,f"Gender_{input_gender}"] = 1
        input_df_shark.loc[0,f"Category_{input_category}"] = 1

        sharkprediction = shark_model.predict(input_df_shark)

        #Returns the deal status and the deal shark
        return dealprediction[0], sharkprediction

    else:
        #Returns just the deal status
        return dealprediction[0], np.nan


import logging
from argparse import ArgumentParser
from typing import Any, Dict, List, Optional, Tuple

from ....arg_parser.utils import str2bool
from ..base_model import GranularBaseModel
from ..loss import LOSS_FACTORY
from ..module import Mlp

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

class MlpPredictor(GranularBaseModel):
    """MlpPredictor - Multi Layer Perceptron predictor."""

    def __init__(
        self,
        name: str,
        from_position: List[int],
        data: Dict[str, str],
        input_size: int,
        hidden_size: int,
        output_size: int,
        n_layers: int,
        activation: str,
        dropout: float,
        loss_function: str,
        class_weights: Optional[List[float]] = None,
        **kwargs,
    ) -> None:
        """Construct MlpPredictor.

        Args:
            name: model name.
            from_position: list of input model positions.
            data: data name mappings.
            input_size: size of the input.
            hidden_size: size of the hidden layers.
            output_size: size of the output.
            n_layers: number of layers.
            activation: name of the activation.
            dropout: dropout rate.
            loss_function: name of the loss function.
            class_weights: weights for the classes. Defaults to None, a.k.a., no weighting.

        Raises:
            ValueError: in case the provided loss function is not supported.
        """
        super().__init__(name=name, data=data)
        self.from_position = from_position
        self.target_key = name + "_" + data["target"]
        self.loss_function_name = loss_function.lower()
        if self.loss_function_name not in LOSS_FACTORY:
            raise ValueError(
                f"loss_function={self.loss_function_name} not supported. Pick a valid one: {sorted(list(LOSS_FACTORY.keys()))}"
            )
        self.loss_function = LOSS_FACTORY[self.loss_function_name]
        self.class_weights = class_weights
        self.mlp = Mlp(
            input_size=input_size,
            hidden_size=hidden_size,
            output_size=output_size,
            n_layers=n_layers,
            activation=activation,
            dropout=dropout,
        )

    def _run_step(self, x: Any, *args, **kwargs) -> Any:
        """Run a step in the model.

        Args:
            x: model input.

        Returns:
            model step output.
        """
        return self.mlp(x)

    def predict(self, x: Any, *args, **kwargs) -> Any:
        """Forward pass in the model.

        Args:
            x: model input.

        Returns:
            model output.
        """
        return self._run_step(x)

    def step(
        self,
        input_data: Any,
        target_data: Any,
        device: str = "cpu",
        current_epoch: int = 0,
        *args,
        **kwargs,
    ) -> Tuple[Any, Any, Any]:
        """Training step for the model.

        Args:
            input_data: input for the step.
            target_data: target for the step.
            device: string representing the device to use. Defaults to "cpu".
            current_epoch: current epoch. Defaults to 0.

        Returns:
            a tuple containing the step output, the loss and the logs for the module.
        """
        output = self._run_step(input_data)
        loss = self.loss_function(output, target_data)
        logs = {f"{self.loss_function_name}_loss": loss, "loss": loss}
        return output, loss, logs

    def val_step(
        self,
        input_data: Any,
        target_data: Any,
        device: str = "cpu",
        current_epoch: int = 0,
        *args,
        **kwargs,
    ) -> Any:
        """Validation step for the model.

        Args:
            input_data: input for the step.
            target_data: target for the step.
            device: string representing the device to use. Defaults to "cpu".
            current_epoch: current epoch. Defaults to 0.

        Returns:
            a tuple containing the step output, the loss and the logs for the module.
        """
        output = self._run_step(input_data)
        loss = self.loss_function(output, target_data)
        logs = {f"{self.loss_function_name}_loss": loss, "loss": loss}

        if self.loss_function_name == "bce":
            output_label = (output > 0.5).float()
            correct_label = (output_label == target_data).float().sum()
            accuracy = correct_label / output_label.shape[0]
            logs["accuracy"] = accuracy
        return output, loss, logs

    @staticmethod
    def add_model_specific_args(
        parent_parser: ArgumentParser, name: str, *args, **kwargs
    ) -> ArgumentParser:
        """Adding to a parser model specific arguments.

        Args:
            parent_parser: patent parser.
            name: model name.

        Returns:
            updated parser.
        """
        parser = ArgumentParser(parents=[parent_parser], add_help=False)

        parser.add_argument(f"--data_path_{name}", type=str)
        parser.add_argument(f"--data_file_{name}", type=str)
        parser.add_argument(f"--dataset_type_{name}", type=str)
        parser.add_argument(f"--target_{name}", type=str)
        parser.add_argument(f"--from_position_{name}", type=int, nargs="+")
        parser.add_argument(f"--checkpoint_path_{name}", type=str)
        parser.add_argument(f"--checkpoint_model_name_{name}", type=str)
        parser.add_argument(f"--start_from_checkpoint_{name}", type=str2bool)
        parser.add_argument(f"--freeze_weights_{name}", type=str2bool)
        parser.add_argument(f"--n_layers_{name}", type=int)
        parser.add_argument(f"--activation_{name}", type=str)
        parser.add_argument(f"--dropout_{name}", type=float)
        parser.add_argument(f"--loss_function_{name}", type=str)
        parser.add_argument(f"--hidden_size_{name}", type=int)
        parser.add_argument(f"--output_size_{name}", type=int)

        return parser


from django.test import TestCase
from unittest.mock import Mock
from django.shortcuts import resolve_url

mock_get = Mock()
mock_get.return_value.text = 'Hello World'
mock_get().text

class CrawlerTest(TestCase):

    def setUp(self):
        self.response = self.client.get(resolve_url('counter_word'))
        self.url = 'http://python.org'
        self.word = 'Python'

    def test_get(self):
        ''' GET / must return status code 200 '''
        self.assertEqual(200, self.response.status_code)

    def test_res(self):
        res = {'Python': 97}
        self.assertEqual({'Python': 97}, res)


from django.views import View
from django.shortcuts import render, get_object_or_404, redirect
from doniServer.models import Products, ProductCategory, ProductItem

class ProductPage(View):
    template_name = 'products.html'

    def get(self, request, *args, **kwargs):
        base_url = request.META.get('HTTP_HOST')
        all_products = Products.get_products_for_website(base_url)
        most_traded_count = ProductItem.objects.filter(price_on_website=True).count()
        context = {
            'most_traded_count': most_traded_count,
            'products': all_products,
            'categories': ProductCategory.get_category_for_website()
        }
        return render(request, self.template_name, context)

class SingleProductPage(View):
    template_name = 'single_product.html'

    def get(self, request, *args, **kwargs):
        product_id = request.GET.get("id")
        base_url = request.META.get('HTTP_HOST')
        product = Products.objects.get(id=product_id)
        context = {
            'product': product,
            'web_obj': product.get_product_single_website(base_url)
        }
        return render(request, self.template_name, context)


from datetime import datetime
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import Wydbid
from BackEnd.WydbidBackEnd import WydbidUIMainLogic, AppointmentLogic, OrderLogic, EventLogic, TaskLogic
from UI.WydbidUI.Prefabs import Settings
from UI.WydbidUI.Prefabs.Customer import ViewCustomer, CreateCustomer, EditCustomer, DelCustomer
from UI.WydbidUI.Prefabs.Appointments import CreateAppointment, EditAppointment, DelAppointment, ShowAppointment
from UI.WydbidUI.Prefabs.News import ShowAllNews
from UI.WydbidUI.Prefabs.Orders import CreateOrder, ShowOrder
from UI.WydbidUI.Prefabs.Events import CreateEvent, ShowEvent
from UI.WydbidUI.Prefabs.Tasks import CreateTask, ShowTask
import screeninfo
import threading

# ToDo: New slogan: There is nothing, oh wait, there is, Wydbid!
# ToDo: Fix news bug on first start

class WydbidUIMain(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()

        self.setWindowTitle('Wydbid - Center')
        width = screeninfo.get_monitors()[0].width
        height = screeninfo.get_monitors()[0].height
        self.setGeometry(0, 0, width, height)

        self.setLayout(QGridLayout())
        self.layout().setContentsMargins(30, 30, 30, 30)

        self.use_close_event = True

        self.vc = ViewCustomer.ViewCustomer()
        self.san = ShowAllNews.ShowAllNews()

        # Tabwidget
        self.tabwidget = QTabWidget(parent=self)

        # Widgets
        self.cc = CreateCustomer.CreateCustomer()
        self.ec = EditCustomer.EditCustomer()
        self.dc = DelCustomer.DelCustomer()

        self.ca = CreateAppointment.CreateAppointment()
        self.gafe = EditAppointment.GetAppointment()
        self.da = DelAppointment.DelAppointment()
        self.sa = ShowAppointment.ShowAppointment()

        self.co = CreateOrder.CreateOrder()
        self.so = ShowOrder.ShowOrder()

        self.ce = CreateEvent.CreateEvent()
        self.se = ShowEvent.ShowEvent()

        self.ct = CreateTask.CreateTask()
        self.st = ShowTask.ShowTask()

        self.setupUI()
        self.setupMenuBar()

    def closeEvent(self, event: QCloseEvent):
        if self.use_close_event:
            reply = QMessageBox.question(self, 'Are you sure?', 'Are you sure you want to quit Wydbid?',
                                         QMessageBox.Yes, QMessageBox.No)

            if reply == QMessageBox.Yes:
                Wydbid.app.exit(0)
            else:
                event.ignore()
                pass
        else:
            event.accept()

    def setupUI(self):
        date_time = QGroupBox(parent=self, title='Date and time')
        date_time.setFixedHeight(150)
        self.setupDateTime(date_time)

        news_widget = QWidget()
        self.setupNewsWidget(newswidget=news_widget)

        customer_widget = QWidget()
        self.setupCustomerList(customerlist=customer_widget)

        appointment_widget = QWidget()
        self.setupAppointments(appointment_widget)

        order_widget = QWidget()
        self.setupOrders(order_widget)

        event_widget = QWidget()
        self.setupEvents(event_widget)

        task_widget = QWidget()
        self.setupTasks(task_widget)

        self.tabwidget.addTab(news_widget, 'News')
        self.tabwidget.addTab(customer_widget, 'Customer list')
        self.tabwidget.addTab(appointment_widget, 'Appointments')
        self.tabwidget.addTab(order_widget, 'Orders')
        self.tabwidget.addTab(event_widget, 'Events')
        self.tabwidget.addTab(task_widget, 'Tasks')

        self.layout().addWidget(date_time, 0, 0, 1, 0)
        self.layout().addWidget(self.tabwidget, 1, 0)

    def setupMenuBar(self):
        self.menubar = QMenuBar(parent=self)
        file = QMenu(parent=self.menubar, title='Wydbid')
        help = QMenu(parent=self.menubar, title='Help')

        logout_employee = QAction('Deregister employee', self)
        logout_company = QAction('Log out from company', self)
        reset_programm = QAction('Reset Wydbid', self)
        update_programm = QAction('Check for Updates', self)
        settings = QAction('Settings', self)
        close = QAction('Exit', self)

        contact = QAction('Contact', self)
        report_bug = QAction('Report error', self)

        logout_employee.triggered.connect(lambda: WydbidUIMainLogic.logoutEmployee(self))
        logout_company.triggered.connect(self.startCompanyLogout)
        reset_programm.triggered.connect(Wydbid.reset)
        update_programm.triggered.connect(lambda: self.startUpdate(p=True))
        settings.triggered.connect(self.startSettings)
        close.triggered.connect(self.closeApp)

        contact.triggered.connect(WydbidUIMainLogic.contact)
        report_bug.triggered.connect(WydbidUIMainLogic.contact)

        file.addAction(logout_employee)
        file.addAction(logout_company)
        file.addSeparator()
        file.addAction(update_programm)
        file.addSeparator()
        file.addAction(reset_programm)
        file.addSeparator()
        file.addAction(settings)
        file.addSeparator()
        file.addAction(close)

        help.addAction(contact)
        help.addAction(report_bug)

        self.menubar.addMenu(file)
        self.menubar.addMenu(help)

    def startSettings(self):
        settings = Settings.Settings()
        settings.show()

    def startCompanyLogout(self):
        WydbidUIMainLogic.logoutCompany(widget=self)

    def resizeEvent(self, QResizeEvent):
        # Is needed because otherwise the MenuBar is only big enough to show the content
        super().resizeEvent(QResizeEvent)

        self.menubar.resize(self.width(), 20)

    # Setup tab widgets

    def setupNewsWidget(self, newswidget: QWidget):
        # Layout declarations
        lyt = QVBoxLayout()
        hlyt = QHBoxLayout()

        self.news_title = QLabel(parent=newswidget)
        self.news_title.setAlignment(Qt.AlignCenter)
        self.news_title.setFont(QFont('Arial', 20))
        self.news_title.setFixedHeight(40)

        self.news_description = QTextEdit(parent=newswidget)
        self.news_description.setEnabled(False)

        self.setLatestNews()

        showall = QPushButton(parent=newswidget, text='Show all')
        showall.clicked.connect(self.startViewAllNews)
        showall.setFixedWidth(120)

        lyt.addWidget(self.news_title, Qt.AlignCenter)
        lyt.addWidget(self.news_description, Qt.AlignCenter)

        hlyt.addWidget(showall)

        lyt.addLayout(hlyt)

        newswidget.setLayout(lyt)

    def setupCustomerList(self, customerlist: QWidget):
        # Layout declarations
        lyt = QVBoxLayout()
        hlyt = QHBoxLayout()

        actions = QGroupBox(parent=self)
        actions.setFixedHeight(40)
        nlyt = QHBoxLayout()
        nlyt.setContentsMargins(1, 1, 1, 1)

        add = QPushButton(parent=self, text='Create')
        add.setToolTip('Create customer')
        add.clicked.connect(self.startCreateCustomer)

        edit = QPushButton(parent=self, text='Edit')
        edit.setToolTip('Edit customer')
        edit.clicked.connect(self.startEditCustomer)

        delete = QPushButton(parent=self, text='Delete')
        delete.setToolTip('Delete customer')
        delete.clicked.connect(self.startDelCustomer)

        nlyt.addWidget(add)
        nlyt.addWidget(edit)
        nlyt.addWidget(delete)
        actions.setLayout(nlyt)

        self.searchbar_first = QLineEdit(parent=customerlist)
        self.searchbar_first.setFixedHeight(40)
        self.searchbar_first.setPlaceholderText('Filter by first name')

        self.searchbar_last = QLineEdit(parent=customerlist)
        self.searchbar_last.setFixedHeight(40)
        self.searchbar_last.setPlaceholderText('Filter by last name')

        self.customerlist = QTableWidget(parent=customerlist)
        self.customerlist.verticalHeader().setVisible(False)

        self.customerlist.setColumnCount(9)
        self.customerlist.setHorizontalHeaderLabels(
        ['Customer id', 'Firstname', 'Lastname', 'E-mail address', 'Adress', 'Number', 'Gender', 'Birth date', ''])

        WydbidUIMainLogic.appendCustomers(customerlist=self.customerlist)

        self.customerlist.setSortingEnabled(True)
        self.customerlist.setFocusPolicy(Qt.NoFocus)
        self.customerlist.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
        self.customerlist.setEditTriggers(QAbstractItemView.NoEditTriggers)

        self.searchbar_first.textChanged.connect(
            lambda: WydbidUIMainLogic.searchForFirstName(searchfirst=self.searchbar_first, searchlast=self.searchbar_last,
                                                         list=self.customerlist)
        )

        self.searchbar_last.textChanged.connect(
            lambda: WydbidUIMainLogic.searchForLastName(searchfirst=self.searchbar_first, searchlast=self.searchbar_last,
                                                        list=self.customerlist)
        )

        self.customerlist.clicked.connect(
            self.loadViewCustomer
        )

        # Add customer list and search bar to main layout
        lyt.addWidget(self.searchbar_first, Qt.AlignCenter)
        lyt.addWidget(self.searchbar_last, Qt.AlignCenter)
        lyt.addWidget(actions, Qt.AlignCenter)
        lyt.addWidget(self.customerlist, Qt.AlignCenter)

        reload = QPushButton('Reload')
        reload.setFixedWidth(120)
        reload.clicked.connect(self.startReloadCustomers)

        # Add reload button to bottom layout
        hlyt.addWidget(reload)

        # Add bottom layout to main layout
        lyt.addLayout(hlyt)

        # Set layout of customer list tab widget
        customerlist.setLayout(lyt)

    def loadViewCustomer(self, item):
        if item.data() == 'ðŸ”Ž':
            WydbidUIMainLogic.viewCustomer(self.customerlist, self.vc, item)

    def setupAppointments(self, appointments: QWidget):
        lyt = QHBoxLayout()
        vl = QVBoxLayout()

        action_box = QGroupBox(parent=appointments)
        action_box.setFixedHeight(40)
        alyt = QHBoxLayout()

        goto_current_date = QPushButton(parent=action_box, text='Jump to current date')
        goto_current_date.clicked.connect(self.gotoCurrentDate)

        add = QPushButton(parent=self, text='Create')
        add.setToolTip('Create new appointment on selected date')
        add.clicked.connect(self.startCreateAppointment)

        edit = QPushButton(parent=self, text='Edit')
        edit.setToolTip('Edit a appointment on selected date')
        edit.clicked.connect(self.startEditAppointment)

        delete = QPushButton(parent=self, text='Delete')
        delete.setToolTip('Delete a appointment on selected date')
        delete.clicked.connect(self.startDelAppointment)

        alyt.setContentsMargins(1, 1, 1, 1)
        alyt.addWidget(goto_current_date)
        alyt.addWidget(add)
        alyt.addWidget(edit)
        alyt.addWidget(delete)
        action_box.setLayout(alyt)

        # Widget content

        self.calander = QCalendarWidget(parent=self)
        if Wydbid.app.styleSheet().strip():
            self.calander.setStyleSheet('''
            QAbstractItemView {
                alternate-background-color: #2d3640;
            }
            ''')
        self.calander.setGridVisible(True)

        ngroup = QGroupBox()
        ngroup.setMaximumWidth(700)

        self.appointment_search_bar = QLineEdit(parent=ngroup)
        self.appointment_search_bar.setPlaceholderText('Filter by customer')
        self.appointment_search_bar.setFixedHeight(40)
        self.appointment_search_bar.textChanged.connect(self.filterForCustomerInAppointments)

        self.appointment_list = QTableWidget(parent=ngroup)
        self.appointment_list.clicked.connect(self.startShowAppointment)

        nlyt = QVBoxLayout()

        lyt.addWidget(self.calander, Qt.AlignLeft)
        nlyt.addWidget(self.appointment_search_bar, Qt.AlignTop)
        nlyt.addWidget(self.appointment_list, Qt.AlignBottom)
        lyt.addWidget(ngroup, Qt.AlignRight)

        ngroup.setLayout(nlyt)

        vl.addWidget(action_box)
        vl.addLayout(lyt)

        appointments.setLayout(vl)
        self.startAppendAppointments()
        self.calander.selectionChanged.connect(self.startAppendAppointments)

    def setupOrders(self, orders_widget: QWidget):
        lyt = QVBoxLayout()

        action_box = QGroupBox(parent=orders_widget)
        action_box.setFixedHeight(40)
        alyt = QHBoxLayout()

        add = QPushButton(parent=action_box, text='Create')
        add.setToolTip('Create new order')
        add.clicked.connect(self.startCreateOrder)

        edit = QPushButton(parent=action_box, text='Edit')
        edit.setToolTip('Edit a order')
        edit.clicked.connect(self.startEditOrder)

        delete = QPushButton(parent=action_box, text='Delete')
        delete.setToolTip('Delete a order')
        delete.clicked.connect(self.startDelOrder)

        reload = QPushButton(parent=action_box, text='Reload')
        reload.setToolTip('Reload all orders')
        reload.clicked.connect(self.startAppendOrders)

        alyt.setContentsMargins(1, 1, 1, 1)
        alyt.addWidget(add)
        alyt.addWidget(edit)
        alyt.addWidget(delete)
        alyt.addWidget(reload)
        action_box.setLayout(alyt)

        self.order_search_bar = QLineEdit(parent=orders_widget)
        self.order_search_bar.setPlaceholderText('Filter by customer')
        self.order_search_bar.setFixedHeight(40)
        self.order_search_bar.textChanged.connect(self.filterForCustomerInOrders)

        self.order_list = QTableWidget(parent=orders_widget)
        self.order_list.clicked.connect(self.startShowOrder)
        self.startAppendOrders()

        lyt.addWidget(action_box)
        lyt.addWidget(self.order_search_bar)
        lyt.addWidget(self.order_list)

        orders_widget.setLayout(lyt)

    def setupEvents(self, event_widget: QWidget):
        lyt = QVBoxLayout()

        action_box = QGroupBox(parent=event_widget)
        action_box.setFixedHeight(40)
        alyt = QHBoxLayout()

        add = QPushButton(parent=action_box, text='Create')
        add.setToolTip('Create new event')
        add.clicked.connect(self.startCreateEvent)

        edit = QPushButton(parent=action_box, text='Edit')
        edit.setToolTip('Edit a event')
        edit.clicked.connect(self.startEditEvent)

        delete = QPushButton(parent=action_box, text='Delete')
        delete.setToolTip('Delete a event')
        delete.clicked.connect(self.startDelEvent)

        reload = QPushButton(parent=action_box, text='Reload')
        reload.setToolTip('Reload all events')
        reload.clicked.connect(self.startAppendEvents)

        alyt.setContentsMargins(1, 1, 1, 1)
        alyt.addWidget(add)
        alyt.addWidget(edit)
        alyt.addWidget(delete)
        alyt.addWidget(reload)
        action_box.setLayout(alyt)

        self.event_search_bar = QLineEdit(parent=event_widget)
        self.event_search_bar.setPlaceholderText('Filter by title')
        self.event_search_bar.setFixedHeight(40)
        self.event_search_bar.textChanged.connect(self.filterForTitleInEvents)

        self.event_list = QTableWidget(parent=event_widget)
        self.event_list.clicked.connect(self.startShowEvent)
        self.startAppendEvents()

        lyt.addWidget(action_box)
        lyt.addWidget(self.event_search_bar)
        lyt.addWidget(self.event_list)

        event_widget.setLayout(lyt)

    def setupTasks(self, task_widget: QWidget):
        lyt = QVBoxLayout()

        action_box = QGroupBox(parent=task_widget)
        action_box.setFixedHeight(40)
        alyt = QHBoxLayout()

        add = QPushButton(parent=action_box, text='Create')
        add.setToolTip('Create new task')
        add.clicked.connect(self.startCreateTask)

        edit = QPushButton(parent=action_box, text='Edit')
        edit.setToolTip('Edit a task')
        edit.clicked.connect(self.startEditTask)

        delete = QPushButton(parent=action_box, text='Delete')
        delete.setToolTip('Delete a task')
        delete.clicked.connect(self.startDelTask)

        reload = QPushButton(parent=action_box, text='Reload')
        reload.setToolTip('Reload all tasks')
        reload.clicked.connect(self.startAppendTasks)

        alyt.setContentsMargins(1, 1, 1, 1)
        alyt.addWidget(add)
        alyt.addWidget(edit)
        alyt.addWidget(delete)
        alyt.addWidget(reload)
        action_box.setLayout(alyt)

        self.task_search_bar = QLineEdit(parent=task_widget)
        self.task_search_bar.setPlaceholderText('Filter by receiver')
        self.task_search_bar.setFixedHeight(40)
        self.task_search_bar.textChanged.connect(self.filterForReceiverInTasks)

        self.task_list = QTableWidget(parent=task_widget)
        self.task_list.clicked.connect(self.startShowTask)
        self.startAppendTasks()

        lyt.addWidget(action_box)
        lyt.addWidget(self.task_search_bar)
        lyt.addWidget(self.task_list)

        task_widget.setLayout(lyt)

    def setupDateTime(self, date_time: QGroupBox):
        self.time_label = QLabel(parent=date_time)
        self.time_label.setText('00:00:00')
        self.date_label = QLabel(parent=date_time)
        self.date_label.setText(datetime.now().date().strftime("%d.%m.%y"))

        self.time_label.setFont(QFont('Montserrat', 40))
        self.date_label.setFont(QFont('Montserrat', 25))

        date_time_layout = QGridLayout()

        date_time_layout.addWidget(self.time_label, 0, 0, 1, 0, Qt.AlignCenter)
        date_time_layout.addWidget(self.date_label, 1, 0, 1, 0, Qt.AlignCenter)

        date_time.setLayout(date_time_layout)

        # Loop to update the time
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateClock)
        self.timer.start(1000)

    def updateClock(self):
        self.time_label.setText(datetime.now().strftime("%H:%M:%S"))

    def startViewAllNews(self):
        self.san.appendNews()
        self.san.show()

    def setNews(self, news):
        self.news_title.setText(news.title)
        self.news_description.setText(news.description)

    def setLatestNews(self):
        news = WydbidUIMainLogic.getLatestNews(self)
        if not news:
            return

        self.news_title.setText(news.title)
        self.news_description.setText(news.description)

    def quit(self):
        self.use_close_event = False

    def startUpdate(self, p: bool):
        updater = Wydbid.Update()
        if not p: updater.show_if_uptodate = False
        updater.checkVersion()

    def closeApp(self):
        Wydbid.app.exit(0)

    # Event Management Methods

    def startReloadCustomers(self):
        WydbidUIMainLogic.reloadCustomers(customerlist=self.customerlist)

    def startCreateCustomer(self):
        self.cc.show()

    def startEditCustomer(self):
        self.ec.show()
        self.ec.setCustomer()

    def startDelCustomer(self):
        self.dc.show()

    def gotoCurrentDate(self):
        self.calander.showToday()
        self.calander.setSelectedDate(QDate.currentDate())

    def startCreateAppointment(self):
        self.ca.show()
        self.ca.clear()

    def startEditAppointment(self):
        self.gafe.setDate(self.calander.selectedDate().toString('dd.MM.yyyy'))
        self.gafe.show()

    def startDelAppointment(self):
        self.da.setDate(self.calander.selectedDate().toString('dd.MM.yyyy'))
        self.da.show()

    def startShowAppointment(self, item):
        if item.data() == 'ðŸ”Ž':
            id = item.data(Qt.UserRole)
            appointment = AppointmentLogic.getAppointment(id)
            self.sa.clear()
            self.sa.setAppointment(appointment)
            self.sa.show()

    def startAppendAppointments(self):
        date = self.calander.selectedDate().toString('dd.MM.yyyy')
        AppointmentLogic.appendAppointments(date, self.calander, self.appointment_list)

    def filterForCustomerInAppointments(self):
        customer = self.appointment_search_bar.text().lower()
        for row in range(self.appointment_list.rowCount()):
            item = self.appointment_list.item(row, 3)

            # if the search is not in the item's text do not hide the row
            self.appointment_list.setRowHidden(row, customer not in item.text().lower())

    def filterForCustomerInOrders(self):
        customer = self.order_search_bar.text().lower()
        for row in range(self.order_list.rowCount()):
            item = self.order_list.item(row, 2)

            # if the search is not in the item's text do not hide the row
            self.order_list.setRowHidden(row, customer not in item.text().lower())

    def filterForTitleInEvents(self):
        title = self.event_search_bar.text().lower()
        for row in range(self.event_list.rowCount()):
            item = self.event_list.item(row, 0)

            # if the search is not in the item's text do not hide the row
            self.event_list.setRowHidden(row, title not in item.text().lower())

    def filterForReceiverInTasks(self):
        receiver = self.task_search_bar.text().lower()
        for row in range(self.task_list.rowCount()):
            item = self.task_list.item(row, 2)

            # if the search is not in the item's text do not hide the row
            self.task_list.setRowHidden(row, receiver not in item.text().lower())

    def startAppendOrders(self):
        OrderLogic.appendOrders(self.order_list)

    def startAppendEvents(self):
        EventLogic.appendEvents(self.event_list)

    def startAppendTasks(self):
        TaskLogic.appendTasks(self.task_list)

    def startCreateOrder(self):
        self.co.clear()
        self.co.show()

    def startEditOrder(self):
        QMessageBox.about(self, 'Attention',
                          'You can edit an order by pressing the '
                          'Loupe symbol by the particular order and then pressing'
                          ' the "Edit" button within the dialog.')

    def startDelOrder(self):
        QMessageBox.about(self, 'Attention',
                          'You can delete an order by pressing the loupe symbol by the particular order and then '
                          'pressing the "Delete" button within the dialog.')

    def startShowOrder(self, item):
        if item.data() == 'ðŸ”Ž':
            id = item.data(Qt.UserRole)
            self.so.clear()
            self.so.setOrder(id)
            self.so.show()

    def startCreateEvent(self):
        self.ce.clear()
        self.ce.show()

    def startEditEvent(self):
        QMessageBox.about(self, 'Attention',
                          'You can edit an event by pressing the '
                          'Loupe symbol by the particular event and then pressing'
                          ' the "Edit" button within the dialog.')

    def startDelEvent(self):
        QMessageBox.about(self, 'Attention',
                          'You can delete an event by pressing the loupe symbol by the particular event and then '
                          'pressing the "Delete" button within the dialog.')

    def startShowEvent(self, item):
        if item.data() == 'ðŸ”Ž':
            id = item.data(Qt.UserRole)
            self.se.clear()
            self.se.setEvent(id)
            self.se.show()

    def startCreateTask(self):
        self.ct.clear()
        self.ct.show()

    def startEditTask(self):
        QMessageBox.about(self, 'Attention',
                          'You can edit an task by pressing the '
                          'Loupe symbol by the particular task and then pressing'
                          ' the "Edit" button within the dialog.')

    def startDelTask(self):
        QMessageBox.about(self, 'Attention',
                          'You can delete an task by pressing the loupe symbol by the particular task and then '
                          'pressing the "Delete" button within the dialog.')

    def startShowTask(self, item):
        if item.data() == 'ðŸ”Ž':
            id = item.data(Qt.UserRole)
            self.st.clear()
            self.st.setTask(id)
            self.st.show()

'''
Date/Time Formats:

QDate -> 'dd.MM.yyyy'
Day.Month.Year

QTime -> 'hh:mm'
Hour:Minute
'''


from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone

from links.forms import LinkCreateForm
from links.models import Link

def add_link(request):
    if request.method == 'POST':
        form = LinkCreateForm(request.POST)
        if form.is_valid():
            link_obj = form.save(commit=False)
            link_obj.timestamp = timezone.now()
            link_obj.save()
            return redirect(link_obj.get_absolute_url())
    else:
        form = LinkCreateForm()
        return render(request, 'links/add_link.html', {'form': form})

def link_detail(request, slug):
    link_obj = get_object_or_404(Link, slug=slug)
    context = {
        'text': link_obj.text,
        'slug': slug,
        'timestamp': link_obj.timestamp
    }
    return render(request, 'links/link_detail.html', context)


from django.conf.urls import url

import programs.views.subprogram_views as subprograms
import programs.views.project_views as projects

urlpatterns = [
    url(r'^$', subprograms.list_subprograms, name='list_subprograms'),
    url(r'^nuevo', subprograms.create_subprogram, name='new_subprogram'),
    url(r'^ver/(?P<subprogram_id>\d+)/$', subprograms.view_subprogram, name='view_subprogram'),
    url(r'^ver/(?P<subprogram_id>\d+)/mapa/$', subprograms.view_map, name='map'),
    url(r'^(?P<subprogram_id>\d+)/nuevo-proyecto/$', projects.create_project, name='proj'),
    url(r'^(?P<subprogram_id>\d+)/reporte/$', subprograms.report, name='subprog_report'),
    url(r'^editar/(?P<subprogram_id>\d+)/', subprograms.update_subprogram, name='edit_subprogram'),

    #url(r'^bloquear/(?P<program_id>\d+)/', subprograms.toggle_lock, name='toggle_block_subprogram'),
]


from asyncio import iscoroutinefunction
from functools import partial
from web3.utils.filters import construct_event_filter_params
from web3.utils.events import get_event_data

class WebsocketFilterSet:
    def __init__(self, contract):
        self.contract = contract
        self.handlers = {}
        self.topic_to_event = {}
        self.topic_filters = []

    async def deliver(self, topic_hash, payload):
        if topic_hash in self.handlers:
            if iscoroutinefunction(self.handlers[topic_hash]):
                await self.handlers[topic_hash](payload)
            else:
                self.handlers[topic_hash](payload)

    def on_event(self, event_name, handler_func=None):
        def decorator(handler_func):
            event_abi = self.contract._find_matching_event_abi(event_name, {})
            _, event_filter_params = construct_event_filter_params(event_abi,
                contract_address=self.contract.address)

            if iscoroutinefunction(handler_func):
                async def decorated(payload):
                    event_data = get_event_data(event_abi, payload)
                    return await handler_func(self.contract, event_name, event_data)
            else:
                def decorated(payload):
                    event_data = get_event_data(event_abi, payload)
                    return handler_func(self.contract, event_name, event_data)

            self.topic_filters.append(event_filter_params)
            self.handlers[event_filter_params["topics"][0]] = decorated

            return decorated

        if handler_func is None:
            return decorator
        return decorator(handler_func)


'''
Author: your name
Date: 2022-01-18 07:45:57
LastEditTime: 2022-02-12 15:22:22
LastEditors: Please set LastEditors
Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
FilePath: \Workspace\XML\block4\task2.py
'''

import rdflib

g1 = rdflib.Graph()
g2 = rdflib.Graph()
g = rdflib.Graph()
g4 = rdflib.Graph()

aut = g2.parse("C:\\Users\\ydzat\\OneDrive\\Workspace\\XML\\block4\\books.rdf")
lit = g1.parse("C:\\Users\\ydzat\\OneDrive\\Workspace\\XML\\block4\\authors.rdf")
trans = g.parse("C:\\Users\\ydzat\\OneDrive\\Workspace\\XML\\block4\\translators.rdf")
loc = g4.parse("C:\\Users\\ydzat\\OneDrive\\Workspace\\XML\\block4\\locations.rdf")

authorPaar = {}
authorAndBook = {}
bookPaar = {}

transPaar = {}
transAndBook = {}

authorAndLocation = {}

for s,p,o in g1:
    if "authorName" in str(p) and str(p) not in authorPaar:
        authorPaar[str(s)] = str(o)
    if "hasWritten" in str(p):
        if str(s) not in authorAndBook:
            authorAndBook[str(s)] = []
        authorAndBook[str(s)].append(str(o))

for s,p,o in g2:
    if "bookName" in str(p):
        bookPaar[str(s)] = str(o)

tempAB = {}

for i in authorAndBook.keys():
    #authorAndBook[authorPaar[i]] = tempAB.pop(i)
    tempAB[authorPaar[i]] = authorAndBook[i]
authorAndBook = tempAB

#print(authorAndBook)

for i in authorAndBook.keys():
    tempBook = []
    for j in authorAndBook[i]:
        tempBook.append(bookPaar[j])
        authorAndBook[i] = tempBook

for s,p,o in g:
    if "authorName" in str(p) and str(p) not in transPaar:
        transPaar[str(s)] = str(o)
    if "hasTranslated" in str(p):
        if str(s) not in transAndBook:
            transAndBook[str(s)] = []
        transAndBook[str(s)].append(str(o))

tempTB = {}
for i in transAndBook.keys():
    tempTB[transPaar[i]] = transAndBook[i]
transAndBook = tempTB

for i in transAndBook.keys():
    tempBook = []
    for j in transAndBook[i]:
        tempBook.append(bookPaar[j])
        transAndBook[i] = tempBook

def findTranslator(transAndBook, bookName):
    nameList = []
    for i in transAndBook.keys():
        if bookName in transAndBook[i]:
            nameList.append(i)
    return nameList

for i in authorAndBook.keys():
    print(i + "wrote:")
    for j in authorAndBook[i]:
        print("\t" + j)
        nameList = findTranslator(transAndBook, j)
        for k in range(len(nameList)):
            print("\t\t translated by: " + nameList[k])

# task 4 skip, falsch doc
#print(authorPaar)
# for s,p,o in g4:
#     print(authorPaar[str(s)])
    #authorAndLocation[authorPaar[str(s)]] = str(o)

# for i in authorAndBook.keys():
#     print(i + " wrote:")
#     for j in authorAndBook[i]:
#         print("  " + j)
#         for k in transAndBook.keys():
#             if j in transAndBook[k]:
#                 print("    translated by: " + k )
#     print()


import requests

from test_APP.Common.Login_test import Login

# 到岗时间下拉

class Test_Arrival_Time():
    def test_arrival_time(self):
        url = "http://192.168.101.102/api/user/app/personal/resume/arrival-time"
        heasers = {
            "content-type": "application/x-www-form-urlencoded;charset=utf-8",
            "token": Login().Login_sms_test()

        }
        res = requests.get(url=url, headers=heasers)
        print(res.text)
        code = res.json()["code"]
        try:
            assert code == 0
            print("到岗下拉成功")
        except:
            assert code != 0
            print("到岗下拉失败")

if __name__ == '__main__':
    Test_Arrival_Time().test_arrival_time()